Skip to content

Commit

Permalink
Refactor LogsContext into a mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelemeny committed Feb 22, 2024
1 parent 03b6beb commit d11100f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
30 changes: 0 additions & 30 deletions src/datasource/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { first as _first, map as _map } from 'lodash';
import { Observable, lastValueFrom, from, of } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';

Expand All @@ -10,10 +9,8 @@ import {
DataQueryRequest,
DataQueryResponse,
DataSourceInstanceSettings,
DataSourceWithLogsContextSupport,
DataSourceWithQueryImportSupport,
getDefaultTimeRange,
LogRowModel,
MetricFindValue,
QueryFixAction,
ScopedVars,
Expand All @@ -32,10 +29,8 @@ import { isMetricAggregationWithField } from 'components/QueryEditor/MetricAggre
import { bucketAggregationConfig } from 'components/QueryEditor/BucketAggregationsEditor/utils';
import { isBucketAggregationWithField } from 'components/QueryEditor/BucketAggregationsEditor/aggregations';
import ElasticsearchLanguageProvider from 'LanguageProvider';
import { ReactNode } from 'react';
import { fieldTypeMap } from 'utils';
import { addAddHocFilter } from 'modifyQuery';
import { LogContextProvider, LogRowContextOptions } from '@/LogContext/LogContextProvider';
import { getQueryResponseProcessor } from 'datasource/processResponse';

import { SECOND } from 'utils/time';
Expand All @@ -53,7 +48,6 @@ type FieldCapsSpec = {
export class BaseQuickwitDataSource
extends DataSourceWithBackend<ElasticsearchQuery, QuickwitOptions>
implements
DataSourceWithLogsContextSupport,
DataSourceWithQueryImportSupport<ElasticsearchQuery>
{
index: string;
Expand All @@ -63,7 +57,6 @@ export class BaseQuickwitDataSource
dataLinks: DataLinkConfig[];
languageProvider: ElasticsearchLanguageProvider;

private logContextProvider: LogContextProvider;

constructor(
instanceSettings: DataSourceInstanceSettings<QuickwitOptions>,
Expand All @@ -77,7 +70,6 @@ export class BaseQuickwitDataSource
this.logLevelField = settingsData.logLevelField || '';
this.dataLinks = settingsData.dataLinks || [];
this.languageProvider = new ElasticsearchLanguageProvider(this);
this.logContextProvider = new LogContextProvider(this);
}

query(request: DataQueryRequest<ElasticsearchQuery>): Observable<DataQueryResponse> {
Expand Down Expand Up @@ -283,28 +275,6 @@ export class BaseQuickwitDataSource
return text;
}

// Log Context

// NOTE : deprecated since grafana-data 10.3
showContextToggle(row?: LogRowModel | undefined): boolean {
return true;
}

getLogRowContext = async (
row: LogRowModel,
options?: LogRowContextOptions,
origQuery?: ElasticsearchQuery
): Promise<{ data: DataFrame[] }> => {
return await this.logContextProvider.getLogRowContext(row, options, origQuery);
}

getLogRowContextUi(
row: LogRowModel,
runContextQuery?: (() => void),
origQuery?: ElasticsearchQuery
): ReactNode {
return this.logContextProvider.getLogRowContextUi(row, runContextQuery, origQuery);
}

/**
* Returns false if the query should be skipped
Expand Down
2 changes: 2 additions & 0 deletions src/datasource/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { BaseQuickwitDataSource } from './base';

import { withSupplementaryQueries } from './supplementaryQueries';
import { withLogContext } from './logsContext';

const mixins = [
withLogContext,
withSupplementaryQueries,
]
const qwds = mixins.reduce(( qwds, fn) => fn(qwds), BaseQuickwitDataSource)
Expand Down
39 changes: 39 additions & 0 deletions src/datasource/logsContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { DataFrame, DataSourceWithLogsContextSupport, LogRowModel, } from '@grafana/data';
import {ElasticsearchQuery } from '@/types';

import { ReactNode } from 'react';
import { LogContextProvider, LogRowContextOptions } from '@/LogContext/LogContextProvider';

import { BaseQuickwitDataSourceConstructor } from './base';

export function withLogContext<T extends BaseQuickwitDataSourceConstructor > ( Base: T ){
return class DSWithLogsContext extends Base implements DataSourceWithLogsContextSupport {
protected logContextProvider: LogContextProvider;
// Log Context
constructor(...args: any[]){
super(...args)
this.logContextProvider = new LogContextProvider(this);
}
// NOTE : deprecated since grafana-data 10.3
showContextToggle(row?: LogRowModel | undefined): boolean {
return true;
}

getLogRowContext = async (
row: LogRowModel,
options?: LogRowContextOptions,
origQuery?: ElasticsearchQuery
): Promise<{ data: DataFrame[] }> => {
return await this.logContextProvider.getLogRowContext(row, options, origQuery);
}

getLogRowContextUi(
row: LogRowModel,
runContextQuery?: (() => void),
origQuery?: ElasticsearchQuery
): ReactNode {
return this.logContextProvider.getLogRowContextUi(row, runContextQuery, origQuery);
}

};
}

0 comments on commit d11100f

Please sign in to comment.