2017-12-20 12:33:33 +01:00
|
|
|
import _ from 'lodash';
|
2016-09-28 10:37:30 +02:00
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
import { QueryCtrl } from 'app/plugins/sdk';
|
2019-04-25 14:01:02 -04:00
|
|
|
import { defaultQuery } from './StreamHandler';
|
|
|
|
import { getBackendSrv } from 'app/core/services/backend_srv';
|
2019-05-08 13:51:44 +02:00
|
|
|
import { dateTime } from '@grafana/ui/src/utils/moment_wrapper';
|
2016-09-27 14:39:51 +02:00
|
|
|
|
|
|
|
export class TestDataQueryCtrl extends QueryCtrl {
|
2017-12-20 12:33:33 +01:00
|
|
|
static templateUrl = 'partials/query.editor.html';
|
2016-09-27 14:39:51 +02:00
|
|
|
|
2016-09-27 18:17:39 +02:00
|
|
|
scenarioList: any;
|
2016-09-28 10:37:30 +02:00
|
|
|
scenario: any;
|
2017-11-01 09:59:24 +01:00
|
|
|
newPointValue: number;
|
|
|
|
newPointTime: any;
|
|
|
|
selectedPoint: any;
|
2016-09-27 14:39:51 +02:00
|
|
|
|
2018-08-31 16:40:43 +02:00
|
|
|
/** @ngInject */
|
2019-04-25 14:01:02 -04:00
|
|
|
constructor($scope: any, $injector: any) {
|
2016-09-27 14:39:51 +02:00
|
|
|
super($scope, $injector);
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
this.target.scenarioId = this.target.scenarioId || 'random_walk';
|
2016-09-27 18:17:39 +02:00
|
|
|
this.scenarioList = [];
|
2019-05-08 13:51:44 +02:00
|
|
|
this.newPointTime = dateTime();
|
2017-12-20 12:33:33 +01:00
|
|
|
this.selectedPoint = { text: 'Select point', value: null };
|
2017-11-01 09:59:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getPoints() {
|
|
|
|
return _.map(this.target.points, (point, index) => {
|
|
|
|
return {
|
2019-05-08 13:51:44 +02:00
|
|
|
text: dateTime(point[1]).format('MMMM Do YYYY, H:mm:ss') + ' : ' + point[0],
|
2017-12-20 12:33:33 +01:00
|
|
|
value: index,
|
2017-11-01 09:59:24 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:56:02 +02:00
|
|
|
pointSelected(option: any) {
|
2017-11-01 09:59:24 +01:00
|
|
|
this.selectedPoint = option;
|
|
|
|
}
|
|
|
|
|
|
|
|
deletePoint() {
|
|
|
|
this.target.points.splice(this.selectedPoint.value, 1);
|
2017-12-20 12:33:33 +01:00
|
|
|
this.selectedPoint = { text: 'Select point', value: null };
|
2017-11-01 09:59:24 +01:00
|
|
|
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();
|
2016-09-27 18:17:39 +02:00
|
|
|
}
|
2016-09-27 14:39:51 +02:00
|
|
|
|
2016-09-27 18:17:39 +02:00
|
|
|
$onInit() {
|
2019-04-25 14:01:02 -04:00
|
|
|
return getBackendSrv()
|
|
|
|
.get('/api/tsdb/testdata/scenarios')
|
2019-06-27 15:56:02 +02:00
|
|
|
.then((res: any) => {
|
2019-04-25 14:01:02 -04:00
|
|
|
this.scenarioList = res;
|
|
|
|
this.scenario = _.find(this.scenarioList, { id: this.target.scenarioId });
|
|
|
|
});
|
2016-09-27 14:39:51 +02:00
|
|
|
}
|
2016-09-28 10:37:30 +02:00
|
|
|
|
|
|
|
scenarioChanged() {
|
2017-11-01 09:59:24 +01:00
|
|
|
this.scenario = _.find(this.scenarioList, { id: this.target.scenarioId });
|
2016-09-28 10:37:30 +02:00
|
|
|
this.target.stringInput = this.scenario.stringInput;
|
2017-11-01 09:59:24 +01:00
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
if (this.target.scenarioId === 'manual_entry') {
|
2017-11-01 09:59:24 +01:00
|
|
|
this.target.points = this.target.points || [];
|
|
|
|
} else {
|
|
|
|
delete this.target.points;
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:01:02 -04:00
|
|
|
if (this.target.scenarioId === 'streaming_client') {
|
|
|
|
this.target.stream = _.defaults(this.target.stream || {}, defaultQuery);
|
|
|
|
} else {
|
|
|
|
delete this.target.stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
streamChanged() {
|
2016-09-28 10:37:30 +02:00
|
|
|
this.refresh();
|
|
|
|
}
|
2016-09-27 14:39:51 +02:00
|
|
|
}
|