是否可以在不使用外部后端的情况下编写grafana数据源插件? [英] Is it possible to write a grafana datasource plugin without using a external backend?
问题描述
我想编写一个不依赖外部后端的grafana数据源插件.
I want to write a grafana datasource plugin which does not rely on a external backend.
我已基于simple-json数据源插件构建了我的插件: https://github.com /grafana/simple-json-datasource
Ive built my plugin based on the simple-json datasource plugin: https://github.com/grafana/simple-json-datasource
我尝试如下更改datasource.js中的查询功能:
I try to change the query function in the datasource.js as follows:
原始:
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' }
});
我的查询功能:
query(options) {
return [
{
"target":"upper_75",
"datapoints":[
[622,1450754160000],
[365,1450754220000]
]
},
{
"target":"upper_90",
"datapoints":[
[861,1450754160000],
[767,1450754220000]
]
}
];
}
当我实现查询功能并尝试在graphana面板中显示图形时,出现错误消息:
When I implement my query function and try to display the graph in the graphana panel I get the error message:
未定义不是对象(正在评估'dataList.map')"
"undefined is not an object (evaluating 'dataList.map')"
不用担心我尝试了所有数据格式,但是grafana希望返回的结果有所不同,但是我无法弄清楚哪种格式.
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.
我相信
this.backendSrv.datasourceRequest({
url: this.url + '/query',
data: query,
method: 'POST',
headers: { 'Content-Type': 'application/json' }
});
应该返回类似http的响应,但是为什么我不能手动返回呢?
is supposed to return something like a http response, but why cant I just return that manually?
感谢您的任何帮助!
推荐答案
似乎需要将其包装到Promise中,并且不能直接将其返回.他们使用有角的$ q分量.我让它可以返回:
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)
});
您将在构造函数中通过依赖注入获得$ q:
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;}
这篇关于是否可以在不使用外部后端的情况下编写grafana数据源插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!