diff --git a/src/measurement/store.ts b/src/measurement/store.ts index 2e225743..b827c427 100644 --- a/src/measurement/store.ts +++ b/src/measurement/store.ts @@ -55,7 +55,7 @@ export class MeasurementStore { const results = this.probesToResults(probes, request.type); const probesAwaitingTtl = config.get('measurement.timeout') + 5; const startTime = new Date(); - let measurement: MeasurementRecord = { + const measurement: MeasurementRecord = { id, type: request.type, status: 'in-progress', @@ -68,13 +68,12 @@ export class MeasurementStore { measurementOptions: request.measurementOptions, results, }; - const defaults = getDefaults(request); - measurement = substractObjects(measurement, defaults) as MeasurementRecord; + const measurementWithoutDefaults = this.removeDefaults(measurement, request); await Promise.all([ this.redis.hSet('gp:in-progress', id, startTime.getTime()), this.redis.set(getMeasurementKey(id, 'probes_awaiting'), probes.length, { EX: probesAwaitingTtl }), - this.redis.json.set(key, '$', measurement), + this.redis.json.set(key, '$', measurementWithoutDefaults), this.redis.expire(key, config.get('measurement.resultTTL')), ]); @@ -154,6 +153,14 @@ export class MeasurementStore { }, intervalTime); } + removeDefaults (measurement: MeasurementRecord, request: MeasurementRequest): Partial { + const defaults = getDefaults(request); + // Remove `"limit": 1` from locations. E.g. [{"country": "US", "limit": 1}] => [{"country": "US"}] + measurement.locations = measurement.locations.map(location => location.limit === 1 ? _.omit(location, 'limit') : location); + + return substractObjects(measurement, defaults) as Partial; + } + probesToResults (probes: Probe[], type: string) { const results = probes.map(probe => ({ probe: { diff --git a/src/measurement/types.ts b/src/measurement/types.ts index 49a64d80..3b524ede 100644 --- a/src/measurement/types.ts +++ b/src/measurement/types.ts @@ -204,9 +204,9 @@ export type MeasurementRecord = { createdAt: string; updatedAt: string; target: string; - limit?: number; + limit: number; probesCount: number; - locations?: LocationWithLimit[]; + locations: LocationWithLimit[]; measurementOptions?: MeasurementOptions; results: MeasurementResult[]; }; diff --git a/test/tests/integration/measurement/probe-communication.test.ts b/test/tests/integration/measurement/probe-communication.test.ts index 5ac59ff6..82c8faa7 100644 --- a/test/tests/integration/measurement/probe-communication.test.ts +++ b/test/tests/integration/measurement/probe-communication.test.ts @@ -98,7 +98,7 @@ describe('Create measurement request', () => { status: 'in-progress', target: 'jsdelivr.com', probesCount: 1, - locations: [{ country: 'US', limit: 1 }], + locations: [{ country: 'US' }], measurementOptions: { packets: 4 }, results: [ { @@ -139,7 +139,7 @@ describe('Create measurement request', () => { status: 'in-progress', target: 'jsdelivr.com', probesCount: 1, - locations: [{ country: 'US', limit: 1 }], + locations: [{ country: 'US' }], measurementOptions: { packets: 4 }, results: [ { @@ -178,7 +178,7 @@ describe('Create measurement request', () => { status: 'in-progress', target: 'jsdelivr.com', probesCount: 1, - locations: [{ country: 'US', limit: 1 }], + locations: [{ country: 'US' }], measurementOptions: { packets: 4 }, results: [ { @@ -223,7 +223,7 @@ describe('Create measurement request', () => { status: 'finished', target: 'jsdelivr.com', probesCount: 1, - locations: [{ country: 'US', limit: 1 }], + locations: [{ country: 'US' }], measurementOptions: { packets: 4 }, results: [ { diff --git a/test/tests/integration/measurement/timeout-result.test.ts b/test/tests/integration/measurement/timeout-result.test.ts index 483824ce..d03e10df 100644 --- a/test/tests/integration/measurement/timeout-result.test.ts +++ b/test/tests/integration/measurement/timeout-result.test.ts @@ -97,7 +97,7 @@ describe('Timeout results', () => { status: 'finished', target: 'jsdelivr.com', probesCount: 1, - locations: [{ country: 'US', limit: 1 }], + locations: [{ country: 'US' }], measurementOptions: { packets: 4 }, results: [ {