mirror of https://github.com/grafana/grafana
parent
650e2faf01
commit
41b5dae606
@ -0,0 +1,139 @@ |
||||
export class MysqlMetaQuery { |
||||
constructor(private target, private queryModel) {} |
||||
|
||||
getOperators(datatype: string) { |
||||
switch (datatype) { |
||||
case 'float4': |
||||
case 'float8': { |
||||
return ['=', '!=', '<', '<=', '>', '>=']; |
||||
} |
||||
case 'text': |
||||
case 'varchar': |
||||
case 'char': { |
||||
return ['=', '!=', '<', '<=', '>', '>=', 'IN', 'NOT IN', 'LIKE', 'NOT LIKE', '~', '~*', '!~', '!~*']; |
||||
} |
||||
default: { |
||||
return ['=', '!=', '<', '<=', '>', '>=', 'IN', 'NOT IN']; |
||||
} |
||||
} |
||||
} |
||||
|
||||
// quote identifier as literal to use in metadata queries
|
||||
quoteIdentAsLiteral(value) { |
||||
return this.queryModel.quoteLiteral(this.queryModel.unquoteIdentifier(value)); |
||||
} |
||||
|
||||
findMetricTable() { |
||||
// query that returns first table found that has a timestamp(tz) column and a float column
|
||||
let query = ` |
||||
SELECT |
||||
table_name as table_name, |
||||
( SELECT |
||||
column_name as column_name |
||||
FROM information_schema.columns c |
||||
WHERE |
||||
c.table_schema = t.table_schema AND |
||||
c.table_name = t.table_name AND |
||||
c.data_type IN ('timestamp', 'datetime') |
||||
ORDER BY ordinal_position LIMIT 1 |
||||
) AS time_column, |
||||
( SELECT |
||||
column_name AS column_name |
||||
FROM information_schema.columns c |
||||
WHERE |
||||
c.table_schema = t.table_schema AND |
||||
c.table_name = t.table_name AND |
||||
c.data_type IN('float', 'int', 'bigint') |
||||
ORDER BY ordinal_position LIMIT 1 |
||||
) AS value_column |
||||
FROM information_schema.tables t |
||||
WHERE |
||||
EXISTS |
||||
( SELECT 1 |
||||
FROM information_schema.columns c |
||||
WHERE |
||||
c.table_schema = t.table_schema AND |
||||
c.table_name = t.table_name AND |
||||
c.data_type IN ('timestamp', 'datetime') |
||||
) AND |
||||
EXISTS |
||||
( SELECT 1 |
||||
FROM information_schema.columns c |
||||
WHERE |
||||
c.table_schema = t.table_schema AND |
||||
c.table_name = t.table_name AND |
||||
c.data_type IN('float', 'int', 'bigint') |
||||
) |
||||
LIMIT 1 |
||||
;`;
|
||||
return query; |
||||
} |
||||
|
||||
buildTableConstraint(table: string) { |
||||
let query = ''; |
||||
|
||||
// check for schema qualified table
|
||||
if (table.includes('.')) { |
||||
let parts = table.split('.'); |
||||
query = 'table_schema = ' + this.quoteIdentAsLiteral(parts[0]); |
||||
query += ' AND table_name = ' + this.quoteIdentAsLiteral(parts[1]); |
||||
return query; |
||||
} else { |
||||
query = ' table_name = ' + this.quoteIdentAsLiteral(table); |
||||
|
||||
return query; |
||||
} |
||||
} |
||||
|
||||
buildTableQuery() { |
||||
return 'SELECT table_name FROM information_schema.tables ORDER BY table_name'; |
||||
} |
||||
|
||||
buildColumnQuery(type?: string) { |
||||
let query = 'SELECT column_name FROM information_schema.columns WHERE '; |
||||
query += this.buildTableConstraint(this.target.table); |
||||
|
||||
switch (type) { |
||||
case 'time': { |
||||
query += " AND data_type IN ('timestamp','datetime','bigint','int','float')"; |
||||
break; |
||||
} |
||||
case 'metric': { |
||||
query += " AND data_type IN ('text' 'tinytext','mediumtext', 'longtext', 'varchar')"; |
||||
break; |
||||
} |
||||
case 'value': { |
||||
query += |
||||
" AND data_type IN ('bigint','int','float','smallint', 'mediumint', 'tinyint', 'double', 'decimal', 'float')"; |
||||
query += ' AND column_name <> ' + this.quoteIdentAsLiteral(this.target.timeColumn); |
||||
break; |
||||
} |
||||
case 'group': { |
||||
query += " AND data_type IN ('text' 'tinytext','mediumtext', 'longtext', 'varchar')"; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
query += ' ORDER BY column_name'; |
||||
|
||||
return query; |
||||
} |
||||
|
||||
buildValueQuery(column: string) { |
||||
let query = 'SELECT DISTINCT QUOTE(' + column + ')'; |
||||
query += ' FROM ' + this.target.table; |
||||
query += ' WHERE $__timeFilter(' + this.target.timeColumn + ')'; |
||||
query += ' ORDER BY 1 LIMIT 100'; |
||||
return query; |
||||
} |
||||
|
||||
buildDatatypeQuery(column: string) { |
||||
let query = ` |
||||
SELECT data_type |
||||
FROM information_schema.columns |
||||
WHERE `;
|
||||
query += ' table_name = ' + this.quoteIdentAsLiteral(this.target.table); |
||||
query += ' AND column_name = ' + this.quoteIdentAsLiteral(column); |
||||
return query; |
||||
} |
||||
} |
||||
@ -0,0 +1,285 @@ |
||||
import _ from 'lodash'; |
||||
|
||||
export default class MysqlQuery { |
||||
target: any; |
||||
templateSrv: any; |
||||
scopedVars: any; |
||||
|
||||
/** @ngInject */ |
||||
constructor(target, templateSrv?, scopedVars?) { |
||||
this.target = target; |
||||
this.templateSrv = templateSrv; |
||||
this.scopedVars = scopedVars; |
||||
|
||||
target.format = target.format || 'time_series'; |
||||
target.timeColumn = target.timeColumn || 'time'; |
||||
target.metricColumn = target.metricColumn || 'none'; |
||||
|
||||
target.group = target.group || []; |
||||
target.where = target.where || [{ type: 'macro', name: '$__timeFilter', params: [] }]; |
||||
target.select = target.select || [[{ type: 'column', params: ['value'] }]]; |
||||
|
||||
// handle pre query gui panels gracefully
|
||||
if (!('rawQuery' in this.target)) { |
||||
if ('rawSql' in target) { |
||||
// pre query gui panel
|
||||
target.rawQuery = true; |
||||
} else { |
||||
// new panel
|
||||
target.rawQuery = false; |
||||
} |
||||
} |
||||
|
||||
// give interpolateQueryStr access to this
|
||||
this.interpolateQueryStr = this.interpolateQueryStr.bind(this); |
||||
} |
||||
|
||||
// remove identifier quoting from identifier to use in metadata queries
|
||||
unquoteIdentifier(value) { |
||||
if (value[0] === '"' && value[value.length - 1] === '"') { |
||||
return value.substring(1, value.length - 1).replace(/""/g, '"'); |
||||
} else { |
||||
return value; |
||||
} |
||||
} |
||||
|
||||
quoteIdentifier(value) { |
||||
return '"' + value.replace(/"/g, '""') + '"'; |
||||
} |
||||
|
||||
quoteLiteral(value) { |
||||
return "'" + value.replace(/'/g, "''") + "'"; |
||||
} |
||||
|
||||
escapeLiteral(value) { |
||||
return value.replace(/'/g, "''"); |
||||
} |
||||
|
||||
hasTimeGroup() { |
||||
return _.find(this.target.group, (g: any) => g.type === 'time'); |
||||
} |
||||
|
||||
hasMetricColumn() { |
||||
return this.target.metricColumn !== 'none'; |
||||
} |
||||
|
||||
interpolateQueryStr(value, variable, defaultFormatFn) { |
||||
// if no multi or include all do not regexEscape
|
||||
if (!variable.multi && !variable.includeAll) { |
||||
return this.escapeLiteral(value); |
||||
} |
||||
|
||||
if (typeof value === 'string') { |
||||
return this.quoteLiteral(value); |
||||
} |
||||
|
||||
let escapedValues = _.map(value, this.quoteLiteral); |
||||
return escapedValues.join(','); |
||||
} |
||||
|
||||
render(interpolate?) { |
||||
let target = this.target; |
||||
|
||||
// new query with no table set yet
|
||||
if (!this.target.rawQuery && !('table' in this.target)) { |
||||
return ''; |
||||
} |
||||
|
||||
if (!target.rawQuery) { |
||||
target.rawSql = this.buildQuery(); |
||||
} |
||||
|
||||
if (interpolate) { |
||||
return this.templateSrv.replace(target.rawSql, this.scopedVars, this.interpolateQueryStr); |
||||
} else { |
||||
return target.rawSql; |
||||
} |
||||
} |
||||
|
||||
hasUnixEpochTimecolumn() { |
||||
return ['int4', 'int8', 'float4', 'float8', 'numeric'].indexOf(this.target.timeColumnType) > -1; |
||||
} |
||||
|
||||
buildTimeColumn(alias = true) { |
||||
let timeGroup = this.hasTimeGroup(); |
||||
let query; |
||||
let macro = '$__timeGroup'; |
||||
|
||||
if (timeGroup) { |
||||
let args; |
||||
if (timeGroup.params.length > 1 && timeGroup.params[1] !== 'none') { |
||||
args = timeGroup.params.join(','); |
||||
} else { |
||||
args = timeGroup.params[0]; |
||||
} |
||||
if (this.hasUnixEpochTimecolumn()) { |
||||
macro = '$__unixEpochGroup'; |
||||
} |
||||
if (alias) { |
||||
macro += 'Alias'; |
||||
} |
||||
query = macro + '(' + this.target.timeColumn + ',' + args + ')'; |
||||
} else { |
||||
query = this.target.timeColumn; |
||||
if (alias) { |
||||
query += ' AS "time"'; |
||||
} |
||||
} |
||||
|
||||
return query; |
||||
} |
||||
|
||||
buildMetricColumn() { |
||||
if (this.hasMetricColumn()) { |
||||
return this.target.metricColumn + ' AS metric'; |
||||
} |
||||
|
||||
return ''; |
||||
} |
||||
|
||||
buildValueColumns() { |
||||
let query = ''; |
||||
for (let column of this.target.select) { |
||||
query += ',\n ' + this.buildValueColumn(column); |
||||
} |
||||
|
||||
return query; |
||||
} |
||||
|
||||
buildValueColumn(column) { |
||||
let query = ''; |
||||
|
||||
let columnName = _.find(column, (g: any) => g.type === 'column'); |
||||
query = columnName.params[0]; |
||||
|
||||
let aggregate = _.find(column, (g: any) => g.type === 'aggregate' || g.type === 'percentile'); |
||||
let windows = _.find(column, (g: any) => g.type === 'window' || g.type === 'moving_window'); |
||||
|
||||
if (aggregate) { |
||||
let func = aggregate.params[0]; |
||||
switch (aggregate.type) { |
||||
case 'aggregate': |
||||
if (func === 'first' || func === 'last') { |
||||
query = func + '(' + query + ',' + this.target.timeColumn + ')'; |
||||
} else { |
||||
query = func + '(' + query + ')'; |
||||
} |
||||
break; |
||||
case 'percentile': |
||||
query = func + '(' + aggregate.params[1] + ') WITHIN GROUP (ORDER BY ' + query + ')'; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
if (windows) { |
||||
let overParts = []; |
||||
if (this.hasMetricColumn()) { |
||||
overParts.push('PARTITION BY ' + this.target.metricColumn); |
||||
} |
||||
overParts.push('ORDER BY ' + this.buildTimeColumn(false)); |
||||
|
||||
let over = overParts.join(' '); |
||||
let curr: string; |
||||
let prev: string; |
||||
switch (windows.type) { |
||||
case 'window': |
||||
switch (windows.params[0]) { |
||||
case 'increase': |
||||
curr = query; |
||||
prev = 'lag(' + curr + ') OVER (' + over + ')'; |
||||
query = '(CASE WHEN ' + curr + ' >= ' + prev + ' THEN ' + curr + ' - ' + prev + ' ELSE ' + curr + ' END)'; |
||||
break; |
||||
case 'rate': |
||||
let timeColumn = this.target.timeColumn; |
||||
if (aggregate) { |
||||
timeColumn = 'min(' + timeColumn + ')'; |
||||
} |
||||
|
||||
curr = query; |
||||
prev = 'lag(' + curr + ') OVER (' + over + ')'; |
||||
query = '(CASE WHEN ' + curr + ' >= ' + prev + ' THEN ' + curr + ' - ' + prev + ' ELSE ' + curr + ' END)'; |
||||
query += '/extract(epoch from ' + timeColumn + ' - lag(' + timeColumn + ') OVER (' + over + '))'; |
||||
break; |
||||
default: |
||||
query = windows.params[0] + '(' + query + ') OVER (' + over + ')'; |
||||
break; |
||||
} |
||||
break; |
||||
case 'moving_window': |
||||
query = windows.params[0] + '(' + query + ') OVER (' + over + ' ROWS ' + windows.params[1] + ' PRECEDING)'; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
let alias = _.find(column, (g: any) => g.type === 'alias'); |
||||
if (alias) { |
||||
query += ' AS ' + this.quoteIdentifier(alias.params[0]); |
||||
} |
||||
|
||||
return query; |
||||
} |
||||
|
||||
buildWhereClause() { |
||||
let query = ''; |
||||
let conditions = _.map(this.target.where, (tag, index) => { |
||||
switch (tag.type) { |
||||
case 'macro': |
||||
return tag.name + '(' + this.target.timeColumn + ')'; |
||||
break; |
||||
case 'expression': |
||||
return tag.params.join(' '); |
||||
break; |
||||
} |
||||
}); |
||||
|
||||
if (conditions.length > 0) { |
||||
query = '\nWHERE\n ' + conditions.join(' AND\n '); |
||||
} |
||||
|
||||
return query; |
||||
} |
||||
|
||||
buildGroupClause() { |
||||
let query = ''; |
||||
let groupSection = ''; |
||||
|
||||
for (let i = 0; i < this.target.group.length; i++) { |
||||
let part = this.target.group[i]; |
||||
if (i > 0) { |
||||
groupSection += ', '; |
||||
} |
||||
if (part.type === 'time') { |
||||
groupSection += '1'; |
||||
} else { |
||||
groupSection += part.params[0]; |
||||
} |
||||
} |
||||
|
||||
if (groupSection.length) { |
||||
query = '\nGROUP BY ' + groupSection; |
||||
if (this.hasMetricColumn()) { |
||||
query += ',2'; |
||||
} |
||||
} |
||||
return query; |
||||
} |
||||
|
||||
buildQuery() { |
||||
let query = 'SELECT'; |
||||
|
||||
query += '\n ' + this.buildTimeColumn(); |
||||
if (this.hasMetricColumn()) { |
||||
query += ',\n ' + this.buildMetricColumn(); |
||||
} |
||||
query += this.buildValueColumns(); |
||||
|
||||
query += '\nFROM ' + this.target.table; |
||||
|
||||
query += this.buildWhereClause(); |
||||
query += this.buildGroupClause(); |
||||
|
||||
query += '\nORDER BY 1'; |
||||
|
||||
return query; |
||||
} |
||||
} |
||||
@ -0,0 +1,86 @@ |
||||
import { SqlPartDef, SqlPart } from 'app/core/components/sql_part/sql_part'; |
||||
|
||||
let index = []; |
||||
|
||||
function createPart(part): any { |
||||
let def = index[part.type]; |
||||
if (!def) { |
||||
return null; |
||||
} |
||||
|
||||
return new SqlPart(part, def); |
||||
} |
||||
|
||||
function register(options: any) { |
||||
index[options.type] = new SqlPartDef(options); |
||||
} |
||||
|
||||
register({ |
||||
type: 'column', |
||||
style: 'label', |
||||
params: [{ type: 'column', dynamicLookup: true }], |
||||
defaultParams: ['value'], |
||||
}); |
||||
|
||||
register({ |
||||
type: 'expression', |
||||
style: 'expression', |
||||
label: 'Expr:', |
||||
params: [ |
||||
{ name: 'left', type: 'string', dynamicLookup: true }, |
||||
{ name: 'op', type: 'string', dynamicLookup: true }, |
||||
{ name: 'right', type: 'string', dynamicLookup: true }, |
||||
], |
||||
defaultParams: ['value', '=', 'value'], |
||||
}); |
||||
|
||||
register({ |
||||
type: 'macro', |
||||
style: 'label', |
||||
label: 'Macro:', |
||||
params: [], |
||||
defaultParams: [], |
||||
}); |
||||
|
||||
register({ |
||||
type: 'aggregate', |
||||
style: 'label', |
||||
params: [ |
||||
{ |
||||
name: 'name', |
||||
type: 'string', |
||||
options: ['avg', 'count', 'min', 'max', 'sum', 'stddev', 'variance'], |
||||
}, |
||||
], |
||||
defaultParams: ['avg'], |
||||
}); |
||||
|
||||
register({ |
||||
type: 'alias', |
||||
style: 'label', |
||||
params: [{ name: 'name', type: 'string', quote: 'double' }], |
||||
defaultParams: ['alias'], |
||||
}); |
||||
|
||||
register({ |
||||
type: 'time', |
||||
style: 'function', |
||||
label: 'time', |
||||
params: [ |
||||
{ |
||||
name: 'interval', |
||||
type: 'interval', |
||||
options: ['$__interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'], |
||||
}, |
||||
{ |
||||
name: 'fill', |
||||
type: 'string', |
||||
options: ['none', 'NULL', 'previous', '0'], |
||||
}, |
||||
], |
||||
defaultParams: ['$__interval', 'none'], |
||||
}); |
||||
|
||||
export default { |
||||
create: createPart, |
||||
}; |
||||
Loading…
Reference in new issue