wfansh 3 месяцев назад
Родитель
Сommit
2e4d4334d2

+ 4 - 4
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/controller/DMPDataController.java

@@ -91,7 +91,7 @@ public class DMPDataController {
             end = dateRange.getRight();
         }
 
-        // 2. 查询并返回
+        // 2. TODO: DB查询 / DataBridge API查询
         // return Result.ok(gaCountryReportService.getCountryStats(siteCode, start, end));
         return Result.ok(dmpReportService.getCountryStats(siteCode, start, end));
     }
@@ -109,7 +109,7 @@ public class DMPDataController {
             end = dateRange.getRight();
         }
 
-        // 2. 查询并返回
+        // 2. TODO: DB查询 / DataBridge API查询
         // return Result.ok(gaSourceMediumReportService.getSourceMediumStats(siteCode, start, end));
         return Result.ok(dmpReportService.getSourceMediumStats(siteCode, start, end));
     }
@@ -128,7 +128,7 @@ public class DMPDataController {
             end = dateRange.getRight();
         }
 
-        // 2. 查询并返回
+        // 2. TODO: DB查询 / DataBridge API查询
         // return Result.ok(
         //        gaPagePathReportService.getPagePathStats(
         //                siteCode, start, end, limit >= 0 ? limit : 10));
@@ -149,7 +149,7 @@ public class DMPDataController {
             end = dateRange.getRight();
         }
 
-        // 2. 查询并返回
+        // 2. TODO: DB查询 / DataBridge API查询
         // return Result.ok(gaDeviceReportService.getDeviceStats(siteCode, start, end));
         return Result.ok(dmpReportService.getDeviceStats(siteCode, start, end));
     }

+ 73 - 72
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/adweb/dmp/service/DMPReportService.java

@@ -7,7 +7,6 @@ import lombok.extern.slf4j.Slf4j;
 
 import org.apache.commons.compress.utils.Lists;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.tuple.Pair;
 import org.jeecg.modules.adweb.common.enums.CountryCode;
 import org.jeecg.modules.adweb.common.util.AdwebRedisUtil;
 import org.jeecg.modules.adweb.common.util.DateUtil;
@@ -33,6 +32,7 @@ import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * 从DataBridge直接生成GA报表数据,不依赖DB查询 - http://data-bridge.v3.adwebcloud.com:9002/swagger-ui/index.html
@@ -63,20 +63,16 @@ public class DMPReportService {
         }
 
         // 1. 构建GA报表请求参数
-        // 使用ReportType.ADWEB_CUSTOM_REPORT
-        Pair<String, String> dateRange = this.getDateRange(start, end);
-
-        GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
-        gaReportRequest.setPropertyResourceName(
-                GAPropertyDTO.toResourceName(googleGTM.getGaPropertyId()));
-        gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
-        gaReportRequest.setStartDate(dateRange.getLeft());
-        gaReportRequest.setEndDate(dateRange.getRight());
-        gaReportRequest.setMetrics(List.of(ReportConstant.METRIC_TOTAL_USERS));
-        gaReportRequest.setDimensions(List.of(ReportConstant.DIMENSION_COUNTRY));
-        gaReportRequest.setOrderByType(OrderByType.METRICS);
-        gaReportRequest.setOrderBy(ReportConstant.METRIC_TOTAL_USERS);
-        gaReportRequest.setOrderByDesc(true);
+        GAReportRequestDTO gaReportRequest =
+                this.buildGAReportRequest(
+                        googleGTM.getGaPropertyId(),
+                        start,
+                        end,
+                        List.of(ReportConstant.METRIC_TOTAL_USERS),
+                        List.of(ReportConstant.DIMENSION_COUNTRY),
+                        OrderByType.METRICS,
+                        ReportConstant.METRIC_TOTAL_USERS,
+                        true);
 
         // 2. 请求API接口
         List<CustomReportData> reportDataList =
@@ -127,27 +123,22 @@ public class DMPReportService {
         }
 
         // 1. 构建GA报表请求参数
