mirror of
https://github.com/grafana/grafana.git
synced 2025-09-23 18:52:33 +08:00

* Create query type switcher * Add and update tests * Add handling in datasource * Refactor * Update tests, when checking higlighting, suppy logs * Remove both option as redundant * Add tooltip, remove old comments * Remove unused importts * Remove console.log, update width * Update public/app/plugins/datasource/loki/components/LokiExploreExtraField.tsx Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Update tests * Prettier fixes * Fix test Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>
84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
// Libraries
|
|
import React, { memo } from 'react';
|
|
import _ from 'lodash';
|
|
|
|
// Types
|
|
import { ExploreQueryFieldProps } from '@grafana/data';
|
|
import { LokiDatasource } from '../datasource';
|
|
import { LokiQuery, LokiOptions } from '../types';
|
|
import { LokiQueryField } from './LokiQueryField';
|
|
import LokiExploreExtraField from './LokiExploreExtraField';
|
|
|
|
type Props = ExploreQueryFieldProps<LokiDatasource, LokiQuery, LokiOptions>;
|
|
|
|
export function LokiExploreQueryEditor(props: Props) {
|
|
const { range, query, data, datasource, history, onChange, onRunQuery } = props;
|
|
|
|
function onChangeQueryLimit(value: string) {
|
|
const { query, onChange } = props;
|
|
const nextQuery = { ...query, maxLines: preprocessMaxLines(value) };
|
|
onChange(nextQuery);
|
|
}
|
|
|
|
function onQueryTypeChange(value: string) {
|
|
const { query, onChange } = props;
|
|
let nextQuery;
|
|
if (value === 'instant') {
|
|
nextQuery = { ...query, instant: true, range: false };
|
|
} else {
|
|
nextQuery = { ...query, instant: false, range: true };
|
|
}
|
|
onChange(nextQuery);
|
|
}
|
|
|
|
function preprocessMaxLines(value: string): number {
|
|
if (value.length === 0) {
|
|
// empty input - falls back to dataSource.maxLines limit
|
|
return NaN;
|
|
} else if (value.length > 0 && (isNaN(+value) || +value < 0)) {
|
|
// input with at least 1 character and that is either incorrect (value in the input field is not a number) or negative
|
|
// falls back to the limit of 0 lines
|
|
return 0;
|
|
} else {
|
|
// default case - correct input
|
|
return +value;
|
|
}
|
|
}
|
|
|
|
function onMaxLinesChange(e: React.SyntheticEvent<HTMLInputElement>) {
|
|
if (query.maxLines !== preprocessMaxLines(e.currentTarget.value)) {
|
|
onChangeQueryLimit(e.currentTarget.value);
|
|
}
|
|
}
|
|
|
|
function onReturnKeyDown(e: React.KeyboardEvent<HTMLInputElement>) {
|
|
if (e.key === 'Enter') {
|
|
onRunQuery();
|
|
}
|
|
}
|
|
|
|
return (
|
|
<LokiQueryField
|
|
datasource={datasource}
|
|
query={query}
|
|
onChange={onChange}
|
|
onBlur={() => {}}
|
|
onRunQuery={onRunQuery}
|
|
history={history}
|
|
data={data}
|
|
range={range}
|
|
ExtraFieldElement={
|
|
<LokiExploreExtraField
|
|
queryType={query.instant ? 'instant' : 'range'}
|
|
lineLimitValue={query?.maxLines?.toString() || ''}
|
|
onQueryTypeChange={onQueryTypeChange}
|
|
onLineLimitChange={onMaxLinesChange}
|
|
onKeyDownFunc={onReturnKeyDown}
|
|
/>
|
|
}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export default memo(LokiExploreQueryEditor);
|