mirror of
https://github.com/grafana/grafana.git
synced 2025-09-22 10:32:49 +08:00
refactor adding sqlPart
This commit is contained in:
@ -16,109 +16,9 @@ function register(options: any) {
|
||||
index[options.type] = new SqlPartDef(options);
|
||||
}
|
||||
|
||||
function replaceAggregationAddStrategy(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 === 'aggregate') {
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
function addColumnStrategy(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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
register({
|
||||
type: 'column',
|
||||
style: 'label',
|
||||
addStrategy: addColumnStrategy,
|
||||
params: [{ type: 'column', dynamicLookup: true }],
|
||||
defaultParams: ['value'],
|
||||
});
|
||||
@ -127,7 +27,6 @@ register({
|
||||
type: 'expression',
|
||||
style: 'expression',
|
||||
label: 'Expr:',
|
||||
addStrategy: addExpressionStrategy,
|
||||
params: [
|
||||
{ name: 'left', type: 'string', dynamicLookup: true },
|
||||
{ name: 'op', type: 'string', dynamicLookup: true },
|
||||
@ -140,7 +39,6 @@ register({
|
||||
type: 'macro',
|
||||
style: 'label',
|
||||
label: 'Macro:',
|
||||
addStrategy: addExpressionStrategy,
|
||||
params: [],
|
||||
defaultParams: [],
|
||||
});
|
||||
@ -148,23 +46,13 @@ register({
|
||||
register({
|
||||
type: 'aggregate',
|
||||
style: 'label',
|
||||
addStrategy: replaceAggregationAddStrategy,
|
||||
params: [{ name: 'name', type: 'string', dynamicLookup: true }],
|
||||
defaultParams: ['avg'],
|
||||
});
|
||||
|
||||
register({
|
||||
type: 'math',
|
||||
style: 'label',
|
||||
addStrategy: addMathStrategy,
|
||||
params: [{ name: 'expr', type: 'string' }],
|
||||
defaultParams: [' / 100'],
|
||||
});
|
||||
|
||||
register({
|
||||
type: 'alias',
|
||||
style: 'label',
|
||||
addStrategy: addAliasStrategy,
|
||||
params: [{ name: 'name', type: 'string', quote: 'double' }],
|
||||
defaultParams: ['alias'],
|
||||
});
|
||||
@ -199,7 +87,6 @@ register({
|
||||
},
|
||||
],
|
||||
defaultParams: ['increase'],
|
||||
addStrategy: replaceSpecialAddStrategy,
|
||||
});
|
||||
|
||||
export default {
|
||||
|
Reference in New Issue
Block a user