2017-12-20 12:33:33 +01:00
|
|
|
import _ from 'lodash';
|
2019-08-06 18:17:12 +02:00
|
|
|
import {
|
|
|
|
|
DataSourceApi,
|
|
|
|
|
DataQueryRequest,
|
|
|
|
|
DataSourceInstanceSettings,
|
2019-09-12 17:28:46 +02:00
|
|
|
DataQueryResponse,
|
2019-08-06 18:17:12 +02:00
|
|
|
MetricFindValue,
|
|
|
|
|
} from '@grafana/ui';
|
2019-07-05 23:05:53 -07:00
|
|
|
import { TableData, TimeSeries } from '@grafana/data';
|
2019-01-18 19:11:30 +01:00
|
|
|
import { TestDataQuery, Scenario } from './types';
|
2019-04-25 14:01:02 -04:00
|
|
|
import { getBackendSrv } from 'app/core/services/backend_srv';
|
2019-08-06 18:17:12 +02:00
|
|
|
import { queryMetricTree } from './metricTree';
|
2019-09-12 17:28:46 +02:00
|
|
|
import { Observable, from, merge } from 'rxjs';
|
|
|
|
|
import { runStream } from './runStreams';
|
2019-08-06 18:17:12 +02:00
|
|
|
import templateSrv from 'app/features/templating/template_srv';
|
2016-09-27 14:39:51 +02:00
|
|
|
|
2019-03-20 12:50:58 -07:00
|
|
|
type TestData = TimeSeries | TableData;
|
|
|
|
|
|
2019-08-06 18:17:12 +02:00
|
|
|
export class TestDataDataSource extends DataSourceApi<TestDataQuery> {
|
2019-04-25 14:01:02 -04:00
|
|
|
constructor(instanceSettings: DataSourceInstanceSettings) {
|
2019-05-10 02:37:43 -07:00
|
|
|
super(instanceSettings);
|
2017-03-29 22:54:07 +02:00
|
|
|
}
|
2016-09-27 14:39:51 +02:00
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
query(options: DataQueryRequest<TestDataQuery>): Observable<DataQueryResponse> {
|
|
|
|
|
const queries: any[] = [];
|
|
|
|
|
const streams: Array<Observable<DataQueryResponse>> = [];
|
2016-09-27 14:39:51 +02:00
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
// Start streams and prepare queries
|
|
|
|
|
for (const target of options.targets) {
|
|
|
|
|
if (target.scenarioId === 'streaming_client') {
|
|
|
|
|
streams.push(runStream(target, options));
|
|
|
|
|
} else {
|
|
|
|
|
queries.push({
|
|
|
|
|
...target,
|
|
|
|
|
intervalMs: options.intervalMs,
|
|
|
|
|
maxDataPoints: options.maxDataPoints,
|
|
|
|
|
datasourceId: this.id,
|
|
|
|
|
alias: templateSrv.replace(target.alias || ''),
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-09-27 14:39:51 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
if (queries.length) {
|
|
|
|
|
const req: Promise<DataQueryResponse> = getBackendSrv()
|
|
|
|
|
.datasourceRequest({
|
|
|
|
|
method: 'POST',
|
|
|
|
|
url: '/api/tsdb/query',
|
|
|
|
|
data: {
|
|
|
|
|
from: options.range.from.valueOf().toString(),
|
|
|
|
|
to: options.range.to.valueOf().toString(),
|
|
|
|
|
queries: queries,
|
|
|
|
|
},
|
|
|
|
|
// This sets up a cancel token
|
|
|
|
|
requestId: options.requestId,
|
|
|
|
|
})
|
|
|
|
|
.then((res: any) => this.processQueryResult(queries, res));
|
|
|
|
|
|
|
|
|
|
streams.push(from(req));
|
2019-04-25 14:01:02 -04:00
|
|
|
}
|
|
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
return merge(...streams);
|
|
|
|
|
}
|
2019-04-18 21:56:27 -07:00
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
processQueryResult(queries: any, res: any): DataQueryResponse {
|
|
|
|
|
const data: TestData[] = [];
|
2016-09-27 18:17:39 +02:00
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
for (const query of queries) {
|
|
|
|
|
const results = res.data.results[query.refId];
|
2019-03-21 08:01:44 +01:00
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
for (const t of results.tables || []) {
|
|
|
|
|
const table = t as TableData;
|
|
|
|
|
table.refId = query.refId;
|
|
|
|
|
table.name = query.alias;
|
|
|
|
|
data.push(table);
|
|
|
|
|
}
|
2019-03-21 08:01:44 +01:00
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
for (const series of results.series || []) {
|
|
|
|
|
data.push({ target: series.name, datapoints: series.points, refId: query.refId, tags: series.tags });
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-27 18:17:39 +02:00
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
return { data };
|
2016-09-27 14:39:51 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-25 14:01:02 -04:00
|
|
|
annotationQuery(options: any) {
|
2018-10-02 13:00:54 -07:00
|
|
|
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'],
|
2018-10-02 18:50:30 +02:00
|
|
|
});
|
2018-10-02 13:00:54 -07:00
|
|
|
timeWalker += step;
|
|
|
|
|
}
|
2019-04-25 14:01:02 -04:00
|
|
|
return Promise.resolve(events);
|
2016-09-27 14:39:51 +02:00
|
|
|
}
|
2018-11-29 18:39:02 +01:00
|
|
|
|
2019-04-10 09:31:32 -07:00
|
|
|
getQueryDisplayText(query: TestDataQuery) {
|
|
|
|
|
if (query.alias) {
|
|
|
|
|
return query.scenarioId + ' as ' + query.alias;
|
|
|
|
|
}
|
|
|
|
|
return query.scenarioId;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-29 18:39:02 +01:00
|
|
|
testDatasource() {
|
|
|
|
|
return Promise.resolve({
|
|
|
|
|
status: 'success',
|
|
|
|
|
message: 'Data source is working',
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-01-18 19:11:30 +01:00
|
|
|
|
|
|
|
|
getScenarios(): Promise<Scenario[]> {
|
2019-04-25 14:01:02 -04:00
|
|
|
return getBackendSrv().get('/api/tsdb/testdata/scenarios');
|
2019-01-18 19:11:30 +01:00
|
|
|
}
|
2019-08-06 18:17:12 +02:00
|
|
|
|
|
|
|
|
metricFindQuery(query: string) {
|
|
|
|
|
return new Promise<MetricFindValue[]>((resolve, reject) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
const children = queryMetricTree(templateSrv.replace(query));
|
|
|
|
|
const items = children.map(item => ({ value: item.name, text: item.name }));
|
|
|
|
|
resolve(items);
|
|
|
|
|
}, 100);
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-09-27 14:39:51 +02:00
|
|
|
}
|