RecommendTest.java 50 KB

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