From b5eb17d8b34c49b55ef098a3db2ef7d4f177820e Mon Sep 17 00:00:00 2001 From: Frankzhaopku Date: Thu, 24 Mar 2022 11:14:04 +0800 Subject: [PATCH] refactor: change hypercrx time span Signed-off-by: Frankzhaopku --- src/cron/tasks/hypercrx_actor.ts | 19 +++++++++++++------ src/cron/tasks/hypercrx_repo.ts | 17 ++++++++++++----- src/cron/tasks/open_leaderboard.ts | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/cron/tasks/hypercrx_actor.ts b/src/cron/tasks/hypercrx_actor.ts index 9d84381f0..7e96776e6 100644 --- a/src/cron/tasks/hypercrx_actor.ts +++ b/src/cron/tasks/hypercrx_actor.ts @@ -10,10 +10,17 @@ const task: Task = { callback: async () => { const neo4jClient = await getNeo4jClient(); const session = neo4jClient.session(); - const now = new Date(); - const lastMonth = new Date(now.getTime() - 30 * 24 * 60 * 60); + let t = new Date(); + const activityQueryArr: string[] = []; + const oneMonthByMilliSec = 30 * 24 * 60 * 60 * 1000; + const lastMonth = new Date(t.getTime() - oneMonthByMilliSec); const year = lastMonth.getFullYear(), month = lastMonth.getMonth() + 1; - const q = `MATCH (u:User) WHERE u.activity_${year}${month} IS NOT NULL RETURN u;`; + for (let i = 0; i < 6; i++) { + t = new Date(t.getTime() - oneMonthByMilliSec); + const y = t.getFullYear(), m = t.getMonth() + 1; + activityQueryArr.push(`u.activity_${y}${m}`); + } + const q = `MATCH (u:User) WHERE (${activityQueryArr.map(c => `${c} > 0`).join(' OR ')}) AND (${activityQueryArr.map(c => `COALESCE(${c},0.0)`).join(' + ')} > 10) RETURN u;`; const result = session.run(q); let count = 0; result.subscribe({ @@ -26,10 +33,10 @@ const task: Task = { influence: {}, }; await forEveryMonth(2015, 1, year, month, async (y, m) => { - userInfo.activity[`${y}-${m}`] = user[`activity_${y}${m}`] ?? 0; - userInfo.influence[`${y}-${m}`] = user[`open_rank_${y}${m}`] ?? 0; + userInfo.activity[`${y}-${m}`] = parseFloat(user[`activity_${y}${m}`]?.toFixed(2) ?? 0); + userInfo.influence[`${y}-${m}`] = parseFloat(user[`open_rank_${y}${m}`]?.toFixed(2) ?? 0); }); - const dir = `./local_files/hypercrx_actor/${user.login.charAt(0).toLowerCase()}`; + const dir = `./local_files/hypercrx_actor`; if (!existsSync(dir)) { mkdirSync(dir); } diff --git a/src/cron/tasks/hypercrx_repo.ts b/src/cron/tasks/hypercrx_repo.ts index 8f0fe4141..8f50018be 100644 --- a/src/cron/tasks/hypercrx_repo.ts +++ b/src/cron/tasks/hypercrx_repo.ts @@ -10,10 +10,17 @@ const task: Task = { callback: async () => { const neo4jClient = await getNeo4jClient(); const session = neo4jClient.session(); - const now = new Date(); - const lastMonth = new Date(now.getTime() - 30 * 24 * 60 * 60); + let t = new Date(); + const oneMonthByMilliSec = 30 * 24 * 60 * 60 * 1000; + const lastMonth = new Date(t.getTime() - oneMonthByMilliSec); const year = lastMonth.getFullYear(), month = lastMonth.getMonth() + 1; - const q = `MATCH (r:Repo) WHERE r.activity_${year}${month} IS NOT NULL RETURN r;`; + const activityQueryArr: string[] = []; + for (let i = 0; i < 6; i++) { + t = new Date(t.getTime() - oneMonthByMilliSec); + const y = t.getFullYear(), m = t.getMonth() + 1; + activityQueryArr.push(`r.activity_${y}${m}`); + } + const q = `MATCH (r:Repo) WHERE (${activityQueryArr.map(c => `${c} > 0`).join(' OR ')}) AND (${activityQueryArr.map(c => `COALESCE(${c},0.0)`).join(' + ')} > 10) RETURN r;`; const result = session.run(q); let count = 0; result.subscribe({ @@ -28,8 +35,8 @@ const task: Task = { influence: {}, }; await forEveryMonth(2015, 1, year, month, async (y, m) => { - repoInfo.activity[`${y}-${m}`] = repo[`activity_${y}${m}`] ?? 0; - repoInfo.influence[`${y}-${m}`] = repo[`open_rank_${y}${m}`] ?? 0; + repoInfo.activity[`${y}-${m}`] = parseFloat(repo[`activity_${y}${m}`]?.toFixed(2) ?? 0); + repoInfo.influence[`${y}-${m}`] = parseFloat(repo[`open_rank_${y}${m}`]?.toFixed(2) ?? 0); }); const [owner, name] = repoInfo.name.split('/'); const dir = `./local_files/hypercrx_repo/${owner}`; diff --git a/src/cron/tasks/open_leaderboard.ts b/src/cron/tasks/open_leaderboard.ts index 2f2cdc10d..827dd233e 100644 --- a/src/cron/tasks/open_leaderboard.ts +++ b/src/cron/tasks/open_leaderboard.ts @@ -9,7 +9,7 @@ import { rankData } from '../../utils'; const task: Task = { cron: '0 0 15 * *', // runs on the 15th day of every month at 00:00 enable: true, - immediate: true, + immediate: false, callback: async () => { console.log(`Start to run open leaderboard task.`);