|
|
@@ -0,0 +1,58 @@
|
|
|
+package cn.seecoder.fdroidrepository.Service.ServiceImpl;
|
|
|
+
|
|
|
+
|
|
|
+import cn.seecoder.fdroidrepository.utils.RedisUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class DateService {
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+ private SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
|
|
|
+
|
|
|
+ public void recordUV(String ip){
|
|
|
+ String redisKey = RedisUtil.getUVKey(df.format(new Date()));
|
|
|
+ redisTemplate.opsForHyperLogLog().add(redisKey,ip);
|
|
|
+ }
|
|
|
+
|
|
|
+ public long calculateUV(Date start,Date end) {
|
|
|
+ if(start==null||end==null){
|
|
|
+ throw new IllegalStateException("params can not be null!");
|
|
|
+ }
|
|
|
+ List<String> keyList = new ArrayList<>();
|
|
|
+ Calendar calendar=Calendar.getInstance();
|
|
|
+ calendar.setTime(start);
|
|
|
+ while (!calendar.getTime().after(end)){
|
|
|
+ String key = RedisUtil.getUVKey(df.format(calendar.getTime()));
|
|
|
+ keyList.add(key);
|
|
|
+ calendar.add(Calendar.DATE,1);
|
|
|
+ }
|
|
|
+ String redisKey = RedisUtil.getUVKey(df.format(start),df.format(end));
|
|
|
+ redisTemplate.opsForHyperLogLog().union(redisKey,keyList.toArray());
|
|
|
+ return redisTemplate.opsForHyperLogLog().size(redisKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void recordDAU(int userId){
|
|
|
+ String redisKey = RedisUtil.getDAUKey(df.format(new Date()));
|
|
|
+ redisTemplate.opsForValue().setBit(redisKey,userId,true);
|
|
|
+ }
|
|
|
+
|
|
|
+ public long calculateDAU(Date start ,Date end){
|
|
|
+ if(start==null||end==null){
|
|
|
+ throw new IllegalStateException("params can not be null!");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|