mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 13:37:30 +00:00
Website: add script to delete older HistoricalUsageSnapshot records (#42529)
Changes: - Added a new script to the website: `cleanup-old-usage-statistics`, a script that deletes `HistoricalUsageSnapshot` records stored in the website's database that are older than 60 days.
This commit is contained in:
parent
0c4e4e4673
commit
428ab3e83a
1 changed files with 33 additions and 0 deletions
33
website/scripts/cleanup-old-usage-statistics.js
vendored
Normal file
33
website/scripts/cleanup-old-usage-statistics.js
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
module.exports = {
|
||||
|
||||
|
||||
friendlyName: 'Cleanup old usage statistics',
|
||||
|
||||
|
||||
description: 'Deletes HistoricalUsageSnapshot records stored in the database that are over 60 days old.',
|
||||
|
||||
|
||||
fn: async function () {
|
||||
|
||||
sails.log('Running custom shell script... (`sails run cleanup-old-usage-statistics`)');
|
||||
|
||||
let nowAt = Date.now();
|
||||
let sixtyDaysAgoAt = nowAt - (1000 * 60 * 60 * 24 * 60);
|
||||
|
||||
let nativeQueryToDeleteRecords = `
|
||||
DELETE FROM "historicalusagesnapshot"
|
||||
WHERE "createdAt" < ${sixtyDaysAgoAt}`;
|
||||
|
||||
let queryResult = await sails.sendNativeQuery(nativeQueryToDeleteRecords);
|
||||
let numberOfRecordsDeleted = queryResult.rowCount;
|
||||
|
||||
|
||||
sails.log(`Successfully deleted ${numberOfRecordsDeleted} old HistoricalUsageSnapshot records.`);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
Loading…
Reference in a new issue