Browse Source

GA report

wfansh 6 tháng trước cách đây
mục cha
commit
867524fab0

+ 38 - 8
src/main/java/com/wechi/adweb/bridge/google/analytics/GADataService.java

@@ -1,14 +1,14 @@
 package com.wechi.adweb.bridge.google.analytics;
 
-import static com.wechi.adweb.bridge.google.analytics.dto.GAReportRequestDTO.*;
+import static com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO.*;
 
 import com.google.analytics.data.v1beta.*;
 import com.google.api.gax.core.FixedCredentialsProvider;
 import com.google.auth.oauth2.GoogleCredentials;
-import com.google.common.base.Splitter;
 import com.google.gson.Gson;
 import com.wechi.adweb.bridge.exception.DataException;
-import com.wechi.adweb.bridge.google.analytics.dto.GAReportRequestDTO;
+import com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO;
+import com.wechi.adweb.bridge.google.analytics.dto.report.ReportType;
 
 import jakarta.annotation.PostConstruct;
 
@@ -17,6 +17,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
 import java.io.IOException;
 import java.util.List;
@@ -54,20 +55,21 @@ public class GADataService {
     }
 
     public List<Object> runReport(GAReportRequestDTO reportRequest) throws DataException {
+        // 0. Enhances the report request.
+        this.amplifyReportRequest(reportRequest);
+
         try (BetaAnalyticsDataClient analyticsDataClient =
                 BetaAnalyticsDataClient.create(dataSettings)) {
             // 1. Dimensions.
             List<Dimension> dimensions =
-                    Splitter.on(GAReportRequestDTO.DEFAULT_SPLITTER)
-                            .splitToStream(StringUtils.defaultString(reportRequest.getDimensions()))
+                    reportRequest.getDimensions().stream()
                             .filter(StringUtils::isNotEmpty)
                             .map(dimension -> Dimension.newBuilder().setName(dimension).build())
                             .toList();
 
             // 2. Metrics.
             List<Metric> metrics =
-                    Splitter.on(GAReportRequestDTO.DEFAULT_SPLITTER)
-                            .splitToStream(StringUtils.defaultString(reportRequest.getMetrics()))
+                    reportRequest.getMetrics().stream()
                             .filter(StringUtils::isNotEmpty)
                             .map(metric -> Metric.newBuilder().setName(metric).build())
                             .toList();
@@ -96,7 +98,7 @@ public class GADataService {
                 }
 
                 // Desc.
-                orderBy.setDesc(reportRequest.isOrderByDesc());
+                orderBy.setDesc(reportRequest.getOrderByDesc());
             }
 
             RunReportRequest.Builder request =
@@ -121,4 +123,32 @@ public class GADataService {
             throw new DataException(e);
         }
     }
+
+    /** Enhances the report request by applying default settings from {@link ReportType}. */
+    private void amplifyReportRequest(GAReportRequestDTO reportRequest) {
+        ReportType reportType = reportRequest.getReportType();
+        if (Objects.isNull(reportType)) {
+            return;
+        }
+
+        if (CollectionUtils.isEmpty(reportRequest.getMetrics())) {
+            reportRequest.setMetrics(reportType.getDefaultMetrics());
+        }
+
+        if (CollectionUtils.isEmpty(reportRequest.getDimensions())) {
+            reportRequest.setDimensions(reportType.getDefaultDimensions());
+        }
+
+        if (Objects.isNull(reportRequest.getOrderBy())) {
+            reportRequest.setOrderBy(reportType.getDefaultOrderBy());
+        }
+
+        if (Objects.isNull(reportRequest.getOrderByType())) {
+            reportRequest.setOrderByType(reportType.getDefaultOrderByType());
+        }
+
+        if (Objects.isNull(reportRequest.getOrderByDesc())) {
+            reportRequest.setOrderByDesc(reportType.isDefaultOrderByDesc());
+        }
+    }
 }

+ 0 - 34
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/GAReportRequestDTO.java

@@ -1,34 +0,0 @@
-package com.wechi.adweb.bridge.google.analytics.dto;
-
-import lombok.Data;
-
-/**
- * @author wfansh
- */
-@Data
-public class GAReportRequestDTO {
-    public static final char DEFAULT_SPLITTER = ',';
-
-    private String propertyName;
-
-    private String startDate;
-
-    private String endDate;
-
-    private String metrics;
-
-    private String dimensions;
-
-    private String orderBy;
-
-    private OrderByType orderByType;
-
-    private boolean orderByDesc;
-
-    private String type;
-
-    public enum OrderByType {
-        METRICS,
-        DIMENSIONS
-    }
-}

+ 33 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/CountryChartRowDTO.java

@@ -0,0 +1,33 @@
+package com.wechi.adweb.bridge.google.analytics.dto.report;
+
+import com.google.analytics.data.v1beta.Row;
+
+import lombok.Builder;
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @author wfansh
+ */
+@Data
+@Builder
+public class CountryChartRowDTO implements GAReportRowDTO {
+    private String country;
+    private int num;
+
+    public static List<CountryChartRowDTO> fromReportRows(
+            List<Row> reportRows, GAReportRequestDTO reportRequest) {
+        int indexCountry = reportRequest.getDimensions().indexOf(ReportConstant.DIMENSION_COUNTRY);
+
+        return reportRows.stream()
+                .map(
+                        row ->
+                                CountryChartRowDTO.builder()
+                                        .country(row.getDimensionValues(indexCountry).getValue())
+                                        // The first metric value.
+                                        .num(Integer.parseInt(row.getMetricValues(0).getValue()))
+                                        .build())
+                .toList();
+    }
+}

+ 40 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/GAReportRequestDTO.java

@@ -0,0 +1,40 @@
+package com.wechi.adweb.bridge.google.analytics.dto.report;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @author wfansh
+ */
+@Data
+public class GAReportRequestDTO {
+
+    private String propertyName;
+
+    private ReportType reportType;
+
+    private String startDate;
+
+    private String endDate;
+
+    /** Use default values in {@link ReportType} instead. */
+    @Deprecated private List<String> metrics;
+
+    /** Use default values in {@link ReportType} instead. */
+    @Deprecated private List<String> dimensions;
+
+    /** Use default values in {@link ReportType} instead. */
+    @Deprecated private String orderBy;
+
+    /** Use default values in {@link ReportType} instead. */
+    @Deprecated private OrderByType orderByType;
+
+    /** Use default values in {@link ReportType} instead. */
+    @Deprecated private Boolean orderByDesc;
+
+    public enum OrderByType {
+        METRICS,
+        DIMENSIONS
+    }
+}

+ 10 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/GAReportRowDTO.java

@@ -0,0 +1,10 @@
+package com.wechi.adweb.bridge.google.analytics.dto.report;
+
+import com.google.analytics.data.v1beta.Row;
+
+/**
+ * @author wfansh
+ */
+public interface GAReportRowDTO {
+
+}

+ 12 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/ReportConstant.java

@@ -0,0 +1,12 @@
+package com.wechi.adweb.bridge.google.analytics.dto.report;
+
+/**
+ * @author wfansh
+ */
+public class ReportConstant {
+
+    public static final String METRIC_TOTAL_USERS = "totalUsers";
+
+    public static final String DIMENSION_COUNTRY = "country";
+    public static final String DIMENSION_DATE = "date";
+}

+ 42 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/dto/report/ReportType.java

@@ -0,0 +1,42 @@
+package com.wechi.adweb.bridge.google.analytics.dto.report;
+
+import static com.wechi.adweb.bridge.google.analytics.dto.report.ReportConstant.*;
+
+import lombok.Getter;
+
+import java.util.List;
+
+/**
+ * @author wfansh
+ */
+@Getter
+public enum ReportType {
+    ADWEB3_COUNTRY_CHART(List.of(METRIC_TOTAL_USERS), List.of(DIMENSION_COUNTRY));
+
+    private ReportType(List<String> defaultMetrics, List<String> defaultDimensions) {
+        this.defaultMetrics = defaultMetrics;
+        this.defaultDimensions = defaultDimensions;
+        this.defaultOrderBy = null;
+        this.defaultOrderByType = null;
+        this.defaultOrderByDesc = false;
+    }
+
+    private ReportType(
+            List<String> defaultMetrics,
+            List<String> defaultDimensions,
+            String defaultOrderBy,
+            GAReportRequestDTO.OrderByType defaultOrderByType,
+            boolean defaultOrderByDesc) {
+        this.defaultMetrics = defaultMetrics;
+        this.defaultDimensions = defaultDimensions;
+        this.defaultOrderBy = defaultOrderBy;
+        this.defaultOrderByType = defaultOrderByType;
+        this.defaultOrderByDesc = defaultOrderByDesc;
+    }
+
+    private List<String> defaultMetrics;
+    private List<String> defaultDimensions;
+    private String defaultOrderBy;
+    private GAReportRequestDTO.OrderByType defaultOrderByType;
+    private boolean defaultOrderByDesc;
+}