From e1a2b258afa2626e5045897af674f03312988c36 Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Thu, 21 Mar 2019 09:52:26 +0900 Subject: [PATCH 1/4] deduplicate same value annotation --- public/app/plugins/datasource/prometheus/datasource.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index bde56431683..9f8a912e554 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -354,6 +354,7 @@ export class PrometheusDatasource implements DataSourceApi { }) .value(); + const dupCheck = {}; for (const value of series.values) { const valueIsTrue = value[1] === '1'; // e.g. ALERTS if (valueIsTrue || annotation.useValueForTime) { @@ -365,6 +366,10 @@ export class PrometheusDatasource implements DataSourceApi { }; if (annotation.useValueForTime) { + if (dupCheck[value[1]]) { + continue; + } + dupCheck[value[1]] = true; event['time'] = Math.floor(parseFloat(value[1])); } else { event['time'] = Math.floor(parseFloat(value[0])) * 1000; From e868475c5b5f2ead9404f12e9e9aeebe9917805c Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Thu, 21 Mar 2019 15:48:22 +0900 Subject: [PATCH 2/4] simplify --- public/app/plugins/datasource/prometheus/datasource.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 9f8a912e554..68e3cdb8149 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -366,11 +366,12 @@ export class PrometheusDatasource implements DataSourceApi { }; if (annotation.useValueForTime) { - if (dupCheck[value[1]]) { + const timestampValue = Math.floor(parseFloat(value[1])); + if (dupCheck[timestampValue]) { continue; } - dupCheck[value[1]] = true; - event['time'] = Math.floor(parseFloat(value[1])); + dupCheck[timestampValue] = true; + event['time'] = timestampValue; } else { event['time'] = Math.floor(parseFloat(value[0])) * 1000; } From 38457fabc1067973fcbddf6dd8b6d7d9b0ab7a2a Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Thu, 21 Mar 2019 16:39:37 +0900 Subject: [PATCH 3/4] fix, assign by event.time --- public/app/plugins/datasource/prometheus/datasource.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 68e3cdb8149..c24000db092 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -358,7 +358,7 @@ export class PrometheusDatasource implements DataSourceApi { for (const value of series.values) { const valueIsTrue = value[1] === '1'; // e.g. ALERTS if (valueIsTrue || annotation.useValueForTime) { - const event = { + const event: any = { annotation: annotation, title: self.resultTransformer.renderTemplate(titleFormat, series.metric), tags: tags, @@ -371,9 +371,9 @@ export class PrometheusDatasource implements DataSourceApi { continue; } dupCheck[timestampValue] = true; - event['time'] = timestampValue; + event.time = timestampValue; } else { - event['time'] = Math.floor(parseFloat(value[0])) * 1000; + event.time = Math.floor(parseFloat(value[0])) * 1000; } eventList.push(event); From d5a56b0bed49c0d41565969482d21ffa37f1fa06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 22 Mar 2019 12:51:12 +0100 Subject: [PATCH 4/4] Minor refactoring of #16127 --- packages/grafana-ui/src/types/data.ts | 14 ++++++++++++++ public/app/features/annotations/all.ts | 4 +--- public/app/features/annotations/event.ts | 11 ----------- public/app/features/annotations/event_editor.ts | 2 +- public/app/features/annotations/event_manager.ts | 16 ++++++++-------- .../plugins/datasource/prometheus/datasource.ts | 4 ++-- 6 files changed, 26 insertions(+), 25 deletions(-) delete mode 100644 public/app/features/annotations/event.ts diff --git a/packages/grafana-ui/src/types/data.ts b/packages/grafana-ui/src/types/data.ts index 69d5261ba3c..fcbf95f9f4a 100644 --- a/packages/grafana-ui/src/types/data.ts +++ b/packages/grafana-ui/src/types/data.ts @@ -76,3 +76,17 @@ export interface TableData { rows: any[][]; tags?: Tags; } + +export interface AnnotationEvent { + annotation?: any; + dashboardId?: number; + panelId?: number; + userId?: number; + time?: number; + timeEnd?: number; + isRegion?: boolean; + title?: string; + text?: string; + type?: string; + tags?: string; +} diff --git a/public/app/features/annotations/all.ts b/public/app/features/annotations/all.ts index d0ef6abf9ed..ed2c65062be 100644 --- a/public/app/features/annotations/all.ts +++ b/public/app/features/annotations/all.ts @@ -1,7 +1,5 @@ import { AnnotationsSrv } from './annotations_srv'; import { eventEditor } from './event_editor'; import { EventManager } from './event_manager'; -import { AnnotationEvent } from './event'; import { annotationTooltipDirective } from './annotation_tooltip'; - -export { AnnotationsSrv, eventEditor, EventManager, AnnotationEvent, annotationTooltipDirective }; +export { AnnotationsSrv, eventEditor, EventManager, annotationTooltipDirective }; diff --git a/public/app/features/annotations/event.ts b/public/app/features/annotations/event.ts deleted file mode 100644 index de86b72c7c0..00000000000 --- a/public/app/features/annotations/event.ts +++ /dev/null @@ -1,11 +0,0 @@ -export class AnnotationEvent { - dashboardId: number; - panelId: number; - userId: number; - time: any; - timeEnd: any; - isRegion: boolean; - text: string; - type: string; - tags: string; -} diff --git a/public/app/features/annotations/event_editor.ts b/public/app/features/annotations/event_editor.ts index a88cc44a251..a6970790bb3 100644 --- a/public/app/features/annotations/event_editor.ts +++ b/public/app/features/annotations/event_editor.ts @@ -2,7 +2,7 @@ import _ from 'lodash'; import moment from 'moment'; import { coreModule } from 'app/core/core'; import { MetricsPanelCtrl } from 'app/plugins/sdk'; -import { AnnotationEvent } from './event'; +import { AnnotationEvent } from '@grafana/ui'; export class EventEditorCtrl { panelCtrl: MetricsPanelCtrl; diff --git a/public/app/features/annotations/event_manager.ts b/public/app/features/annotations/event_manager.ts index 6966d3cdc82..fbd7897ba92 100644 --- a/public/app/features/annotations/event_manager.ts +++ b/public/app/features/annotations/event_manager.ts @@ -1,5 +1,4 @@ import _ from 'lodash'; -import moment from 'moment'; import tinycolor from 'tinycolor2'; import { OK_COLOR, @@ -11,7 +10,7 @@ import { } from '@grafana/ui'; import { MetricsPanelCtrl } from 'app/plugins/sdk'; -import { AnnotationEvent } from './event'; +import { AnnotationEvent } from '@grafana/ui'; export class EventManager { event: AnnotationEvent; @@ -31,16 +30,17 @@ export class EventManager { updateTime(range) { if (!this.event) { - this.event = new AnnotationEvent(); + this.event = {}; this.event.dashboardId = this.panelCtrl.dashboard.id; this.event.panelId = this.panelCtrl.panel.id; } // update time - this.event.time = moment(range.from); + this.event.time = range.from; this.event.isRegion = false; + if (range.to) { - this.event.timeEnd = moment(range.to); + this.event.timeEnd = range.to; this.event.isRegion = true; } @@ -90,8 +90,8 @@ export class EventManager { annotations = [ { isRegion: true, - min: this.event.time.valueOf(), - timeEnd: this.event.timeEnd.valueOf(), + min: this.event.time, + timeEnd: this.event.timeEnd, text: this.event.text, eventType: '$__editing', editModel: this.event, @@ -100,7 +100,7 @@ export class EventManager { } else { annotations = [ { - min: this.event.time.valueOf(), + min: this.event.time, text: this.event.text, editModel: this.event, eventType: '$__editing', diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index ecdfcf30612..432a7fd6717 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -15,7 +15,7 @@ import { expandRecordingRules } from './language_utils'; // Types import { PromQuery } from './types'; -import { DataQueryOptions, DataSourceApi } from '@grafana/ui/src/types'; +import { DataQueryOptions, DataSourceApi, AnnotationEvent } from '@grafana/ui/src/types'; import { ExploreUrlState } from 'app/types/explore'; export class PrometheusDatasource implements DataSourceApi { @@ -359,7 +359,7 @@ export class PrometheusDatasource implements DataSourceApi { for (const value of series.values) { const valueIsTrue = value[1] === '1'; // e.g. ALERTS if (valueIsTrue || annotation.useValueForTime) { - const event: any = { + const event: AnnotationEvent = { annotation: annotation, title: self.resultTransformer.renderTemplate(titleFormat, series.metric), tags: tags,