Graphite Swap returned timestamp and value in Grafana

I am testing Grafana to read and graph data from the Graphite system.

Here's how Grafana expects json data from Graphite:

{ "data": [ { "target": "test-series-0", "datapoints": [ [ 22.504392773143504, 1.476693264195e+12 ], [ 22.719552781746028, 1.476693301825e+12 ] ] } ] } 

The system with which I want to read data, swap and metric value, for example

 { "data": [ { "target": "test-series-0", "datapoints": [ [ 1.476693264195e+12 22.504392773143504, ], [ 1.476693301825e+12 22.719552781746028, ] ] } ] } 

screenshot Is it possible to create a new data source (a copy from the default graphic object data source) that either swaps the values ​​back before processing, or works with the values ​​as is?

I looked at the .js files, but it's hard for me to determine where I need to make changes so that all pointers are appreciated!

EDIT: I tried this: I made a copy of the Graphite plugin by default and renamed it to a graphical copy and adjusted the identifier in plugin.json .

Then I edited datasource.js and datasource.ts as follows:

  var e = { method: "POST", url: "/render", data: d.join("&"), headers: { "Content-Type": "application/x-www-form-urlencoded" } }; return a.panelId && (e.requestId = this.name + ".panelId." + a.panelId), this.doGraphiteRequest(e).then(this.convertDataPointsToMs) }, this.convertDataPointsToMs = function(a) { if (!a || !a.data) return []; for (var b = 0; b < a.data.length; b++) for (var c = a.data[b], d = 0; d < c.datapoints.length; d++) { var t = c.datapoints[d][0]; c.datapoints[d][0] = c.datapoints[d][1]; c.datapoints[d][0] = t; c.datapoints[d][1] *= 1e3; } 

With a change:

  var t = c.datapoints[d][0]; c.datapoints[d][0] = c.datapoints[d][1]; c.datapoints[d][0] = t; 

I did this for both GET and POST datasource.js/ts in datasource.js/ts , but it gives me the same result (timestamp and metric).

+6
source share
1 answer

You can do it something like this in angular using angular.factory

 var module = angular.module(grafana.services); module.factory('Datasrc',function($q, backendsrv, templatesrv){ //$q,backendsrv templatesrv supported by grafana function Datasrc(datasource){ this.type =// the datasource type; this.url = datasource.url; this.auth = datasource.basicAuth; this.timestamp = true; this.supportMetrics = true; } AtsdDatasource.prototype.query = function (options) { var queries = _.compact(qs); if (_.isEmpty(queries)) { var d = $q.defer(); d.resolve({ data: [] }); return d.promise; } Datasrc.prototype._performQuery = function (queries) { var query = []; query.push( { data :[ objecttype = query.type, datapoints = query.//swap the values here //enter the other necessary fields or declare more in the factory ] }); if (query.length === 0) { var d = $q.defer(); d.resolve({ data: undefined }); return d.promise; //promise called here } var options = { method: 'POST', url: this.url + '/api/v1/series', data: { queries: tsQueries }, headers: { Authorization: this.basicAuth } }; return backendSrv.datasourceRequest(options).then(function (result) { return result; }); }; } }); 

Full author attribution and GitHub link

0
source

All Articles