feat: Support for annotations: only one time events

This commit is contained in:
Ivan Mikheykin
2019-03-11 15:38:47 +03:00
parent 8d2039bf37
commit 4994f2f956
9 changed files with 558 additions and 15 deletions
+134
View File
@@ -0,0 +1,134 @@
import d3 from 'd3';
import $ from 'jquery';
import _ from 'lodash';
import kbn from 'app/core/utils/kbn';
let TOOLTIP_PADDING_X = 30;
let TOOLTIP_PADDING_Y = 10;
export class AnnotationTooltip {
constructor(elem, scope) {
this.scope = scope;
this.dashboard = scope.ctrl.dashboard;
this.panelCtrl = scope.ctrl;
this.panel = scope.ctrl.panel;
this.mouseOverAnnotationTick = false;
elem.on("mouseover", this.onMouseOver.bind(this));
elem.on("mouseleave", this.onMouseLeave.bind(this));
}
onMouseOver(e) {
if (!this.panel.tooltip.show || !this.scope.ctrl.data || _.isEmpty(this.scope.ctrl.data)) { return; }
if (!this.tooltip) {
this.add();
this.move(e);
}
}
onMouseLeave() {
this.destroy();
}
onMouseMove(e) {
if (!this.panel.tooltip.show) { return; }
this.move(e);
}
add() {
this.tooltipBase = d3.select("body")
.append("div")
.attr("class", "statusmap-annotation-tooltip drop drop-popover drop-popover--annotation drop-element drop-enabled drop-target-attached-center drop-open drop-open-transitionend drop-after-open")
.style("position", "absolute")
this.tooltip = this.tooltipBase
.append("div")
.attr("class", "drop-content")
.append("div")
.append("annotation-tooltip")
.append("div")
.attr("class", "graph-annotation");
}
destroy() {
if (this.tooltip) {
this.tooltip.remove();
}
this.tooltip = null;
if (this.tooltipBase) {
this.tooltipBase.remove();
}
this.tooltipBase = null;
}
show(pos) {
if (!this.panel.tooltip.show || !this.tooltip) { return; }
// shared tooltip mode
//if (pos.panelRelY) {
// return;
//}
let annoId = d3.select(pos.target).attr('annoId');
if (!annoId) {
this.destroy();
return;
}
let anno = this.panelCtrl.annotations[annoId];
if (!anno) {
this.destroy();
return;
}
let annoTitle = "";
let tooltipTimeFormat = 'YYYY-MM-DD HH:mm:ss';
let annoTime = this.dashboard.formatDate(anno.time, tooltipTimeFormat);
let annoText = anno.text;
let annoTags = [];
if (anno.tags) {
annoTags = _.map(anno.tags, t => ({"text": t, "backColor": "rgb(63, 43, 91)", "borderColor":"rgb(101, 81, 129)"}))
}
let tooltipHtml = `<div class="graph-annotation__header">
<span class="graph-annotation__title">${annoTitle}</span>
<span class="graph-annotation__time">${annoTime}</span></div>
<div class="graph-annotation__body">
<div>${annoText}</div>
${_.join(_.map(annoTags, t => `<span class="label label-tag small" style="background-color: ${t.backColor}; border-color: ${t.borderColor}">${t.text}</span>`), "")}
</div>
<div class="statusmap-histogram"></div>`;
this.tooltip.html(tooltipHtml);
this.move(pos);
}
move(pos) {
if (!this.tooltipBase) { return; }
let elem = $(this.tooltipBase.node())[0];
let tooltipWidth = elem.clientWidth;
let tooltipHeight = elem.clientHeight;
let left = pos.pageX - tooltipWidth/2;
let top = pos.pageY + TOOLTIP_PADDING_Y;
if (pos.pageX + tooltipWidth/2 + 10 > window.innerWidth) {
left = pos.pageX - tooltipWidth - TOOLTIP_PADDING_X;
}
if (pos.pageY - window.pageYOffset + tooltipHeight + 20 > window.innerHeight) {
top = pos.pageY - tooltipHeight - TOOLTIP_PADDING_Y;
}
return this.tooltipBase
.style("left", left + "px")
.style("top", top + "px");
}
}
+64 -1
View File
@@ -7,6 +7,7 @@ import {tickStep, getScaledDecimals, getFlotTickSize} from 'app/core/utils/ticks
import d3 from 'd3';
import * as d3ScaleChromatic from './libs/d3-scale-chromatic/index';
import {StatusHeatmapTooltip} from './tooltip';
import {AnnotationTooltip} from './annotations';
let MIN_CARD_SIZE = 5,
CARD_H_SPACING = 2,
@@ -25,6 +26,7 @@ export default function link(scope, elem, attrs, ctrl) {
// $heatmap is JQuery object, but heatmap is D3
let $heatmap = elem.find('.status-heatmap-panel');
let tooltip = new StatusHeatmapTooltip($heatmap, scope);
let annotationTooltip = new AnnotationTooltip($heatmap, scope);
let width, height,
yScale, xScale,
@@ -77,7 +79,7 @@ export default function link(scope, elem, attrs, ctrl) {
return text.getBBox().width;
}));
return max_text_width;
return Math.ceil(max_text_width);
}
function getXAxisHeight(elem) {
@@ -310,6 +312,8 @@ export default function link(scope, elem, attrs, ctrl) {
resetCardHighLight(event);
});
_renderAnnotations();
ctrl.events.emit('render-complete', {
"chartWidth": chartWidth
});
@@ -506,6 +510,7 @@ export default function link(scope, elem, attrs, ctrl) {
function onMouseLeave() {
appEvents.emit('graph-hover-clear');
clearCrosshair();
//annotationTooltip.destroy();
}
function onMouseMove(event) {
@@ -515,6 +520,7 @@ export default function link(scope, elem, attrs, ctrl) {
// Clear crosshair and tooltip
clearCrosshair();
tooltip.destroy();
annotationTooltip.destroy();
selection.x2 = limitSelection(event.offsetX);
drawSelection(selection.x1, selection.x2);
@@ -522,6 +528,7 @@ export default function link(scope, elem, attrs, ctrl) {
emitGraphHoverEvet(event);
drawCrosshair(event.offsetX);
tooltip.show(event); //, data);
annotationTooltip.show(event);
}
}
@@ -637,6 +644,62 @@ export default function link(scope, elem, attrs, ctrl) {
$heatmap.on("mousedown", onMouseDown);
$heatmap.on("mousemove", onMouseMove);
$heatmap.on("mouseleave", onMouseLeave);
function _renderAnnotations() {
if (!ctrl.annotations || ctrl.annotations.length == 0) {
return;
}
if (!heatmap) {
return;
}
let annoData = _.map(ctrl.annotations, (d,i) => ({"x": Math.floor(yAxisWidth + xScale(d.time)), "id":i, "anno": d.source}));
let anno = heatmap
.append("g")
.attr("class", "statusmap-annotations")
.attr("transform", "translate(0.5,0)")
.selectAll(".statusmap-annotations")
.data(annoData)
.enter().append("g")
;
anno.append("line")
//.attr("class", "statusmap-annotation-tick")
.attr("x1", d => d.x)
.attr("y1", chartTop)
.attr("x2", d => d.x)
.attr("y2", chartBottom)
.style("stroke", d => d.anno.iconColor)
.style("stroke-width", 1)
.style("stroke-dasharray", "3,3")
;
anno.append("polygon")
.attr("points", d => [[d.x, chartBottom+1], [d.x-5, chartBottom+6], [d.x+5, chartBottom+6]].join(" "))
.style("stroke-width", 0)
.style("fill", d => d.anno.iconColor)
;
// Polygons didn't fire mouseevents
anno.append("rect")
.attr("x", d => d.x-5)
.attr("width", 10)
.attr("y", chartBottom+1)
.attr("height", 5)
.attr("class", "statusmap-annotation-tick")
.attr("annoId", d => d.id)
.style("opacity", 0)
;
let $ticks = $heatmap.find(".statusmap-annotation-tick");
$ticks.on("mouseenter", (event) => {
annotationTooltip.mouseOverAnnotationTick = true;
})
.on("mouseleave", (event) => {
annotationTooltip.mouseOverAnnotationTick = false;
});
}
}
function grafanaTimeFormat(ticks, min, max) {
+48 -3
View File
@@ -100,7 +100,8 @@ let opacityScales = ['linear', 'sqrt'];
export class StatusHeatmapCtrl extends MetricsPanelCtrl {
static templateUrl = 'module.html';
constructor($scope, $injector, $rootScope, timeSrv) {
/** @ngInject */
constructor($scope, $injector, $rootScope, timeSrv, annotationsSrv) {
super($scope, $injector);
_.defaultsDeep(this.panel, panelDefaults);
@@ -129,6 +130,9 @@ export class StatusHeatmapCtrl extends MetricsPanelCtrl {
}
};
this.annotations = [];
this.annotationsSrv = annotationsSrv;
this.events.on('data-received', this.onDataReceived);
this.events.on('data-snapshot-load', this.onDataReceived);
this.events.on('data-error', this.onDataError);
@@ -189,11 +193,48 @@ export class StatusHeatmapCtrl extends MetricsPanelCtrl {
this.interval = kbn.secondsToHms(intervalMs / 1000);
};
issueQueries = (datasource) => {
this.annotationsPromise = this.annotationsSrv.getAnnotations({
dashboard: this.dashboard,
panel: this.panel,
range: this.range,
});
/* Wait for annotationSrv requests to get datasources to
* resolve before issuing queries. This allows the annotations
* service to fire annotations queries before graph queries
* (but not wait for completion). This resolves
* issue 11806.
*/
return this.annotationsSrv.datasourcePromises.then(r => {
return super.issueQueries(datasource);
});
}
onDataReceived = (dataList) => {
this.data = dataList;
this.cardsData = this.convertToCards(this.data);
this.render();
this.annotationsPromise.then(
result => {
this.loading = false;
//this.alertState = result.alertState;
if (result.annotations && result.annotations.length > 0) {
this.annotations = result.annotations;
} else {
this.annotations = null;
}
this.render();
},
() => {
this.loading = false;
this.annotations = null;
this.render();
}
);
//this.render();
};
onInitEditMode = () => {
@@ -227,6 +268,7 @@ export class StatusHeatmapCtrl extends MetricsPanelCtrl {
onDataError = () => {
this.data = [];
this.annotations = [];
this.render();
};
@@ -313,6 +355,8 @@ export class StatusHeatmapCtrl extends MetricsPanelCtrl {
values: [],
multipleValues: false,
noColorDefined: false,
y: target,
x: -1,
};
// collect values from all timeseries with target
@@ -324,7 +368,6 @@ export class StatusHeatmapCtrl extends MetricsPanelCtrl {
let datapoint = s.datapoints[j];
if (card.values.length === 0) {
card.x = datapoint[TIME_INDEX];
card.y = s.target;
}
card.values.push(datapoint[VALUE_INDEX]);
}
@@ -344,7 +387,9 @@ export class StatusHeatmapCtrl extends MetricsPanelCtrl {
if (cardsData.minValue > card.minValue)
cardsData.minValue = card.minValue;
if (card.x != -1) {
cardsData.cards.push(card);
}
}
}