Pārlūkot izejas kodu

GA report controller

wfansh 6 mēneši atpakaļ
vecāks
revīzija
92b2374118

+ 23 - 0
src/main/java/com/wechi/adweb/bridge/common/APIStatus.java

@@ -0,0 +1,23 @@
+package com.wechi.adweb.bridge.common;
+
+import com.fasterxml.jackson.annotation.JsonValue;
+
+import lombok.AllArgsConstructor;
+
+/**
+ * @author wfansh
+ */
+@AllArgsConstructor
+public enum APIStatus {
+    SUCCESS(200),
+    BAD_REQUEST(400),
+    UNAUTHORIZED(401),
+    INTERNAL_SERVER_ERROR(500);
+
+    @JsonValue
+    public int getCode() {
+        return code;
+    }
+
+    private final int code;
+}

+ 13 - 0
src/main/java/com/wechi/adweb/bridge/common/BaseController.java

@@ -0,0 +1,13 @@
+package com.wechi.adweb.bridge.common;
+
+/**
+ * @author wfansh
+ */
+public abstract class BaseController {
+
+    protected double getElapsedSeconds(long start) {
+        long now = System.currentTimeMillis();
+        long elapsedMilliSeconds = now - start;
+        return (double) elapsedMilliSeconds / 1000;
+    }
+}

+ 18 - 0
src/main/java/com/wechi/adweb/bridge/common/OpenAPIRequest.java

@@ -0,0 +1,18 @@
+package com.wechi.adweb.bridge.common;
+
+import lombok.Builder;
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+@Builder
+public class OpenAPIRequest<T> {
+
+    private String remoteIp;
+
+    private long requestTime;
+
+    private T data;
+}

+ 18 - 0
src/main/java/com/wechi/adweb/bridge/common/OpenAPIResponse.java

@@ -0,0 +1,18 @@
+package com.wechi.adweb.bridge.common;
+
+import lombok.Builder;
+import lombok.Data;
+
+/**
+ * @author wfansh
+ */
+@Data
+@Builder
+public class OpenAPIResponse<T> {
+
+    @Builder.Default private APIStatus status = APIStatus.SUCCESS;
+
+    private String message;
+
+    private T data;
+}

+ 67 - 0
src/main/java/com/wechi/adweb/bridge/google/analytics/controller/GAController.java

@@ -0,0 +1,67 @@
+package com.wechi.adweb.bridge.google.analytics.controller;
+
+import com.google.gson.Gson;
+import com.wechi.adweb.bridge.common.APIStatus;
+import com.wechi.adweb.bridge.common.BaseController;
+import com.wechi.adweb.bridge.common.OpenAPIRequest;
+import com.wechi.adweb.bridge.common.OpenAPIResponse;
+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.data.GAReportDataDTO;
+import com.wechi.adweb.bridge.google.analytics.service.GADataService;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.CollectionUtils;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * @author wfansh
+ */
+@Slf4j
+@RestController
+@RequestMapping("/api/google/ga")
+public class GAController extends BaseController {
+    @Autowired private GADataService gaDataService;
+
+    private final Gson gson = new Gson();
+
+    @RequestMapping("/report")
+    public OpenAPIResponse<List<? extends GAReportDataDTO>> getGAReport(
+            @RequestBody OpenAPIRequest<GAReportRequestDTO> apiRequest) throws DataException {
+        long start = System.currentTimeMillis();
+        log.info("****** getGAReport() ****** apiRequest = {}", gson.toJson(apiRequest));
+        GAReportRequestDTO reportRequest = apiRequest.getData();
+
+        // 1. Validates request parameters.
+        if (Objects.isNull(reportRequest)
+                || StringUtils.isEmpty(reportRequest.getPropertyName())
+                || Objects.isNull(reportRequest.getReportType())
+                || StringUtils.isEmpty(reportRequest.getStartDate())
+                || StringUtils.isEmpty(reportRequest.getEndDate())
+                || (ReportType.ADWEB_CUSTOM_REPORT.equals(reportRequest.getReportType())
+                        && CollectionUtils.isEmpty(reportRequest.getMetrics())
+                        && CollectionUtils.isEmpty(reportRequest.getDimensions()))) {
+            return OpenAPIResponse.<List<? extends GAReportDataDTO>>builder()
+                    .status(APIStatus.BAD_REQUEST)
+                    .message("Invalid request parameters")
+                    .build();
+        }
+
+        // 2. Executes API requests.
+        List<? extends GAReportDataDTO> report = gaDataService.runReport(apiRequest.getData());
+        log.info("****** getGAReport() ****** duration = {} seconds", getElapsedSeconds(start));
+        return OpenAPIResponse.<List<? extends GAReportDataDTO>>builder()
+                .status(APIStatus.SUCCESS)
+                .data(report)
+                .build();
+    }
+}

+ 1 - 1
src/main/java/com/wechi/adweb/bridge/google/analytics/GAAdminService.java → src/main/java/com/wechi/adweb/bridge/google/analytics/service/GAAdminService.java

@@ -1,4 +1,4 @@
-package com.wechi.adweb.bridge.google.analytics;
+package com.wechi.adweb.bridge.google.analytics.service;
 
 import com.google.analytics.admin.v1beta.*;
 import com.google.api.client.util.Lists;

+ 1 - 1
src/main/java/com/wechi/adweb/bridge/google/analytics/GADataService.java → src/main/java/com/wechi/adweb/bridge/google/analytics/service/GADataService.java

@@ -1,4 +1,4 @@
-package com.wechi.adweb.bridge.google.analytics;
+package com.wechi.adweb.bridge.google.analytics.service;
 
 import static com.wechi.adweb.bridge.google.analytics.dto.report.GAReportRequestDTO.*;
 

+ 2 - 0
src/test/java/com/wechi/adweb/bridge/google/analytics/GAServiceTests.java

@@ -6,6 +6,8 @@ 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.service.GAAdminService;
+import com.wechi.adweb.bridge.google.analytics.service.GADataService;
 
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;