-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: daily event report of all of a user's node packages (#348)
* Initial branch commit * Step 1-6 implementation * Event Listener & nodePackage fix * cleanup cronjob into a file and add native cron dep to release/app * fix: delete all nodes and data to clear detatched data * fix: limit node data sent for daily report --------- Co-authored-by: jgresham <[email protected]>
- Loading branch information
Showing
13 changed files
with
210 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { CronJob } from 'cron'; | ||
import logger from './logger'; | ||
import { reportEvent } from './events'; | ||
import { getUserNodePackagesWithNodes } from './state/nodePackages'; | ||
import store from './state/store'; | ||
import { NodeId, NodeStatus, UserNodePackages } from '../common/node'; | ||
|
||
const CRON_ONCE_A_DAY = '0 0 * * *'; | ||
// const CRON_ONCE_A_DAY = '* * * * *'; // one minute for testing | ||
|
||
type NodeReportData = { | ||
specId: string; | ||
specVersion: string; | ||
status: NodeStatus; | ||
diskUsedGBs?: number; | ||
}; | ||
|
||
type PackageReportData = { | ||
specId: string; | ||
specVersion: string; | ||
status: NodeStatus; | ||
nodes: Record<NodeId, NodeReportData>; | ||
}; | ||
export const reportDataForNodePackages = ( | ||
userNodePackages: UserNodePackages, | ||
) => { | ||
const reportData: Record<NodeId, PackageReportData> = {}; | ||
|
||
userNodePackages?.nodeIds.forEach((nodeId: NodeId) => { | ||
const nodePackage = userNodePackages.nodes[nodeId]; | ||
const packageReportData: PackageReportData = { | ||
specId: nodePackage.spec.specId, | ||
specVersion: nodePackage.spec.version, | ||
status: nodePackage.status, | ||
nodes: {}, | ||
}; | ||
|
||
nodePackage.nodes.forEach((node) => { | ||
let diskUsedGBs; | ||
if (node.runtime?.usage?.diskGBs?.[0]?.y !== undefined) { | ||
diskUsedGBs = node.runtime.usage.diskGBs[0].y; | ||
} | ||
packageReportData.nodes[node.id] = { | ||
specId: node.spec.specId, | ||
specVersion: node.spec.version, | ||
status: node.status, | ||
diskUsedGBs, | ||
}; | ||
}); | ||
|
||
reportData[nodeId] = packageReportData; | ||
}); | ||
return reportData; | ||
}; | ||
|
||
const dailyReportFunc = async () => { | ||
const lastDailyReportTimestamp = store.get( | ||
'lastDailyReportTimestamp', | ||
) as number; | ||
const nowTimestamp = Date.now(); | ||
// Subtracts 23 hours and 59 minutes ago, (86,340,000 milliseconds) | ||
const oneDayAgo = nowTimestamp - 23 * 60 * 60 * 1000 - 59 * 60 * 1000; | ||
// const oneDayAgo = nowTimestamp + 1 * 60 * 1000; // one minute for testing | ||
if ( | ||
lastDailyReportTimestamp < oneDayAgo || | ||
lastDailyReportTimestamp === undefined | ||
) { | ||
// It's been more than a day, proceed with the report | ||
logger.info( | ||
'Cron dailyReportJob: it has been more than a day. Reporting dailyReport.', | ||
); | ||
const userNodePackages = await getUserNodePackagesWithNodes(); | ||
// console.log( | ||
// 'userNodePackages: ', | ||
// JSON.stringify(userNodePackages, null, 2), | ||
// ); | ||
const reportData = reportDataForNodePackages(userNodePackages); | ||
// console.log('reportData: ', JSON.stringify(reportData)); | ||
reportEvent('DailyUserReport', reportData); | ||
store.set('lastDailyReportTimestamp', nowTimestamp); | ||
} | ||
}; | ||
|
||
// Run once everyday at midnight in the user's local timezone | ||
const dailyReportJob = new CronJob(CRON_ONCE_A_DAY, async () => { | ||
logger.info('Running cron dailyReportJob...'); | ||
await dailyReportFunc(); | ||
logger.info('End cron dailyReportJob.'); | ||
}); | ||
|
||
export const initialize = () => { | ||
// Start the cron jobs and then run some for a first time now | ||
dailyReportJob.start(); | ||
dailyReportFunc(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { ReportEventData } from 'renderer/events/reportEvent'; | ||
import { ReportEventData } from '../renderer/events/reportEvent'; | ||
import { CHANNELS, send } from './messenger'; | ||
|
||
export const reportEvent = (event: string, eventData?: ReportEventData) => { | ||
// console.log('reportEvent: ', event, eventData); | ||
send(CHANNELS.reportEvent, event, eventData); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.