Is it possible to write a grafana datasource plugin without using a external backend?

牧云@^-^@ 提交于 2019-12-06 06:03:19

问题


I want to write a grafana datasource plugin which does not rely on a external backend.

Ive built my plugin based on the simple-json datasource plugin: https://github.com/grafana/simple-json-datasource

I try to change the query function in the datasource.js as follows:

original:

 query(options) {
    var query = this.buildQueryParameters(options);

    if (query.targets.length <= 0) {
      return this.q.when([]);
    }

    return this.backendSrv.datasourceRequest({
      url: this.url + '/query',
      data: query,
      method: 'POST',
      headers: { 'Content-Type': 'application/json' }
    });

my query function:

  query(options) {
      return [
      {
        "target":"upper_75",
        "datapoints":[
          [622,1450754160000],
          [365,1450754220000]
        ]
      },
      {
        "target":"upper_90",
        "datapoints":[
          [861,1450754160000],
          [767,1450754220000]
        ]
      }
    ];
  }

When I implement my query function and try to display the graph in the graphana panel I get the error message:

"undefined is not an object (evaluating 'dataList.map')"

Dont worry about the data format i tried everything, but it seems grafana expects something different as a return, but i cant figure out what format.

I traced down what the original implementation returns and duplicated it, but it doesnt work.

I believe

this.backendSrv.datasourceRequest({
      url: this.url + '/query',
      data: query,
      method: 'POST',
      headers: { 'Content-Type': 'application/json' }
    });

is supposed to return something like a http response, but why cant I just return that manually?

Thanks for any help in advance!


回答1:


It looks like it need to be wrapped into a promise and can't be returned directly. They use the angular $q component. I made it working with returning:

return this.q(function(resolve, reject) {
    var result = {
      data : [
        {
          "target":"upper_75",
          "datapoints":[
            [622,1450754160000],
            [365,1450754220000]
          ]
        },
        {
          "target":"upper_90",
          "datapoints":[
            [861,1450754160000],
            [767,1450754220000]
          ]
        }
      ]
    }
    resolve(result)
});

You will get $q via dependency injection in the constructor:

constructor(instanceSettings, $q, backendSrv) {
this.type = instanceSettings.type;
this.url = instanceSettings.url;
this.name = instanceSettings.name;
this.q = $q;
this.backendSrv = backendSrv;}


来源:https://stackoverflow.com/questions/37075040/is-it-possible-to-write-a-grafana-datasource-plugin-without-using-a-external-bac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!