-        // 使用ReportType.ADWEB_CUSTOM_REPORT
-        Pair<String, String> dateRange = this.getDateRange(start, end);
-
-        GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
-        gaReportRequest.setPropertyResourceName(
-                GAPropertyDTO.toResourceName(googleGTM.getGaPropertyId()));
-        gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
-        gaReportRequest.setStartDate(dateRange.getLeft());
-        gaReportRequest.setEndDate(dateRange.getRight());
-        gaReportRequest.setMetrics(
-                List.of(
+        GAReportRequestDTO gaReportRequest =
+                this.buildGAReportRequest(
+                        googleGTM.getGaPropertyId(),
+                        start,
+                        end,
+                        List.of(
+                                ReportConstant.METRIC_TOTAL_USERS,
+                                ReportConstant.METRIC_NEW_USERS,
+                                ReportConstant.METRIC_SESSIONS,
+                                ReportConstant.METRIC_SCREEN_PAGE_VIEWS,
+                                ReportConstant.METRIC_AVG_SESSION_DURATION,
+                                ReportConstant.METRIC_SCREEN_PAGE_VIEWS_PER_SESSION),
+                        List.of(ReportConstant.DIMENSION_SESSION_SOURCE_MEDIUM),
+                        OrderByType.METRICS,
                         ReportConstant.METRIC_TOTAL_USERS,
-                        ReportConstant.METRIC_NEW_USERS,
-                        ReportConstant.METRIC_SESSIONS,
-                        ReportConstant.METRIC_SCREEN_PAGE_VIEWS,
-                        ReportConstant.METRIC_AVG_SESSION_DURATION,
-                        ReportConstant.METRIC_SCREEN_PAGE_VIEWS_PER_SESSION));
-        gaReportRequest.setDimensions(List.of(ReportConstant.DIMENSION_SESSION_SOURCE_MEDIUM));
-        gaReportRequest.setOrderByType(OrderByType.METRICS);
-        gaReportRequest.setOrderBy(ReportConstant.METRIC_TOTAL_USERS);
-        gaReportRequest.setOrderByDesc(true);
+                        true);
 
         // 2. 请求API接口
         List<CustomReportData> reportDataList =
@@ -222,23 +213,18 @@ public class DMPReportService {
         }
 
         // 1. 构建GA报表请求参数
-        // 使用ReportType.ADWEB_CUSTOM_REPORT
-        Pair<String, String> dateRange = this.getDateRange(start, end);
-
-        GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
-        gaReportRequest.setPropertyResourceName(
-                GAPropertyDTO.toResourceName(googleGTM.getGaPropertyId()));
-        gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
-        gaReportRequest.setStartDate(dateRange.getLeft());
-        gaReportRequest.setEndDate(dateRange.getRight());
-        gaReportRequest.setMetrics(
-                List.of(
+        GAReportRequestDTO gaReportRequest =
+                this.buildGAReportRequest(
+                        googleGTM.getGaPropertyId(),
+                        start,
+                        end,
+                        List.of(
+                                ReportConstant.METRIC_SCREEN_PAGE_VIEWS,
+                                ReportConstant.METRIC_USER_ENGAGEMENT_DURATION),
+                        List.of(ReportConstant.DIMENSION_PAGE_PATH),
+                        OrderByType.METRICS,
                         ReportConstant.METRIC_SCREEN_PAGE_VIEWS,
-                        ReportConstant.METRIC_USER_ENGAGEMENT_DURATION));
-        gaReportRequest.setDimensions(List.of(ReportConstant.DIMENSION_PAGE_PATH));
-        gaReportRequest.setOrderByType(OrderByType.METRICS);
-        gaReportRequest.setOrderBy(ReportConstant.METRIC_SCREEN_PAGE_VIEWS);
-        gaReportRequest.setOrderByDesc(true);
+                        true);
 
         // 2. 请求API接口
         List<CustomReportData> reportDataList =
