|
@@ -2,6 +2,7 @@ package org.jeecg.modules.adweb.dmp.service.google;
|
|
|
|
|
|
import static org.jeecg.modules.adweb.dmp.dto.google.analytics.report.ReportConstant.*;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.annotation.TableName;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
|
|
@@ -35,6 +36,7 @@ import org.jeecg.modules.adweb.dmp.service.IGASourceMediumReportService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.core.ParameterizedTypeReference;
|
|
|
+import org.springframework.core.annotation.AnnotationUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
@@ -52,6 +54,14 @@ import java.util.Objects;
|
|
|
@Service
|
|
|
public class GAReportService {
|
|
|
|
|
|
+ // GA report entities对应的数据表名称
|
|
|
+ private static final String TABLE_GA_COUNTRY_REPORT =
|
|
|
+ AnnotationUtils.findAnnotation(GACountryReport.class, TableName.class).value();
|
|
|
+ private static final String TABLE_GA_SOURCE_MEDIUM_REPORT =
|
|
|
+ AnnotationUtils.findAnnotation(GASourceMediumReport.class, TableName.class).value();
|
|
|
+ private static final String TABLE_GA_PAGE_PATH_REPORT =
|
|
|
+ AnnotationUtils.findAnnotation(GAPagePathReport.class, TableName.class).value();
|
|
|
+
|
|
|
private static final String GA_REPORT_API_PATH = "/api/google/ga/report";
|
|
|
|
|
|
@Value("${data-bridge.api.host}")
|
|
@@ -61,9 +71,7 @@ public class GAReportService {
|
|
|
private String dataBridgeApiToken;
|
|
|
|
|
|
@Autowired private IGACountryReportService gaCountryReportService;
|
|
|
-
|
|
|
@Autowired private IGASourceMediumReportService gaSourceMediumReportService;
|
|
|
-
|
|
|
@Autowired private IGAPagePathReportService gaPagePathReportService;
|
|
|
|
|
|
@Autowired private CommonMapper commonMapper;
|
|
@@ -77,11 +85,11 @@ public class GAReportService {
|
|
|
|
|
|
/** 拉取并同步Google Analytics报表 */
|
|
|
public void syncGAReport() {
|
|
|
- // TODO
|
|
|
+ // TODO: 获取可查的GoogleGTM对象
|
|
|
List<GoogleGTM> googleGTMS = Collections.emptyList();
|
|
|
|
|
|
- // 每个帐号同步三张报表
|
|
|
for (GoogleGTM googleGTM : googleGTMS) {
|
|
|
+ // 每个帐号同步更新三张报表
|
|
|
try {
|
|
|
this.syncGACountryReport(googleGTM);
|
|
|
this.syncGASourceMediumReport(googleGTM);
|
|
@@ -99,7 +107,7 @@ public class GAReportService {
|
|
|
*/
|
|
|
private void syncGACountryReport(GoogleGTM googleGTM) {
|
|
|
// 1. 报表时间区间
|
|
|
- Date startDate = this.getReportStartDate("dmp_ga_country_report", googleGTM.getSiteId());
|
|
|
+ Date startDate = this.getReportStartDate(TABLE_GA_COUNTRY_REPORT, googleGTM.getSiteId());
|
|
|
Date endDate = new Date();
|
|
|
|
|
|
// 2. 构建GA报表请求参数
|
|
@@ -135,11 +143,9 @@ public class GAReportService {
|
|
|
}
|
|
|
|
|
|
// 3. 更新数据库 - 删除旧数据,插入新数据
|
|
|
- QueryWrapper<GACountryReport> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.eq("site_id", googleGTM.getSiteId());
|
|
|
- queryWrapper.ge("date", startDate);
|
|
|
- queryWrapper.le("date", endDate);
|
|
|
- gaCountryReportService.remove(queryWrapper);
|
|
|
+ gaCountryReportService.remove(
|
|
|
+ this.getRemoveQueryWrapper(
|
|
|
+ GACountryReport.class, googleGTM.getSiteId(), startDate, endDate));
|
|
|
|
|
|
gaCountryReportService.saveBatch(countryReport, countryReport.size());
|
|
|
}
|
|
@@ -152,7 +158,7 @@ public class GAReportService {
|
|
|
private void syncGASourceMediumReport(GoogleGTM googleGTM) {
|
|
|
// 1. 报表时间区间
|
|
|
Date startDate =
|
|
|
- this.getReportStartDate("dmp_ga_source_medium_report", googleGTM.getSiteId());
|
|
|
+ this.getReportStartDate(TABLE_GA_SOURCE_MEDIUM_REPORT, googleGTM.getSiteId());
|
|
|
Date endDate = new Date();
|
|
|
|
|
|
// 2. 构建GA报表请求参数
|
|
@@ -207,11 +213,9 @@ public class GAReportService {
|
|
|
}
|
|
|
|
|
|
// 3. 更新数据库 - 删除旧数据,插入新数据
|
|
|
- QueryWrapper<GASourceMediumReport> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.eq("site_id", googleGTM.getSiteId());
|
|
|
- queryWrapper.ge("date", startDate);
|
|
|
- queryWrapper.le("date", endDate);
|
|
|
- gaSourceMediumReportService.remove(queryWrapper);
|
|
|
+ gaSourceMediumReportService.remove(
|
|
|
+ this.getRemoveQueryWrapper(
|
|
|
+ GASourceMediumReport.class, googleGTM.getSiteId(), startDate, endDate));
|
|
|
|
|
|
gaSourceMediumReportService.saveBatch(sourceMediumReport, sourceMediumReport.size());
|
|
|
}
|
|
@@ -223,7 +227,7 @@ public class GAReportService {
|
|
|
*/
|
|
|
private void syncGAPagePathReport(GoogleGTM googleGTM) {
|
|
|
// 1. 报表时间区间
|
|
|
- Date startDate = this.getReportStartDate("dmp_ga_page_path_report", googleGTM.getSiteId());
|
|
|
+ Date startDate = this.getReportStartDate(TABLE_GA_PAGE_PATH_REPORT, googleGTM.getSiteId());
|
|
|
Date endDate = new Date();
|
|
|
|
|
|
// 2. 构建GA报表请求参数
|
|
@@ -268,11 +272,9 @@ public class GAReportService {
|
|
|
}
|
|
|
|
|
|
// 3. 更新数据库 - 删除旧数据,插入新数据
|
|
|
- QueryWrapper<GAPagePathReport> queryWrapper = new QueryWrapper<>();
|
|
|
- queryWrapper.eq("site_id", googleGTM.getSiteId());
|
|
|
- queryWrapper.ge("date", startDate);
|
|
|
- queryWrapper.le("date", endDate);
|
|
|
- gaPagePathReportService.remove(queryWrapper);
|
|
|
+ gaPagePathReportService.remove(
|
|
|
+ this.getRemoveQueryWrapper(
|
|
|
+ GAPagePathReport.class, googleGTM.getSiteId(), startDate, endDate));
|
|
|
|
|
|
gaPagePathReportService.saveBatch(pagePathReport, pagePathReport.size());
|
|
|
}
|
|
@@ -281,13 +283,13 @@ public class GAReportService {
|
|
|
* 请求Google Analytics报表,http://data-bridge.v3.adwebcloud.com:9002/swagger-ui/index.html
|
|
|
*
|
|
|
* @param gaReportRequest
|
|
|
- * @param reportDataType
|
|
|
+ * @param reportDataClass
|
|
|
* @return
|
|
|
* @param <T>
|
|
|
*/
|
|
|
@VisibleForTesting
|
|
|
<T extends GAReportDataDTO> List<T> runGAReport(
|
|
|
- GAReportRequestDTO gaReportRequest, Class<T> reportDataType) {
|
|
|
+ GAReportRequestDTO gaReportRequest, Class<T> reportDataClass) {
|
|
|
log.info("runGAReport: report request = {}", FastJsonUtil.toJSONString(gaReportRequest));
|
|
|
|
|
|
// 1. 创建API request
|
|
@@ -305,7 +307,7 @@ public class GAReportService {
|
|
|
ParameterizedTypeReference.forType(
|
|
|
TypeUtils.parameterize(
|
|
|
OpenAPIResponse.class,
|
|
|
- TypeUtils.parameterize(List.class, reportDataType))));
|
|
|
+ TypeUtils.parameterize(List.class, reportDataClass))));
|
|
|
|
|
|
// 3. 日志并返回
|
|
|
log.info(
|
|
@@ -319,7 +321,7 @@ public class GAReportService {
|
|
|
*
|
|
|
* <p>1. 若表中没有数据,取一年前
|
|
|
*
|
|
|
- * <p>2. 表中最大时间减一天 - 如10月10号凌晨2点执行,最大时间可能是10号,但9号数据GA侧有更新
|
|
|
+ * <p>2. 表中最大时间减一天 - 如10月10号凌晨2点执行,最大时间可能是10号;同时9号数据GA侧有可能更新
|
|
|
*/
|
|
|
private Date getReportStartDate(String tableName, int siteId) {
|
|
|
Date maxDate = commonMapper.getMaxDate(tableName, "date", "site_id = " + siteId);
|
|
@@ -330,4 +332,14 @@ public class GAReportService {
|
|
|
// 2. 最大时间减一天
|
|
|
return DateUtil.plusDays(maxDate, -1);
|
|
|
}
|
|
|
+
|
|
|
+ /** 生成{@link QueryWrapper}, 用于删除GA报表历史数据的 */
|
|
|
+ private <T> QueryWrapper<T> getRemoveQueryWrapper(
|
|
|
+ Class<T> entityClass, int siteId, Date startDate, Date endDate) {
|
|
|
+ QueryWrapper<T> queryWrapper = new QueryWrapper<>(entityClass);
|
|
|
+ queryWrapper.eq("site_id", siteId);
|
|
|
+ queryWrapper.ge("date", startDate);
|
|
|
+ queryWrapper.le("date", endDate);
|
|
|
+ return queryWrapper;
|
|
|
+ }
|
|
|
}
|