2016-10-18 30 views
6

我正在使用Grafana測試以讀取和繪製Graphite系統中的數據。交換Grafana中Graphite返回的時間戳和值

這是Grafana如何從預計石墨JSON數據:

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

,我想讀取數據的系統,交換時間戳和度量值,例如

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

screenshot 是否有可能建立一個新的數據源(從默認石墨的數據源副本),要麼回處理之前的交換值或用值是工作?

我已經看過.js文件,但我發現很難確定需要進行更改的位置,以便欣賞任何指針!

編輯: 我試過這個:我做了一個默認的Graphite插件的副本,並將其更名爲graphite-copy並在plugin.json中調整了ID。

然後我編輯datasource.jsdatasource.ts這樣的:

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; 
    } 

隨着變化是這樣的:

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

我這樣做對datasource.js/ts GET和POST方法,但它給了我同樣的結果(時間戳和度量切換)。

回答

0

可以使用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; 
     }); 
}; 

    } 
}); 

全部歸屬於author和GitHub的link

+0

將測試明天做在angular這樣的事情,放在哪裏呢? – Remko

+0

同樣在datasource.js中,我也給了github鏈接,你可以根據你的代碼修改變量,你需要把它包裝在一個* promise –

+0

我想我需要更多的細節來說明如何實現你的建議解決方案,它似乎你建議完全重寫datasource.js,但我只想在數據點數組中進行字段交換 – Remko

相關問題