RecommendTest.java 54 KB

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