Commit 0ece3abb by Torkel Ödegaard Committed by GitHub

Merge pull request #16152 from grafana/mtanda-prometheus_annotation_deduplication

Prometheus: Dedup annotations events with same timestamp 
parents 10d46bb6 d5a56b0b
......@@ -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;
}
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 };
export class AnnotationEvent {
dashboardId: number;
panelId: number;
userId: number;
time: any;
timeEnd: any;
isRegion: boolean;
text: string;
type: string;
tags: string;
}
......@@ -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;
......
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',
......
......@@ -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<PromQuery> {
......@@ -355,10 +355,11 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
})
.value();
const dupCheck = {};
for (const value of series.values) {
const valueIsTrue = value[1] === '1'; // e.g. ALERTS
if (valueIsTrue || annotation.useValueForTime) {
const event = {
const event: AnnotationEvent = {
annotation: annotation,
title: self.resultTransformer.renderTemplate(titleFormat, series.metric),
tags: tags,
......@@ -366,9 +367,14 @@ export class PrometheusDatasource implements DataSourceApi<PromQuery> {
};
if (annotation.useValueForTime) {
event['time'] = Math.floor(parseFloat(value[1]));
const timestampValue = Math.floor(parseFloat(value[1]));
if (dupCheck[timestampValue]) {
continue;
}
dupCheck[timestampValue] = true;
event.time = timestampValue;
} else {
event['time'] = Math.floor(parseFloat(value[0])) * 1000;
event.time = Math.floor(parseFloat(value[0])) * 1000;
}
eventList.push(event);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment