The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/plugins/datasource/testdata/datasource.ts

97 lines
2.4 KiB

import _ from 'lodash';
import TableModel from 'app/core/table_model';
class TestDataDatasource {
id: any;
/** @ngInject */
constructor(instanceSettings, private backendSrv, private $q) {
this.id = instanceSettings.id;
}
query(options) {
const queries = _.filter(options.targets, item => {
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,
datasourceId: this.id,
};
});
if (queries.length === 0) {
return this.$q.when({ data: [] });
}
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,
datapoints: series.points,
});
}
});
}
return { data: data };
});
}
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);
}
testDatasource() {
return Promise.resolve({
status: 'success',
message: 'Data source is working',
});
}
}
export { TestDataDatasource };