Files
grafana/public/app/plugins/datasource/testdata/datasource.ts

101 lines
2.6 KiB
TypeScript
Raw Normal View History

2017-12-20 12:33:33 +01:00
import _ from 'lodash';
import TableModel from 'app/core/table_model';
2019-01-18 17:19:35 +01:00
import { DataSourceApi, DataQueryOptions } from '@grafana/ui';
2019-01-18 19:11:30 +01:00
import { TestDataQuery, Scenario } from './types';
2016-09-27 14:39:51 +02:00
2019-01-18 17:19:35 +01:00
export class TestDataDatasource implements DataSourceApi<TestDataQuery> {
id: number;
2016-09-27 14:39:51 +02:00
/** @ngInject */
2017-03-29 22:54:07 +02:00
constructor(instanceSettings, private backendSrv, private $q) {
this.id = instanceSettings.id;
}
2016-09-27 14:39:51 +02:00
2019-01-18 17:19:35 +01:00
query(options: DataQueryOptions<TestDataQuery>) {
const queries = _.filter(options.targets, item => {
2016-09-27 14:39:51 +02:00
return item.hide !== true;
}).map(item => {
return {
refId: item.refId,
scenarioId: item.scenarioId,
intervalMs: options.intervalMs,
maxDataPoints: options.maxDataPoints,
stringInput: item.stringInput,
points: item.points,
alias: item.alias,
2017-12-20 12:33:33 +01:00
datasourceId: this.id,
};
2016-09-27 14:39:51 +02:00
});
if (queries.length === 0) {
return this.$q.when({ data: [] });
2016-09-27 14:39:51 +02:00
}
return this.backendSrv
.datasourceRequest({
method: 'POST',
url: '/api/tsdb/query',
data: {
from: options.range.from.valueOf().toString(),
to: options.range.to.valueOf().toString(),
queries: queries,
},
})
.then(res => {
const data = [];
if (res.data.results) {
_.forEach(res.data.results, queryRes => {
if (queryRes.tables) {
for (const table of queryRes.tables) {
const model = new TableModel();
model.rows = table.rows;
model.columns = table.columns;
data.push(model);
}
}
for (const series of queryRes.series) {
data.push({
target: series.name,
2017-12-20 12:33:33 +01:00
datapoints: series.points,
});
}
});
}
return { data: data };
});
2016-09-27 14:39:51 +02:00
}
annotationQuery(options) {
let timeWalker = options.range.from.valueOf();
const to = options.range.to.valueOf();
const events = [];
const eventCount = 10;
const step = (to - timeWalker) / eventCount;
for (let i = 0; i < eventCount; i++) {
events.push({
annotation: options.annotation,
time: timeWalker,
text: 'This is the text, <a href="https://grafana.com">Grafana.com</a>',
tags: ['text', 'server'],
});
timeWalker += step;
}
return this.$q.when(events);
2016-09-27 14:39:51 +02:00
}
testDatasource() {
return Promise.resolve({
status: 'success',
message: 'Data source is working',
});
}
2019-01-18 19:11:30 +01:00
getScenarios(): Promise<Scenario[]> {
return this.backendSrv.get('/api/tsdb/testdata/scenarios');
}
2016-09-27 14:39:51 +02:00
}