mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 16:45:43 -06:00
* Explore: updates grafana-data explore query field props with explore mode * Explore: updates query row to pass down explore mode to query fields * Explore: adds LokiExploreQueryEditor * Explore: updates loki query field form to render children * Explore: adds loki explore extra field component * Explore: adds extra field element to loki query field form * Explore: updates loki explore query editor to use extra field element * Explore: moves ExploreMode to grafana-data * Explore: updates query row limit string * Explore: adds maxLines to DataQuery * Explore: adds maxLines to loki datasource runRangeQueryWithFallback * Explore: adds onChangeQueryLimit to LokiExploreQueryEditor * Explore: updates loki explore query editor to render extra field only in logs mode * Explore: fixes query limits for live and legacy queries * Explore: fixes result processor max lines limit in get logs result * Explore: fixes Loki datasource limit test * Explore: removes unnecessary ExploreMode from Loki language provider * Explore: fixes formatting * Explore: updates grafana-data datasource types - replaces strings with explore mode enum * Explore: updates loki explore query field props to take ReactNode * Explore: updates the way we calculate loki query lines limit to fall back to 0 lines on negative or invalid input instead of datasource maxLines * Explore: updates result processor get logs result method to avoid counting invalid/negative line limits * Explore: updates loki result transformer to process only an appropriate slice of a result instead of an entire one * Explore: adds a method for query limit preprocessing/mapping * Explore: updates loki datasource run range query with fallback method to use options.maxDataPoints in dashboards * Explore: removes unnecessary maxlineslimt from getLogsResult in resultProcessor * Explore: moves line limit to metadata * Explore: adds an ability to specify input type of extra field * Explore: updates LokiExploreQueryEditor - adds an input type * Explore: updates LokiExploreQueryEditor to run queries when maxLines is positive * Explore: fixes failing import of ExploreMode * Explore: fixes reducers test imports formatting * Explore: updates Loki extra field with min value set to 0 * Explore: exports LokiExploreExtraFieldProps * Explore: adds render test of LokiExploreQueryEditor * Explore: adds LokiExploreQueryEditor snapshot * Explore: updates LokiExploreQueryEditor onChangeQueryLimit method to prevent it from running when query input is empty - fixes cheatsheet display issue * Explore: updates Loki editor snapshots * Explore: fixes typo in test set name in LokiExploreQueryEditor * Explore: adds a render test of LokiExploreExtraField * Explore: fixes typo in LokiExploreQueryEditor * Explore: updates LokiExploreQueryEditor snapshot due to timezone issues * Explore: updates LokiExploreExtraField to export both functional component and a version using memo * Explore: updates LokiExploreQueryEditor to export both functional component and memoized function * Explore: updates LokiExploreQueryEditor - removes unnecessary react fragment * Explore: updates LokiExploreQueryEditor snapshot * Explore: adds LokiExploreQueryEditor tests for different explore mode cases * Explore: fixes Loki datasource and result transformer * Explore: updates LokiExploreQueryEditor snapshot * Explore: updates LokiExploreQueryEditor tests and test setup * Explore: updates LokiExploreQueryEditor - refactors component * Explore: updates LokiExploreQueryEditor to use default import from LokiExploreExtraField * Explore: updates LokiExploreQueryEditor snapshot * Explore: fixes formatting * Explore: updates LokiExploreQueryEditor max lines change * Explore: updates LokiExploreQueryEditor tests checking ExtraFieldElement * Explore: adds mock loki datasource to LokiExploreQueryEditor * Explore: updates LokiExploreQueryEditor test mock - adds language provider * Explore: updates LokiExploreQueryEditor snapshot * Explore: updates Loki ResultTransformer to filter out rows on limit - logic to be moved into a component with new form styles * Explore: updates LokiExploreQueryEditor tests
210 lines
6.1 KiB
TypeScript
210 lines
6.1 KiB
TypeScript
// Libraries
|
|
import React, { PureComponent } from 'react';
|
|
import debounce from 'lodash/debounce';
|
|
import has from 'lodash/has';
|
|
import { hot } from 'react-hot-loader';
|
|
// @ts-ignore
|
|
import { connect } from 'react-redux';
|
|
// Components
|
|
import QueryEditor from './QueryEditor';
|
|
import { QueryRowActions } from './QueryRowActions';
|
|
// Actions
|
|
import { changeQuery, modifyQueries, runQueries } from './state/actions';
|
|
// Types
|
|
import { StoreState } from 'app/types';
|
|
import {
|
|
DataQuery,
|
|
DataSourceApi,
|
|
PanelData,
|
|
HistoryItem,
|
|
TimeRange,
|
|
AbsoluteTimeRange,
|
|
LoadingState,
|
|
ExploreMode,
|
|
} from '@grafana/data';
|
|
|
|
import { ExploreItemState, ExploreId } from 'app/types/explore';
|
|
import { Emitter } from 'app/core/utils/emitter';
|
|
import { highlightLogsExpressionAction, removeQueryRowAction } from './state/actionTypes';
|
|
|
|
interface PropsFromParent {
|
|
exploreId: ExploreId;
|
|
index: number;
|
|
exploreEvents: Emitter;
|
|
}
|
|
|
|
interface QueryRowProps extends PropsFromParent {
|
|
changeQuery: typeof changeQuery;
|
|
className?: string;
|
|
exploreId: ExploreId;
|
|
datasourceInstance: DataSourceApi;
|
|
highlightLogsExpressionAction: typeof highlightLogsExpressionAction;
|
|
history: HistoryItem[];
|
|
query: DataQuery;
|
|
modifyQueries: typeof modifyQueries;
|
|
range: TimeRange;
|
|
absoluteRange: AbsoluteTimeRange;
|
|
removeQueryRowAction: typeof removeQueryRowAction;
|
|
runQueries: typeof runQueries;
|
|
queryResponse: PanelData;
|
|
mode: ExploreMode;
|
|
latency: number;
|
|
}
|
|
|
|
interface QueryRowState {
|
|
textEditModeEnabled: boolean;
|
|
}
|
|
|
|
// Empty function to override blur execution on query field
|
|
const noopOnBlur = () => {};
|
|
|
|
export class QueryRow extends PureComponent<QueryRowProps, QueryRowState> {
|
|
state: QueryRowState = {
|
|
textEditModeEnabled: false,
|
|
};
|
|
|
|
onRunQuery = () => {
|
|
const { exploreId } = this.props;
|
|
this.props.runQueries(exploreId);
|
|
};
|
|
|
|
onChange = (query: DataQuery, override?: boolean) => {
|
|
const { datasourceInstance, exploreId, index } = this.props;
|
|
this.props.changeQuery(exploreId, query, index, override);
|
|
if (query && !override && datasourceInstance.getHighlighterExpression && index === 0) {
|
|
// Live preview of log search matches. Only use on first row for now
|
|
this.updateLogsHighlights(query);
|
|
}
|
|
};
|
|
|
|
componentWillUnmount() {
|
|
console.log('QueryRow will unmount');
|
|
}
|
|
|
|
onClickToggleDisabled = () => {
|
|
const { exploreId, index, query } = this.props;
|
|
const newQuery = {
|
|
...query,
|
|
hide: !query.hide,
|
|
};
|
|
this.props.changeQuery(exploreId, newQuery, index, true);
|
|
};
|
|
|
|
onClickRemoveButton = () => {
|
|
const { exploreId, index } = this.props;
|
|
this.props.removeQueryRowAction({ exploreId, index });
|
|
this.props.runQueries(exploreId);
|
|
};
|
|
|
|
onClickToggleEditorMode = () => {
|
|
this.setState({ textEditModeEnabled: !this.state.textEditModeEnabled });
|
|
};
|
|
|
|
updateLogsHighlights = debounce((value: DataQuery) => {
|
|
const { datasourceInstance } = this.props;
|
|
if (datasourceInstance.getHighlighterExpression) {
|
|
const { exploreId } = this.props;
|
|
const expressions = datasourceInstance.getHighlighterExpression(value);
|
|
this.props.highlightLogsExpressionAction({ exploreId, expressions });
|
|
}
|
|
}, 500);
|
|
|
|
render() {
|
|
const {
|
|
datasourceInstance,
|
|
history,
|
|
query,
|
|
exploreEvents,
|
|
range,
|
|
absoluteRange,
|
|
queryResponse,
|
|
mode,
|
|
latency,
|
|
} = this.props;
|
|
|
|
const canToggleEditorModes =
|
|
mode === ExploreMode.Metrics && has(datasourceInstance, 'components.QueryCtrl.prototype.toggleEditorMode');
|
|
const isNotStarted = queryResponse.state === LoadingState.NotStarted;
|
|
const queryErrors = queryResponse.error && queryResponse.error.refId === query.refId ? [queryResponse.error] : [];
|
|
let QueryField;
|
|
|
|
if (mode === ExploreMode.Metrics && datasourceInstance.components?.ExploreMetricsQueryField) {
|
|
QueryField = datasourceInstance.components.ExploreMetricsQueryField;
|
|
} else if (mode === ExploreMode.Logs && datasourceInstance.components?.ExploreLogsQueryField) {
|
|
QueryField = datasourceInstance.components.ExploreLogsQueryField;
|
|
} else {
|
|
QueryField = datasourceInstance.components?.ExploreQueryField;
|
|
}
|
|
|
|
return (
|
|
<div className="query-row">
|
|
<div className="query-row-field flex-shrink-1">
|
|
{QueryField ? (
|
|
<QueryField
|
|
datasource={datasourceInstance}
|
|
query={query}
|
|
history={history}
|
|
onRunQuery={this.onRunQuery}
|
|
onBlur={noopOnBlur}
|
|
onChange={this.onChange}
|
|
data={queryResponse}
|
|
absoluteRange={absoluteRange}
|
|
exploreMode={mode}
|
|
/>
|
|
) : (
|
|
<QueryEditor
|
|
error={queryErrors}
|
|
datasource={datasourceInstance}
|
|
onQueryChange={this.onChange}
|
|
onExecuteQuery={this.onRunQuery}
|
|
initialQuery={query}
|
|
exploreEvents={exploreEvents}
|
|
range={range}
|
|
textEditModeEnabled={this.state.textEditModeEnabled}
|
|
/>
|
|
)}
|
|
</div>
|
|
<QueryRowActions
|
|
canToggleEditorModes={canToggleEditorModes}
|
|
isDisabled={query.hide}
|
|
isNotStarted={isNotStarted}
|
|
latency={latency}
|
|
onClickToggleEditorMode={this.onClickToggleEditorMode}
|
|
onClickToggleDisabled={this.onClickToggleDisabled}
|
|
onClickRemoveButton={this.onClickRemoveButton}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
|
|
function mapStateToProps(state: StoreState, { exploreId, index }: QueryRowProps) {
|
|
const explore = state.explore;
|
|
const item: ExploreItemState = explore[exploreId];
|
|
const { datasourceInstance, history, queries, range, absoluteRange, mode, queryResponse, latency } = item;
|
|
const query = queries[index];
|
|
|
|
return {
|
|
datasourceInstance,
|
|
history,
|
|
query,
|
|
range,
|
|
absoluteRange,
|
|
queryResponse,
|
|
mode,
|
|
latency,
|
|
};
|
|
}
|
|
|
|
const mapDispatchToProps = {
|
|
changeQuery,
|
|
highlightLogsExpressionAction,
|
|
modifyQueries,
|
|
removeQueryRowAction,
|
|
runQueries,
|
|
};
|
|
|
|
export default hot(module)(
|
|
connect(mapStateToProps, mapDispatchToProps)(QueryRow) as React.ComponentType<PropsFromParent>
|
|
);
|