| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- define([
- 'lodash',
- 'jquery'
- ],
- function (_, $) {
- 'use strict';
- var index = [];
- var categories = {
- Combine: [],
- Transform: [],
- Calculate: [],
- Filter: [],
- Special: []
- };
- function addFuncDef(funcDef) {
- funcDef.params = funcDef.params || [];
- funcDef.defaultParams = funcDef.defaultParams || [];
- if (funcDef.category) {
- funcDef.category.push(funcDef);
- }
- index[funcDef.name] = funcDef;
- index[funcDef.shortName || funcDef.name] = funcDef;
- }
- addFuncDef({
- name: 'field',
- category: categories.Transform,
- params: [{type: 'field'}],
- defaultParams: ['value'],
- });
- addFuncDef({
- name: 'mean',
- category: categories.Transform,
- params: [],
- defaultParams: [],
- });
- addFuncDef({
- name: 'derivate',
- category: categories.Transform,
- params: [{ name: "rate", type: "interval", options: ['1s', '10s', '1m', '5min', '10m', '15m', '1h'] }],
- defaultParams: ['10s'],
- });
- addFuncDef({
- name: 'time',
- category: categories.Transform,
- params: [{ name: "rate", type: "interval", options: ['$interval', '1s', '10s', '1m', '5min', '10m', '15m', '1h'] }],
- defaultParams: ['$interval'],
- });
- addFuncDef({
- name: 'math',
- category: categories.Transform,
- params: [{ name: "expr", type: "string"}],
- defaultParams: [' / 100'],
- });
- addFuncDef({
- name: 'alias',
- category: categories.Transform,
- params: [{ name: "name", type: "string"}],
- defaultParams: ['alias'],
- });
- _.each(categories, function(funcList, catName) {
- categories[catName] = _.sortBy(funcList, 'name');
- });
- function FuncInstance(funcDef, options) {
- this.def = funcDef;
- this.params = [];
- if (options && options.withDefaultParams) {
- this.params = funcDef.defaultParams.slice(0);
- }
- this.updateText();
- }
- FuncInstance.prototype.render = function(metricExp) {
- var str = this.def.name + '(';
- var parameters = _.map(this.params, function(value, index) {
- var paramType = this.def.params[index].type;
- if (paramType === 'int' || paramType === 'value_or_series' || paramType === 'boolean') {
- return value;
- }
- else if (paramType === 'int_or_interval' && $.isNumeric(value)) {
- return value;
- }
- return "'" + value + "'";
- }, this);
- if (metricExp) {
- parameters.unshift(metricExp);
- }
- return str + parameters.join(', ') + ')';
- };
- FuncInstance.prototype._hasMultipleParamsInString = function(strValue, index) {
- if (strValue.indexOf(',') === -1) {
- return false;
- }
- return this.def.params[index + 1] && this.def.params[index + 1].optional;
- };
- FuncInstance.prototype.updateParam = function(strValue, index) {
- // handle optional parameters
- // if string contains ',' and next param is optional, split and update both
- if (this._hasMultipleParamsInString(strValue, index)) {
- _.each(strValue.split(','), function(partVal, idx) {
- this.updateParam(partVal.trim(), idx);
- }, this);
- return;
- }
- if (strValue === '' && this.def.params[index].optional) {
- this.params.splice(index, 1);
- }
- else {
- this.params[index] = strValue;
- }
- this.updateText();
- };
- FuncInstance.prototype.updateText = function () {
- if (this.params.length === 0) {
- this.text = this.def.name + '()';
- return;
- }
- var text = this.def.name + '(';
- text += this.params.join(', ');
- text += ')';
- this.text = text;
- };
- return {
- create: function(funcDef, options) {
- if (_.isString(funcDef)) {
- if (!index[funcDef]) {
- throw { message: 'Method not found ' + name };
- }
- funcDef = index[funcDef];
- }
- return new FuncInstance(funcDef, options);
- },
- getFuncDef: function(name) {
- return index[name];
- },
- getCategories: function() {
- return categories;
- }
- };
- });
|