diff --git a/dist/cjs/index.js b/dist/cjs/index.js index 22b9317..7aa837f 100644 --- a/dist/cjs/index.js +++ b/dist/cjs/index.js @@ -231,10 +231,23 @@ class CustomMetrics { let dimensions = this.makeDimensionString(dim); let old = await this.getMetric(owner, namespace, metricName, dimensions, options.log); metric = this.upgradeMetric(old); + await this.putMetric(metric, options); } return metric; } upgradeMetric(old) { + let required = false; + if (this.spans.length == old.spans.length) { + for (let [index, span] of Object.entries(old.spans)) { + if (span.period != this.spans[index].period || + span.samples != this.spans[index].samples) { + required = true; + } + } + if (!required) { + return old; + } + } let timestamp = old.spans[0].end || Math.floor(Date.now() / 1000); let metric = this.initMetric(old.owner, old.namespace, old.metric, old.dimensions, timestamp); for (let span of old.spans) { diff --git a/dist/mjs/index.js b/dist/mjs/index.js index b5d81fa..bbaf844 100644 --- a/dist/mjs/index.js +++ b/dist/mjs/index.js @@ -239,10 +239,23 @@ export class CustomMetrics { let dimensions = this.makeDimensionString(dim); let old = await this.getMetric(owner, namespace, metricName, dimensions, options.log); metric = this.upgradeMetric(old); + await this.putMetric(metric, options); } return metric; } upgradeMetric(old) { + let required = false; + if (this.spans.length == old.spans.length) { + for (let [index, span] of Object.entries(old.spans)) { + if (span.period != this.spans[index].period || + span.samples != this.spans[index].samples) { + required = true; + } + } + if (!required) { + return old; + } + } let timestamp = old.spans[0].end || Math.floor(Date.now() / 1000); let metric = this.initMetric(old.owner, old.namespace, old.metric, old.dimensions, timestamp); for (let span of old.spans) { diff --git a/test/upgrade.ts b/test/upgrade.ts index 650f975..24ebdae 100644 --- a/test/upgrade.ts +++ b/test/upgrade.ts @@ -25,7 +25,6 @@ const MoreSpans: SpanDef[] = [ test('Upgrade Spans', async () => { let metrics = new CustomMetrics({client, table, log: true}) let timestamp = new Date(2000, 0, 1).getTime() - console.log("TS", timestamp) let metric for (let i = 0; i < 140; i++) { @@ -38,23 +37,19 @@ test('Upgrade Spans', async () => { expect(metric.spans[1].points.length).toBe(12) expect(metric.spans[2].points.length).toBe(1) expect(metric.spans[3].points.length).toBe(0) - dump(metric) // Test upgrade metrics = new CustomMetrics({client, table, log: true, spans: MoreSpans}) metric = await metrics.upgrade('test/upgrade', 'UpMetric', []) - dump(metric) expect(metric).toBeDefined() expect(metric.spans.length).toBe(9) expect(metric.spans[0].points.length).toBe(0) expect(metric.spans[1].points.length).toBe(10) expect(metric.spans[2].points.length).toBe(0) - expect(metric.spans[3].points.length).toBe(9) // Test downgrade metrics = new CustomMetrics({client, table, log: true, spans: LessSpans}) metric = await metrics.emit('test/upgrade', 'UpMetric', 7, [], {upgrade: true}) - dump(metric) expect(metric).toBeDefined() expect(metric.spans.length).toBe(2) expect(metric.spans[0].points.length).toBe(1) @@ -67,6 +62,5 @@ test('Upgrade Spans', async () => { // Test already upgraded metrics = new CustomMetrics({client, table, log: true, spans: LessSpans}) metric = await metrics.emit('test/upgrade', 'UpMetric', 7, [], {upgrade: true}) - dump(metric) expect(metric).toBeDefined() }) \ No newline at end of file