|
@@ -72,16 +72,17 @@ public class GoogleAdsReportService {
|
|
|
cacheNames =
|
|
|
"googleads:getCustomerStats"
|
|
|
+ TTLCacheManager.TTL_SPLITTER
|
|
|
- + 60 * 10) // Redis TTL为10分钟
|
|
|
+ + 60 * 5) // Redis TTL为5分钟
|
|
|
public CustomerStatsVO getCustomerStats(String siteCode) {
|
|
|
- String customerId = this.getCustomerId(siteCode);
|
|
|
- if (StringUtils.isEmpty(customerId)) {
|
|
|
+ GoogleAds googleAds = this.getAdsAccount(siteCode);
|
|
|
+ if (StringUtils.isBlank(googleAds.getCustomerId())) {
|
|
|
log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
|
|
|
return new CustomerStatsVO(); // @Cacheable方法不允许返回null,返回CustomerStatsVO默认值
|
|
|
}
|
|
|
|
|
|
// 1. 创建API request
|
|
|
- OpenAPIRequest<String> openAPIRequest = this.buildAPIRequest(customerId);
|
|
|
+ OpenAPIRequest<ReportRequestDTO> openAPIRequest =
|
|
|
+ this.buildAPIRequest(googleAds, null, null, 1); // limit值无效
|
|
|
|
|
|
// 2. 请求API接口
|
|
|
OpenAPIResponse<CustomerStatsDTO> openAPIResponse =
|
|
@@ -101,10 +102,10 @@ public class GoogleAdsReportService {
|
|
|
cacheNames =
|
|
|
"googleads:getDailyStats"
|
|
|
+ TTLCacheManager.TTL_SPLITTER
|
|
|
- + 60 * 10) // Redis TTL为10分钟
|
|
|
+ + 60 * 5) // Redis TTL为5分钟
|
|
|
public List<DailyStatsVO> getDailyStats(String siteCode, Date start, Date end) {
|
|
|
- String customerId = this.getCustomerId(siteCode);
|
|
|
- if (StringUtils.isEmpty(customerId)) {
|
|
|
+ GoogleAds googleAds = this.getAdsAccount(siteCode);
|
|
|
+ if (StringUtils.isBlank(googleAds.getCustomerId())) {
|
|
|
log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
|
|
|
return Collections.EMPTY_LIST;
|
|
|
}
|
|
@@ -122,7 +123,7 @@ public class GoogleAdsReportService {
|
|
|
|
|
|
// 1. 创建API request
|
|
|
OpenAPIRequest<ReportRequestDTO> openAPIRequest =
|
|
|
- this.buildAPIRequest(this.buildReportRequest(customerId, start, end, limit));
|
|
|
+ this.buildAPIRequest(googleAds, start, end, limit);
|
|
|
|
|
|
// 2. 请求API接口
|
|
|
OpenAPIResponse<Map<String, MetricsDTO>> openAPIResponse =
|
|
@@ -146,18 +147,17 @@ public class GoogleAdsReportService {
|
|
|
cacheNames =
|
|
|
"googleads:getCampaignStats"
|
|
|
+ TTLCacheManager.TTL_SPLITTER
|
|
|
- + 60 * 10) // Redis TTL为10分钟
|
|
|
+ + 60 * 5) // Redis TTL为5分钟
|
|
|
public List<CampaignStatsVO> getCampaignStats(String siteCode, Date start, Date end) {
|
|
|
- String customerId = this.getCustomerId(siteCode);
|
|
|
- if (StringUtils.isEmpty(customerId)) {
|
|
|
+ GoogleAds googleAds = this.getAdsAccount(siteCode);
|
|
|
+ if (StringUtils.isBlank(googleAds.getCustomerId())) {
|
|
|
log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
|
|
|
return Collections.EMPTY_LIST;
|
|
|
}
|
|
|
|
|
|
// 1. 创建API request
|
|
|
OpenAPIRequest<ReportRequestDTO> openAPIRequest =
|
|
|
- this.buildAPIRequest(
|
|
|
- this.buildReportRequest(customerId, start, end, 50)); // 返回top 50
|
|
|
+ this.buildAPIRequest(googleAds, start, end, 50); // 返回top 50
|
|
|
|
|
|
// 2. 请求API接口
|
|
|
OpenAPIResponse<List<CampaignStatsDTO>> openAPIResponse =
|
|
@@ -181,18 +181,17 @@ public class GoogleAdsReportService {
|
|
|
cacheNames =
|
|
|
"googleads:getKeywordStats"
|
|
|
+ TTLCacheManager.TTL_SPLITTER
|
|
|
- + 60 * 10) // Redis TTL为10分钟
|
|
|
+ + 60 * 5) // Redis TTL为5分钟
|
|
|
public List<KeywordStatsVO> getKeywordStats(String siteCode, Date start, Date end) {
|
|
|
- String customerId = this.getCustomerId(siteCode);
|
|
|
- if (StringUtils.isEmpty(customerId)) {
|
|
|
+ GoogleAds googleAds = this.getAdsAccount(siteCode);
|
|
|
+ if (StringUtils.isBlank(googleAds.getCustomerId())) {
|
|
|
log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
|
|
|
return Collections.EMPTY_LIST;
|
|
|
}
|
|
|
|
|
|
// 1. 创建API request
|
|
|
OpenAPIRequest<ReportRequestDTO> openAPIRequest =
|
|
|
- this.buildAPIRequest(
|
|
|
- this.buildReportRequest(customerId, start, end, 50)); // 返回top 50
|
|
|
+ this.buildAPIRequest(googleAds, start, end, 50); // 返回top 50
|
|
|
|
|
|
// 2. 请求API接口
|
|
|
OpenAPIResponse<Map<String, MetricsDTO>> openAPIResponse =
|
|
@@ -216,18 +215,17 @@ public class GoogleAdsReportService {
|
|
|
cacheNames =
|
|
|
"googleads:getPlacementStats"
|
|
|
+ TTLCacheManager.TTL_SPLITTER
|
|
|
- + 60 * 10) // Redis TTL为10分钟
|
|
|
+ + 60 * 5) // Redis TTL为5分钟
|
|
|
public List<PlacementStatsVO> getPlacementStats(String siteCode, Date start, Date end) {
|
|
|
- String customerId = this.getCustomerId(siteCode);
|
|
|
- if (StringUtils.isEmpty(customerId)) {
|
|
|
+ GoogleAds googleAds = this.getAdsAccount(siteCode);
|
|
|
+ if (StringUtils.isBlank(googleAds.getCustomerId())) {
|
|
|
log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
|
|
|
return Collections.EMPTY_LIST;
|
|
|
}
|
|
|
|
|
|
// 1. 创建API request
|
|
|
OpenAPIRequest<ReportRequestDTO> openAPIRequest =
|
|
|
- this.buildAPIRequest(
|
|
|
- this.buildReportRequest(customerId, start, end, 50)); // 返回top 50
|
|
|
+ this.buildAPIRequest(googleAds, start, end, 50); // 返回top 50
|
|
|
|
|
|
// 2. 请求API接口
|
|
|
OpenAPIResponse<Map<String, MetricsDTO>> openAPIResponse =
|
|
@@ -251,18 +249,17 @@ public class GoogleAdsReportService {
|
|
|
cacheNames =
|
|
|
"googleads:getCountryStats"
|
|
|
+ TTLCacheManager.TTL_SPLITTER
|
|
|
- + 60 * 10) // Redis TTL为10分钟
|
|
|
+ + 60 * 5) // Redis TTL为5分钟
|
|
|
public List<CountryStatsVO> getCountryStats(String siteCode, Date start, Date end) {
|
|
|
- String customerId = this.getCustomerId(siteCode);
|
|
|
- if (StringUtils.isEmpty(customerId)) {
|
|
|
+ GoogleAds googleAds = this.getAdsAccount(siteCode);
|
|
|
+ if (StringUtils.isBlank(googleAds.getCustomerId())) {
|
|
|
log.info("Google Ads帐号未配置, siteCode = {}", siteCode);
|
|
|
return Collections.EMPTY_LIST;
|
|
|
}
|
|
|
|
|
|
// 1. 创建API request
|
|
|
OpenAPIRequest<ReportRequestDTO> openAPIRequest =
|
|
|
- this.buildAPIRequest(
|
|
|
- this.buildReportRequest(customerId, start, end, 50)); // 返回top 50
|
|
|
+ this.buildAPIRequest(googleAds, start, end, 50); // 返回top 50
|
|
|
|
|
|
// 2. 请求API接口
|
|
|
OpenAPIResponse<Map<String, MetricsDTO>> openAPIResponse =
|
|
@@ -282,41 +279,34 @@ public class GoogleAdsReportService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取Google Ads customer ID - 放入Redis缓存,避免同一页面多个HTTP请求频繁DB查询
|
|
|
+ * 获取{@link GoogleAds}帐号信息 - 放入Redis缓存,避免同一页面多个HTTP请求频繁DB查询
|
|
|
*
|
|
|
* <p>使用AdwebRedisUtil操作 - @Cacheable注解无法嵌套使用
|
|
|
*/
|
|
|
- private synchronized String getCustomerId(String siteCode) {
|
|
|
- final String redisKey = String.format("%s:%s:%s", "googleads", "customerId", siteCode);
|
|
|
+ private synchronized GoogleAds getAdsAccount(String siteCode) {
|
|
|
+ final String redisKey = String.format("%s:account:%s", "googleads", siteCode);
|
|
|
|
|
|
- String customerId = redisUtil.getString(redisKey);
|
|
|
- if (StringUtils.isNotEmpty(customerId)) {
|
|
|
- return customerId;
|
|
|
+ GoogleAds googleAds = (GoogleAds) redisUtil.get(redisKey);
|
|
|
+ if (Objects.nonNull(googleAds)) {
|
|
|
+ return googleAds;
|
|
|
}
|
|
|
|
|
|
// DB查询
|
|
|
- GoogleAds googleAds =
|
|
|
+ googleAds =
|
|
|
googleAdsService.getOne(
|
|
|
new LambdaQueryWrapper<GoogleAds>().eq(GoogleAds::getSiteCode, siteCode),
|
|
|
false);
|
|
|
- customerId = Objects.nonNull(googleAds) ? googleAds.getCustomerId() : "";
|
|
|
- redisUtil.set(redisKey, customerId, 60); // Redis TTL为1分钟
|
|
|
-
|
|
|
- return customerId;
|
|
|
- }
|
|
|
+ googleAds = Objects.nonNull(googleAds) ? googleAds : new GoogleAds(); // Redis存储对象不允许为空
|
|
|
+ redisUtil.set(redisKey, googleAds, 30); // Redis TTL为30秒
|
|
|
|
|
|
- private <T> OpenAPIRequest<T> buildAPIRequest(T data) {
|
|
|
- OpenAPIRequest<T> apiRequest = new OpenAPIRequest<>();
|
|
|
- apiRequest.setRequestServer(this.getClass().getSimpleName());
|
|
|
- apiRequest.setRequestTime(System.currentTimeMillis());
|
|
|
- apiRequest.setData(data);
|
|
|
- return apiRequest;
|
|
|
+ return googleAds;
|
|
|
}
|
|
|
|
|
|
- private ReportRequestDTO buildReportRequest(
|
|
|
- String customerId, Date start, Date end, int limit) {
|
|
|
+ private OpenAPIRequest<ReportRequestDTO> buildAPIRequest(
|
|
|
+ GoogleAds googleAds, Date start, Date end, int limit) {
|
|
|
ReportRequestDTO reportRequest = new ReportRequestDTO();
|
|
|
- reportRequest.setCustomerId(customerId);
|
|
|
+ reportRequest.setCustomerId(googleAds.getCustomerId());
|
|
|
+ reportRequest.setRefreshToken(googleAds.getRefreshToken());
|
|
|
reportRequest.setStartDate(
|
|
|
DateUtil.formatDate(
|
|
|
Optional.ofNullable(start)
|
|
@@ -327,6 +317,11 @@ public class GoogleAdsReportService {
|
|
|
DateUtil.formatDate(
|
|
|
Optional.ofNullable(end).orElse(new Date()), DateUtil.DATE_FORMAT));
|
|
|
reportRequest.setLimit(limit);
|
|
|
- return reportRequest;
|
|
|
+
|
|
|
+ OpenAPIRequest<ReportRequestDTO> apiRequest = new OpenAPIRequest<>();
|
|
|
+ apiRequest.setRequestServer(this.getClass().getSimpleName());
|
|
|
+ apiRequest.setRequestTime(System.currentTimeMillis());
|
|
|
+ apiRequest.setData(reportRequest);
|
|
|
+ return apiRequest;
|
|
|
}
|
|
|
}
|