feat(prometheus): began work on prometheus query model
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import {
|
||||
QueryPartDef,
|
||||
QueryPart,
|
||||
functionRenderer,
|
||||
suffixRenderer,
|
||||
identityRenderer,
|
||||
quotedIdentityRenderer,
|
||||
} from 'app/core/components/query_part/query_part';
|
||||
|
||||
var index = [];
|
||||
var categories = {
|
||||
Functions: [],
|
||||
};
|
||||
|
||||
export class PromQuery {
|
||||
target: any;
|
||||
metric: string;
|
||||
range: string;
|
||||
filters: any[];
|
||||
functions: any[];
|
||||
templateSrv: any;
|
||||
scopedVars: any;
|
||||
|
||||
constructor(target, templateSrv?, scopedVars?) {
|
||||
this.target = target;
|
||||
this.templateSrv = templateSrv;
|
||||
this.scopedVars = scopedVars;
|
||||
}
|
||||
|
||||
render() {
|
||||
var query = this.target.metric;
|
||||
if (this.target.range) {
|
||||
query += '[' + this.target.range + ']';
|
||||
}
|
||||
|
||||
for (let funcModel of this.target.functions) {
|
||||
var partDef = index[funcModel.type];
|
||||
if (!partDef) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var part = new QueryPart(funcModel, partDef);
|
||||
query = part.render(query);
|
||||
}
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
|
||||
export function createPart(part): any {
|
||||
var def = index[part.type];
|
||||
if (!def) {
|
||||
throw {message: 'Could not find query part ' + part.type};
|
||||
}
|
||||
|
||||
return new QueryPart(part, def);
|
||||
}
|
||||
|
||||
function register(options: any) {
|
||||
index[options.type] = new QueryPartDef(options);
|
||||
options.category.push(index[options.type]);
|
||||
}
|
||||
|
||||
function addFunctionStrategy(model, partModel) {
|
||||
model.functions.push(partModel);
|
||||
}
|
||||
|
||||
register({
|
||||
type: 'rate',
|
||||
addStrategy: addFunctionStrategy,
|
||||
category: categories.Functions,
|
||||
params: [],
|
||||
defaultParams: [],
|
||||
renderer: functionRenderer,
|
||||
});
|
||||
|
||||
export function getCategories() {
|
||||
return categories;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
|
||||
|
||||
import {PromQuery} from '../prom_query';
|
||||
|
||||
describe.only('PromQuery', function() {
|
||||
var templateSrv = {replace: val => val};
|
||||
|
||||
describe('render series with mesurement only', function() {
|
||||
it('should generate correct query', function() {
|
||||
var query = new PromQuery({
|
||||
metric: 'cpu',
|
||||
range: '5m',
|
||||
functions: [
|
||||
{type: 'rate', params: []}
|
||||
]
|
||||
}, templateSrv, {});
|
||||
|
||||
var queryText = query.render();
|
||||
expect(queryText).to.be('rate(cpu[5m])');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user