Bladeren bron

fix(cockpit): preserve type metrics history total (#23)

Kazerin 8 uur geleden
bovenliggende
commit
90e233ffd4

+ 37 - 0
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/CockpitTypeMetricsControllerTest.java

@@ -120,6 +120,27 @@ class CockpitTypeMetricsControllerTest {
                 .andExpect(jsonPath("$.data.metrics[0].points[0].freshness").value("HISTORICAL"));
     }
 
+    @Test
+    void historyResponseUsesAdapterTotalForPartialPage() throws Exception {
+        when(adapter.loadHistory(
+                DEVICE_CODE,
+                java.time.OffsetDateTime.parse("2026-09-01T10:00:00+08:00"),
+                java.time.OffsetDateTime.parse("2026-09-02T10:00:00+08:00"),
+                2,
+                1)).thenReturn(partialSewageHistoryFixture());
+
+        mockMvc.perform(get("/api/cockpit/v1/equipments/" + DEVICE_CODE + "/metrics/history")
+                        .param("startTime", "2026-09-01T10:00:00+08:00")
+                        .param("endTime", "2026-09-02T10:00:00+08:00")
+                        .param("page", "2")
+                        .param("pageSize", "1"))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.data.page").value(2))
+                .andExpect(jsonPath("$.data.total").value(7))
+                .andExpect(jsonPath("$.data.metrics", org.hamcrest.Matchers.hasSize(1)))
+                .andExpect(jsonPath("$.data.metrics[0].points", org.hamcrest.Matchers.hasSize(1)));
+    }
+
     @Test
     void historyRejectsInvertedRange() throws Exception {
         mockMvc.perform(get("/api/cockpit/v1/equipments/" + DEVICE_CODE + "/metrics/history")
@@ -232,4 +253,20 @@ class CockpitTypeMetricsControllerTest {
                 sampledAt,
                 points);
     }
+    private CockpitTypeMetricsSnapshot partialSewageHistoryFixture() {
+        return new CockpitTypeMetricsSnapshot(
+                DEVICE_CODE,
+                "ws_sewage_environment",
+                "污水环境监测设备",
+                Collections.singletonList(new CockpitMetricSnapshot(
+                        "PH",
+                        "pH",
+                        null,
+                        "",
+                        null,
+                        null,
+                        Collections.singletonList(new CockpitMetricSnapshot.PointSnapshot(
+                                new BigDecimal("7.2"), "NORMAL", "2026-09-02T09:59:00+08:00")))),
+                7);
+    }
 }

+ 3 - 1
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/type/CockpitTypeMetricsService.java

@@ -145,6 +145,8 @@ public class CockpitTypeMetricsService {
             }
         }
 
+        Integer adapterTotal = snapshot.getTotal();
+        int responseTotal = adapterTotal == null ? total : adapterTotal;
         String state = metrics.isEmpty() || total == 0 ? "EMPTY" : "READY";
         return new CockpitTypeMetricsResponse(
                 requestId,
@@ -161,7 +163,7 @@ public class CockpitTypeMetricsService {
                 "Asia/Shanghai",
                 resolvedPage,
                 resolvedPageSize,
-                total);
+                responseTotal);
     }
 
     public CockpitErrorPayload invalidRequest(String requestId) {

+ 13 - 1
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/type/CockpitTypeMetricsSnapshot.java

@@ -7,20 +7,32 @@ public class CockpitTypeMetricsSnapshot {
     private final String deviceType;
     private final String deviceTypeName;
     private final List<CockpitMetricSnapshot> metrics;
+    private final Integer total;
 
     public CockpitTypeMetricsSnapshot(
             String deviceCode,
             String deviceType,
             String deviceTypeName,
             List<CockpitMetricSnapshot> metrics) {
+        this(deviceCode, deviceType, deviceTypeName, metrics, null);
+    }
+
+    public CockpitTypeMetricsSnapshot(
+            String deviceCode,
+            String deviceType,
+            String deviceTypeName,
+            List<CockpitMetricSnapshot> metrics,
+            Integer total) {
         this.deviceCode = deviceCode;
         this.deviceType = deviceType;
         this.deviceTypeName = deviceTypeName;
         this.metrics = metrics;
+        this.total = total;
     }
 
     public String getDeviceCode() { return deviceCode; }
     public String getDeviceType() { return deviceType; }
     public String getDeviceTypeName() { return deviceTypeName; }
     public List<CockpitMetricSnapshot> getMetrics() { return metrics; }
-}
+    public Integer getTotal() { return total; }
+}