From cca37caf331ce62eb3cbdb16b02bbbeb1d9583c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 10 May 2016 15:48:07 +0200 Subject: [PATCH] feat(prometheus): began work on prometheus query model --- .../datasource/prometheus/prom_query.ts | 79 +++++++++++++++++++ .../prometheus/specs/query_specs.ts | 23 ++++++ 2 files changed, 102 insertions(+) create mode 100644 public/app/plugins/datasource/prometheus/prom_query.ts create mode 100644 public/app/plugins/datasource/prometheus/specs/query_specs.ts diff --git a/public/app/plugins/datasource/prometheus/prom_query.ts b/public/app/plugins/datasource/prometheus/prom_query.ts new file mode 100644 index 00000000000..e9ef6ddf40d --- /dev/null +++ b/public/app/plugins/datasource/prometheus/prom_query.ts @@ -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; +} diff --git a/public/app/plugins/datasource/prometheus/specs/query_specs.ts b/public/app/plugins/datasource/prometheus/specs/query_specs.ts new file mode 100644 index 00000000000..5bb164c306e --- /dev/null +++ b/public/app/plugins/datasource/prometheus/specs/query_specs.ts @@ -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])'); + }); + }); + +});