wfansh 6 ヶ月 前
コミット
8a75e45190

+ 19 - 3
src/main/java/com/wechi/adweb/bridge/google/analytics/GADataService.java

@@ -1,6 +1,7 @@
 package com.wechi.adweb.bridge.google.analytics;
 
 import static com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO.*;
+import static com.wechi.adweb.bridge.google.analytics.dto.report.ReportType.*;
 
 import com.google.analytics.data.v1beta.*;
 import com.google.api.gax.core.FixedCredentialsProvider;
@@ -9,6 +10,9 @@ import com.google.gson.Gson;
 import com.wechi.adweb.bridge.exception.DataException;
 import com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO;
 import com.wechi.adweb.bridge.google.analytics.dto.report.ReportType;
+import com.wechi.adweb.bridge.google.analytics.dto.report.row.CountryChartData;
+import com.wechi.adweb.bridge.google.analytics.dto.report.row.GAReportDataDTO;
+import com.wechi.adweb.bridge.google.analytics.dto.report.row.SourceMediaViewData;
 
 import jakarta.annotation.PostConstruct;
 
@@ -20,6 +24,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
 import java.io.IOException;
+import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 
@@ -51,7 +56,8 @@ public class GADataService {
                         .build();
     }
 
-    public List<Object> runReport(GAReportRequestDTO reportRequest) throws DataException {
+    public List<? extends GAReportDataDTO> runReport(GAReportRequestDTO reportRequest)
+            throws DataException {
         // 0. Enhances the report request.
         this.amplifyReportRequest(reportRequest);
 
@@ -113,8 +119,8 @@ public class GADataService {
 
             RunReportResponse response = analyticsDataClient.runReport(request.build());
 
-            // TODO: Parses the response.
-            return null;
+            // Converts and returns.
+            return toReportData(response.getRowsList(), reportRequest);
         } catch (IOException e) {
             log.error(e.getMessage());
             throw new DataException(e);
@@ -148,4 +154,14 @@ public class GADataService {
             reportRequest.setOrderByDesc(reportType.isDefaultOrderByDesc());
         }
     }
+
+    private List<? extends GAReportDataDTO> toReportData(
+            List<Row> reportRows, GAReportRequestDTO reportRequest) {
+        return switch (reportRequest.getReportType()) {
+            case ADWEB_COUNTRY_CHART -> CountryChartData.fromReportRows(reportRows, reportRequest);
+            case ADWEB_SOURCE_MEDIUM_VIEW ->
+                    SourceMediaViewData.fromReportRows(reportRows, reportRequest);
+            default -> Collections.emptyList();
+        };
+    }
 }

+ 3 - 3
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/row/CountryChartRow.java → src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/row/CountryChartData.java

@@ -15,18 +15,18 @@ import java.util.List;
  */
 @Data
 @Builder
-public class CountryChartRow implements GAReportRowDTO {
+public class CountryChartData implements GAReportDataDTO {
     private String country;
     private int num;
 
-    public static List<CountryChartRow> fromReportRows(
+    public static List<CountryChartData> fromReportRows(
             List<Row> reportRows, GAReportRequestDTO reportRequest) {
         int indexCountry = reportRequest.getDimensions().indexOf(DIMENSION_COUNTRY);
 
         return reportRows.stream()
                 .map(
                         row ->
-                                CountryChartRow.builder()
+                                CountryChartData.builder()
                                         .country(row.getDimensionValues(indexCountry).getValue())
                                         // The first metric value.
                                         .num(Integer.parseInt(row.getMetricValues(0).getValue()))

+ 1 - 1
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/row/GAReportRowDTO.java → src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/row/GAReportDataDTO.java

@@ -3,4 +3,4 @@ package com.wechi.adweb.bridge.google.analytics.dto.report.row;
 /**
  * @author wfansh
  */
-public interface GAReportRowDTO {}
+public interface GAReportDataDTO {}

+ 3 - 3
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/row/SourceMediaViewRow.java → src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/row/SourceMediaViewData.java

@@ -16,7 +16,7 @@ import java.util.List;
  */
 @Data
 @Builder
-public class SourceMediaViewRow implements GAReportRowDTO {
+public class SourceMediaViewData implements GAReportDataDTO {
     private String type;
 
     private int totalUsers;
@@ -31,7 +31,7 @@ public class SourceMediaViewRow implements GAReportRowDTO {
 
     private String pageViewsPerSession;
 
-    public static List<SourceMediaViewRow> fromReportRows(
+    public static List<SourceMediaViewData> fromReportRows(
             List<Row> reportRows, GAReportRequestDTO reportRequest) {
         int indexType = reportRequest.getDimensions().indexOf(DIMENSION_SESSION_SOURCE_MEDIUM);
         int indexTotalUsers = reportRequest.getMetrics().indexOf(METRIC_TOTAL_USERS);
@@ -46,7 +46,7 @@ public class SourceMediaViewRow implements GAReportRowDTO {
         return reportRows.stream()
                 .map(
                         row ->
-                                SourceMediaViewRow.builder()
+                                SourceMediaViewData.builder()
                                         .type(row.getDimensionValues(indexType).getValue())
                                         .totalUsers(
                                                 Integer.parseInt(