Files
grafana/public/app/features/explore/Logs/LogsContainer.tsx
Matias Chomicki 84f94cdc24 Logs: Show active state of "filter for value" buttons in Logs Details (#70328)
* Datasource test: fix describe nesting

* Parsing: export handleQuotes function

* Modify query: add functions to detect the presence of a label and remove it

* Loki: add support to toggle filters if already present

* Datasource test: fix describe nesting

* Loki: add support to toggle filter out if present

* Remove label: handle escaped values

* Datasource: add test case for escaped label values

* Loki: remove = filter when applying !=

* Remove selector: add support for Selector node being far from Matcher

* Modify query: add unit tests

* Elasticsearch: create modifyQuery for elastic

* Elastic modify query: implement functions

* Elasticsearch: implement modifyQuery functions in datasource

* Elasticsearch: update datasource test

* Loki modify query: check for streamSelectorPositions length

* Elasticsearch query has filter: escape filter value in regex

* Remove unused type

* Modify query: add functions to detect the presence of a label and remove it

* Remove label: handle escaped values

* Logs: create props to check for label filters in the query

* Log Details Row: use label state props to show visual feedback

* Make isCallbacks async

* Explore: add placeholder for checking for filter in query

* Datasource: define new API method

* Inspect query: add base implementation

* Remove isFilterOutLabelActive as it will not be needed

* Check for "isActive" on every render

Otherwise the active state will be out of sync

* Elasticsearch: implement inspectQuery in the datasource

* Logs: update test

* Log details: update test

* Datasources: update tests

* Inspect query: rename to analize query to prevent confusion

* Datasource types: mark method as alpha

* Explore: add comment to log-specific functions

* Remove duplicated code from bad rebase

* Remove label filter: check node type

* getMatchersWithFilter: rename argument

* Fix bad rebase

* Create DataSourceWithQueryManipulationSupport interface

* Implement type guard for DataSourceWithQueryManipulationSupport

* DataSourceWithQueryManipulationSupport: move to logs module

* hasQueryManipulationSupport: change implementation

`modifyQuery` comes from the prototype.

* DataSourceWithQueryManipulationSupport: expand code comments

* AnalyzeQueryOptions: move to logs module

* DataSourceWithQueryManipulationSupport: add support for more return types

* Fix merge error

* Update packages/grafana-data/src/types/logs.ts

Co-authored-by: Sven Grossmann <sven.grossmann@grafana.com>

* DatasourceAPI: deprecate modifyQuery

* Explore: refactor isFilterLabelActive

* DataSourceWithQueryModificationSupport: rename interface

* Split interfaces into Analyze and Modify

* Query analysis: better name for interface

* Fix guard

* Create feature flag for active state

* Use new feature flag in Explore

* DataSourceToggleableQueryFiltersSupport: create a specific interface for this feature

* Rename feature flag

* De-deprecate modifyQuery

* DataSourceToggleableQueryFiltersSupport: Rethink types and methods

* Explore: adjust modifyQuery and isFilterLabelActive to new methods

* Loki: implement new interface and revert modifyQuery

* DataSourceToggleableQueryFiltersSupport: better name for arguments

* Elasticsearch: implement new interface and revert modifyQuery

* Loki: better name for arguments

* Explore: document current limitation on isFilterLabelActive

* Explore: place toggleable filters under feature flag

* Loki: add tests for the new methods

* Loki: add legacy modifyQuery tests

* Elasticsearch: add tests for the new methods

* Elasticsearch: add legacy modifyQuery tests

* Toggle filter action: improve type values

* Logs types: update interface description

* DataSourceWithToggleableQueryFiltersSupport: update interface name

* Update feature flag description

* Explore: add todo comment for isFilterLabelActive

---------

Co-authored-by: Sven Grossmann <sven.grossmann@grafana.com>
2023-07-24 11:22:47 +03:00

281 lines
8.5 KiB
TypeScript

import React, { PureComponent } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import {
AbsoluteTimeRange,
Field,
hasLogsContextSupport,
hasLogsContextUiSupport,
LoadingState,
LogRowModel,
RawTimeRange,
EventBus,
SplitOpen,
DataFrame,
SupplementaryQueryType,
DataQueryResponse,
LogRowContextOptions,
DataSourceWithLogsContextSupport,
DataSourceApi,
} from '@grafana/data';
import { DataQuery } from '@grafana/schema';
import { Collapse } from '@grafana/ui';
import { StoreState } from 'app/types';
import { ExploreItemState } from 'app/types/explore';
import { getTimeZone } from '../../profile/state/selectors';
import {
addResultsToCache,
clearCache,
loadSupplementaryQueryData,
selectIsWaitingForData,
setSupplementaryQueryEnabled,
} from '../state/query';
import { updateTimeRange } from '../state/time';
import { LiveTailControls } from '../useLiveTailControls';
import { getFieldLinksForExplore } from '../utils/links';
import { LiveLogsWithTheme } from './LiveLogs';
import { Logs } from './Logs';
import { LogsCrossFadeTransition } from './utils/LogsCrossFadeTransition';
interface LogsContainerProps extends PropsFromRedux {
width: number;
exploreId: string;
scanRange?: RawTimeRange;
syncedTimes: boolean;
loadingState: LoadingState;
onClickFilterLabel: (key: string, value: string) => void;
onClickFilterOutLabel: (key: string, value: string) => void;
onStartScanning: () => void;
onStopScanning: () => void;
eventBus: EventBus;
splitOpenFn: SplitOpen;
scrollElement?: HTMLDivElement;
isFilterLabelActive: (key: string, value: string) => Promise<boolean>;
}
class LogsContainer extends PureComponent<LogsContainerProps> {
onChangeTime = (absoluteRange: AbsoluteTimeRange) => {
const { exploreId, updateTimeRange } = this.props;
updateTimeRange({ exploreId, absoluteRange });
};
private getQuery(
logsQueries: DataQuery[] | undefined,
row: LogRowModel,
datasourceInstance: DataSourceApi<DataQuery> & DataSourceWithLogsContextSupport<DataQuery>
) {
// we need to find the query, and we need to be very sure that it's a query
// from this datasource
return (logsQueries ?? []).find(
(q) => q.refId === row.dataFrame.refId && q.datasource != null && q.datasource.type === datasourceInstance.type
);
}
getLogRowContext = async (
row: LogRowModel,
origRow: LogRowModel,
options: LogRowContextOptions
): Promise<DataQueryResponse | []> => {
const { datasourceInstance, logsQueries } = this.props;
if (hasLogsContextSupport(datasourceInstance)) {
const query = this.getQuery(logsQueries, origRow, datasourceInstance);
return datasourceInstance.getLogRowContext(row, options, query);
}
return [];
};
getLogRowContextQuery = async (row: LogRowModel, options?: LogRowContextOptions): Promise<DataQuery | null> => {
const { datasourceInstance, logsQueries } = this.props;
if (hasLogsContextSupport(datasourceInstance) && datasourceInstance.getLogRowContextQuery) {
const query = this.getQuery(logsQueries, row, datasourceInstance);
return datasourceInstance.getLogRowContextQuery(row, options, query);
}
return null;
};
getLogRowContextUi = (row: LogRowModel, runContextQuery?: () => void): React.ReactNode => {
const { datasourceInstance, logsQueries } = this.props;
if (hasLogsContextUiSupport(datasourceInstance) && datasourceInstance.getLogRowContextUi) {
const query = this.getQuery(logsQueries, row, datasourceInstance);
return datasourceInstance.getLogRowContextUi(row, runContextQuery, query);
}
return <></>;
};
showContextToggle = (row?: LogRowModel): boolean => {
const { datasourceInstance } = this.props;
if (hasLogsContextSupport(datasourceInstance)) {
return datasourceInstance.showContextToggle(row);
}
return false;
};
getFieldLinks = (field: Field, rowIndex: number, dataFrame: DataFrame) => {
const { splitOpenFn, range } = this.props;
return getFieldLinksForExplore({ field, rowIndex, splitOpenFn, range, dataFrame });
};
render() {
const {
loading,
loadingState,
logRows,
logsMeta,
logsSeries,
logsQueries,
loadSupplementaryQueryData,
setSupplementaryQueryEnabled,
onClickFilterLabel,
onClickFilterOutLabel,
onStartScanning,
onStopScanning,
absoluteRange,
timeZone,
visibleRange,
scanning,
range,
width,
splitOpenFn,
isLive,
exploreId,
addResultsToCache,
clearCache,
logsVolume,
scrollElement,
} = this.props;
if (!logRows) {
return null;
}
return (
<>
<LogsCrossFadeTransition visible={isLive}>
<Collapse label="Logs" loading={false} isOpen>
<LiveTailControls exploreId={exploreId}>
{(controls) => (
<LiveLogsWithTheme
logRows={logRows}
timeZone={timeZone}
stopLive={controls.stop}
isPaused={this.props.isPaused}
onPause={controls.pause}
onResume={controls.resume}
onClear={controls.clear}
clearedAtIndex={this.props.clearedAtIndex}
/>
)}
</LiveTailControls>
</Collapse>
</LogsCrossFadeTransition>
<LogsCrossFadeTransition visible={!isLive}>
<Logs
exploreId={exploreId}
datasourceType={this.props.datasourceInstance?.type}
logRows={logRows}
logsMeta={logsMeta}
logsSeries={logsSeries}
logsVolumeEnabled={logsVolume.enabled}
onSetLogsVolumeEnabled={(enabled) =>
setSupplementaryQueryEnabled(exploreId, enabled, SupplementaryQueryType.LogsVolume)
}
logsVolumeData={logsVolume.data}
logsQueries={logsQueries}
width={width}
splitOpen={splitOpenFn}
loading={loading}
loadingState={loadingState}
loadLogsVolumeData={() => loadSupplementaryQueryData(exploreId, SupplementaryQueryType.LogsVolume)}
onChangeTime={this.onChangeTime}
onClickFilterLabel={onClickFilterLabel}
onClickFilterOutLabel={onClickFilterOutLabel}
onStartScanning={onStartScanning}
onStopScanning={onStopScanning}
absoluteRange={absoluteRange}
visibleRange={visibleRange}
timeZone={timeZone}
scanning={scanning}
scanRange={range.raw}
showContextToggle={this.showContextToggle}
getRowContext={this.getLogRowContext}
getRowContextQuery={this.getLogRowContextQuery}
getLogRowContextUi={this.getLogRowContextUi}
getFieldLinks={this.getFieldLinks}
addResultsToCache={() => addResultsToCache(exploreId)}
clearCache={() => clearCache(exploreId)}
eventBus={this.props.eventBus}
panelState={this.props.panelState}
logsFrames={this.props.logsFrames}
scrollElement={scrollElement}
isFilterLabelActive={this.props.isFilterLabelActive}
range={range}
/>
</LogsCrossFadeTransition>
</>
);
}
}
function mapStateToProps(state: StoreState, { exploreId }: { exploreId: string }) {
const explore = state.explore;
const item: ExploreItemState = explore.panes[exploreId]!;
const {
logsResult,
scanning,
datasourceInstance,
isLive,
isPaused,
clearedAtIndex,
range,
absoluteRange,
supplementaryQueries,
} = item;
const loading = selectIsWaitingForData(exploreId)(state);
const panelState = item.panelsState;
const timeZone = getTimeZone(state.user);
const logsVolume = supplementaryQueries[SupplementaryQueryType.LogsVolume];
return {
loading,
logRows: logsResult?.rows,
logsMeta: logsResult?.meta,
logsSeries: logsResult?.series,
logsQueries: logsResult?.queries,
visibleRange: logsResult?.visibleRange,
scanning,
timeZone,
datasourceInstance,
isLive,
isPaused,
clearedAtIndex,
range,
absoluteRange,
logsVolume,
panelState,
logsFrames: item.queryResponse.logsFrames,
};
}
const mapDispatchToProps = {
updateTimeRange,
addResultsToCache,
clearCache,
loadSupplementaryQueryData,
setSupplementaryQueryEnabled,
};
const connector = connect(mapStateToProps, mapDispatchToProps);
type PropsFromRedux = ConnectedProps<typeof connector>;
export default connector(LogsContainer);