Skip to content

Commit

Permalink
chore: script to delete old records
Browse files Browse the repository at this point in the history
  • Loading branch information
LironEr committed Sep 15, 2023
1 parent bc3c57a commit 1d624df
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ Limitations of the free hosted service:

- Records created by a PR will be deleted after 30 days.
- Records in branches without activity (new commits) will be deleted after 180 days.
- After 90 days only the latest record per day will be kept.

More limitations may be added in the future as more and more projects use the free hosted service.

Expand Down
116 changes: 116 additions & 0 deletions service/scripts/deleteOldRecords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Keep the latest record per day after AGGREGATE_RECORDS_OLDER_THAN_DAYS days

import { closeMongoClient } from '@/framework/mongo/client';
import { getCommitRecordsCollection } from '@/framework/mongo/commitRecords';
import { ObjectId } from 'mongodb';

const AGGREGATE_RECORDS_OLDER_THAN_DAYS = 90;

(async () => {
try {
console.log('Fetch records...');
const commitRecordsCollection = await getCommitRecordsCollection();
const agg = await commitRecordsCollection
.aggregate<{
projectId: string;
subProject?: string;
branch: string;
aggDate: string;
idsToDelete: string[];
}>([
{
$sort: {
creationDate: -1,
},
},
{
$match: {
creationDate: {
$lt: new Date(new Date().setDate(new Date().getDate() - AGGREGATE_RECORDS_OLDER_THAN_DAYS)),
},
},
},
{
$group: {
_id: {
projectId: '$projectId',
subProject: '$subProject',
branch: '$branch',
aggDate: {
$dateToString: {
format: '%Y-%m-%d',
date: '$creationDate',
},
},
},
records: {
$push: '$_id',
},
},
},
{
// check that the records has at least 2 records
$match: {
'records.1': {
$exists: true,
},
},
},
{
$project: {
projectId: '$_id.projectId',
subProject: '$_id.subProject',
branch: '$_id.branch',
aggDate: '$_id.aggDate',
// the first id is the last record per that period, so we keep it
idsToDelete: {
$slice: [
'$records',
1,
{
$subtract: [
{
$size: '$records',
},
1,
],
},
],
},
},
},
{
$unset: '_id',
},
])
.toArray();

const idsToDelete = agg.map((x) => x.idsToDelete).flat();

console.log(`Total records to delete: ${idsToDelete.length}`);

console.log('Are you sure you want to delete all these records? (y/n)');
const answer = await new Promise<string>((resolve) => {
process.stdin.on('data', (data) => {
resolve(data.toString().trim());
});
});

if (answer !== 'y') {
console.log('Abort');
process.exit(0);
}

console.log('Deleting records...');

const result = await commitRecordsCollection.deleteMany({
_id: { $in: idsToDelete.map((id) => new ObjectId(id)) },
});

console.log(`Deleted records: ${result.deletedCount}`);

process.exit(0);
} finally {
await closeMongoClient();
}
})();

0 comments on commit 1d624df

Please sign in to comment.