1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-05 17:53:12 +02:00

fix: dedupe duplicates on batchInsert

This commit is contained in:
Ivar Conradi Østhus 2021-10-06 14:50:43 +02:00
parent 2d04088c23
commit c7689ddf69
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
3 changed files with 42 additions and 12 deletions

View File

@ -79,21 +79,29 @@ export class ClientMetricsStoreV2 implements IClientMetricsStoreV2 {
// Nothing to do!
}
// this function will collapse metrics before sending it to the database.
async batchInsertMetrics(metrics: IClientMetricsEnv[]): Promise<void> {
const rows = metrics.map(toRow);
// Consider rewriting to SQL batch!
for (const row of rows) {
const insert = this.db<ClientMetricsEnvTable>(TABLE)
.insert(row)
.toQuery();
const batch = rows.reduce((prev, curr) => {
// eslint-disable-next-line prettier/prettier
const key = `${curr.feature_name}_${curr.app_name}_${curr.environment}_${curr.timestamp.getTime()}`;
if (prev[key]) {
prev[key].yes += curr.yes;
prev[key].no += curr.no;
} else {
prev[key] = curr;
}
return prev;
}, {});
const query = `${insert.toString()} ON CONFLICT (feature_name, app_name, environment, timestamp)
DO UPDATE SET
"yes" = "client_metrics_env"."yes" + ?,
"no" = "client_metrics_env"."no" + ?`;
await this.db.raw(query, [row.yes, row.no]);
}
// Consider rewriting to SQL batch!
const insert = this.db<ClientMetricsEnvTable>(TABLE)
.insert(Object.values(batch))
.toQuery();
const query = `${insert.toString()} ON CONFLICT (feature_name, app_name, environment, timestamp) DO UPDATE SET "yes" = "client_metrics_env"."yes" + EXCLUDED.yes, "no" = "client_metrics_env"."no" + EXCLUDED.no`;
await this.db.raw(query);
}
async getMetricsForFeatureToggle(

View File

@ -4,10 +4,10 @@ export interface IClientMetricsEnvKey {
featureName: string;
appName: string;
environment: string;
timestamp: Date;
}
export interface IClientMetricsEnv extends IClientMetricsEnvKey {
timestamp: Date;
yes: number;
no: number;
}

View File

@ -192,3 +192,25 @@ test('Should get toggle metrics', async () => {
expect(savedMetrics[0].yes).toBe(4950);
expect(savedMetrics[0].no).toBe(5050);
});
test('Should insert 1500 feature toggle metrics', async () => {
const metrics: IClientMetricsEnv[] = [];
const date = new Date();
for (let i = 0; i < 1500; i++) {
metrics.push({
featureName: `demo-${i}`,
appName: `web`,
environment: 'dev',
timestamp: date,
yes: 2,
no: 2,
});
}
await clientMetricsStore.batchInsertMetrics(metrics);
const savedMetrics = await clientMetricsStore.getAll();
expect(savedMetrics).toHaveLength(1500);
});