RecommendTest.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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.ArticleTypeEnum;
  7. import com.tzld.longarticle.recommend.server.mapper.crawler.CrawlerBaseMapper;
  8. import com.tzld.longarticle.recommend.server.model.entity.crawler.AccountAvgInfo;
  9. import com.tzld.longarticle.recommend.server.model.entity.crawler.Article;
  10. import com.tzld.longarticle.recommend.server.model.entity.crawler.ArticleDetailInfo;
  11. import com.tzld.longarticle.recommend.server.model.entity.crawler.PublishSortLog;
  12. import com.tzld.longarticle.recommend.server.repository.crawler.AccountAvgInfoRepository;
  13. import com.tzld.longarticle.recommend.server.repository.crawler.ArticleDetailInfoRepository;
  14. import com.tzld.longarticle.recommend.server.repository.crawler.ArticleRepository;
  15. import com.tzld.longarticle.recommend.server.repository.crawler.PublishSortLogRepository;
  16. import com.tzld.longarticle.recommend.server.service.RecommendService;
  17. import com.tzld.longarticle.recommend.server.service.recall.RecallService;
  18. import com.tzld.longarticle.recommend.server.util.DateUtils;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.Row;
  22. import org.apache.poi.ss.usermodel.Sheet;
  23. import org.apache.poi.ss.usermodel.Workbook;
  24. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  25. import org.junit.jupiter.api.Test;
  26. import org.springframework.boot.test.context.SpringBootTest;
  27. import javax.annotation.Resource;
  28. import java.io.File;
  29. import java.io.FileOutputStream;
  30. import java.io.IOException;
  31. import java.nio.charset.StandardCharsets;
  32. import java.nio.file.Files;
  33. import java.util.*;
  34. import java.util.stream.Collectors;
  35. @SpringBootTest(classes = Application.class)
  36. @Slf4j
  37. public class RecommendTest {
  38. @Resource
  39. private RecommendService recommendService;
  40. @Resource
  41. private RecallService recallService;
  42. @Resource
  43. private ArticleRepository articleRepository;
  44. @Resource
  45. private ArticleDetailInfoRepository articleDetailInfoRepository;
  46. @Resource
  47. private AccountAvgInfoRepository accountAvgInfoRepository;
  48. @Resource
  49. private CrawlerBaseMapper crawlerBaseMapper;
  50. @Resource
  51. private PublishSortLogRepository publishSortLogRepository;
  52. // @Test
  53. // void recall() {
  54. // RecallParam param = new RecallParam();
  55. // param.setAccountId("20231213123536190184852");
  56. // param.setPlanId("20240718181730864154902");
  57. // RecallResult recallResult = recallService.recall(param);
  58. // System.out.println(JSONObject.toJSONString(recallResult));
  59. // }
  60. //
  61. // @Test
  62. // void exportData() {
  63. // Set<String> ghIds = new HashSet<>(Arrays.asList("gh_adca24a8f429", "gh_e0eb490115f5", "gh_51e4ad40466d", "gh_95ed5ecf9363"));
  64. // List<Article> articleList = articleRepository.getByGhIdInAndUpdateTimeGreaterThanAndTypeEquals(ghIds, 1722441600L, ArticleTypeEnum.qunfa.getVal());
  65. //
  66. // Map<String, Map<Integer, List<Article>>> map = articleList.stream()
  67. // .collect(Collectors.groupingBy(Article::getTitle, Collectors.groupingBy(Article::getItemIndex)));
  68. // Set<String> snList = articleList.stream().map(Article::getWxSn).collect(Collectors.toSet());
  69. // List<ArticleDetailInfo> articleDetailInfoList = articleDetailInfoRepository.getAllByWxSnIn(new ArrayList<>(snList));
  70. // Map<String, List<ArticleDetailInfo>> articleDetailInfoMap = articleDetailInfoList.stream()
  71. // .collect(Collectors.groupingBy(ArticleDetailInfo::getWxSn));
  72. //
  73. // List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdInAndStatusEquals(ghIds, 1);
  74. // Map<String, Map<String, AccountAvgInfo>> accountAvgInfoIndexMap = accountAvgInfoList.stream().collect(
  75. // Collectors.groupingBy(AccountAvgInfo::getGhId, Collectors.toMap(AccountAvgInfo::getPosition, o -> o)));
  76. // JSONArray jsonArray = new JSONArray();
  77. // for (Article article : articleList) {
  78. // List<ArticleDetailInfo> articleDetailInfos = articleDetailInfoMap.get(article.getWxSn());
  79. // if (CollectionUtils.isEmpty(articleDetailInfos)) {
  80. // continue;
  81. // }
  82. // Date minDate = articleDetailInfos.stream().map(ArticleDetailInfo::getRecallDt).min(Date::compareTo).orElse(new Date());
  83. // int sumfirstLevel = 0;
  84. // int sumFission0 = 0;
  85. // int sumFission1 = 0;
  86. // int sumFission2 = 0;
  87. // for (ArticleDetailInfo articleDetailInfo : articleDetailInfos) {
  88. // if (articleDetailInfo.getRecallDt().equals(minDate)) {
  89. // sumfirstLevel += Optional.ofNullable(articleDetailInfo.getFirstLevel()).orElse(0);
  90. // sumFission0 += Optional.ofNullable(articleDetailInfo.getFission0()).orElse(0);
  91. // sumFission1 += Optional.ofNullable(articleDetailInfo.getFission1()).orElse(0);
  92. // sumFission2 += Optional.ofNullable(articleDetailInfo.getFission2()).orElse(0);
  93. // }
  94. // }
  95. // Map<String, AccountAvgInfo> accountAvgInfoMap = accountAvgInfoIndexMap.get(article.getGhId());
  96. // AccountAvgInfo avgInfo = accountAvgInfoMap.get(article.getItemIndex().toString());
  97. // SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  98. // String date = sdf.format(new Date(article.getUpdateTime() * 1000));
  99. // JSONObject obj = new JSONObject();
  100. // obj.put("ghId", article.getGhId());
  101. // obj.put("accountName", article.getAccountName());
  102. // obj.put("title", article.getTitle());
  103. // obj.put("index", article.getItemIndex());
  104. // obj.put("viewCount", article.getShowViewCount());
  105. // obj.put("time", date);
  106. // if (Objects.nonNull(avgInfo)) {
  107. // obj.put("fans", avgInfo.getFans());
  108. // obj.put("avgViewCount", avgInfo.getReadAvg());
  109. // obj.put("viewCountRate", (article.getShowViewCount() * 1.0) / avgInfo.getReadAvg());
  110. // }
  111. // obj.put("firstLevel", sumfirstLevel);
  112. // obj.put("fission0", sumFission0);
  113. // obj.put("fission1", sumFission1);
  114. // obj.put("fission2", sumFission2);
  115. // jsonArray.add(obj);
  116. // }
  117. // System.out.println(jsonArray.toJSONString());
  118. // }
  119. //
  120. // @Test
  121. // void ii() throws IOException {
  122. // String dateStr = "20240911";
  123. // List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByStatusEquals(1);
  124. // Map<String, String> accountMap = accountAvgInfoList.stream().collect(
  125. // Collectors.toMap(AccountAvgInfo::getAccountName, AccountAvgInfo::getGhId, (existing, replacement) -> replacement));
  126. // BufferedReader reader = new BufferedReader(new FileReader("/Users/wangyunpeng/Downloads/账号相关性.json"));
  127. // StringBuilder sb = new StringBuilder();
  128. // String line;
  129. // while ((line = reader.readLine()) != null) {
  130. // sb.append(line);
  131. // }
  132. // String jsonStr = sb.toString();
  133. // // 使用 ObjectMapper 解析 JSON
  134. // ObjectMapper objectMapper = new ObjectMapper();
  135. //
  136. // try {
  137. // // 将 JSON 转换为 Map<String, Map<String, Double>>
  138. // Map<String, Map<String, Double>> result = objectMapper.readValue(jsonStr,
  139. // new TypeReference<Map<String, Map<String, Double>>>() {
  140. // });
  141. //
  142. // // 输出转换结果
  143. // System.out.println(result);
  144. // List<AccountCorrelation> saveList = new ArrayList<>();
  145. // result.forEach((k, v) -> {
  146. // String ghId = accountMap.get(k);
  147. // v.forEach((k1, v1) -> {
  148. // String relGhId = accountMap.get(k1);
  149. // AccountCorrelation save = new AccountCorrelation();
  150. // save.setDateStr(dateStr);
  151. // save.setGhId(ghId);
  152. // save.setAccountName(k);
  153. // save.setRelGhId(relGhId);
  154. // save.setRelAccountName(k1);
  155. // save.setStatus(1);
  156. // save.setCorrelation(v1);
  157. // saveList.add(save);
  158. // });
  159. // });
  160. // List<AccountCorrelation> all = new ArrayList<>(saveList);
  161. // for (AccountCorrelation item : all) {
  162. // if (!item.getGhId().equals(item.getRelGhId())) {
  163. // AccountCorrelation save = new AccountCorrelation();
  164. // BeanUtils.copyProperties(item, save);
  165. // save.setGhId(item.getRelGhId());
  166. // save.setAccountName(item.getRelAccountName());
  167. // save.setRelGhId(item.getGhId());
  168. // save.setRelAccountName(item.getAccountName());
  169. // saveList.add(save);
  170. // }
  171. // }
  172. // crawlerBaseMapper.batchInsertAccountCorrelation(saveList);
  173. //
  174. //
  175. // } catch (IOException e) {
  176. // e.printStackTrace();
  177. // }
  178. // }
  179. @Test
  180. public void test() {
  181. List<String> morning = Lists.newArrayList("gh_084a485e859a", "gh_183d80deffb8", "gh_5ff48e9fb9ef", "gh_6d9f36e3a7be", "gh_9f8dc5b0c74e", "gh_e0eb490115f5", "gh_e24da99dc899");
  182. 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");
  183. String dateStr = "2024-09-12";
  184. List<Article> articleList = articleRepository.getByUpdateTimeGreaterThanAndTypeEquals(1725120000L, ArticleTypeEnum.QUNFA.getVal());
  185. articleList = articleList.stream().filter(o -> o.getItemIndex() == 1 && o.getUpdateTime() < 1726675200).collect(Collectors.toList());
  186. Map<String, List<Article>> map = articleList.stream().collect(Collectors.groupingBy(Article::getTitle));
  187. List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByUpdateTime(dateStr);
  188. accountAvgInfoList = accountAvgInfoList.stream().filter(o -> o.getPosition().equals("1")).collect(Collectors.toList());
  189. Map<String, AccountAvgInfo> accountAvgInfoMap = accountAvgInfoList.stream().collect(Collectors.toMap(AccountAvgInfo::getGhId, o -> o));
  190. List<String> wxSnList = articleList.stream().map(Article::getWxSn).collect(Collectors.toList());
  191. List<ArticleDetailInfo> articleDetailInfoList = articleDetailInfoRepository.getAllByWxSnIn(wxSnList);
  192. Map<String, List<ArticleDetailInfo>> articleDetailInfoMap = articleDetailInfoList.stream()
  193. .collect(Collectors.groupingBy(ArticleDetailInfo::getWxSn));
  194. JSONArray result = new JSONArray();
  195. int sumFissionMorning = 0;
  196. int sumFissionMoon = 0;
  197. int sumReadMorning = 0;
  198. int sumReadNoon = 0;
  199. long sumFansMorning = 0;
  200. long sumFansMoon = 0;
  201. double readAvgMorning = 0.0;
  202. double readAvgMoon = 0.0;
  203. for (Article article : articleList) {
  204. List<ArticleDetailInfo> articleDetailInfos = articleDetailInfoMap.get(article.getWxSn());
  205. if (CollectionUtil.isEmpty(articleDetailInfos)) {
  206. continue;
  207. }
  208. Date minDate = articleDetailInfos.stream().map(ArticleDetailInfo::getRecallDt).min(Date::compareTo).orElse(new Date());
  209. AccountAvgInfo accountAvgInfo = accountAvgInfoMap.get(article.getGhId());
  210. for (ArticleDetailInfo articleDetailInfo : articleDetailInfos) {
  211. if (articleDetailInfo.getRecallDt().equals(minDate)) {
  212. if (morning.contains(article.getGhId())) {
  213. sumFissionMorning += Optional.ofNullable(articleDetailInfo.getFission0()).orElse(0);
  214. sumFissionMorning += Optional.ofNullable(articleDetailInfo.getFission1()).orElse(0);
  215. sumFissionMorning += Optional.ofNullable(articleDetailInfo.getFission2()).orElse(0);
  216. }
  217. if (noon.contains(article.getGhId())) {
  218. sumFissionMoon += Optional.ofNullable(articleDetailInfo.getFission0()).orElse(0);
  219. sumFissionMoon += Optional.ofNullable(articleDetailInfo.getFission1()).orElse(0);
  220. sumFissionMoon += Optional.ofNullable(articleDetailInfo.getFission2()).orElse(0);
  221. }
  222. }
  223. }
  224. if (Objects.nonNull(accountAvgInfo)) {
  225. if (morning.contains(article.getGhId())) {
  226. readAvgMorning += accountAvgInfo.getReadAvg();
  227. sumFansMorning += accountAvgInfo.getFans();
  228. sumReadMorning += article.getShowViewCount();
  229. }
  230. if (noon.contains(article.getGhId())) {
  231. readAvgMoon += accountAvgInfo.getReadAvg();
  232. sumFansMoon += accountAvgInfo.getFans();
  233. sumReadNoon += article.getShowViewCount();
  234. }
  235. }
  236. }
  237. JSONObject jsonObjectMorning = new JSONObject();
  238. jsonObjectMorning.put("时间", "早上");
  239. jsonObjectMorning.put("sumFission", sumFissionMorning);
  240. jsonObjectMorning.put("readAvg", readAvgMorning);
  241. jsonObjectMorning.put("rate", sumFissionMorning / readAvgMorning);
  242. jsonObjectMorning.put("sumRead", sumReadMorning);
  243. jsonObjectMorning.put("sumFans", sumFansMorning);
  244. jsonObjectMorning.put("阅读率", sumReadMorning / (double) sumFansMorning);
  245. result.add(jsonObjectMorning);
  246. JSONObject jsonObjectMoon = new JSONObject();
  247. jsonObjectMoon.put("时间", "中午");
  248. jsonObjectMoon.put("sumFission", sumFissionMoon);
  249. jsonObjectMoon.put("readAvg", readAvgMoon);
  250. jsonObjectMoon.put("rate", sumFissionMoon / readAvgMoon);
  251. jsonObjectMoon.put("sumRead", sumReadNoon);
  252. jsonObjectMoon.put("sumFans", sumFansMoon);
  253. jsonObjectMoon.put("阅读率", sumReadNoon / (double) sumFansMoon);
  254. result.add(jsonObjectMoon);
  255. System.out.println(JSONObject.toJSONString(result));
  256. }
  257. @Test
  258. public void exportScoreData() {
  259. List<String> strategies = Arrays.asList("ArticleRankV11", "ArticleRankV12");
  260. List<PublishSortLog> sortLogList = publishSortLogRepository.findByStrategyInAndDateStrGreaterThanEqual(strategies, "20240928");
  261. sortLogList = sortLogList.stream().filter(o -> o.getIndex() == 1).collect(Collectors.toList());
  262. sortLogList.sort(Comparator.comparing(PublishSortLog::getGhId).thenComparing(PublishSortLog::getDateStr));
  263. List<String> ghIds = sortLogList.stream().map(PublishSortLog::getGhId).distinct().collect(Collectors.toList());
  264. List<Article> articleList = articleRepository.getByGhIdInAndUpdateTimeGreaterThanAndTypeEquals(ghIds, 1727452800L, ArticleTypeEnum.QUNFA.getVal());
  265. articleList = articleList.stream().filter(o -> o.getItemIndex() == 1).collect(Collectors.toList());
  266. Map<String, Map<String, Article>> articleMap = articleList.stream().collect(Collectors.groupingBy(Article::getGhId, Collectors.toMap(
  267. o -> DateUtils.timestampToYMDStr(o.getUpdateTime(),"yyyyMMdd"), o -> o,
  268. (existing, replacement) -> replacement)));
  269. List<AccountAvgInfo> accountAvgInfoList = accountAvgInfoRepository.getAllByGhIdIn(new HashSet<>(ghIds));
  270. Map<String, Map<String, AccountAvgInfo>> accountAvgInfoMap = accountAvgInfoList.stream()
  271. .filter(o -> Objects.equals(o.getPosition(), "1")).collect(Collectors.groupingBy(AccountAvgInfo::getGhId,
  272. Collectors.toMap(AccountAvgInfo::getUpdateTime, o -> o)));
  273. Workbook workbook = new XSSFWorkbook();
  274. Sheet sheet = workbook.createSheet("ExampleSheet");
  275. int rowNum = 0;
  276. // 创建标题行
  277. Row titleRow = sheet.createRow(rowNum);
  278. Cell titleCell = titleRow.createCell(0);
  279. titleCell.setCellValue("日期");
  280. titleCell = titleRow.createCell(1);
  281. titleCell.setCellValue("ghID");
  282. titleCell = titleRow.createCell(2);
  283. titleCell.setCellValue("账号名称");
  284. titleCell = titleRow.createCell(3);
  285. titleCell.setCellValue("标题");
  286. titleCell = titleRow.createCell(4);
  287. titleCell.setCellValue("策略");
  288. titleCell = titleRow.createCell(5);
  289. titleCell.setCellValue("得分");
  290. titleCell = titleRow.createCell(6);
  291. titleCell.setCellValue("HisFissionFansRateRateStrategy");
  292. titleCell = titleRow.createCell(7);
  293. titleCell.setCellValue("HisFissionAvgReadRateRateStrategy");
  294. titleCell = titleRow.createCell(8);
  295. titleCell.setCellValue("PublishTimesStrategy");
  296. titleCell = titleRow.createCell(9);
  297. titleCell.setCellValue("ViewCountRateCorrelationStrategy");
  298. titleCell = titleRow.createCell(10);
  299. titleCell.setCellValue("HisFissionAvgReadSumRateStrategy");
  300. titleCell = titleRow.createCell(11);
  301. titleCell.setCellValue("HisFissionAvgReadRateCorrelationRateStrategy");
  302. titleCell = titleRow.createCell(12);
  303. titleCell.setCellValue("HisFissionFansSumRateStrategy");
  304. titleCell = titleRow.createCell(13);
  305. titleCell.setCellValue("SimilarityStrategy");
  306. titleCell = titleRow.createCell(14);
  307. titleCell.setCellValue("ViewCountStrategy");
  308. titleCell = titleRow.createCell(15);
  309. titleCell.setCellValue("ViewCountRateStrategy");
  310. titleCell = titleRow.createCell(16);
  311. titleCell.setCellValue("HisFissionDeWeightAvgReadSumRateStrategy");
  312. titleCell = titleRow.createCell(17);
  313. titleCell.setCellValue("阅读量");
  314. titleCell = titleRow.createCell(18);
  315. titleCell.setCellValue("阅读均值");
  316. titleCell = titleRow.createCell(19);
  317. titleCell.setCellValue("阅读均值倍数");
  318. // 填充数据
  319. String title = "";
  320. for (PublishSortLog publishSortLog : sortLogList) {
  321. Map<String, Article> dateArticleMap = articleMap.get(publishSortLog.getGhId());
  322. Article article = dateArticleMap.get(publishSortLog.getDateStr());
  323. if (Objects.isNull(article) || !publishSortLog.getTitle().equals(article.getTitle())) {
  324. continue;
  325. }
  326. if (publishSortLog.getTitle().equals(title)) {
  327. continue;
  328. }
  329. title = publishSortLog.getTitle();
  330. rowNum++;
  331. Row row = sheet.createRow(rowNum);
  332. Cell cell = row.createCell(0);
  333. cell.setCellValue(publishSortLog.getDateStr());
  334. cell = row.createCell(1);
  335. cell.setCellValue(publishSortLog.getGhId());
  336. cell = row.createCell(2);
  337. cell.setCellValue(publishSortLog.getAccountName());
  338. cell = row.createCell(3);
  339. cell.setCellValue(publishSortLog.getTitle());
  340. cell = row.createCell(4);
  341. cell.setCellValue(publishSortLog.getStrategy());
  342. cell = row.createCell(5);
  343. cell.setCellValue(publishSortLog.getScore());
  344. cell = row.createCell(6);
  345. JSONObject scoreMap = JSONObject.parseObject(publishSortLog.getScoreMap());
  346. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansRateRateStrategy")).orElse(0.0)));
  347. cell = row.createCell(7);
  348. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateRateStrategy")).orElse(0.0)));
  349. cell = row.createCell(8);
  350. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("PublishTimesStrategy")).orElse(0.0)));
  351. cell = row.createCell(9);
  352. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateCorrelationStrategy")).orElse(0.0)));
  353. cell = row.createCell(10);
  354. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadSumRateStrategy")).orElse(0.0)));
  355. cell = row.createCell(11);
  356. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateCorrelationRateStrategy")).orElse(0.0)));
  357. cell = row.createCell(12);
  358. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansSumRateStrategy")).orElse(0.0)));
  359. cell = row.createCell(13);
  360. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("SimilarityStrategy")).orElse(0.0)));
  361. cell = row.createCell(14);
  362. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountStrategy")).orElse(0.0)));
  363. cell = row.createCell(15);
  364. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateStrategy")).orElse(0.0)));
  365. cell = row.createCell(16);
  366. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionDeWeightAvgReadSumRateStrategy")).orElse(0.0)));
  367. cell = row.createCell(17);
  368. cell.setCellValue(article.getShowViewCount());
  369. cell = row.createCell(18);
  370. Map<String, AccountAvgInfo> map = accountAvgInfoMap.get(article.getGhId());
  371. if (Objects.nonNull(map)) {
  372. List<String> avgMapDateList = new ArrayList<>(map.keySet());
  373. String publishDate = DateUtils.findNearestDate(avgMapDateList,
  374. DateUtils.timestampToYMDStr(article.getUpdateTime(), "yyyy-MM-dd"), "yyyy-MM-dd");
  375. AccountAvgInfo accountAvgInfo = map.get(publishDate);
  376. if (Objects.nonNull(accountAvgInfo)) {
  377. cell.setCellValue(accountAvgInfo.getReadAvg());
  378. cell = row.createCell(19);
  379. cell.setCellValue(String.format("%.3f", article.getShowViewCount() / (double) accountAvgInfo.getReadAvg()));
  380. }
  381. }
  382. }
  383. try (FileOutputStream outputStream = new FileOutputStream("/Users/wangyunpeng/Downloads/example.xlsx")) {
  384. workbook.write(outputStream);
  385. } catch (IOException e) {
  386. e.printStackTrace();
  387. } finally {
  388. try {
  389. workbook.close();
  390. } catch (IOException e) {
  391. e.printStackTrace();
  392. }
  393. }
  394. }
  395. @Test
  396. public void exportFromAliyunLog() {
  397. String folderPath = "/Users/wangyunpeng/Downloads/longarticle-recommend-server-test_info-log_20241014_150245.json";
  398. File file = new File(folderPath);
  399. Workbook workbook = new XSSFWorkbook();
  400. Sheet sheet = workbook.createSheet("ExampleSheet");
  401. int rowNum = 0;
  402. // 创建标题行
  403. Row titleRow = sheet.createRow(rowNum);
  404. Cell titleCell = titleRow.createCell(0);
  405. titleCell.setCellValue("日期");
  406. titleCell = titleRow.createCell(1);
  407. titleCell.setCellValue("账号名称");
  408. titleCell = titleRow.createCell(2);
  409. titleCell.setCellValue("id");
  410. titleCell = titleRow.createCell(3);
  411. titleCell.setCellValue("标题");
  412. titleCell = titleRow.createCell(4);
  413. titleCell.setCellValue("策略");
  414. titleCell = titleRow.createCell(5);
  415. titleCell.setCellValue("得分");
  416. titleCell = titleRow.createCell(6);
  417. titleCell.setCellValue("HisFissionFansRateRateStrategy");
  418. titleCell = titleRow.createCell(7);
  419. titleCell.setCellValue("HisFissionAvgReadRateRateStrategy");
  420. titleCell = titleRow.createCell(8);
  421. titleCell.setCellValue("PublishTimesStrategy");
  422. titleCell = titleRow.createCell(9);
  423. titleCell.setCellValue("ViewCountRateCorrelationStrategy");
  424. titleCell = titleRow.createCell(10);
  425. titleCell.setCellValue("HisFissionAvgReadSumRateStrategy");
  426. titleCell = titleRow.createCell(11);
  427. titleCell.setCellValue("HisFissionAvgReadRateCorrelationRateStrategy");
  428. titleCell = titleRow.createCell(12);
  429. titleCell.setCellValue("HisFissionFansSumRateStrategy");
  430. titleCell = titleRow.createCell(13);
  431. titleCell.setCellValue("SimilarityStrategy");
  432. titleCell = titleRow.createCell(14);
  433. titleCell.setCellValue("ViewCountStrategy");
  434. titleCell = titleRow.createCell(15);
  435. titleCell.setCellValue("ViewCountRateStrategy");
  436. titleCell = titleRow.createCell(16);
  437. titleCell.setCellValue("HisFissionDeWeightAvgReadSumRateStrategy");
  438. try {
  439. String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
  440. JSONArray jsonArray = JSONArray.parseArray(content);
  441. for (Object o : jsonArray) {
  442. JSONObject jsonObject = (JSONObject) o;
  443. Long time = jsonObject.getLong("__time__");
  444. String message = jsonObject.getString("message");
  445. int index = message.indexOf("[");
  446. String info = message.substring(0, index);
  447. String strategy = info.substring(0, info.indexOf(" "));
  448. String accountName = info.substring(info.indexOf(" ")).replace("账号名称 ", "")
  449. .replace(" 头条评分结果", "");
  450. String json = message.substring(index);
  451. JSONArray scoreArray = JSONArray.parseArray(json);
  452. for (Object scoreJSON : scoreArray) {
  453. JSONObject scoreObject = (JSONObject) scoreJSON;
  454. String id = scoreObject.getString("id");
  455. String title = scoreObject.getString("title");
  456. String score = scoreObject.getString("score");
  457. String scoreMapStr = scoreObject.getString("scoreMap");
  458. rowNum++;
  459. Row row = sheet.createRow(rowNum);
  460. Cell cell = row.createCell(0);
  461. cell.setCellValue(DateUtils.timestampToYMDStr(time, "yyyyMMdd"));
  462. cell = row.createCell(1);
  463. cell.setCellValue(accountName);
  464. cell = row.createCell(2);
  465. cell.setCellValue(id);
  466. cell = row.createCell(3);
  467. cell.setCellValue(title);
  468. cell = row.createCell(4);
  469. cell.setCellValue(strategy);
  470. cell = row.createCell(5);
  471. cell.setCellValue(score);
  472. cell = row.createCell(6);
  473. JSONObject scoreMap = JSONObject.parseObject(scoreMapStr);
  474. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansRateRateStrategy")).orElse(0.0)));
  475. cell = row.createCell(7);
  476. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateRateStrategy")).orElse(0.0)));
  477. cell = row.createCell(8);
  478. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("PublishTimesStrategy")).orElse(0.0)));
  479. cell = row.createCell(9);
  480. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateCorrelationStrategy")).orElse(0.0)));
  481. cell = row.createCell(10);
  482. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadSumRateStrategy")).orElse(0.0)));
  483. cell = row.createCell(11);
  484. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionAvgReadRateCorrelationRateStrategy")).orElse(0.0)));
  485. cell = row.createCell(12);
  486. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionFansSumRateStrategy")).orElse(0.0)));
  487. cell = row.createCell(13);
  488. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("SimilarityStrategy")).orElse(0.0)));
  489. cell = row.createCell(14);
  490. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountStrategy")).orElse(0.0)));
  491. cell = row.createCell(15);
  492. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("ViewCountRateStrategy")).orElse(0.0)));
  493. cell = row.createCell(16);
  494. cell.setCellValue(String.format("%.3f", Optional.of(scoreMap.getDoubleValue("HisFissionDeWeightAvgReadSumRateStrategy")).orElse(0.0)));
  495. }
  496. }
  497. try (FileOutputStream outputStream = new FileOutputStream("/Users/wangyunpeng/Downloads/example.xlsx")) {
  498. workbook.write(outputStream);
  499. } catch (IOException e) {
  500. e.printStackTrace();
  501. } finally {
  502. try {
  503. workbook.close();
  504. } catch (IOException e) {
  505. e.printStackTrace();
  506. }
  507. }
  508. } catch (Exception e) {
  509. log.error("readFileError fileName:{}", file.getName(), e);
  510. }
  511. }
  512. }