grafana/public/app/plugins/datasource/postgres/sql_part.ts

229 lines
5.3 KiB
TypeScript
Raw Normal View History

2018-01-30 15:57:38 -06:00
import _ from 'lodash';
2018-05-21 04:10:00 -05:00
import { SqlPartDef, SqlPart, functionRenderer, suffixRenderer } from 'app/core/components/sql_part/sql_part';
2018-01-30 15:57:38 -06:00
var index = [];
function createPart(part): any {
var def = index[part.type];
if (!def) {
2018-07-10 07:45:36 -05:00
return null;
2018-01-30 15:57:38 -06:00
}
2018-05-21 04:10:00 -05:00
return new SqlPart(part, def);
2018-01-30 15:57:38 -06:00
}
function register(options: any) {
2018-05-21 04:10:00 -05:00
index[options.type] = new SqlPartDef(options);
2018-01-30 15:57:38 -06:00
}
function aliasRenderer(part, innerExpr) {
return innerExpr + ' AS ' + '"' + part.params[0] + '"';
}
function aggregateRenderer(part, innerExpr) {
return part.params[0] + '(' + innerExpr + ')';
}
2018-03-03 13:57:00 -06:00
function columnRenderer(part, innerExpr) {
return part.params[0];
2018-01-30 15:57:38 -06:00
}
function replaceAggregationAddStrategy(selectParts, partModel) {
var hasAlias = false;
2018-01-30 15:57:38 -06:00
// look for existing aggregation
for (var i = 0; i < selectParts.length; i++) {
var part = selectParts[i];
2018-06-30 04:11:34 -05:00
if (part.def.type === 'aggregate') {
2018-01-30 15:57:38 -06:00
selectParts[i] = partModel;
return;
}
if (part.def.type === 'alias') {
hasAlias = true;
}
}
// add alias if none exists yet
if (!hasAlias) {
var aliasModel = createPart({ type: 'alias', params: [selectParts[0].params[0]] });
selectParts.push(aliasModel);
2018-01-30 15:57:38 -06:00
}
selectParts.splice(1, 0, partModel);
}
2018-07-10 14:43:10 -05:00
function replaceSpecialAddStrategy(selectParts, partModel) {
var hasAlias = false;
// look for existing aggregation
for (var i = 0; i < selectParts.length; i++) {
var part = selectParts[i];
if (part.def.type === 'special') {
selectParts[i] = partModel;
return;
}
if (part.def.type === 'alias') {
hasAlias = true;
}
}
// add alias if none exists yet
if (!hasAlias) {
var aliasModel = createPart({ type: 'alias', params: [selectParts[0].params[0]] });
selectParts.push(aliasModel);
}
selectParts.splice(1, 0, partModel);
}
2018-01-30 15:57:38 -06:00
function addMathStrategy(selectParts, partModel) {
var partCount = selectParts.length;
if (partCount > 0) {
// if last is math, replace it
if (selectParts[partCount - 1].def.type === 'math') {
selectParts[partCount - 1] = partModel;
return;
}
// if next to last is math, replace it
if (partCount > 1 && selectParts[partCount - 2].def.type === 'math') {
selectParts[partCount - 2] = partModel;
return;
} else if (selectParts[partCount - 1].def.type === 'alias') {
// if last is alias add it before
selectParts.splice(partCount - 1, 0, partModel);
return;
}
}
selectParts.push(partModel);
}
function addAliasStrategy(selectParts, partModel) {
var partCount = selectParts.length;
if (partCount > 0) {
// if last is alias, replace it
if (selectParts[partCount - 1].def.type === 'alias') {
selectParts[partCount - 1] = partModel;
return;
}
}
selectParts.push(partModel);
}
2018-03-03 13:57:00 -06:00
function addColumnStrategy(selectParts, partModel, query) {
2018-01-30 15:57:38 -06:00
// copy all parts
var parts = _.map(selectParts, function(part: any) {
return createPart({ type: part.def.type, params: _.clone(part.params) });
});
query.selectModels.push(parts);
}
2018-06-30 04:11:34 -05:00
function addExpressionStrategy(selectParts, partModel, query) {
// copy all parts
var parts = _.map(selectParts, function(part: any) {
return createPart({ type: part.def.type, params: _.clone(part.params) });
});
query.selectModels.push(parts);
}
2018-01-30 15:57:38 -06:00
register({
2018-03-03 13:57:00 -06:00
type: 'column',
style: 'label',
2018-03-03 13:57:00 -06:00
addStrategy: addColumnStrategy,
params: [{ type: 'column', dynamicLookup: true }],
2018-01-30 15:57:38 -06:00
defaultParams: ['value'],
2018-03-03 13:57:00 -06:00
renderer: columnRenderer,
2018-01-30 15:57:38 -06:00
});
2018-06-30 04:11:34 -05:00
register({
type: 'expression',
style: 'expression',
label: 'Expr:',
addStrategy: addExpressionStrategy,
params: [
{ name: 'left', type: 'string', dynamicLookup: true },
{ name: 'op', type: 'string', dynamicLookup: true },
{ name: 'right', type: 'string', dynamicLookup: true },
],
defaultParams: ['value', '=', 'value'],
renderer: columnRenderer,
});
register({
type: 'macro',
style: 'label',
label: 'Macro:',
addStrategy: addExpressionStrategy,
params: [],
defaultParams: [],
renderer: columnRenderer,
});
2018-01-30 15:57:38 -06:00
register({
type: 'aggregate',
style: 'label',
2018-01-30 15:57:38 -06:00
addStrategy: replaceAggregationAddStrategy,
2018-06-30 04:11:34 -05:00
params: [{ name: 'name', type: 'string', dynamicLookup: true }],
defaultParams: ['avg'],
renderer: aggregateRenderer,
2018-01-30 15:57:38 -06:00
});
register({
type: 'math',
style: 'label',
2018-01-30 15:57:38 -06:00
addStrategy: addMathStrategy,
params: [{ name: 'expr', type: 'string' }],
defaultParams: [' / 100'],
renderer: suffixRenderer,
});
register({
type: 'alias',
style: 'label',
2018-01-30 15:57:38 -06:00
addStrategy: addAliasStrategy,
params: [{ name: 'name', type: 'string', quote: 'double' }],
defaultParams: ['alias'],
renderMode: 'suffix',
renderer: aliasRenderer,
});
2018-03-04 03:18:11 -06:00
register({
type: 'time',
style: 'function',
2018-05-21 04:44:37 -05:00
label: 'time',
2018-03-04 03:18:11 -06:00
params: [
{
name: 'interval',
type: 'interval',
options: ['$__interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'],
},
{
name: 'fill',
type: 'string',
options: ['none', 'NULL', '0'],
},
],
2018-06-30 04:11:34 -05:00
defaultParams: ['$__interval', 'none'],
2018-03-04 03:18:11 -06:00
renderer: functionRenderer,
});
2018-07-10 14:43:10 -05:00
register({
type: 'special',
style: 'label',
params: [
{
name: 'function',
type: 'string',
options: ['increase', 'rate'],
},
],
defaultParams: ['increase'],
addStrategy: replaceSpecialAddStrategy,
renderer: aggregateRenderer,
});
2018-01-30 15:57:38 -06:00
export default {
create: createPart,
};