ggbocoder 2 лет назад
Родитель
Сommit
979c2a61b9

+ 19 - 2
src/main/java/cn/seecoder/fdroidrepository/Controller/DataController.java

@@ -9,6 +9,7 @@ import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PostMapping;
 
 import java.util.Date;
+import java.util.HashMap;
 import java.util.Map;
 
 @Controller
@@ -18,7 +19,23 @@ public class DataController {
 
 
     @PostMapping("/data/uv")
-    public SingleResult<Map> getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @DateTimeFormat(pattern = "yyyy-MM-dd")Date end){
+    public SingleResult<?> getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @DateTimeFormat(pattern = "yyyy-MM-dd")Date end){
+        long uv = dateService.calculateUV(start,end);
+        Map<String,Object> map =new HashMap<>();
+        map.put("uvResult",uv);
+        map.put("uvStartDate",start);
+        map.put("uvEndDate",end);
+        return SingleResult.success(map);
+    }
+
+    @PostMapping("/data/dau")
+    public SingleResult<?> getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @DateTimeFormat(pattern = "yyyy-MM-dd")Date end) {
+        long dau = dateService.calculateDAU(start,end);
+        Map<String,Object> map =new HashMap<>();
+        map.put("dauResult",dau);
+        map.put("dauStartDate",start);
+        map.put("dauEndDate",end);
+        return SingleResult.success(map);
+    }
 
-    })
 }

+ 1 - 1
src/main/java/cn/seecoder/fdroidrepository/Service/AppService.java

@@ -1,4 +1,4 @@
 package cn.seecoder.fdroidrepository.Service;
 
-public class ApkService {
+public class AppService {
 }

+ 20 - 0
src/main/java/cn/seecoder/fdroidrepository/Service/ServiceImpl/DateService.java

@@ -3,6 +3,9 @@ package cn.seecoder.fdroidrepository.Service.ServiceImpl;
 
 import cn.seecoder.fdroidrepository.utils.RedisUtil;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.connection.RedisConnection;
+import org.springframework.data.redis.connection.RedisStringCommands;
+import org.springframework.data.redis.core.RedisCallback;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
@@ -51,7 +54,24 @@ public class DateService {
             throw new IllegalStateException("params can not be null!");
         }
 
+        List<byte[]> keyList = new ArrayList<>();
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(start);
+        while (!calendar.getTime().after(end)){
+            String key = RedisUtil.getDAUKey(df.format(calendar.getTime()));
+            keyList.add(key.getBytes());
+            calendar.add(Calendar.DATE,1);
+        }
 
+        return (long) redisTemplate.execute(new RedisCallback() {
+            @Override
+            public Object doInRedis(RedisConnection connection)  {
+                String redisKey = RedisUtil.getDAUKey(df.format(start), df.format(end));
+                connection.stringCommands().bitOp(RedisStringCommands.BitOperation.OR,
+                    redisKey.getBytes(), keyList.toArray(new byte[0][0]));
+                return connection.stringCommands().bitCount(redisKey.getBytes());
+            }
+        });
     }
 }
 

+ 8 - 0
src/main/java/cn/seecoder/fdroidrepository/config/WebMvcConfig.java

@@ -1,8 +1,10 @@
 package cn.seecoder.fdroidrepository.config;
 
 
+import cn.seecoder.fdroidrepository.interceptor.DataInterceptor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
 @Configuration
@@ -11,5 +13,11 @@ public class WebMvcConfig implements WebMvcConfigurer {
     @Autowired
     private DataInterceptor dataInterceptor;
 
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        registry.addInterceptor(dataInterceptor)
+            .excludePathPatterns("/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.jpeg");
+    }
+
 
 }

+ 30 - 0
src/main/java/cn/seecoder/fdroidrepository/interceptor/DataInterceptor.java

@@ -0,0 +1,30 @@
+package cn.seecoder.fdroidrepository.interceptor;
+
+import cn.seecoder.fdroidrepository.Service.ServiceImpl.DateService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.HandlerInterceptor;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@Component
+public class DataInterceptor implements HandlerInterceptor {
+
+    @Autowired
+    private DateService dateService;
+
+
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+        // count UV
+        String ip = request.getRemoteHost();
+        dateService.recordUV(ip);
+
+        // count DAU
+
+        dateService.recordDAU(Integer.parseInt(ip));
+
+        return true;
+    }
+}

+ 4 - 0
src/main/java/cn/seecoder/fdroidrepository/utils/RedisUtil.java

@@ -26,4 +26,8 @@ public class RedisUtil {
     public static String getDAUKey(String time){
         return "dau"+REDIS_SPLIT_KEY+time;
     }
+
+    public static String getDAUKey(String start,String end){
+        return "dau"+REDIS_SPLIT_KEY+start+REDIS_SPLIT_KEY+end;
+    }
 }

+ 25 - 0
src/test/java/cn/seecoder/fdroidrepository/Service/ServiceImpl/DateServiceTest.java

@@ -0,0 +1,25 @@
+package cn.seecoder.fdroidrepository.Service.ServiceImpl;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import java.util.Date;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@SpringBootTest
+class DateServiceTest {
+
+    @Autowired
+    private DateService dateService;
+
+
+
+    @Test
+    void calculateDAU() {
+        dateService.recordDAU(1);
+        dateService.recordDAU(2);
+        assertEquals(2,dateService.calculateDAU(new Date(),new Date()));
+    }
+}