mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
handle aggregate functions more generic
This commit is contained in:
parent
12600a0e95
commit
6c2ef7dca6
@ -81,30 +81,12 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
}
|
||||
|
||||
buildSelectMenu() {
|
||||
|
||||
if (!queryPart.hasAggregates()) {
|
||||
this.datasource.metricFindQuery(this.queryBuilder.buildAggregateQuery())
|
||||
.then(results => {
|
||||
queryPart.clearAggregates();
|
||||
_.map(results, segment => { queryPart.registerAggregate(segment.text); });
|
||||
})
|
||||
.catch(this.handleQueryError.bind(this));
|
||||
}
|
||||
var categories = queryPart.getCategories();
|
||||
this.selectMenu = _.reduce(
|
||||
categories,
|
||||
function(memo, cat, key) {
|
||||
var menu = {
|
||||
text: key,
|
||||
submenu: cat.map(item => {
|
||||
return { text: item.type, value: item.type };
|
||||
}),
|
||||
};
|
||||
memo.push(menu);
|
||||
return memo;
|
||||
},
|
||||
[]
|
||||
);
|
||||
this.selectMenu = [
|
||||
{text: "aggregate", value: "aggregate"},
|
||||
{text: "math", value: "math"},
|
||||
{text: "alias", value: "alias"},
|
||||
{text: "column", value: "column"},
|
||||
];
|
||||
}
|
||||
|
||||
toggleEditorMode() {
|
||||
@ -216,10 +198,19 @@ export class PostgresQueryCtrl extends QueryCtrl {
|
||||
handleSelectPartEvent(selectParts, part, evt) {
|
||||
switch (evt.name) {
|
||||
case 'get-param-options': {
|
||||
return this.datasource
|
||||
.metricFindQuery(this.queryBuilder.buildColumnQuery("value"))
|
||||
.then(this.transformToSegments(true))
|
||||
.catch(this.handleQueryError.bind(this));
|
||||
switch (part.def.type) {
|
||||
case "aggregate":
|
||||
return this.datasource
|
||||
.metricFindQuery(this.queryBuilder.buildAggregateQuery())
|
||||
.then(this.transformToSegments(false))
|
||||
.catch(this.handleQueryError.bind(this));
|
||||
case "column":
|
||||
return this.datasource
|
||||
.metricFindQuery(this.queryBuilder.buildColumnQuery("value"))
|
||||
.then(this.transformToSegments(true))
|
||||
.catch(this.handleQueryError.bind(this));
|
||||
}
|
||||
|
||||
}
|
||||
case 'part-param-changed': {
|
||||
this.panelCtrl.refresh();
|
||||
|
@ -23,23 +23,16 @@ function register(options: any) {
|
||||
options.category.push(index[options.type]);
|
||||
}
|
||||
|
||||
function registerAggregate(name: string) {
|
||||
register({
|
||||
type: name,
|
||||
addStrategy: replaceAggregationAddStrategy,
|
||||
category: categories.Aggregations,
|
||||
params: [],
|
||||
defaultParams: [],
|
||||
renderer: functionRenderer,
|
||||
});
|
||||
}
|
||||
|
||||
var groupByTimeFunctions = [];
|
||||
|
||||
function aliasRenderer(part, innerExpr) {
|
||||
return innerExpr + ' AS ' + '"' + part.params[0] + '"';
|
||||
}
|
||||
|
||||
function aggregateRenderer(part, innerExpr) {
|
||||
return part.params[0] + '(' + innerExpr + ')';
|
||||
}
|
||||
|
||||
function columnRenderer(part, innerExpr) {
|
||||
return '"' + part.params[0] + '"';
|
||||
}
|
||||
@ -108,59 +101,13 @@ register({
|
||||
renderer: columnRenderer,
|
||||
});
|
||||
|
||||
// Aggregations
|
||||
register({
|
||||
type: 'avg',
|
||||
type: 'aggregate',
|
||||
addStrategy: replaceAggregationAddStrategy,
|
||||
category: categories.Aggregations,
|
||||
params: [],
|
||||
defaultParams: [],
|
||||
renderer: functionRenderer,
|
||||
});
|
||||
|
||||
register({
|
||||
type: 'count',
|
||||
addStrategy: replaceAggregationAddStrategy,
|
||||
category: categories.Aggregations,
|
||||
params: [],
|
||||
defaultParams: [],
|
||||
renderer: functionRenderer,
|
||||
});
|
||||
|
||||
register({
|
||||
type: 'sum',
|
||||
addStrategy: replaceAggregationAddStrategy,
|
||||
category: categories.Aggregations,
|
||||
params: [],
|
||||
defaultParams: [],
|
||||
renderer: functionRenderer,
|
||||
});
|
||||
|
||||
register({
|
||||
type: 'stddev',
|
||||
addStrategy: replaceAggregationAddStrategy,
|
||||
category: categories.Aggregations,
|
||||
params: [],
|
||||
defaultParams: [],
|
||||
renderer: functionRenderer,
|
||||
});
|
||||
|
||||
register({
|
||||
type: 'min',
|
||||
addStrategy: replaceAggregationAddStrategy,
|
||||
category: categories.Aggregations,
|
||||
params: [],
|
||||
defaultParams: [],
|
||||
renderer: functionRenderer,
|
||||
});
|
||||
|
||||
register({
|
||||
type: 'max',
|
||||
addStrategy: replaceAggregationAddStrategy,
|
||||
category: categories.Aggregations,
|
||||
params: [],
|
||||
defaultParams: [],
|
||||
renderer: functionRenderer,
|
||||
params: [{name: 'name', type: 'string', dynamicLookup: true}],
|
||||
defaultParams: ['avg'],
|
||||
renderer: aggregateRenderer,
|
||||
});
|
||||
|
||||
register({
|
||||
@ -203,12 +150,6 @@ register({
|
||||
|
||||
export default {
|
||||
create: createPart,
|
||||
registerAggregate: registerAggregate,
|
||||
clearAggregates: function() { categories.Aggregations = []; },
|
||||
hasAggregates: function() {
|
||||
// FIXME
|
||||
return categories.Aggregations.length > 6;
|
||||
},
|
||||
getCategories: function() {
|
||||
return categories;
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user