Răsfoiți Sursa

修改tag分析接口算法

JensionDzero 1 an în urmă
părinte
comite
c76c28aa08

+ 16 - 5
benyun-core/src/main/java/com/ruoyi/benyun/config/QuartzConfig.java

@@ -18,7 +18,11 @@ import java.util.TimeZone;
 @Slf4j
 @Configuration
 public class QuartzConfig {
-    // 创建一个封装Job对象的类型JobDetall,并放入Spring容器中
+
+    /**
+     * 给网点打标签
+     * @return
+     */
     @Bean
     public JobDetail AnalyseJobDetail() {
         log.info("AnalyseJobDetail");
@@ -29,11 +33,11 @@ public class QuartzConfig {
             .build();
     }
 
-    //触发器声明,设置job的运行时机,并放入Spring容器中
+
     @Bean
     public Trigger AnalyseTrigger() {
         //定义Cron表达式
-        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0 0 1/15 * ? ");
+        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0 23 24 * ? ");
         cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 
         return TriggerBuilder
@@ -44,6 +48,10 @@ public class QuartzConfig {
             .build();
     }
 
+    /**
+     * 将网点载入redis中
+     * @return
+     */
     @Bean
     public JobDetail WriteRedisJobDetail() {
         log.info("WriteRedisJobDetail");
@@ -59,7 +67,7 @@ public class QuartzConfig {
     @Bean
     public Trigger WriteRedisTrigger(){
         //定义Cron表达式
-        CronScheduleBuilder cron  = CronScheduleBuilder.cronSchedule("0 0 0 22 11 ? *");
+        CronScheduleBuilder cron  = CronScheduleBuilder.cronSchedule("0 0 1 ? * ? ");
 
         cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 
@@ -71,6 +79,9 @@ public class QuartzConfig {
             .build();
     }
 
+    /**
+     * 品牌分析
+     * */
     @Bean
     public JobDetail BrandStatsiticsJobDetail() {
         log.info("BrandStatsiticsJobDetail");
@@ -85,7 +96,7 @@ public class QuartzConfig {
     @Bean
     public Trigger BrandStatsiticsTrigger() {
         //定义Cron表达式
-        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0 22 1/14 * ? *");
+        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0 23 30 * ? ");
         //CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 30 11 16 11 ? ");
         cron.inTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
 

+ 15 - 5
benyun-core/src/main/java/com/ruoyi/benyun/job/WriteRedisJob.java

@@ -8,6 +8,7 @@ import org.quartz.Job;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
 import java.time.LocalDateTime;
@@ -23,12 +24,21 @@ public class WriteRedisJob implements Job {
     @Autowired
     WdInfoDao wdInfoDao;
 
+    @Autowired
+    RedisTemplate redisTemplate;
+
     @Override
     public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
-        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
-        //LocalDateTime localDateTime = now.plusDays(-1);
-        LocalDateTime localDateTime = LocalDateTime.of(2023, 6, 1, 0,0);
-        log.info("开始载入redis:" + localDateTime.toString());
-        wdRedisStoreage.writeWdGeoRedis1(localDateTime.toString());
+        Boolean aBoolean = redisTemplate.hasKey("4501");
+        if (aBoolean){
+            LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
+            LocalDateTime localDateTime = now.plusDays(-1);
+            log.info("开始载入redis:" + localDateTime.toString());
+            wdRedisStoreage.writeWdGeoRedis1(localDateTime.toString());
+        }else {
+            LocalDateTime localDateTime = LocalDateTime.of(2023, 6, 1, 0,0);
+            log.info("开始载入redis:" + localDateTime.toString());
+            wdRedisStoreage.writeWdGeoRedis1(localDateTime.toString());
+        }
     }
 }

+ 67 - 44
benyun-core/src/main/java/com/ruoyi/benyun/service/impl/ChannelAnalyseServiceImpl.java

@@ -94,7 +94,6 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
     }
 
 
-    //TODO 待测试(redis版本)
     @Override
     public HashMap<String, Object> tagAnalyse(ChannelMapAceeptVo channelMapAceeptVo) {
         //1.根据条件的到位网点信息
@@ -102,60 +101,84 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
         queryWrapper.select("wd_id");
         assembleQueryWrapper(queryWrapper,channelMapAceeptVo);
         List<WdInfo> queryWd = wdInfoDao.selectList(queryWrapper);
+        if (!queryWd.isEmpty()) {
+            List<String> wdIds = queryWd.stream().map(item -> {
+                return item.getWdId();
+            }).collect(Collectors.toList());
+            queryWd.clear();
+
+
+            //分批统计
+            int pageSizePoint = 20;
+            int p = 0;
+            int q = pageSizePoint;
+            int total = 0;
+            HashMap<String,Integer> hashMap = new HashMap<>();
+            while(p < queryWd.size()){
+                if(q >= queryWd.size())
+                    q = queryWd.size();
+
+                //统计
+                total+=tagAnalyseItem(hashMap,wdIds.subList(p,q));
+                p=q;
+                q+=pageSizePoint;
+            }
+
+            //3.封装统计值
+            HashMap<String, Object> result = new HashMap<>();
+            List<TagAnalyse> list = new ArrayList<>();
+            for (String s : hashMap.keySet()) {
+                TagAnalyse tagAnalyse = new TagAnalyse();
+                tagAnalyse.setName(s);
+                tagAnalyse.setCount(hashMap.get(s));
+                BigDecimal bigDecimal = new BigDecimal((double) tagAnalyse.getCount() / total);
+                double v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
+                tagAnalyse.setRadio(v);
+                list.add(tagAnalyse);
+            }
+
+            //4.对结果集进行排序
+            Collections.sort(list, new Comparator<TagAnalyse>() {
+                @Override
+                public int compare(TagAnalyse o1, TagAnalyse o2) {
+                    return o2.getCount() - o1.getCount();
+                }
+            });
 
+            //5.分页返回结果集
+            int i = 1;
+            int start = 0,end = 6;
+            while(start<list.size()){
+                if(end>=list.size())
+                    end = list.size();
+                result.put(""+i++,new ArrayList(list.subList(start,end)));
+                start=end;
+                end+=6;
+            }
+            result.put("pages",i-1);
+            result.put("total",total);
+            return result;
+        }
+        return new HashMap<>();
+    }
+
+    public int tagAnalyseItem(HashMap<String,Integer> hashMap,List<String> keys){
         //2.获取网点周边标签,并进行统计
         int total = 0;
-        HashMap<String,Integer> hashMap = new HashMap<>();
-        for (WdInfo wdInfo : queryWd) {
-            List<String> wdTag = wdRedisStoreage.getWdTag(wdInfo);
-            if (wdTag == null)
+        List<List<String>> wdTagList = wdRedisStoreage.getWdTagList(keys);
+        for (List<String> list : wdTagList) {
+            if (list == null || list.size() == 0)
                 continue;
-            total++;
-            for (String s : wdTag) {
+            for (String s : list) {
                 Integer integer = hashMap.get(s);
                 if (integer == null)
                     hashMap.put(s,1);
                 else
                     hashMap.put(s,integer+1);
             }
-
-        }
-
-        //3.封装统计值
-        HashMap<String, Object> result = new HashMap<>();
-        List<TagAnalyse> list = new ArrayList<>();
-        for (String s : hashMap.keySet()) {
-            TagAnalyse tagAnalyse = new TagAnalyse();
-            tagAnalyse.setName(s);
-            tagAnalyse.setCount(hashMap.get(s));
-            BigDecimal bigDecimal = new BigDecimal((double) tagAnalyse.getCount() / total);
-            double v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
-            tagAnalyse.setRadio(v);
-            list.add(tagAnalyse);
-        }
-
-        //4.对结果集进行排序
-        Collections.sort(list, new Comparator<TagAnalyse>() {
-            @Override
-            public int compare(TagAnalyse o1, TagAnalyse o2) {
-                return o2.getCount() - o1.getCount();
-            }
-        });
-
-
-        //5.分页返回结果集
-        int i = 1;
-        int start = 0,end = 6;
-        while(start<list.size()){
-            if(end>=list.size())
-                end = list.size();
-            result.put(""+i++,new ArrayList(list.subList(start,end)));
-            start=end;
-            end+=6;
+            total++;
         }
-        result.put("pages",i-1);
-        result.put("total",total);
-        return result;
+        return total;
     }
 
     @Override

+ 11 - 11
pom.xml

@@ -77,17 +77,17 @@
                 <activeByDefault>true</activeByDefault>
             </activation>
         </profile>
-        <profile>
-            <id>prod</id>
-            <properties>
-                <profiles.active>prod</profiles.active>
-                <logging.level>warn</logging.level>
-            </properties>
-            <activation>
-                <!-- 默认环境 -->
-                <activeByDefault>false</activeByDefault>
-            </activation>
-        </profile>
+<!--        <profile>-->
+<!--            <id>prod</id>-->
+<!--            <properties>-->
+<!--                <profiles.active>prod</profiles.active>-->
+<!--                <logging.level>warn</logging.level>-->
+<!--            </properties>-->
+<!--            <activation>-->
+<!--                &lt;!&ndash; 默认环境 &ndash;&gt;-->
+<!--                <activeByDefault>false</activeByDefault>-->
+<!--            </activation>-->
+<!--        </profile>-->
     </profiles>
 
     <!-- 依赖声明 -->

+ 7 - 7
ruoyi-demo/src/main/java/com/ruoyi/demo/controller/ChannelAnalyseController.java

@@ -93,13 +93,13 @@ public class ChannelAnalyseController {
 
         //2.查看redis中是否存在有缓存
         HashMap<String, Object> result = new HashMap<>();
-        HashMap<String,Object> wdCount = (HashMap<String,Object>) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_TAG_ANLYSE).get(md5);
-        if (wdCount != null) {
-            result.put("data",(List<TagAnalyse>) wdCount.get(page + ""));
-            result.put("pages",(int)wdCount.get("pages"));
-            result.put("total",(int)wdCount.get("total"));
-            return R.ok(result);
-        }
+//        HashMap<String,Object> wdCount = (HashMap<String,Object>) redisTemplate.boundHashOps(RedisContant.CHANNEL_ANALYSE_TAG_ANLYSE).get(md5);
+//        if (wdCount != null) {
+//            result.put("data",(List<TagAnalyse>) wdCount.get(page + ""));
+//            result.put("pages",(int)wdCount.get("pages"));
+//            result.put("total",(int)wdCount.get("total"));
+//            return R.ok(result);
+//        }
         HashMap<String, Object> hashMap = channelAnalyseService.tagAnalyse(channelMapAceeptVo);
 
         //4.保存到redis中

+ 148 - 44
ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/ChannelAnalyseServiceImpl.java

@@ -36,6 +36,7 @@ import org.springframework.web.client.RestTemplate;
 import java.math.BigDecimal;
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.stream.Collectors;
@@ -112,7 +113,119 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
     }
 
 
-    //TODO 待测试(redis版本)
+    //版本1
+//    @Override
+//    public HashMap<String, Object> tagAnalyse(ChannelMapAceeptVo channelMapAceeptVo) {
+//        //1.根据条件的到位网点信息
+//        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
+//        queryWrapper.select("wd_id");
+//        assembleQueryWrapper(queryWrapper,channelMapAceeptVo);
+//        queryWrapper.and(queryWrapper2 ->{
+//            queryWrapper2.eq("show_delete",0);
+//        });
+//        List<WdInfo> queryWd = wdInfoDao.selectList(queryWrapper);
+//
+//        //2.获取周边标签
+//        List<String> collect = queryWd.stream().map(item -> {
+//            return RedisContant.WD_TAG + "_" + item.getWdId();
+//        }).collect(Collectors.toList());
+//
+//        //切割获取redis数据
+//        int split = 60000;
+//        int splitCount = collect.size()/split;
+//        int p1 = 0,p2 = split;
+//        ArrayList<CompletableFuture<List<List<String>>>> splitList = new ArrayList<>();
+//        for (int i=1;i<=splitCount;i++){
+//            if (p2 > collect.size())
+//                p2 = collect.size();
+//            List<String> list2 = collect.subList(p1, p2);
+//            CompletableFuture<List<List<String>>> future = CompletableFuture.supplyAsync(()->{
+//                return wdRedisStoreage.getWdTagList(list2);
+//            },executor);
+//            splitList.add(future);
+//            p1 = p2;
+//            p2+=split;
+//        }
+//
+//        //3.统计
+//        Hashtable<String, Integer> hashtable = new Hashtable<>();
+//        hashtable.put("total",0);
+//        ArrayList<CompletableFuture<Void>> list1 = new ArrayList<>();
+//        for (CompletableFuture<List<List<String>>> listCompletableFuture : splitList) {
+//            try {
+//                List<List<String>> lists = listCompletableFuture.get();
+//                for (List<String> list : lists) {
+//                    if (list != null && !list.isEmpty()){
+//                        CompletableFuture<Void> future = CompletableFuture.runAsync(()->{
+//                            for (String s : list) {
+//                                s += "边";
+//                                Integer integer = hashtable.get(s);
+//                                if (integer == null)
+//                                    hashtable.put(s,1);
+//                                else
+//                                    hashtable.put(s,integer+1);
+//                            }
+//                            hashtable.put("total",hashtable.get("total")+1);
+//                        },executor);
+//                        list1.add(future);
+//                    }
+//                }
+//                CompletableFuture.allOf(list1.toArray(new CompletableFuture[list1.size()])).join();
+//                lists = null;
+//            } catch (InterruptedException e) {
+//                throw new RuntimeException(e);
+//            } catch (ExecutionException e) {
+//                throw new RuntimeException(e);
+//            }
+//
+//        }
+//
+//        collect = null;
+//        queryWd = null;
+//
+//
+//        //4.封装统计值
+//        HashMap<String, Object> result = new HashMap<>();
+//        List<TagAnalyse> list = new ArrayList<>();
+//        Integer total = hashtable.get("total");
+//        for (String s : hashtable.keySet()) {
+//            if (s.equals("total"))
+//                continue;
+//
+//            TagAnalyse tagAnalyse = new TagAnalyse();
+//            tagAnalyse.setName(s);
+//            tagAnalyse.setCount(hashtable.get(s));
+//            BigDecimal bigDecimal = new BigDecimal((double) tagAnalyse.getCount() / total);
+//            double v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
+//            tagAnalyse.setRadio(v);
+//            list.add(tagAnalyse);
+//        }
+//
+//        //5.对结果集进行排序
+//        Collections.sort(list, new Comparator<TagAnalyse>() {
+//            @Override
+//            public int compare(TagAnalyse o1, TagAnalyse o2) {
+//                return o2.getCount() - o1.getCount();
+//            }
+//        });
+//
+//
+//        //5.分页返回结果集
+//        int i = 1;
+//        int start = 0,end = 6;
+//        while(start<list.size()){
+//            if(end>=list.size())
+//                end = list.size();
+//            result.put(""+i++,new ArrayList(list.subList(start,end)));
+//            start=end;
+//            end+=6;
+//        }
+//        result.put("pages",i-1);
+//        result.put("total",total);
+//        return result;
+//    }
+
+    //版本2
     @Override
     public HashMap<String, Object> tagAnalyse(ChannelMapAceeptVo channelMapAceeptVo) {
         //1.根据条件的到位网点信息
@@ -128,18 +241,22 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
         List<String> collect = queryWd.stream().map(item -> {
             return RedisContant.WD_TAG + "_" + item.getWdId();
         }).collect(Collectors.toList());
+        queryWd.clear();
 
-        //切割获取redis数据
-        int split = 60000;
+        //切割网点 并 统计分析标签
+        ConcurrentHashMap<String, Integer> concurrentHashMap = new ConcurrentHashMap<>();
+        concurrentHashMap.put("total",0);
+
+        int split = 6000;
         int splitCount = collect.size()/split;
         int p1 = 0,p2 = split;
-        ArrayList<CompletableFuture<List<List<String>>>> splitList = new ArrayList<>();
+        ArrayList<CompletableFuture<Void>> splitList = new ArrayList<>();
         for (int i=1;i<=splitCount;i++){
             if (p2 > collect.size())
                 p2 = collect.size();
             List<String> list2 = collect.subList(p1, p2);
-            CompletableFuture<List<List<String>>> future = CompletableFuture.supplyAsync(()->{
-                return wdRedisStoreage.getWdTagList(list2);
+            CompletableFuture<Void> future = CompletableFuture.runAsync(()->{
+                tagAnalyseItem(concurrentHashMap,list2);
             },executor);
             splitList.add(future);
             p1 = p2;
@@ -147,53 +264,20 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
         }
 
         //3.统计
-        Hashtable<String, Integer> hashtable = new Hashtable<>();
-        hashtable.put("total",0);
-        ArrayList<CompletableFuture<Void>> list1 = new ArrayList<>();
-        for (CompletableFuture<List<List<String>>> listCompletableFuture : splitList) {
-            try {
-                List<List<String>> lists = listCompletableFuture.get();
-                for (List<String> list : lists) {
-                    if (list != null && !list.isEmpty()){
-                        CompletableFuture<Void> future = CompletableFuture.runAsync(()->{
-                            for (String s : list) {
-                                s += "边";
-                                Integer integer = hashtable.get(s);
-                                if (integer == null)
-                                    hashtable.put(s,1);
-                                else
-                                    hashtable.put(s,integer+1);
-                            }
-                            hashtable.put("total",hashtable.get("total")+1);
-                        },executor);
-                        list1.add(future);
-                    }
-                }
-                CompletableFuture.allOf(list1.toArray(new CompletableFuture[list1.size()])).join();
-                lists = null;
-            } catch (InterruptedException e) {
-                throw new RuntimeException(e);
-            } catch (ExecutionException e) {
-                throw new RuntimeException(e);
-            }
-
-        }
-
-        collect = null;
-        queryWd = null;
-
+        CompletableFuture.allOf(splitList.toArray(new CompletableFuture[splitList.size()])).join();
+        splitList.clear();
 
         //4.封装统计值
         HashMap<String, Object> result = new HashMap<>();
         List<TagAnalyse> list = new ArrayList<>();
-        Integer total = hashtable.get("total");
-        for (String s : hashtable.keySet()) {
+        Integer total = concurrentHashMap.get("total");
+        for (String s : concurrentHashMap.keySet()) {
             if (s.equals("total"))
                 continue;
 
             TagAnalyse tagAnalyse = new TagAnalyse();
             tagAnalyse.setName(s);
-            tagAnalyse.setCount(hashtable.get(s));
+            tagAnalyse.setCount(concurrentHashMap.get(s));
             BigDecimal bigDecimal = new BigDecimal((double) tagAnalyse.getCount() / total);
             double v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
             tagAnalyse.setRadio(v);
@@ -224,6 +308,26 @@ public class ChannelAnalyseServiceImpl implements ChannelAnalyseService {
         return result;
     }
 
+    public void tagAnalyseItem(ConcurrentHashMap<String,Integer> concurrentHashMap, List<String> keys){
+        //2.获取网点周边标签,并进行统计
+        int total = 0;
+        List<List<String>> wdTagList = wdRedisStoreage.getWdTagList(keys);
+        for (List<String> list : wdTagList) {
+            if (list == null || list.size() == 0)
+                continue;
+            for (String s : list) {
+                s += "边";
+                Integer integer = concurrentHashMap.get(s);
+                if (integer == null)
+                    concurrentHashMap.put(s,1);
+                else
+                    concurrentHashMap.put(s,integer+1);
+            }
+            total++;
+        }
+        concurrentHashMap.put("total",concurrentHashMap.get("total")+total);
+    }
+
     @Override
     public List<StoreWdCategoryCount> category(ChannelMapAceeptVo channelMapAceeptVo) {