7

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

This is 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 that I want to read data from, swaps timestamp and metric value, e.g.

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

screenshot Is it possible to create a new datasource (a copy from the default graphite datasource) that either swaps the values back before processing or work with the values as is?

I have looked at the .js files but I find it hard to determine where I need to make changes so any pointers are appreciated!

EDIT: I have tried this: I have made a copy of the default Graphite plugin and have renamed it to graphite-copy and adjusted the id in plugin.json.

Then I edited datasource.js and datasource.ts like this:

   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 the change being this:

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

I have done this for both GET and POST methods in datasource.js/ts but it gives me same result (timestamp and metric switched).

Remko
  • 7,214
  • 2
  • 32
  • 52

1 Answers1

0

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 attribution to the author and the GitHub link

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pritish Vaidya
  • 21,561
  • 3
  • 58
  • 76
  • Will test tomorrow, where to put this? – Remko Oct 24 '16 at 20:51
  • same in datasource.js,i ve also given the github link,you can modify the variables according to your code and you need to wrap it in a *promise – Pritish Vaidya Oct 25 '16 at 01:22
  • I think I need more details on how to implement your suggested solution, it seems that you are proposing to completely rewrite datasource.js but I just want to make a field swap in the datapoints array – Remko Oct 25 '16 at 18:38