Jelajahi Sumber

第二次接口代码优化

fuyuchuan 1 hari lalu
induk
melakukan
e191df9071

+ 42 - 197
fiveep-persistence/src/main/resources/mapper/mysql/HtAnalogDataMapper.xml

@@ -1562,234 +1562,79 @@
     </select>
 
     <select id="aggregateAll" resultType="map">
-        <!-- 1. today -->
-        SELECT 'today' AS type,
-        IFNULL(
-        (SELECT epp
+        SELECT 'today' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND freezingTime = #{today}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND freezingTime = #{today}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND freezingTime = #{today}
         UNION ALL
-        <!-- 2. yesterday -->
-        SELECT 'yesterday' AS type,
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND freezingTime = #{yesterday}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
+        SELECT 'month' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND freezingTime = #{yesterday}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND freezingTime BETWEEN #{monthStart}
+          AND #{monthEnd}
         UNION ALL
-        <!-- 3. month -->
-        SELECT 'month' AS type,
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND freezingTime BETWEEN #{monthStart} AND #{monthEnd}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
+        SELECT 'year' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND freezingTime BETWEEN #{monthStart} AND #{monthEnd}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND freezingTime BETWEEN #{yearStart}
+          AND #{yearEnd}
         UNION ALL
-        <!-- 4. last_month -->
-        SELECT 'last_month' AS type,
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND freezingTime BETWEEN #{lastMonthStart} AND #{lastMonthEnd}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
+        SELECT 'yesterday' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND freezingTime BETWEEN #{lastMonthStart} AND #{lastMonthEnd}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND freezingTime = #{yesterday}
         UNION ALL
-        <!-- 5. year -->
-        SELECT 'year' AS type,
-        IFNULL(
-        (SELECT epp
+        SELECT 'last_month' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND freezingTime BETWEEN #{yearStart} AND #{yearEnd}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND freezingTime BETWEEN #{yearStart} AND #{yearEnd}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND freezingTime BETWEEN #{lastMonthStart}
+          AND #{lastMonthEnd}
         UNION ALL
-        <!-- 6. last_year -->
-        SELECT 'last_year' AS type,
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND freezingTime BETWEEN #{lastYearStart} AND #{lastYearEnd}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
+        SELECT 'last_year' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND freezingTime BETWEEN #{lastYearStart} AND #{lastYearEnd}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND freezingTime BETWEEN #{lastYearStart}
+          AND #{lastYearEnd}
         UNION ALL
-        <!-- 7. same_day (按 dataTime 区间) -->
-        SELECT 'same_day' AS type,
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{sameDayStart} AND #{sameDayEnd}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
+        SELECT 'same_day' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{sameDayStart} AND #{sameDayEnd}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND dataTime BETWEEN #{sameDayStart}
+          AND #{sameDayEnd}
         UNION ALL
-        <!-- 8. same_month -->
-        SELECT 'same_month' AS type,
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{sameMonthStart} AND #{sameMonthEnd}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
+        SELECT 'same_month' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{sameMonthStart} AND #{sameMonthEnd}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND dataTime BETWEEN #{sameMonthStart}
+          AND #{sameMonthEnd}
         UNION ALL
-        <!-- 9. same_year -->
-        SELECT 'same_year' AS type,
-        IFNULL(
-        (SELECT epp
+        SELECT 'same_year' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{sameYearStart} AND #{sameYearEnd}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{sameYearStart} AND #{sameYearEnd}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND dataTime BETWEEN #{sameYearStart}
+          AND #{sameYearEnd}
         UNION ALL
-        <!-- 10. last_year_same_day -->
-        SELECT 'last_year_same_day' AS type,
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{lastYearSameDayStart} AND #{lastYearSameDayEnd}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
+        SELECT 'last_year_same_day' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{lastYearSameDayStart} AND #{lastYearSameDayEnd}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
-
+          AND dataTime BETWEEN #{lastYearSameDayStart}
+          AND #{lastYearSameDayEnd}
         UNION ALL
-        <!-- 11. last_year_same_month -->
-        SELECT 'last_year_same_month' AS type,
-        IFNULL(
-        (SELECT epp
-        FROM ht_analog_data
-        WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{lastYearSameMonthStart} AND #{lastYearSameMonthEnd}
-        ORDER BY dataTime DESC
-        LIMIT 1)
-        ,0)
-        -
-        IFNULL(
-        (SELECT epp
+        SELECT 'last_year_same_month' as type,
+               COALESCE(MAX(epp) - MIN(epp), 0) as value
         FROM ht_analog_data
         WHERE deviceName = #{deviceCode}
-        AND dataTime BETWEEN #{lastYearSameMonthStart} AND #{lastYearSameMonthEnd}
-        ORDER BY dataTime ASC
-        LIMIT 1)
-        ,0) AS value
+          AND dataTime BETWEEN #{lastYearSameMonthStart}
+          AND #{lastYearSameMonthEnd}
     </select>
 </mapper>

+ 15 - 3
fiveep-service/src/main/java/com/bizmatics/service/util/SiteFeeCacheService.java

@@ -5,6 +5,7 @@ import com.bizmatics.model.SiteElectricityRecord;
 import com.bizmatics.persistence.mapper.SiteElectricityRecordMapper;
 import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.stats.CacheStats;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import lombok.var;
@@ -99,8 +100,19 @@ public class SiteFeeCacheService {
      * 定时任务,用于打印本地缓存的命中率
      */
     @Scheduled(fixedDelay = 30_000)
-    public void siteLogStats() {
-        var stats = cache.stats();
-        log.info("本地缓存命中率: {}%", String.format("%.2f", stats.hitRate() * 100));
+    public void logCacheStats() {
+        CacheStats stats = cache.stats();
+        long size = cache.estimatedSize();
+
+        // 预先格式化数字
+        String hitRateFormatted = String.format("%.2f", stats.hitRate() * 100);
+        String avgLoadTimeFormatted = String.format("%.2f", stats.averageLoadPenalty() / 1_000_000.0);
+
+        log.info("缓存统计 - 大小: {}, 命中率: {}%, 加载次数: {}, 加载失败: {}, 平均加载时间: {}ms",
+                size,
+                hitRateFormatted,
+                stats.loadCount(),
+                stats.loadFailureCount(),
+                avgLoadTimeFormatted);
     }
 }