RecommendTest.java 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. package com.tzld.longarticle.recommend.server;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;
  6. import com.google.common.collect.Lists;
  7. import com.tzld.longarticle.recommend.server.common.enums.recommend.ArticleTypeEnum;
  8. import com.tzld.longarticle.recommend.server.mapper.aigc.AigcBaseMapper;
  9. import com.tzld.longarticle.recommend.server.mapper.aigc.PublishContentMapper;
  10. import com.tzld.longarticle.recommend.server.mapper.crawler.CrawlerBaseMapper;
  11. import com.tzld.longarticle.recommend.server.model.dto.PublishContentDTO;
  12. import com.tzld.longarticle.recommend.server.model.entity.aigc.PublishAccount;
  13. import com.tzld.longarticle.recommend.server.model.entity.crawler.AccountAvgInfo;
  14. import com.tzld.longarticle.recommend.server.model.entity.crawler.Article;
  15. import com.tzld.longarticle.recommend.server.model.entity.crawler.ArticleDetailInfo;
  16. import com.tzld.longarticle.recommend.server.model.entity.crawler.PublishSortLog;
  17. import com.tzld.longarticle.recommend.server.model.param.PublishContentParam;
  18. import com.tzld.longarticle.recommend.server.repository.aigc.PublishAccountRepository;
  19. import com.tzld.longarticle.recommend.server.repository.crawler.AccountAvgInfoRepository;
  20. import com.tzld.longarticle.recommend.server.repository.crawler.ArticleDetailInfoRepository;
  21. import com.tzld.longarticle.recommend.server.repository.crawler.ArticleRepository;
  22. import com.tzld.longarticle.recommend.server.repository.crawler.PublishSortLogRepository;
  23. import com.tzld.longarticle.recommend.server.service.recommend.RecommendService;
  24. import com.tzld.longarticle.recommend.server.service.recommend.recall.RecallService;
  25. import com.tzld.longarticle.recommend.server.util.DateUtils;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.apache.commons.math3.stat.correlation.PearsonsCorrelation;
  28. import org.apache.poi.ss.usermodel.Cell;
  29. import org.apache.poi.ss.usermodel.Row;
  30. import org.apache.poi.ss.usermodel.Sheet;
  31. import org.apache.poi.ss.usermodel.Workbook;
  32. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  33. import org.junit.jupiter.api.Test;
  34. import org.springframework.beans.BeanUtils;
  35. import org.springframework.beans.factory.annotation.Autowired;
  36. import org.springframework.boot.test.context.SpringBootTest;
  37. import javax.annotation.Resource;
  38. import java.io.File;
  39. import java.io.FileOutputStream;
  40. import java.io.IOException;
  41. import java.nio.charset.StandardCharsets;
  42. import java.nio.file.Files;
  43. import java.util.*;
  44. import java.util.stream.Collectors;
  45. @SpringBootTest(classes = Application.class)
  46. @Slf4j
  47. public class RecommendTest {
  48. @Resource
  49. private RecommendService recommendService;
  50. @Resource
  51. private RecallService recallService;
  52. @Resource
  53. private ArticleRepository articleRepository;
  54. @Resource
  55. private ArticleDetailInfoRepository articleDetailInfoRepository;
  56. @Resource
  57. private AccountAvgInfoRepository accountAvgInfoRepository;
  58. @Resource
  59. private CrawlerBaseMapper crawlerBaseMapper;
  60. @Resource
  61. private PublishSortLogRepository publishSortLogRepository;
  62. @Autowired
  63. private PublishAccountRepository publishAccountRepository;
  64. @Autowired
  65. private AigcBaseMapper aigcBaseMapper;
  66. @Autowired
  67. private PublishContentMapper publishContentMapper;
  68. @ApolloJsonValue("${accountStrategyConfig:{}}")
  69. private Map<String, String> accountStrategyConfigMap;
  70. // @Test
  71. // void recall() {
  72. // RecallParam param = new RecallParam();
  73. // param.setAccountId("20231213123536190184852");
  74. // param.setPlanId("20240718181730864154902");
  75. // RecallResult recallResult = recallService.recall(param);
  76. // System.out.println(JSONObject.toJSONString(recallResult));
  77. // }
  78. //
  79. // @Test
  80. // void exportData() {
  81. // Set<String> ghIds = new HashSet<>(Arrays.asList("gh_adca24a8f429", "gh_e0eb490115f5", "gh_51e4ad40466d", "gh_95ed5ecf9363"));
  82. // List<Article> articleList = articleRepository.getByGhIdInAndPublishTimestampGreaterThanAndTypeEquals(ghIds, 1722441600L, ArticleTypeEnum.qunfa.getVal());
  83. //
  84. // Map<String, Map<Integer, List<Article>>> map = articleList.stream()
  85. // .collect(Collectors.groupingBy(Article::getTitle, Collectors.groupingBy(Article::getItemIndex)));
  86. // Set<String> snList = articleList.stream().map(Article::getWxSn).collect(Collectors.toSet());
  87. // List<ArticleDetailInfo> articleDetailInfoList = articleDetailInfoRepository.getAllByWxSnIn(new ArrayList<>(snList));
  88. // Map<String, List<ArticleDetailInfo>> articleDetailInfoMap = articleDetailInfoList.stream()
  89. // .collect(Collectors.groupingBy(ArticleDetailInfo::getWxSn));
  90. //
  91. // List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdInAndStatusEquals(ghIds, 1);
  92. // Map<String, Map<String, AccountAvgInfo>> accountAvgInfoIndexMap = accountAvgInfoList.stream().collect(
  93. // Collectors.groupingBy(AccountAvgInfo::getGhId, Collectors.toMap(AccountAvgInfo::getPosition, o -> o)));
  94. // JSONArray jsonArray = new JSONArray();
  95. // for (Article article : articleList) {
  96. // List<ArticleDetailInfo> articleDetailInfos = articleDetailInfoMap.get(article.getWxSn());
  97. // if (CollectionUtils.isEmpty(articleDetailInfos)) {
  98. // continue;
  99. // }
  100. // Date minDate = articleDetailInfos.stream().map(ArticleDetailInfo::getRecallDt).min(Date::compareTo).orElse(new Date());
  101. // int sumfirstLevel = 0;
  102. // int sumFission0 = 0;
  103. // int sumFission1 = 0;
  104. // int sumFission2 = 0;
  105. // for (ArticleDetailInfo articleDetailInfo : articleDetailInfos) {
  106. // if (articleDetailInfo.getRecallDt().equals(minDate)) {
  107. // sumfirstLevel += Optional.ofNullable(articleDetailInfo.getFirstLevel()).orElse(0);
  108. // sumFission0 += Optional.ofNullable(articleDetailInfo.getFission0()).orElse(0);
  109. // sumFission1 += Optional.ofNullable(articleDetailInfo.getFission1()).orElse(0);
  110. // sumFission2 += Optional.ofNullable(articleDetailInfo.getFission2()).orElse(0);
  111. // }
  112. // }
  113. // Map<String, AccountAvgInfo> accountAvgInfoMap = accountAvgInfoIndexMap.get(article.getGhId());
  114. // AccountAvgInfo avgInfo = accountAvgInfoMap.get(article.getItemIndex().toString());
  115. // SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  116. // String date = sdf.format(new Date(article.getPublishTimestamp() * 1000));
  117. // JSONObject obj = new JSONObject();
  118. // obj.put("ghId", article.getGhId());
  119. // obj.put("accountName", article.getAccountName());
  120. // obj.put("title", article.getTitle());
  121. // obj.put("index", article.getItemIndex());
  122. // obj.put("viewCount", article.getShowViewCount());
  123. // obj.put("time", date);
  124. // if (Objects.nonNull(avgInfo)) {
  125. // obj.put("fans", avgInfo.getFans());
  126. // obj.put("avgViewCount", avgInfo.getReadAvg());
  127. // obj.put("viewCountRate", (article.getShowViewCount() * 1.0) / avgInfo.getReadAvg());
  128. // }
  129. // obj.put("firstLevel", sumfirstLevel);
  130. // obj.put("fission0", sumFission0);
  131. // obj.put("fission1", sumFission1);
  132. // obj.put("fission2", sumFission2);
  133. // jsonArray.add(obj);
  134. // }
  135. // System.out.println(jsonArray.toJSONString());
  136. // }
  137. //
  138. // @Test
  139. // void ii() throws IOException {
  140. // String dateStr = "20240911";
  141. // List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByStatusEquals(1);
  142. // Map<String, String> accountMap = accountAvgInfoList.stream().collect(
  143. // Collectors.toMap(AccountAvgInfo::getAccountName, AccountAvgInfo::getGhId, (existing, replacement) -> replacement));
  144. // BufferedReader reader = new BufferedReader(new FileReader("/Users/wangyunpeng/Downloads/账号相关性.json"));
  145. // StringBuilder sb = new StringBuilder();
  146. // String line;
  147. // while ((line = reader.readLine()) != null) {
  148. // sb.append(line);
  149. // }
  150. // String jsonStr = sb.toString();
  151. // // 使用 ObjectMapper 解析 JSON
  152. // ObjectMapper objectMapper = new ObjectMapper();
  153. //
  154. // try {
  155. // // 将 JSON 转换为 Map<String, Map<String, Double>>
  156. // Map<String, Map<String, Double>> result = objectMapper.readValue(jsonStr,
  157. // new TypeReference<Map<String, Map<String, Double>>>() {
  158. // });
  159. //
  160. // // 输出转换结果
  161. // System.out.println(result);
  162. // List<AccountCorrelation> saveList = new ArrayList<>();
  163. // result.forEach((k, v) -> {
  164. // String ghId = accountMap.get(k);
  165. // v.forEach((k1, v1) -> {
  166. // String relGhId = accountMap.get(k1);
  167. // AccountCorrelation save = new AccountCorrelation();
  168. // save.setDateStr(dateStr);
  169. // save.setGhId(ghId);
  170. // save.setAccountName(k);
  171. // save.setRelGhId(relGhId);
  172. // save.setRelAccountName(k1);
  173. // save.setStatus(1);
  174. // save.setCorrelation(v1);
  175. // saveList.add(save);
  176. // });
  177. // });
  178. // List<AccountCorrelation> all = new ArrayList<>(saveList);
  179. // for (AccountCorrelation item : all) {
  180. // if (!item.getGhId().equals(item.getRelGhId())) {
  181. // AccountCorrelation save = new AccountCorrelation();
  182. // BeanUtils.copyProperties(item, save);
  183. // save.setGhId(item.getRelGhId());
  184. // save.setAccountName(item.getRelAccountName());
  185. // save.setRelGhId(item.getGhId());
  186. // save.setRelAccountName(item.getAccountName());
  187. // saveList.add(save);
  188. // }
  189. // }
  190. // crawlerBaseMapper.batchInsertAccountCorrelation(saveList);
  191. //
  192. //
  193. // } catch (IOException e) {
  194. // e.printStackTrace();
  195. // }
  196. // }
  197. @Test
  198. public void test() {
  199. List<String> morning = Lists.newArrayList("gh_084a485e859a", "gh_183d80deffb8", "gh_5ff48e9fb9ef", "gh_6d9f36e3a7be", "gh_9f8dc5b0c74e", "gh_e0eb490115f5", "gh_e24da99dc899");
  200. List<String> noon = Lists.newArrayList("gh_080bb43aa0dc", "gh_0c89e11f8bf3", "gh_192c9cf58b13", "gh_1b27dd1beeca", "gh_1d887d61088c", "gh_29074b51f2b7", "gh_3ed305b5817f", "gh_5ae65db96cb7", "gh_6b7c2a257263", "gh_6cfd1132df94", "gh_6d205db62f04", "gh_72bace6b3059", "gh_7e5818b2dd83", "gh_7f5075624a50", "gh_89ef4798d3ea", "gh_9877c8541764", "gh_9eef14ad6c16", "gh_a2901d34f75b", "gh_b15de7c99912", "gh_b676b7ad9b74", "gh_b6f2c5332c72", "gh_bfe5b705324a", "gh_bff0bcb0694a", "gh_c5cdf60d9ab4", "gh_c69776baf2cd", "gh_d49df5e974ca", "gh_d4dffc34ac39", "gh_dd4c857bbb36", "gh_ee78360d06f5", "gh_f25b5fb01977", "gh_f902cea89e48", "gh_ff487cb5dab3");
  201. String dateStr = "2024-09-12";
  202. List<Article> articleList = articleRepository.getByPublishTimestampGreaterThanAndTypeEquals(1725120000L, ArticleTypeEnum.QUNFA.getVal());
  203. articleList = articleList.stream().filter(o -> o.getItemIndex() == 1 && o.getPublishTimestamp() < 1726675200).collect(Collectors.toList());
  204. Map<String, List<Article>> map = articleList.stream().collect(Collectors.groupingBy(Article::getTitle));
  205. List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByUpdateTime(dateStr);
  206. accountAvgInfoList = accountAvgInfoList.stream().filter(o -> o.getPosition().equals("1")).collect(Collectors.toList());
  207. Map<String, AccountAvgInfo> accountAvgInfoMap = accountAvgInfoList.stream().collect(Collectors.toMap(AccountAvgInfo::getGhId, o -> o));
  208. List<String> wxSnList = articleList.stream().map(Article::getWxSn).collect(Collectors.toList());
  209. List<ArticleDetailInfo> articleDetailInfoList = articleDetailInfoRepository.getAllByWxSnIn(wxSnList);
  210. Map<String, List<ArticleDetailInfo>> articleDetailInfoMap = articleDetailInfoList.stream()
  211. .collect(Collectors.groupingBy(ArticleDetailInfo::getWxSn));
  212. JSONArray result = new JSONArray();
  213. int sumFissionMorning = 0;
  214. int sumFissionMoon = 0;
  215. int sumReadMorning = 0;
  216. int sumReadNoon = 0;
  217. long sumFansMorning = 0;
  218. long sumFansMoon = 0;
  219. double readAvgMorning = 0.0;
  220. double readAvgMoon = 0.0;
  221. for (Article article : articleList) {
  222. List<ArticleDetailInfo> articleDetailInfos = articleDetailInfoMap.get(article.getWxSn());
  223. if (CollectionUtil.isEmpty(articleDetailInfos)) {
  224. continue;
  225. }
  226. Date minDate = articleDetailInfos.stream().map(ArticleDetailInfo::getRecallDt).min(Date::compareTo).orElse(new Date());
  227. AccountAvgInfo accountAvgInfo = accountAvgInfoMap.get(article.getGhId());
  228. for (ArticleDetailInfo articleDetailInfo : articleDetailInfos) {
  229. if (articleDetailInfo.getRecallDt().equals(minDate)) {
  230. if (morning.contains(article.getGhId())) {
  231. sumFissionMorning += Optional.ofNullable(articleDetailInfo.getFission0()).orElse(0);
  232. sumFissionMorning += Optional.ofNullable(articleDetailInfo.getFission1()).orElse(0);
  233. sumFissionMorning += Optional.ofNullable(articleDetailInfo.getFission2()).orElse(0);
  234. }
  235. if (noon.contains(article.getGhId())) {
  236. sumFissionMoon += Optional.ofNullable(articleDetailInfo.getFission0()).orElse(0);
  237. sumFissionMoon += Optional.ofNullable(articleDetailInfo.getFission1()).orElse(0);
  238. sumFissionMoon += Optional.ofNullable(articleDetailInfo.getFission2()).orElse(0);
  239. }
  240. }
  241. }
  242. if (Objects.nonNull(accountAvgInfo)) {
  243. if (morning.contains(article.getGhId())) {
  244. readAvgMorning += accountAvgInfo.getReadAvg();
  245. sumFansMorning += accountAvgInfo.getFans();
  246. sumReadMorning += article.getShowViewCount();
  247. }
  248. if (noon.contains(article.getGhId())) {
  249. readAvgMoon += accountAvgInfo.getReadAvg();
  250. sumFansMoon += accountAvgInfo.getFans();
  251. sumReadNoon += article.getShowViewCount();
  252. }
  253. }
  254. }
  255. JSONObject jsonObjectMorning = new JSONObject();
  256. jsonObjectMorning.put("时间", "早上");
  257. jsonObjectMorning.put("sumFission", sumFissionMorning);
  258. jsonObjectMorning.put("readAvg", readAvgMorning);
  259. jsonObjectMorning.put("rate", sumFissionMorning / readAvgMorning);
  260. jsonObjectMorning.put("sumRead", sumReadMorning);
  261. jsonObjectMorning.put("sumFans", sumFansMorning);
  262. jsonObjectMorning.put("阅读率", sumReadMorning / (double) sumFansMorning);
  263. result.add(jsonObjectMorning);
  264. JSONObject jsonObjectMoon = new JSONObject();
  265. jsonObjectMoon.put("时间", "中午");
  266. jsonObjectMoon.put("sumFission", sumFissionMoon);
  267. jsonObjectMoon.put("readAvg", readAvgMoon);
  268. jsonObjectMoon.put("rate", sumFissionMoon / readAvgMoon);
  269. jsonObjectMoon.put("sumRead", sumReadNoon);
  270. jsonObjectMoon.put("sumFans", sumFansMoon);
  271. jsonObjectMoon.put("阅读率", sumReadNoon / (double) sumFansMoon);
  272. result.add(jsonObjectMoon);
  273. System.out.println(JSONObject.toJSONString(result));
  274. }
  275. @Test
  276. public void exportScoreData() {
  277. List<String> strategies = Arrays.asList("ArticleRankV11", "ArticleRankV12");
  278. List<PublishSortLog> sortLogList = publishSortLogRepository.findByStrategyInAndDateStrGreaterThanEqual(strategies, "20240928");
  279. sortLogList = sortLogList.stream().filter(o -> o.getIndex() == 1).collect(Collectors.toList());
  280. sortLogList.sort(Comparator.comparing(PublishSortLog::getGhId).thenComparing(PublishSortLog::getDateStr));
  281. List<String> ghIds = sortLogList.stream().map(PublishSortLog::getGhId).distinct().collect(Collectors.toList());
  282. List<Article> articleList = articleRepository.getByGhIdInAndPublishTimestampGreaterThanAndTypeEquals(ghIds, 1727452800L, ArticleTypeEnum.QUNFA.getVal());
  283. articleList = articleList.stream().filter(o -> o.getItemIndex() == 1).collect(Collectors.toList());
  284. Map<String, Map<String, Article>> articleMap = articleList.stream().collect(Collectors.groupingBy(Article::getGhId, Collectors.toMap(
  285. o -> DateUtils.timestampToYMDStr(o.getPublishTimestamp(),"yyyyMMdd"), o -> o,
  286. (existing, replacement) -> replacement)));
  287. List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdIn(new HashSet<>(ghIds));
  288. Map<String, Map<String, AccountAvgInfo>> accountAvgInfoMap = accountAvgInfoList.stream()
  289. .filter(o -> Objects.equals(o.getPosition(), "1")).collect(Collectors.groupingBy(AccountAvgInfo::getGhId,
  290. Collectors.toMap(AccountAvgInfo::getUpdateTime, o -> o)));
  291. Workbook workbook = new XSSFWorkbook();
  292. Sheet sheet = workbook.createSheet("ExampleSheet");
  293. int rowNum = 0;
  294. // 创建标题行
  295. Row titleRow = sheet.createRow(rowNum);
  296. Cell titleCell = titleRow.createCell(0);
  297. titleCell.setCellValue("日期");
  298. titleCell = titleRow.createCell(1);
  299. titleCell.setCellValue("ghID");
  300. titleCell = titleRow.createCell(2);
  301. titleCell.setCellValue("账号名称");
  302. titleCell = titleRow.createCell(3);
  303. titleCell.setCellValue("标题");
  304. titleCell = titleRow.createCell(4);
  305. titleCell.setCellValue("策略");
  306. titleCell = titleRow.createCell(5);
  307. titleCell.setCellValue("得分");
  308. titleCell = titleRow.createCell(6);
  309. titleCell.setCellValue("HisFissionFansRateRateStrategy");
  310. titleCell = titleRow.createCell(7);
  311. titleCell.setCellValue("HisFissionAvgReadRateRateStrategy");
  312. titleCell = titleRow.createCell(8);
  313. titleCell.setCellValue("PublishTimesStrategy");
  314. titleCell = titleRow.createCell(9);
  315. titleCell.setCellValue("ViewCountRateCorrelationStrategy");
  316. titleCell = titleRow.createCell(10);
  317. titleCell.setCellValue("HisFissionAvgReadSumRateStrategy");
  318. titleCell = titleRow.createCell(11);
  319. titleCell.setCellValue("HisFissionAvgReadRateCorrelationRateStrategy");
  320. titleCell = titleRow.createCell(12);
  321. titleCell.setCellValue("HisFissionFansSumRateStrategy");
  322. titleCell = titleRow.createCell(13);
  323. titleCell.setCellValue("SimilarityStrategy");
  324. titleCell = titleRow.createCell(14);
  325. titleCell.setCellValue("ViewCountStrategy");
  326. titleCell = titleRow.createCell(15);
  327. titleCell.setCellValue("ViewCountRateStrategy");
  328. titleCell = titleRow.createCell(16);
  329. titleCell.setCellValue("HisFissionDeWeightAvgReadSumRateStrategy");
  330. titleCell = titleRow.createCell(17);
  331. titleCell.setCellValue("阅读量");
  332. titleCell = titleRow.createCell(18);
  333. titleCell.setCellValue("阅读均值");
  334. titleCell = titleRow.createCell(19);
  335. titleCell.setCellValue("阅读均值倍数");
  336. // 填充数据
  337. String title = "";
  338. for (PublishSortLog publishSortLog : sortLogList) {
  339. Map<String, Article> dateArticleMap = articleMap.get(publishSortLog.getGhId());
  340. Article article = dateArticleMap.get(publishSortLog.getDateStr());
  341. if (Objects.isNull(article) || !publishSortLog.getTitle().equals(article.getTitle())) {
  342. continue;
  343. }
  344. if (publishSortLog.getTitle().equals(title)) {
  345. continue;
  346. }
  347. title = publishSortLog.getTitle();
  348. rowNum++;
  349. Row row = sheet.createRow(rowNum);
  350. Cell cell = row.createCell(0);
  351. cell.setCellValue(publishSortLog.getDateStr());
  352. cell = row.createCell(1);
  353. cell.setCellValue(publishSortLog.getGhId());
  354. cell = row.createCell(2);
  355. cell.setCellValue(publishSortLog.getAccountName());
  356. cell = row.createCell(3);
  357. cell.setCellValue(publishSortLog.getTitle());
  358. cell = row.createCell(4);
  359. cell.setCellValue(publishSortLog.getStrategy());
  360. cell = row.createCell(5);
  361. cell.setCellValue(publishSortLog.getScore());
  362. cell = row.createCell(6);
  363. JSONObject scoreMap = JSONObject.parseObject(publishSortLog.getScoreMap());
  364. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansRateRateStrategy")).orElse(0.0)));
  365. cell = row.createCell(7);
  366. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateRateStrategy")).orElse(0.0)));
  367. cell = row.createCell(8);
  368. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("PublishTimesStrategy")).orElse(0.0)));
  369. cell = row.createCell(9);
  370. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateCorrelationStrategy")).orElse(0.0)));
  371. cell = row.createCell(10);
  372. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadSumRateStrategy")).orElse(0.0)));
  373. cell = row.createCell(11);
  374. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateCorrelationRateStrategy")).orElse(0.0)));
  375. cell = row.createCell(12);
  376. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansSumRateStrategy")).orElse(0.0)));
  377. cell = row.createCell(13);
  378. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("SimilarityStrategy")).orElse(0.0)));
  379. cell = row.createCell(14);
  380. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountStrategy")).orElse(0.0)));
  381. cell = row.createCell(15);
  382. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateStrategy")).orElse(0.0)));
  383. cell = row.createCell(16);
  384. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionDeWeightAvgReadSumRateStrategy")).orElse(0.0)));
  385. cell = row.createCell(17);
  386. cell.setCellValue(article.getShowViewCount());
  387. cell = row.createCell(18);
  388. Map<String, AccountAvgInfo> map = accountAvgInfoMap.get(article.getGhId());
  389. if (Objects.nonNull(map)) {
  390. List<String> avgMapDateList = new ArrayList<>(map.keySet());
  391. String publishDate = DateUtils.findNearestDate(avgMapDateList,
  392. DateUtils.timestampToYMDStr(article.getPublishTimestamp(), "yyyy-MM-dd"), "yyyy-MM-dd");
  393. AccountAvgInfo accountAvgInfo = map.get(publishDate);
  394. if (Objects.nonNull(accountAvgInfo)) {
  395. cell.setCellValue(accountAvgInfo.getReadAvg());
  396. cell = row.createCell(19);
  397. cell.setCellValue(String.format("%.3f", article.getShowViewCount() / (double) accountAvgInfo.getReadAvg()));
  398. }
  399. }
  400. }
  401. try (FileOutputStream outputStream = new FileOutputStream("/Users/wangyunpeng/Downloads/example.xlsx")) {
  402. workbook.write(outputStream);
  403. } catch (IOException e) {
  404. e.printStackTrace();
  405. } finally {
  406. try {
  407. workbook.close();
  408. } catch (IOException e) {
  409. e.printStackTrace();
  410. }
  411. }
  412. }
  413. @Test
  414. public void exportFromAliyunLog() {
  415. String folderPath = "/Users/wangyunpeng/Downloads/longarticle-recommend-server-test_info-log_20241014_150245.json";
  416. File file = new File(folderPath);
  417. Workbook workbook = new XSSFWorkbook();
  418. Sheet sheet = workbook.createSheet("ExampleSheet");
  419. int rowNum = 0;
  420. // 创建标题行
  421. Row titleRow = sheet.createRow(rowNum);
  422. Cell titleCell = titleRow.createCell(0);
  423. titleCell.setCellValue("日期");
  424. titleCell = titleRow.createCell(1);
  425. titleCell.setCellValue("账号名称");
  426. titleCell = titleRow.createCell(2);
  427. titleCell.setCellValue("id");
  428. titleCell = titleRow.createCell(3);
  429. titleCell.setCellValue("标题");
  430. titleCell = titleRow.createCell(4);
  431. titleCell.setCellValue("策略");
  432. titleCell = titleRow.createCell(5);
  433. titleCell.setCellValue("得分");
  434. titleCell = titleRow.createCell(6);
  435. titleCell.setCellValue("HisFissionFansRateRateStrategy");
  436. titleCell = titleRow.createCell(7);
  437. titleCell.setCellValue("HisFissionAvgReadRateRateStrategy");
  438. titleCell = titleRow.createCell(8);
  439. titleCell.setCellValue("PublishTimesStrategy");
  440. titleCell = titleRow.createCell(9);
  441. titleCell.setCellValue("ViewCountRateCorrelationStrategy");
  442. titleCell = titleRow.createCell(10);
  443. titleCell.setCellValue("HisFissionAvgReadSumRateStrategy");
  444. titleCell = titleRow.createCell(11);
  445. titleCell.setCellValue("HisFissionAvgReadRateCorrelationRateStrategy");
  446. titleCell = titleRow.createCell(12);
  447. titleCell.setCellValue("HisFissionFansSumRateStrategy");
  448. titleCell = titleRow.createCell(13);
  449. titleCell.setCellValue("SimilarityStrategy");
  450. titleCell = titleRow.createCell(14);
  451. titleCell.setCellValue("ViewCountStrategy");
  452. titleCell = titleRow.createCell(15);
  453. titleCell.setCellValue("ViewCountRateStrategy");
  454. titleCell = titleRow.createCell(16);
  455. titleCell.setCellValue("HisFissionDeWeightAvgReadSumRateStrategy");
  456. try {
  457. String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
  458. JSONArray jsonArray = JSONArray.parseArray(content);
  459. for (Object o : jsonArray) {
  460. JSONObject jsonObject = (JSONObject) o;
  461. Long time = jsonObject.getLong("__time__");
  462. String message = jsonObject.getString("message");
  463. int index = message.indexOf("[");
  464. String info = message.substring(0, index);
  465. String strategy = info.substring(0, info.indexOf(" "));
  466. String accountName = info.substring(info.indexOf(" ")).replace("账号名称 ", "")
  467. .replace(" 头条评分结果", "");
  468. String json = message.substring(index);
  469. JSONArray scoreArray = JSONArray.parseArray(json);
  470. for (Object scoreJSON : scoreArray) {
  471. JSONObject scoreObject = (JSONObject) scoreJSON;
  472. String id = scoreObject.getString("id");
  473. String title = scoreObject.getString("title");
  474. String score = scoreObject.getString("score");
  475. String scoreMapStr = scoreObject.getString("scoreMap");
  476. rowNum++;
  477. Row row = sheet.createRow(rowNum);
  478. Cell cell = row.createCell(0);
  479. cell.setCellValue(DateUtils.timestampToYMDStr(time, "yyyyMMdd"));
  480. cell = row.createCell(1);
  481. cell.setCellValue(accountName);
  482. cell = row.createCell(2);
  483. cell.setCellValue(id);
  484. cell = row.createCell(3);
  485. cell.setCellValue(title);
  486. cell = row.createCell(4);
  487. cell.setCellValue(strategy);
  488. cell = row.createCell(5);
  489. cell.setCellValue(score);
  490. cell = row.createCell(6);
  491. JSONObject scoreMap = JSONObject.parseObject(scoreMapStr);
  492. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansRateRateStrategy")).orElse(0.0)));
  493. cell = row.createCell(7);
  494. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateRateStrategy")).orElse(0.0)));
  495. cell = row.createCell(8);
  496. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("PublishTimesStrategy")).orElse(0.0)));
  497. cell = row.createCell(9);
  498. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateCorrelationStrategy")).orElse(0.0)));
  499. cell = row.createCell(10);
  500. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadSumRateStrategy")).orElse(0.0)));
  501. cell = row.createCell(11);
  502. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateCorrelationRateStrategy")).orElse(0.0)));
  503. cell = row.createCell(12);
  504. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansSumRateStrategy")).orElse(0.0)));
  505. cell = row.createCell(13);
  506. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("SimilarityStrategy")).orElse(0.0)));
  507. cell = row.createCell(14);
  508. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountStrategy")).orElse(0.0)));
  509. cell = row.createCell(15);
  510. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateStrategy")).orElse(0.0)));
  511. cell = row.createCell(16);
  512. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionDeWeightAvgReadSumRateStrategy")).orElse(0.0)));
  513. }
  514. }
  515. try (FileOutputStream outputStream = new FileOutputStream("/Users/wangyunpeng/Downloads/example.xlsx")) {
  516. workbook.write(outputStream);
  517. } catch (IOException e) {
  518. e.printStackTrace();
  519. } finally {
  520. try {
  521. workbook.close();
  522. } catch (IOException e) {
  523. e.printStackTrace();
  524. }
  525. }
  526. } catch (Exception e) {
  527. log.error("readFileError fileName:{}", file.getName(), e);
  528. }
  529. }
  530. @Test
  531. public void account() {
  532. List<String> ghIds = Arrays.asList("gh_d7fa1998b4e1", "gh_52100b6803fb", "gh_8d7fc54d5026");
  533. List<String> accountNames = Arrays.asList("生活超读", "灵读生活", "生活情感读");
  534. List<Integer> fans = Arrays.asList(85759, 103083, 79214);
  535. List<Article> articleList = articleRepository.getByGhIdInAndPublishTimestampLessThanAndTypeEquals(
  536. Arrays.asList("gh_02f5bca5b5d9"), 1729353600L, ArticleTypeEnum.QUNFA.getVal());
  537. for (int i = 0; i < ghIds.size(); i++) {
  538. String ghId = ghIds.get(i);
  539. String accountName = accountNames.get(i);
  540. Integer fanCount = fans.get(i);
  541. Double rate = fanCount / 233474.0;
  542. for (Article article : articleList) {
  543. Article saveItem = new Article();
  544. BeanUtils.copyProperties(article, saveItem);
  545. saveItem.setGhId(ghId);
  546. saveItem.setAccountName(accountName);
  547. saveItem.setShowViewCount((int) (article.getShowViewCount() * rate));
  548. saveItem.setWxSn(UUID.randomUUID().toString().replace("-", ""));
  549. articleRepository.save(saveItem);
  550. }
  551. }
  552. }
  553. @Test
  554. public void correlation() {
  555. List<String> ghIds = Lists.newArrayList("gh_e24da99dc899",
  556. "gh_183d80deffb8",
  557. "gh_be8c29139989",
  558. "gh_c69776baf2cd",
  559. "gh_b15de7c99912",
  560. "gh_1d887d61088c",
  561. "gh_3ed305b5817f",
  562. "gh_3e91f0624545",
  563. "gh_30816d8adb52",
  564. "gh_970460d9ccec",
  565. "gh_749271f1ccd5",
  566. "gh_ac43e43b253b"
  567. );
  568. List<PublishSortLog> sortLogList = publishSortLogRepository.findByGhIdInAndDateStrGreaterThanEqual(ghIds, "20240907");
  569. sortLogList = sortLogList.stream().filter(o -> o.getIndex() == 1).collect(Collectors.toList());
  570. sortLogList.sort(Comparator.comparing(PublishSortLog::getGhId).thenComparing(PublishSortLog::getDateStr));
  571. List<Article> articleList = articleRepository.getByGhIdInAndUpdateTimeGreaterThanAndTypeEquals(ghIds, 1725638400L, "9");
  572. articleList = articleList.stream().filter(o -> o.getItemIndex() == 1).collect(Collectors.toList());
  573. Map<String, Map<String, Article>> articleMap = articleList.stream().collect(Collectors.groupingBy(Article::getGhId, Collectors.toMap(
  574. o -> DateUtils.timestampToYMDStr(o.getUpdateTime(), "yyyyMMdd"), o -> o,
  575. (existing, replacement) -> replacement)));
  576. List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdIn(new HashSet<>(ghIds));
  577. Map<String, Map<String, AccountAvgInfo>> accountAvgInfoMap = accountAvgInfoList.stream()
  578. .filter(o -> Objects.equals(o.getPosition(), "1")).collect(Collectors.groupingBy(AccountAvgInfo::getGhId,
  579. Collectors.toMap(AccountAvgInfo::getUpdateTime, o -> o)));
  580. int rowNum = 0;
  581. Map<String, List<PublishSortLog>> sortLogMap = sortLogList.stream().collect(Collectors.groupingBy(PublishSortLog::getGhId));
  582. PearsonsCorrelation correlation = new PearsonsCorrelation();
  583. Workbook workbook = new XSSFWorkbook();
  584. Sheet sheet = workbook.createSheet("ExampleSheet");
  585. // 创建标题行
  586. Row titleRow = sheet.createRow(rowNum);
  587. for (Map.Entry<String, List<PublishSortLog>> entry : sortLogMap.entrySet()) {
  588. String ghId = entry.getKey();
  589. String name = entry.getValue().get(0).getAccountName();
  590. List<PublishSortLog> itemList = entry.getValue();
  591. String title = "";
  592. double[] scoreArr = new double[itemList.size()];
  593. double[] HisFissionFansRateRateStrategyArr = new double[itemList.size()];
  594. double[] HisFissionAvgReadRateRateStrategyArr = new double[itemList.size()];
  595. double[] PublishTimesStrategyArr = new double[itemList.size()];
  596. double[] ViewCountRateCorrelationStrategyArr = new double[itemList.size()];
  597. double[] HisFissionAvgReadSumRateStrategyArr = new double[itemList.size()];
  598. double[] HisFissionAvgReadRateCorrelationRateStrategyArr = new double[itemList.size()];
  599. double[] HisFissionFansSumRateStrategyArr = new double[itemList.size()];
  600. double[] SimilarityStrategyArr = new double[itemList.size()];
  601. double[] ViewCountStrategyArr = new double[itemList.size()];
  602. double[] ViewCountRateStrategyArr = new double[itemList.size()];
  603. double[] HisFissionDeWeightAvgReadSumRateStrategyArr = new double[itemList.size()];
  604. double[] scoreRateArr = new double[itemList.size()];
  605. for (int i = 0; i < itemList.size(); i++) {
  606. PublishSortLog publishSortLog = itemList.get(i);
  607. Map<String, Article> dateArticleMap = articleMap.get(publishSortLog.getGhId());
  608. Article article = dateArticleMap.get(publishSortLog.getDateStr());
  609. if (Objects.isNull(article) || !publishSortLog.getTitle().equals(article.getTitle())) {
  610. continue;
  611. }
  612. if (publishSortLog.getTitle().equals(title)) {
  613. continue;
  614. }
  615. title = publishSortLog.getTitle();
  616. scoreArr[i] = Double.parseDouble(publishSortLog.getScore());
  617. JSONObject scoreMap = JSONObject.parseObject(publishSortLog.getScoreMap());
  618. HisFissionFansRateRateStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansRateRateStrategy")).orElse(0.0)));
  619. HisFissionAvgReadRateRateStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateRateStrategy")).orElse(0.0)));
  620. PublishTimesStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("PublishTimesStrategy")).orElse(0.0)));
  621. ViewCountRateCorrelationStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateCorrelationStrategy")).orElse(0.0)));
  622. HisFissionAvgReadSumRateStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadSumRateStrategy")).orElse(0.0)));
  623. HisFissionAvgReadRateCorrelationRateStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateCorrelationRateStrategy")).orElse(0.0)));
  624. HisFissionFansSumRateStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansSumRateStrategy")).orElse(0.0)));
  625. SimilarityStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("SimilarityStrategy")).orElse(0.0)));
  626. ViewCountStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountStrategy")).orElse(0.0)));
  627. ViewCountRateStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateStrategy")).orElse(0.0)));
  628. HisFissionDeWeightAvgReadSumRateStrategyArr[i] = Double.parseDouble(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionDeWeightAvgReadSumRateStrategy")).orElse(0.0)));
  629. Map<String, AccountAvgInfo> map = accountAvgInfoMap.get(article.getGhId());
  630. if (Objects.nonNull(map)) {
  631. List<String> avgMapDateList = new ArrayList<>(map.keySet());
  632. String publishDate = DateUtils.findNearestDate(avgMapDateList,
  633. DateUtils.timestampToYMDStr(article.getUpdateTime(), "yyyy-MM-dd"), "yyyy-MM-dd");
  634. AccountAvgInfo accountAvgInfo = map.get(publishDate);
  635. if (Objects.nonNull(accountAvgInfo)) {
  636. scoreRateArr[i] = Double.parseDouble(String.format("%.3f", article.getShowViewCount() / (double) accountAvgInfo.getReadAvg()));
  637. }
  638. }
  639. }
  640. rowNum++;
  641. Row row = sheet.createRow(rowNum);
  642. Cell cell = row.createCell(0);
  643. cell = row.createCell(1);
  644. cell.setCellValue(ghId);
  645. cell = row.createCell(2);
  646. cell.setCellValue(name);
  647. cell = row.createCell(3);
  648. cell = row.createCell(4);
  649. cell = row.createCell(5);
  650. cell.setCellValue(correlation.correlation(scoreArr, scoreRateArr));
  651. cell = row.createCell(6);
  652. cell.setCellValue(correlation.correlation(HisFissionFansRateRateStrategyArr, scoreRateArr));
  653. cell = row.createCell(7);
  654. cell.setCellValue(correlation.correlation(HisFissionAvgReadRateRateStrategyArr, scoreRateArr));
  655. cell = row.createCell(8);
  656. cell.setCellValue(correlation.correlation(PublishTimesStrategyArr, scoreRateArr));
  657. cell = row.createCell(9);
  658. cell.setCellValue(correlation.correlation(ViewCountRateCorrelationStrategyArr, scoreRateArr));
  659. cell = row.createCell(10);
  660. cell.setCellValue(correlation.correlation(HisFissionAvgReadSumRateStrategyArr, scoreRateArr));
  661. cell = row.createCell(11);
  662. cell.setCellValue(correlation.correlation(HisFissionAvgReadRateCorrelationRateStrategyArr, scoreRateArr));
  663. cell = row.createCell(12);
  664. cell.setCellValue(correlation.correlation(HisFissionFansSumRateStrategyArr, scoreRateArr));
  665. cell = row.createCell(13);
  666. cell.setCellValue(correlation.correlation(SimilarityStrategyArr, scoreRateArr));
  667. cell = row.createCell(14);
  668. cell.setCellValue(correlation.correlation(ViewCountStrategyArr, scoreRateArr));
  669. cell = row.createCell(15);
  670. cell.setCellValue(correlation.correlation(ViewCountRateStrategyArr, scoreRateArr));
  671. cell = row.createCell(16);
  672. cell.setCellValue(correlation.correlation(HisFissionDeWeightAvgReadSumRateStrategyArr, scoreRateArr));
  673. }
  674. try (FileOutputStream outputStream = new FileOutputStream("/Users/wangyunpeng/Downloads/example.xlsx")) {
  675. workbook.write(outputStream);
  676. } catch (IOException e) {
  677. e.printStackTrace();
  678. } finally {
  679. try {
  680. workbook.close();
  681. } catch (IOException e) {
  682. e.printStackTrace();
  683. }
  684. }
  685. }
  686. @Test
  687. void getScoreFromLogFile() {
  688. String folderPath = "/Users/wangyunpeng/Downloads/b78020b8-d9df-466f-bd01-cd982bb986d0.json";
  689. File file = new File(folderPath);
  690. Workbook workbook = new XSSFWorkbook();
  691. Sheet sheet = workbook.createSheet("ExampleSheet");
  692. int rowNum = 0;
  693. // 创建标题行
  694. Row titleRow = sheet.createRow(rowNum);
  695. Cell titleCell = titleRow.createCell(0);
  696. titleCell.setCellValue("日期");
  697. titleCell = titleRow.createCell(1);
  698. titleCell.setCellValue("账号名称");
  699. titleCell = titleRow.createCell(2);
  700. titleCell.setCellValue("id");
  701. titleCell = titleRow.createCell(3);
  702. titleCell.setCellValue("标题");
  703. titleCell = titleRow.createCell(4);
  704. titleCell.setCellValue("策略");
  705. titleCell = titleRow.createCell(5);
  706. titleCell.setCellValue("得分");
  707. titleCell = titleRow.createCell(6);
  708. titleCell.setCellValue("HisFissionFansRateRateStrategy");
  709. titleCell = titleRow.createCell(7);
  710. titleCell.setCellValue("HisFissionAvgReadRateRateStrategy");
  711. titleCell = titleRow.createCell(8);
  712. titleCell.setCellValue("PublishTimesStrategy");
  713. titleCell = titleRow.createCell(9);
  714. titleCell.setCellValue("ViewCountRateCorrelationStrategy");
  715. titleCell = titleRow.createCell(10);
  716. titleCell.setCellValue("HisFissionAvgReadSumRateStrategy");
  717. titleCell = titleRow.createCell(11);
  718. titleCell.setCellValue("HisFissionAvgReadRateCorrelationRateStrategy");
  719. titleCell = titleRow.createCell(12);
  720. titleCell.setCellValue("HisFissionFansSumRateStrategy");
  721. titleCell = titleRow.createCell(13);
  722. titleCell.setCellValue("SimilarityStrategy");
  723. titleCell = titleRow.createCell(14);
  724. titleCell.setCellValue("ViewCountStrategy");
  725. titleCell = titleRow.createCell(15);
  726. titleCell.setCellValue("ViewCountRateStrategy");
  727. titleCell = titleRow.createCell(16);
  728. titleCell.setCellValue("HisFissionDeWeightAvgReadSumRateStrategy");
  729. try {
  730. String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
  731. JSONArray jsonArray = JSONArray.parseArray(content);
  732. for (Object o : jsonArray) {
  733. JSONObject jsonObject = (JSONObject) o;
  734. Long time = jsonObject.getLong("__time__");
  735. String message = jsonObject.getString("message");
  736. int index = message.indexOf("[");
  737. String info = message.substring(0, index);
  738. String strategy = info.substring(0, info.indexOf(" "));
  739. String accountName = info.substring(info.indexOf(" ")).replace("账号名称 ", "")
  740. .replace(" 头条评分结果", "");
  741. String json = message.substring(index);
  742. JSONArray scoreArray = JSONArray.parseArray(json);
  743. for (Object scoreJSON : scoreArray) {
  744. JSONObject scoreObject = (JSONObject) scoreJSON;
  745. String id = scoreObject.getString("id");
  746. String title = scoreObject.getString("title");
  747. String score = scoreObject.getString("score");
  748. String scoreMapStr = scoreObject.getString("scoreMap");
  749. rowNum++;
  750. Row row = sheet.createRow(rowNum);
  751. Cell cell = row.createCell(0);
  752. cell.setCellValue(DateUtils.timestampToYMDStr(time, "yyyyMMdd"));
  753. cell = row.createCell(1);
  754. cell.setCellValue(accountName);
  755. cell = row.createCell(2);
  756. cell.setCellValue(id);
  757. cell = row.createCell(3);
  758. cell.setCellValue(title);
  759. cell = row.createCell(4);
  760. cell.setCellValue(strategy);
  761. cell = row.createCell(5);
  762. cell.setCellValue(score);
  763. cell = row.createCell(6);
  764. JSONObject scoreMap = JSONObject.parseObject(scoreMapStr);
  765. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansRateRateStrategy")).orElse(0.0)));
  766. cell = row.createCell(7);
  767. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateRateStrategy")).orElse(0.0)));
  768. cell = row.createCell(8);
  769. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("PublishTimesStrategy")).orElse(0.0)));
  770. cell = row.createCell(9);
  771. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateCorrelationStrategy")).orElse(0.0)));
  772. cell = row.createCell(10);
  773. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadSumRateStrategy")).orElse(0.0)));
  774. cell = row.createCell(11);
  775. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateCorrelationRateStrategy")).orElse(0.0)));
  776. cell = row.createCell(12);
  777. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansSumRateStrategy")).orElse(0.0)));
  778. cell = row.createCell(13);
  779. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("SimilarityStrategy")).orElse(0.0)));
  780. cell = row.createCell(14);
  781. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountStrategy")).orElse(0.0)));
  782. cell = row.createCell(15);
  783. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateStrategy")).orElse(0.0)));
  784. cell = row.createCell(16);
  785. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionDeWeightAvgReadSumRateStrategy")).orElse(0.0)));
  786. }
  787. }
  788. try (FileOutputStream outputStream = new FileOutputStream("/Users/wangyunpeng/Downloads/example.xlsx")) {
  789. workbook.write(outputStream);
  790. } catch (IOException e) {
  791. e.printStackTrace();
  792. } finally {
  793. try {
  794. workbook.close();
  795. } catch (IOException e) {
  796. e.printStackTrace();
  797. }
  798. }
  799. } catch (Exception e) {
  800. log.error("readFileError fileName:{}", file.getName(), e);
  801. }
  802. }
  803. @Test
  804. public void checkTest() {
  805. List<Article> articleList = articleRepository.getByPublishTimestampGreaterThan(1732982400L);
  806. List<Article> singleArticleList = new ArrayList<>();
  807. for (Article article : articleList) {
  808. if (Objects.nonNull(article.getRootSourceIdList())) {
  809. try {
  810. List<String> rootSourceIdList = JSONArray.parseArray(article.getRootSourceIdList(), String.class);
  811. if (rootSourceIdList.size() == 1) {
  812. singleArticleList.add(article);
  813. }
  814. } catch (Exception ignore) {
  815. }
  816. }
  817. }
  818. List<String> ghIds = singleArticleList.stream().map(Article::getGhId).distinct().collect(Collectors.toList());
  819. List<PublishAccount> publishAccountList = publishAccountRepository.getAllByGhIdIn(ghIds);
  820. log.info("newSortStrategyData publishAccountList finish");
  821. Map<String, PublishAccount> publishAccountMap = publishAccountList.stream().collect(Collectors.toMap(PublishAccount::getGhId, o -> o));
  822. // 获取发布内容
  823. List<PublishContentParam> publishContentParamList = singleArticleList.stream().map(article -> {
  824. PublishContentParam item = new PublishContentParam();
  825. item.setTitle(article.getTitle());
  826. PublishAccount account = publishAccountMap.get(article.getGhId());
  827. if (Objects.nonNull(account)) {
  828. item.setPublishAccountId(account.getId());
  829. return item;
  830. }
  831. return null;
  832. }).filter(Objects::nonNull).collect(Collectors.toList());
  833. List<PublishContentDTO> publishContents = new ArrayList<>();
  834. for (List<PublishContentParam> partitions : Lists.partition(publishContentParamList, 100)) {
  835. publishContents.addAll(publishContentMapper.getPublishContentByTitle(partitions));
  836. }
  837. List<String> publishContentIds = publishContents.stream().map(PublishContentDTO::getId).collect(Collectors.toList());
  838. for (List<String> partition : Lists.partition(publishContentIds, 500)) {
  839. publishContentMapper.updatePublishContentSingleMiniProgram(partition);
  840. }
  841. }
  842. @Test
  843. public void testAccountStrategy() {
  844. List<String> ghIds = Arrays.asList("gh_9f8dc5b0c74e", "gh_6d9f36e3a7be", "gh_183d80deffb8", "gh_4568b5a7e2fe", "gh_5ff48e9fb9ef", "gh_084a485e859a", "gh_970460d9ccec", "gh_e24da99dc899", "gh_adca24a8f429", "gh_e0eb490115f5", "gh_0e4fd9e88386", "gh_03d32e83122f", "gh_95ed5ecf9363", "gh_749271f1ccd5", "gh_7c66e0dbd2cf", "gh_660afe87b6fd", "gh_03d45c260115", "gh_1686250f15b6", "gh_57c9e8babea7", "gh_bfea052b5baa", "gh_98ec0ffe69b3", "gh_6d3aa9d13402", "gh_2e615fa75ffb", "gh_5d18ac6e3118", "gh_c9b664360ce6", "gh_486568379bf8", "gh_325188c9ea8b", "gh_f93af770fb55", "gh_18c6258ec8f7", "gh_26c906592150", "gh_9bd3ec87db86", "gh_4f34eb52e641", "gh_be0aa7c09379", "gh_fe6ef3a65a48", "gh_631fb48b83a5", "gh_86cb64d57a1d", "gh_74827e516740", "gh_008ef23062ee", "gh_30816d8adb52", "gh_3e91f0624545", "gh_51e4ad40466d", "gh_57573f01b2ee", "gh_744cb16f6e16", "gh_789a40fe7935", "gh_969f5ea5fee1", "gh_ac43eb24376d", "gh_be8c29139989", "gh_c91b42649690", "gh_d5f935d0d1f2", "gh_b1553fe3ef26", "gh_1a2bb3fef76a", "gh_c952bc10dfad", "gh_871c78fca0fa", "gh_c19a94a3fcc1", "gh_a51201bcff28", "gh_292aa2577527", "gh_8355128f7787", "gh_aca0f00404ec", "gh_447df8696a6b", "gh_784083e6713c", "gh_64cff9328215", "gh_a42807f192dc", "gh_2f80ba39f996", "gh_290e053f7b90", "gh_09f0b4c51279");
  845. List<PublishAccount> publishAccountList = publishAccountRepository.getAllByGhIdIn(ghIds);
  846. for (PublishAccount publishAccount : publishAccountList) {
  847. accountStrategyConfigMap.put(publishAccount.getName(), "ArticleRankV15");
  848. }
  849. System.out.println(JSONObject.toJSONString(accountStrategyConfigMap));
  850. }
  851. }