Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schedule daily metrics deletion #5807

Merged
merged 9 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/lib/db/client-metrics-store-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ afterAll(async () => {
await db.destroy();
});

beforeEach(async () => {
await clientMetricsStore.clearMetrics(0);
await clientMetricsStore.clearDailyMetrics(0);
});

test('aggregate daily metrics from previous day', async () => {
const yesterday = subDays(new Date(), 1);
await clientMetricsStore.batchInsertMetrics([
Expand Down Expand Up @@ -84,3 +89,46 @@ test('aggregate daily metrics from previous day', async () => {
},
]);
});

test('clear daily metrics', async () => {
const yesterday = subDays(new Date(), 1);
const twoDaysAgo = subDays(new Date(), 2);
await clientMetricsStore.batchInsertMetrics([
{
appName: 'irrelevant',
featureName: 'irrelevant',
environment: 'irrelevant',
timestamp: yesterday,
no: 0,
yes: 1,
variants: {
a: 0,
b: 1,
},
},
{
appName: 'irrelevant',
featureName: 'irrelevant',
environment: 'irrelevant',
timestamp: twoDaysAgo,
no: 0,
yes: 2,
variants: {
a: 1,
b: 1,
},
},
]);
await clientMetricsStore.aggregateDailyMetrics();

await clientMetricsStore.clearDailyMetrics(2);

const results = await db.rawDatabase
.table('client_metrics_env_daily')
.select('*');
expect(results.length).toBe(1);
const variantResults = await db.rawDatabase
.table('client_metrics_env_variants_daily')
.select('*');
expect(variantResults.length).toBe(2);
});
6 changes: 6 additions & 0 deletions src/lib/db/client-metrics-store-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ export class ClientMetricsStoreV2 implements IClientMetricsStoreV2 {
.del();
}

async clearDailyMetrics(daysAgo: number): Promise<void> {
return this.db(DAILY_TABLE)
.whereRaw(`date <= CURRENT_DATE - INTERVAL '${daysAgo} days'`)
.del();
}

// aggregates all hourly metrics from a previous day into daily metrics
async aggregateDailyMetrics(): Promise<void> {
const rawQuery: string = `
Expand Down
8 changes: 8 additions & 0 deletions src/lib/features/scheduler/schedule-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ export const scheduleServices = async (
'clearMetrics',
);

schedulerService.schedule(
() => {
clientMetricsServiceV2.clearDailyMetrics(92).catch(console.error);
},
hoursToMilliseconds(24),
'clearDailyMetrics',
);

schedulerService.schedule(
() => {
clientMetricsServiceV2.aggregateDailyMetrics().catch(console.error);
Expand Down
6 changes: 6 additions & 0 deletions src/lib/services/client-metrics/metrics-service-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export default class ClientMetricsServiceV2 {
return this.clientMetricsStoreV2.clearMetrics(hoursAgo);
}

async clearDailyMetrics(daysAgo: number) {
if (this.flagResolver.isEnabled('extendedUsageMetrics')) {
return this.clientMetricsStoreV2.clearDailyMetrics(daysAgo);
}
}

async aggregateDailyMetrics() {
if (this.flagResolver.isEnabled('extendedUsageMetrics')) {
await this.clientMetricsStoreV2.aggregateDailyMetrics();
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/stores/client-metrics-store-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ export interface IClientMetricsStoreV2
hoursBack?: number,
): Promise<string[]>;
clearMetrics(hoursAgo: number): Promise<void>;
clearDailyMetrics(daysAgo: number): Promise<void>;
aggregateDailyMetrics(): Promise<void>;
}
3 changes: 3 additions & 0 deletions src/test/fixtures/fake-client-metrics-store-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default class FakeClientMetricsStoreV2
clearMetrics(hoursBack: number): Promise<void> {
return Promise.resolve();
}
clearDailyMetrics(daysBack: number): Promise<void> {
return Promise.resolve();
}
aggregateDailyMetrics(): Promise<void> {
return Promise.resolve();
}
Expand Down