Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Delete old API logs
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 13, 2020
1 parent bbb002f commit 89b7bd2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ export interface Configuration {
tracking: {
mode: 'all' | 'api-key';
index: string;
deleteOldLogs: boolean;
deleteOldLogsDays: number;
};
}
5 changes: 5 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ const configuration: Configuration = {
tracking: {
mode: process.env.TRACKING_MODE === 'all' ? 'all' : 'api-key',
index: process.env.TRACKING_INDEX ?? 'staart-logs',
deleteOldLogs:
process.env.TRACKING_DELETE_OLD_LOGS !== undefined
? Boolean(process.env.TRACKING_DELETE_OLD_LOGS)
: true,
deleteOldLogsDays: int(process.env.TRACKING_DELETE_OLD_LOGS_DAYS, 90),
},
};

Expand Down
29 changes: 29 additions & 0 deletions src/providers/elasticsearch/elasticsearch.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ export class ElasticSearchService {
.catch(() => {});
}

/**
* Delete old records from ElasticSearch
* @param index - Index
* @param days - Number of days ago (e.g., 30 will delete month-old data)
*/
deleteOldRecords = async (index: string, days: number) => {
const now = new Date();
now.setDate(now.getDate() - days);
if (this.client)
return this.client.deleteByQuery({
index,
body: {
query: {
bool: {
must: [
{
range: {
date: {
lte: now,
},
},
},
],
},
},
},
});
};

private async indexRecord(
index: string,
record: Record<string, any>,
Expand Down
3 changes: 2 additions & 1 deletion src/providers/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ElasticSearchModule } from '../elasticsearch/elasticsearch.module';
import { PrismaModule } from '../prisma/prisma.module';
import { TasksService } from './tasks.service';

@Module({
imports: [ConfigModule, PrismaModule],
imports: [ConfigModule, PrismaModule, ElasticSearchModule],
providers: [TasksService],
exports: [TasksService],
})
Expand Down
15 changes: 15 additions & 0 deletions src/providers/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Cron, CronExpression } from '@nestjs/schedule';
import { Configuration } from '../../config/configuration.interface';
import { ElasticSearchService } from '../elasticsearch/elasticsearch.service';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class TasksService {
constructor(
private prisma: PrismaService,
private configService: ConfigService,
private elasticSearchService: ElasticSearchService,
) {}
private readonly logger = new Logger(TasksService.name);

Expand Down Expand Up @@ -40,4 +43,16 @@ export class TasksService {
if (deleted.count)
this.logger.debug(`Deleted ${deleted.count} inactive users`);
}

@Cron(CronExpression.EVERY_DAY_AT_3PM)
async deleteOldLogs() {
const tracking = this.configService.get<Configuration['tracking']>(
'tracking',
);
if (tracking.deleteOldLogs)
return this.elasticSearchService.deleteOldRecords(
tracking.index,
tracking.deleteOldLogsDays,
);
}
}

0 comments on commit 89b7bd2

Please sign in to comment.