Skip to content

Instantly share code, notes, and snippets.

@athulmurali
Last active August 20, 2020 00:01
Show Gist options
  • Save athulmurali/bb92142961813da525d084badbd9203c to your computer and use it in GitHub Desktop.
Save athulmurali/bb92142961813da525d084badbd9203c to your computer and use it in GitHub Desktop.
grafana-datasource.js
// original code
async query(options) {
var query = await this.buildQueryParameters(options);
if (query.targets.length <= 0) {
return this.q.when({ data: [] });
}
return this.doRequest(query).then(result => {
var res = []
_.forEach(result.data.results, r => {
_.forEach(r.series, s => {
res.push({ target: s.name, datapoints: s.points })
})
_.forEach(r.tables, t => {
t.type = 'table'
t.refId = r.refId
res.push(t)
})
})
// test code with hardcoded data for log panel
import * as graf from '@grafana/data'
async query(options) {
const MutableDataFrame = graf.MutableDataFrame;
const FieldType = graf.FieldType;
const frame = new MutableDataFrame({
refId: query.refId,
fields: [
{ name: 'time', type: FieldType.time },
{ name: 'content', type: FieldType.string, labels: { filename: 'file.txt' } }
],
});
frame.add({ time: 1589189388597, content: 'user registered' })
frame.add({ time: 1589189406480, content: 'user logged in' })
console.log('frame to JSON' , frame.toJSON())
result.data = frame.toJSON()
return result
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment