| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import {
- QueryPartDef,
- QueryPart,
- functionRenderer,
- suffixRenderer,
- identityRenderer,
- quotedIdentityRenderer,
- } from 'app/core/components/query_part/query_part';
- import _ from 'lodash';
- 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.target.expr = this.target.expr || '';
- this.target.intervalFactor = this.target.intervalFactor || 2;
- this.target.functions = this.target.functions || [];
- this.templateSrv = templateSrv;
- this.scopedVars = scopedVars;
- this.updateProjection();
- }
- updateProjection() {
- this.functions = _.map(this.target.functions, function(func: any) {
- return createPart(func);
- });
- }
- render() {
- var query = this.target.metric;
- if (this.target.range) {
- query += '[' + this.target.range + ']';
- }
- for (let func of this.functions) {
- query = func.render(query);
- }
- return query;
- }
- addQueryPart(category, item) {
- var partModel = createPart({type: item.text});
- partModel.def.addStrategy(this, partModel);
- }
- }
- 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);
- model.target.functions.push(partModel.part);
- }
- register({
- type: 'rate',
- addStrategy: addFunctionStrategy,
- category: categories.Functions,
- params: [],
- defaultParams: [],
- renderer: functionRenderer,
- });
- export function getQueryPartCategories() {
- return categories;
- }
|