mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
create annotations work
This commit is contained in:
@@ -35,6 +35,7 @@ export class AnnotationsSrv {
|
||||
|
||||
// combine the annotations and flatten results
|
||||
var annotations = _.flattenDeep([results[0], results[1]]);
|
||||
|
||||
// filter out annotations that do not belong to requesting panel
|
||||
annotations = _.filter(annotations, item => {
|
||||
if (item.panelId && options.panel.id !== item.panelId) {
|
||||
@@ -60,7 +61,7 @@ export class AnnotationsSrv {
|
||||
var panel = options.panel;
|
||||
var dashboard = options.dashboard;
|
||||
|
||||
if (panel) {
|
||||
if (panel && panel.alert) {
|
||||
return this.backendSrv.get('/api/annotations', {
|
||||
from: options.range.from.valueOf(),
|
||||
to: options.range.to.valueOf(),
|
||||
@@ -133,7 +134,7 @@ export class AnnotationsSrv {
|
||||
return this.globalAnnotationsPromise;
|
||||
}
|
||||
|
||||
postAnnotation(annotation) {
|
||||
saveAnnotationEvent(annotation) {
|
||||
return this.backendSrv.post('/api/annotations', annotation);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,11 @@ import moment from 'moment';
|
||||
import coreModule from 'app/core/core_module';
|
||||
import {MetricsPanelCtrl} from 'app/plugins/sdk';
|
||||
|
||||
export class AnnotationItem {
|
||||
export class AnnotationEvent {
|
||||
dashboardId: number;
|
||||
panelId: number;
|
||||
time: Date;
|
||||
timeEnd: Date;
|
||||
time: any;
|
||||
timeEnd: any;
|
||||
isRegion: boolean;
|
||||
title: string;
|
||||
text: string;
|
||||
@@ -17,14 +17,13 @@ export class AnnotationItem {
|
||||
|
||||
export class EventEditorCtrl {
|
||||
panelCtrl: MetricsPanelCtrl;
|
||||
timeFormat = 'YYYY-MM-DD HH:mm:ss';
|
||||
annotation: AnnotationItem;
|
||||
annotation: AnnotationEvent;
|
||||
timeRange: {from: number, to: number};
|
||||
form: any;
|
||||
|
||||
/** @ngInject **/
|
||||
constructor() {
|
||||
this.annotation = new AnnotationItem();
|
||||
constructor(private annotationsSrv) {
|
||||
this.annotation = new AnnotationEvent();
|
||||
this.annotation.panelId = this.panelCtrl.panel.id;
|
||||
this.annotation.dashboardId = this.panelCtrl.dashboard.id;
|
||||
this.annotation.text = "hello";
|
||||
@@ -40,6 +39,19 @@ export class EventEditorCtrl {
|
||||
if (!this.form.$valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
let saveModel = _.cloneDeep(this.annotation);
|
||||
saveModel.time = saveModel.time.valueOf();
|
||||
if (saveModel.isRegion) {
|
||||
saveModel.timeEnd = saveModel.timeEnd.valueOf();
|
||||
}
|
||||
|
||||
if (saveModel.timeEnd < saveModel.time) {
|
||||
console.log('invalid time');
|
||||
return;
|
||||
}
|
||||
|
||||
this.annotationsSrv.saveAnnotationEvent(saveModel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +64,8 @@ export function eventEditor() {
|
||||
templateUrl: 'public/app/features/annotations/partials/event_editor.html',
|
||||
scope: {
|
||||
"panelCtrl": "=",
|
||||
"timeRange": "="
|
||||
"timeRange": "=",
|
||||
"cancel": "&",
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -9,31 +9,30 @@
|
||||
</div>
|
||||
<!-- single event -->
|
||||
<div ng-if="!ctrl.annotation.isRegion">
|
||||
<div class="gf-form-inline">
|
||||
<div class="gf-form">
|
||||
<span class="gf-form-label width-7">Time</span>
|
||||
<input type="text" ng-model="ctrl.annotation.time" class="gf-form-input max-width-20" input-datetime required>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- region event -->
|
||||
<div ng-if="ctrl.annotation.isRegion">
|
||||
<div class="gf-form">
|
||||
<span class="gf-form-label width-7">Start</span>
|
||||
<input type="text" ng-model="ctrl.annotation.time" class="gf-form-input max-width-20" input-datetime required>
|
||||
</div>
|
||||
<div class="gf-form">
|
||||
<span class="gf-form-label width-7">End</span>
|
||||
<input type="text" ng-model="ctrl.annotation.timeEnd" class="gf-form-input max-width-20" input-datetime required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="gf-form gf-form--v-stretch">
|
||||
<span class="gf-form-label width-7">Description</span>
|
||||
<textarea class="gf-form-input width-20" rows="3" ng-model="ctrl.annotation.text" placeholder="Event description"></textarea>
|
||||
</div>
|
||||
<div class="gf-form">
|
||||
<span class="gf-form-label width-7">Time</span>
|
||||
<input type="text" ng-model="ctrl.annotation.time" class="gf-form-input max-width-20" input-datetime required>
|
||||
</div>
|
||||
</div>
|
||||
<!-- region event -->
|
||||
<div ng-if="ctrl.annotation.isRegion">
|
||||
<div class="gf-form">
|
||||
<span class="gf-form-label width-7">Start</span>
|
||||
<input type="text" ng-model="ctrl.annotation.time" class="gf-form-input max-width-20" input-datetime required>
|
||||
</div>
|
||||
<div class="gf-form">
|
||||
<span class="gf-form-label width-7">End</span>
|
||||
<input type="text" ng-model="ctrl.annotation.timeEnd" class="gf-form-input max-width-20" input-datetime required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="gf-form gf-form--v-stretch">
|
||||
<span class="gf-form-label width-7">Description</span>
|
||||
<textarea class="gf-form-input width-20" rows="3" ng-model="ctrl.annotation.text" placeholder="Event description"></textarea>
|
||||
</div>
|
||||
|
||||
<div class="gf-form-button-row">
|
||||
<button type="submit" class="btn gf-form-btn btn-success" ng-click="ctrl.save()">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="gf-form-button-row">
|
||||
<button type="submit" class="btn gf-form-btn btn-success" ng-click="ctrl.save()">Save</button>
|
||||
<a class="btn-text" ng-click="ctrl.cancel();">Cancel</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
///<reference path="../../headers/common.d.ts" />
|
||||
|
||||
import angular from 'angular';
|
||||
import moment from 'moment';
|
||||
|
||||
export class AddAnnotationModalCtrl {
|
||||
timeFormat = 'YYYY-MM-DD HH:mm:ss';
|
||||
annotation: any;
|
||||
graphCtrl: any;
|
||||
|
||||
/** @ngInject */
|
||||
constructor(private $scope) {
|
||||
this.graphCtrl = $scope.ctrl;
|
||||
$scope.ctrl = this;
|
||||
|
||||
let dashboardId = this.graphCtrl.dashboard.id;
|
||||
let panelId = this.graphCtrl.panel.id;
|
||||
this.annotation = {
|
||||
dashboardId: dashboardId,
|
||||
panelId: panelId,
|
||||
time: null,
|
||||
timeTo: null,
|
||||
title: "",
|
||||
text: ""
|
||||
};
|
||||
|
||||
this.annotation.time = moment($scope.annotationTimeRange.from).format(this.timeFormat);0
|
||||
if ($scope.annotationTimeRange.to) {
|
||||
this.annotation.timeTo = moment($scope.annotationTimeRange.to).format(this.timeFormat);
|
||||
}
|
||||
}
|
||||
|
||||
addAnnotation() {
|
||||
this.annotation.time = moment(this.annotation.time, this.timeFormat).valueOf();
|
||||
if (this.annotation.timeTo) {
|
||||
this.annotation.timeTo = moment(this.annotation.timeTo, this.timeFormat).valueOf();
|
||||
}
|
||||
|
||||
this.graphCtrl.pushAnnotation(this.annotation)
|
||||
.then(response => {
|
||||
this.close();
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
this.close();
|
||||
});
|
||||
}
|
||||
|
||||
close() {
|
||||
this.$scope.dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
angular
|
||||
.module('grafana.controllers')
|
||||
.controller('AddAnnotationModalCtrl', AddAnnotationModalCtrl);
|
||||
Reference in New Issue
Block a user