From aed020d9b5ebaab64935ae690156eb68e904d8c8 Mon Sep 17 00:00:00 2001 From: Brendan O'Handley Date: Fri, 3 Mar 2023 13:28:58 -0500 Subject: [PATCH] Influxdb: Fix missing columns with raw query (#64058) * add selected columns to table when select object is not in query model because of rawQuery * handle * and SHOW raw queries * handle metricFindQuery * Update public/app/plugins/datasource/influxdb/response_parser.ts Co-authored-by: ismail simsek * Update public/app/plugins/datasource/influxdb/response_parser.ts Co-authored-by: ismail simsek * Update public/app/plugins/datasource/influxdb/response_parser.ts Co-authored-by: ismail simsek --------- Co-authored-by: ismail simsek --- .../datasource/influxdb/response_parser.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/public/app/plugins/datasource/influxdb/response_parser.ts b/public/app/plugins/datasource/influxdb/response_parser.ts index a156e98be56..9e0a1343935 100644 --- a/public/app/plugins/datasource/influxdb/response_parser.ts +++ b/public/app/plugins/datasource/influxdb/response_parser.ts @@ -198,6 +198,25 @@ function getTableCols(dfs: DataFrame[], table: TableModel, target: InfluxQuery): table.columns.push({ text: selectedParams[i] }); } + // ISSUE: https://github.com/grafana/grafana/issues/63842 + // if rawQuery and + // has other selected fields in the query and + // dfs field names are in the rawQuery but + // the selected params object doesn't exist in the query then + // add columns to the table + if ( + target.rawQuery && + selectedParams.length === 0 && + rawQuerySelectedFieldsInDataframe(target.query, dfs) && + dfs[0].refId !== 'metricFindQuery' + ) { + dfs.map((df) => { + if (df.name) { + table.columns.push({ text: df.name }); + } + }); + } + return table; } @@ -248,3 +267,30 @@ function incrementName(name: string, nameIncremenet: string, params: string[], i function addUnique(s: Set, value: string | number) { s.add(value.toString()); } + +function rawQuerySelectedFieldsInDataframe(query: string | undefined, dfs: DataFrame[]) { + const names: Array = dfs.map((df: DataFrame) => df.name); + + const colsInRawQuery = names.every((name: string | undefined) => { + if (name && query) { + // table name and field, i.e. cpu.usage_guest_nice becomes ['cpu', 'usage_guest_nice'] + const nameParts = name.split('.'); + + return nameParts.every((np) => query.toLowerCase().includes(np.toLowerCase())); + } + + return false; + }); + + const queryChecks = ['*', 'SHOW']; + + const otherChecks: boolean = queryChecks.some((qc: string) => { + if (query) { + return query.toLowerCase().includes(qc.toLowerCase()); + } + + return false; + }); + + return colsInRawQuery || otherChecks; +}