From c3bd07f9b4cae013ff108bc4cc0a21415eb29620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 1 Nov 2017 09:59:24 +0100 Subject: [PATCH] testdata: added manual entry mode to test data --- pkg/tsdb/testdata/scenarios.go | 40 ++++++++++++++++ .../dashboard/timepicker/input_date.ts | 19 ++++---- .../app/testdata/datasource/datasource.ts | 6 +-- .../plugins/app/testdata/datasource/module.ts | 2 - .../app/testdata/datasource/query_ctrl.ts | 48 ++++++++++++++++--- .../app/testdata/partials/query.editor.html | 22 ++++++++- 6 files changed, 112 insertions(+), 25 deletions(-) diff --git a/pkg/tsdb/testdata/scenarios.go b/pkg/tsdb/testdata/scenarios.go index 0a7f1467933..e907fa8aae0 100644 --- a/pkg/tsdb/testdata/scenarios.go +++ b/pkg/tsdb/testdata/scenarios.go @@ -1,6 +1,7 @@ package testdata import ( + "encoding/json" "math/rand" "strconv" "strings" @@ -142,6 +143,45 @@ func init() { }, }) + registerScenario(&Scenario{ + Id: "manual_entry", + Name: "Manual Entry", + Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult { + queryRes := tsdb.NewQueryResult() + + points := query.Model.Get("points").MustArray() + + series := newSeriesForQuery(query) + startTime := context.TimeRange.GetFromAsMsEpoch() + endTime := context.TimeRange.GetToAsMsEpoch() + + for _, val := range points { + pointValues := val.([]interface{}) + + var value null.Float + var time int64 + + if valueFloat, err := strconv.ParseFloat(string(pointValues[0].(json.Number)), 64); err == nil { + value = null.FloatFrom(valueFloat) + } + + if timeInt, err := strconv.ParseInt(string(pointValues[1].(json.Number)), 10, 64); err != nil { + continue + } else { + time = timeInt + } + + if time >= startTime && time <= endTime { + series.Points = append(series.Points, tsdb.NewTimePoint(value, float64(time))) + } + } + + queryRes.Series = append(queryRes.Series, series) + + return queryRes + }, + }) + registerScenario(&Scenario{ Id: "csv_metric_values", Name: "CSV Metric Values", diff --git a/public/app/features/dashboard/timepicker/input_date.ts b/public/app/features/dashboard/timepicker/input_date.ts index b6988a3ff16..ca3f5fa7ae9 100644 --- a/public/app/features/dashboard/timepicker/input_date.ts +++ b/public/app/features/dashboard/timepicker/input_date.ts @@ -1,5 +1,3 @@ -/// - import moment from 'moment'; import * as dateMath from 'app/core/utils/datemath'; @@ -7,16 +5,16 @@ export function inputDateDirective() { return { restrict: 'A', require: 'ngModel', - link: function ($scope, $elem, attrs, ngModel) { + link: function($scope, $elem, attrs, ngModel) { var format = 'YYYY-MM-DD HH:mm:ss'; - var fromUser = function (text) { + var fromUser = function(text) { if (text.indexOf('now') !== -1) { if (!dateMath.isValid(text)) { - ngModel.$setValidity("error", false); + ngModel.$setValidity('error', false); return undefined; } - ngModel.$setValidity("error", true); + ngModel.$setValidity('error', true); return text; } @@ -28,15 +26,15 @@ export function inputDateDirective() { } if (!parsed.isValid()) { - ngModel.$setValidity("error", false); + ngModel.$setValidity('error', false); return undefined; } - ngModel.$setValidity("error", true); + ngModel.$setValidity('error', true); return parsed; }; - var toUser = function (currentValue) { + var toUser = function(currentValue) { if (moment.isMoment(currentValue)) { return currentValue.format(format); } else { @@ -46,7 +44,6 @@ export function inputDateDirective() { ngModel.$parsers.push(fromUser); ngModel.$formatters.push(toUser); - } + }, }; } - diff --git a/public/app/plugins/app/testdata/datasource/datasource.ts b/public/app/plugins/app/testdata/datasource/datasource.ts index 90ae9e3aa47..2df33ea4ac6 100644 --- a/public/app/plugins/app/testdata/datasource/datasource.ts +++ b/public/app/plugins/app/testdata/datasource/datasource.ts @@ -1,7 +1,4 @@ -/// - import _ from 'lodash'; -import angular from 'angular'; class TestDataDatasource { id: any; @@ -21,7 +18,8 @@ class TestDataDatasource { intervalMs: options.intervalMs, maxDataPoints: options.maxDataPoints, stringInput: item.stringInput, - jsonInput: angular.fromJson(item.jsonInput), + points: item.points, + alias: item.alias, datasourceId: this.id, }; }); diff --git a/public/app/plugins/app/testdata/datasource/module.ts b/public/app/plugins/app/testdata/datasource/module.ts index 309b7443836..9d7eaf3cc83 100644 --- a/public/app/plugins/app/testdata/datasource/module.ts +++ b/public/app/plugins/app/testdata/datasource/module.ts @@ -1,5 +1,3 @@ -/// - import {TestDataDatasource} from './datasource'; import {TestDataQueryCtrl} from './query_ctrl'; diff --git a/public/app/plugins/app/testdata/datasource/query_ctrl.ts b/public/app/plugins/app/testdata/datasource/query_ctrl.ts index e783584eb5d..dd5f59c0a5a 100644 --- a/public/app/plugins/app/testdata/datasource/query_ctrl.ts +++ b/public/app/plugins/app/testdata/datasource/query_ctrl.ts @@ -1,14 +1,16 @@ -/// - import _ from 'lodash'; -import {QueryCtrl} from 'app/plugins/sdk'; +import { QueryCtrl } from 'app/plugins/sdk'; +import moment from 'moment'; export class TestDataQueryCtrl extends QueryCtrl { static templateUrl = 'partials/query.editor.html'; scenarioList: any; scenario: any; + newPointValue: number; + newPointTime: any; + selectedPoint: any; /** @ngInject **/ constructor($scope, $injector, private backendSrv) { @@ -16,19 +18,53 @@ export class TestDataQueryCtrl extends QueryCtrl { this.target.scenarioId = this.target.scenarioId || 'random_walk'; this.scenarioList = []; + this.newPointTime = moment(); + this.selectedPoint = { text: 'Select point', value: null }; + } + + getPoints() { + return _.map(this.target.points, (point, index) => { + return { + text: moment(point[1]).format('MMMM Do YYYY, H:mm:ss') + ' : ' + point[0], + value: index, + }; + }); + } + + pointSelected(option) { + this.selectedPoint = option; + } + + deletePoint() { + this.target.points.splice(this.selectedPoint.value, 1); + this.selectedPoint = { text: 'Select point', value: null }; + this.refresh(); + } + + addPoint() { + this.target.points = this.target.points || []; + this.target.points.push([this.newPointValue, this.newPointTime.valueOf()]); + this.target.points = _.sortBy(this.target.points, p => p[1]); + this.refresh(); } $onInit() { return this.backendSrv.get('/api/tsdb/testdata/scenarios').then(res => { this.scenarioList = res; - this.scenario = _.find(this.scenarioList, {id: this.target.scenarioId}); + this.scenario = _.find(this.scenarioList, { id: this.target.scenarioId }); }); } scenarioChanged() { - this.scenario = _.find(this.scenarioList, {id: this.target.scenarioId}); + this.scenario = _.find(this.scenarioList, { id: this.target.scenarioId }); this.target.stringInput = this.scenario.stringInput; + + if (this.target.scenarioId === 'manual_entry') { + this.target.points = this.target.points || []; + } else { + delete this.target.points; + } + this.refresh(); } } - diff --git a/public/app/plugins/app/testdata/partials/query.editor.html b/public/app/plugins/app/testdata/partials/query.editor.html index a39582d5397..247918bce1f 100644 --- a/public/app/plugins/app/testdata/partials/query.editor.html +++ b/public/app/plugins/app/testdata/partials/query.editor.html @@ -1,8 +1,8 @@
- -
+ +
@@ -18,5 +18,23 @@
+
+
+ + + + + + + + +
+
+ +
+
+
+
+