Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…rafana-datasource into v4-deploy
  • Loading branch information
SpencerTorres committed Jan 19, 2024
2 parents afa7944 + 86a1932 commit 7a96680
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/data/CHDatasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
queryLogsVolume,
TIME_FIELD_ALIAS,
} from './logs';
import { generateSql, getColumnByHint } from './sqlGenerator';
import { generateSql, getColumnByHint, logAliasToColumnHints } from './sqlGenerator';
import { versions as otelVersions } from 'otel';
import { ReactNode } from 'react';
import { transformQueryResponseWithTraceLinks } from './utils';
Expand Down Expand Up @@ -165,15 +165,15 @@ export class Datasource
});
}

const filters = (query.builderOptions.filters?.slice() || []);
filters.forEach(f => {
const filters = (query.builderOptions.filters?.slice() || []).map(f => {
// In order for a hinted filter to work, the hinted column must be SELECTed OR provide "key"
// For this histogram query the "level" column isn't selected, so we must find the original column name
if (f.hint && !f.key) {
const originalColumn = getColumnByHint(query.builderOptions, f.hint);
f.key = originalColumn?.alias || originalColumn?.name || '';
}

return f;
});

const logVolumeSqlBuilderOptions: QueryBuilderOptions = {
Expand All @@ -190,7 +190,8 @@ export class Datasource
const logVolumeSupplementaryQuery = generateSql(logVolumeSqlBuilderOptions);
return {
pluginVersion,
editorType: EditorType.SQL,
editorType: EditorType.Builder,
builderOptions: logVolumeSqlBuilderOptions,
rawSql: logVolumeSupplementaryQuery,
refId: '',
};
Expand Down Expand Up @@ -287,7 +288,8 @@ export class Datasource
// Find selected column by alias/name
const lookupByAlias = query.builderOptions.columns?.find(c => c.alias === columnName); // Check all aliases first,
const lookupByName = query.builderOptions.columns?.find(c => c.name === columnName); // then try matching column name
const column = lookupByAlias || lookupByName;
const lookupByLogsAlias = logAliasToColumnHints.has(columnName) ? getColumnByHint(query.builderOptions, logAliasToColumnHints.get(columnName)!) : undefined;
const column = lookupByAlias || lookupByName || lookupByLogsAlias;

let nextFilters: Filter[] = (query.builderOptions.filters?.slice() || []);
if (action.type === 'ADD_FILTER') {
Expand Down
20 changes: 17 additions & 3 deletions src/data/sqlGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ const generateTraceIdQuery = (options: QueryBuilderOptions): string => {
*
* note: column order seems to matter as well as alias name
*/
const generateLogsQuery = (options: QueryBuilderOptions): string => {
const generateLogsQuery = (_options: QueryBuilderOptions): string => {
// Copy columns so column aliases can be safely mutated
const options = { ..._options, columns: _options.columns?.map(c => ({ ...c })) };
const { database, table } = options;

const queryParts: string[] = [];
Expand Down Expand Up @@ -267,7 +269,7 @@ const generateLogsQuery = (options: QueryBuilderOptions): string => {
* Generates a simple time series query. Includes user selected columns.
*/
const generateSimpleTimeSeriesQuery = (_options: QueryBuilderOptions): string => {
// Copy columns so time alias can be safely mutated
// Copy columns so column aliases can be safely mutated
const options = { ..._options, columns: _options.columns?.map(c => ({ ...c })) };
const { database, table } = options;

Expand Down Expand Up @@ -352,7 +354,7 @@ const generateSimpleTimeSeriesQuery = (_options: QueryBuilderOptions): string =>
* Generates an aggregate time series query.
*/
const generateAggregateTimeSeriesQuery = (_options: QueryBuilderOptions): string => {
// Copy columns so time column can be safely mutated
// Copy columns so column aliases can be safely mutated
const options = { ..._options, columns: _options.columns?.map(c => ({ ...c })) };
const { database, table } = options;

Expand Down Expand Up @@ -663,3 +665,15 @@ const isDateFilter = (type: string): boolean => isDateType(type);
const isStringFilter = (type: string, operator: FilterOperator): boolean => isStringType(type) && !(operator === FilterOperator.In || operator === FilterOperator.NotIn);
const isMultiFilter = (type: string, operator: FilterOperator): boolean => isStringType(type) && (operator === FilterOperator.In || operator === FilterOperator.NotIn);
const formatStringValue = (filter: string): string => filter.startsWith('$') ? (filter || '') : `'${filter || ''}'`;

/**
* When filtering in the logs panel in explore view, we need a way to
* map from the SQL generator's aliases back to the original column hints
* so that filters can be added properly.
*/
export const logAliasToColumnHints: Map<string, ColumnHint> = new Map([
['timestamp', ColumnHint.Time],
['body', ColumnHint.LogMessage],
['level', ColumnHint.LogLevel],
['traceID', ColumnHint.TraceId],
]);

0 comments on commit 7a96680

Please sign in to comment.