88} from '@grafana/data' ;
99
1010import { GreptimeDataTypes } from './types' ;
11- import { getColumnsByHint } from 'data/sqlGenerator' ;
11+ import { getColumnsByHint , logColumnHintsToAlias } from 'data/sqlGenerator' ;
1212import { ColumnHint , QueryBuilderOptions } from 'types/queryBuilder' ;
1313import { CHQuery } from 'types/sql' ;
1414
@@ -272,28 +272,25 @@ export function transformGreptimeDBLogs(sqlResponse: GreptimeResponse, query: CH
272272 const labelColumnIndices : Record < string , number > = { } ;
273273 const contextColumnIndices : Record < string , number > = { } ;
274274
275- columnSchemas . forEach ( ( schema , index ) => {
276- const lowerCaseName = schema . name . toLowerCase ( ) ;
277- if ( lowerCaseName === 'ts' || lowerCaseName === 'timestamp' ) {
278- timestampColumnIndex = index ;
279- } else if ( lowerCaseName === 'body' || lowerCaseName === 'message' ) {
280- bodyColumnIndex = index ;
281- } else if ( lowerCaseName === 'severity' || lowerCaseName === 'level' ) {
282- severityColumnIndex = index ;
283- } else if ( lowerCaseName === 'id' ) {
284- idColumnIndex = index ;
285- } else if ( contextColumns . includes ( schema . name ) ) {
286- contextColumnIndices [ schema . name ] = index ;
287- } else {
288- // Consider other columns as potential labels
289- labelColumnIndices [ schema . name ] = index ;
290- }
291- } ) ;
292-
293- // if (timestampColumnIndex === -1 || bodyColumnIndex === -1) {
294- // console.error('Timestamp or body column not found in GreptimeDB response.');
295- // return null;
296- // }
275+
276+ if ( 'builderOptions' in query ) {
277+
278+ columnSchemas . forEach ( ( schema , index ) => {
279+ const lowerCaseName = schema . name . toLowerCase ( ) ;
280+ if ( lowerCaseName === logColumnHintsToAlias . get ( ColumnHint . Time ) ) {
281+ timestampColumnIndex = index ;
282+ } else if ( lowerCaseName === logColumnHintsToAlias . get ( ColumnHint . LogMessage ) ) {
283+ bodyColumnIndex = index ;
284+ } else if ( lowerCaseName === logColumnHintsToAlias . get ( ColumnHint . LogLevel ) ) {
285+ severityColumnIndex = index ;
286+ } else if ( contextColumns . includes ( schema . name ) ) {
287+ contextColumnIndices [ schema . name ] = index ;
288+ } else {
289+ // Consider other columns as potential labels
290+ labelColumnIndices [ schema . name ] = index ;
291+ }
292+ } ) ;
293+ }
297294
298295 const timestamps : number [ ] = [ ] ;
299296 const bodies : string [ ] = [ ] ;
@@ -309,9 +306,13 @@ export function transformGreptimeDBLogs(sqlResponse: GreptimeResponse, query: CH
309306 ? new Date ( timestampValue ) . getTime ( )
310307 : timestampValue
311308 ) ;
312- bodies . push ( String ( row [ bodyColumnIndex ] ) ) ;
313- severities . push ( severityColumnIndex !== - 1 ? String ( row [ severityColumnIndex ] ) : '' ) ;
314- ids . push ( idColumnIndex !== - 1 ? String ( row [ idColumnIndex ] ) : '' ) ;
309+ if ( bodyColumnIndex !== - 1 ) {
310+ bodies . push ( String ( row [ bodyColumnIndex ] ) ) ;
311+ }
312+ if ( severityColumnIndex !== - 1 ) {
313+ severities . push ( String ( row [ severityColumnIndex ] ) ) ;
314+ }
315+
315316
316317 const labels : Record < string , any > = { } ;
317318 for ( const labelName in labelColumnIndices ) {
0 commit comments