@@ -278,7 +264,9 @@ public class DMPReportService {
                             NumberUtil.safeDivide(pagePathStatsVO.getPageViews(), totalPVs), 2));
         }
 
-        return pagePathStatsVOs.subList(0, Math.min(pagePathStatsVOs.size(), limit));
+        return pagePathStatsVOs.stream()
+                .limit(limit)
+                .collect(Collectors.toList()); // 使用toList()方法返回UnmodifiableList,导致Redis类型解析异常
     }
 
     /** 拉取Google Analytics - Device报表 */
@@ -294,24 +282,19 @@ public class DMPReportService {
         }
 
         // 1. 构建GA报表请求参数
-        // 使用ReportType.ADWEB_CUSTOM_REPORT
-        Pair<String, String> dateRange = this.getDateRange(start, end);
-
-        GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
-        gaReportRequest.setPropertyResourceName(
-                GAPropertyDTO.toResourceName(googleGTM.getGaPropertyId()));
-        gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
-        gaReportRequest.setStartDate(dateRange.getLeft());
-        gaReportRequest.setEndDate(dateRange.getRight());
-        gaReportRequest.setMetrics(
-                List.of(
+        GAReportRequestDTO gaReportRequest =
+                this.buildGAReportRequest(
+                        googleGTM.getGaPropertyId(),
+                        start,
+                        end,
+                        List.of(
+                                ReportConstant.METRIC_TOTAL_USERS,
+                                ReportConstant.METRIC_SESSIONS,
+                                ReportConstant.METRIC_SCREEN_PAGE_VIEWS),
+                        List.of(ReportConstant.DIMENSION_PLATFORM_DEVICE_CATEGORY),
+                        OrderByType.METRICS,
                         ReportConstant.METRIC_TOTAL_USERS,
-                        ReportConstant.METRIC_SESSIONS,
-                        ReportConstant.METRIC_SCREEN_PAGE_VIEWS));
-        gaReportRequest.setDimensions(List.of(ReportConstant.DIMENSION_PLATFORM_DEVICE_CATEGORY));
-        gaReportRequest.setOrderByType(OrderByType.METRICS);
-        gaReportRequest.setOrderBy(ReportConstant.METRIC_TOTAL_USERS);
-        gaReportRequest.setOrderByDesc(true);
+                        true);
 
         // 2. 请求API接口
         List<CustomReportData> reportDataList =
@@ -360,14 +343,32 @@ public class DMPReportService {
         return googleGTM;
     }
 
-    private Pair<String, String> getDateRange(Date start, Date end) {
-        return Pair.of(
+    private GAReportRequestDTO buildGAReportRequest(
+            String propertyId,
+            Date start,
+            Date end,
+            List<String> metrics,
+            List<String> dimensions,
+            OrderByType orderByType,
+            String orderBy,
+            boolean orderByDesc) {
+        GAReportRequestDTO gaReportRequest = new GAReportRequestDTO();
+        gaReportRequest.setPropertyResourceName(GAPropertyDTO.toResourceName(propertyId));
+        gaReportRequest.setReportType(ReportType.ADWEB_CUSTOM_REPORT);
+        gaReportRequest.setStartDate(
                 DateUtil.formatDate(
                         Optional.ofNullable(start)
                                 // 如果start为空,设置为无限早时间
                                 .orElse(DateUtil.parseDate("2016-01-01", DateUtil.DATE_FORMAT)),
-                        DateUtil.DATE_FORMAT),
+                        DateUtil.DATE_FORMAT));
+        gaReportRequest.setEndDate(
                 DateUtil.formatDate(
                         Optional.ofNullable(end).orElse(new Date()), DateUtil.DATE_FORMAT));
+        gaReportRequest.setMetrics(metrics);
+        gaReportRequest.setDimensions(dimensions);
+        gaReportRequest.setOrderByType(orderByType);
+        gaReportRequest.setOrderBy(orderBy);
+        gaReportRequest.setOrderByDesc(orderByDesc);
+        return gaReportRequest;
     }
 }