2019-03-25 06:08:28 -05:00
|
|
|
// Libraries
|
|
|
|
import React from 'react';
|
2019-04-01 00:38:00 -05:00
|
|
|
// @ts-ignore
|
2019-03-25 06:08:28 -05:00
|
|
|
import Cascader from 'rc-cascader';
|
2019-04-01 00:38:00 -05:00
|
|
|
// @ts-ignore
|
2019-03-25 06:08:28 -05:00
|
|
|
import PluginPrism from 'slate-prism';
|
|
|
|
// Components
|
|
|
|
import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField';
|
|
|
|
// Utils & Services
|
|
|
|
// dom also includes Element polyfills
|
|
|
|
import BracesPlugin from 'app/features/explore/slate-plugins/braces';
|
|
|
|
// Types
|
|
|
|
import { LokiQuery } from '../types';
|
|
|
|
import { TypeaheadOutput, HistoryItem } from 'app/types/explore';
|
2019-07-08 12:02:16 -05:00
|
|
|
import { DataSourceApi, ExploreQueryFieldProps, DataSourceStatus, DOMUtil } from '@grafana/ui';
|
2019-07-08 10:14:48 -05:00
|
|
|
import { AbsoluteTimeRange } from '@grafana/data';
|
2019-03-25 06:08:28 -05:00
|
|
|
|
2019-04-04 11:30:15 -05:00
|
|
|
function getChooserText(hasSyntax: boolean, hasLogLabels: boolean, datasourceStatus: DataSourceStatus) {
|
|
|
|
if (datasourceStatus === DataSourceStatus.Disconnected) {
|
2019-04-01 00:38:00 -05:00
|
|
|
return '(Disconnected)';
|
|
|
|
}
|
|
|
|
if (!hasSyntax) {
|
2019-03-25 06:08:28 -05:00
|
|
|
return 'Loading labels...';
|
|
|
|
}
|
|
|
|
if (!hasLogLabels) {
|
|
|
|
return '(No labels found)';
|
|
|
|
}
|
|
|
|
return 'Log labels';
|
|
|
|
}
|
|
|
|
|
|
|
|
function willApplySuggestion(suggestion: string, { typeaheadContext, typeaheadText }: QueryFieldState): string {
|
|
|
|
// Modify suggestion based on context
|
|
|
|
switch (typeaheadContext) {
|
|
|
|
case 'context-labels': {
|
2019-07-08 12:02:16 -05:00
|
|
|
const nextChar = DOMUtil.getNextCharacter();
|
2019-03-25 06:08:28 -05:00
|
|
|
if (!nextChar || nextChar === '}' || nextChar === ',') {
|
|
|
|
suggestion += '=';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'context-label-values': {
|
|
|
|
// Always add quotes and remove existing ones instead
|
|
|
|
if (!typeaheadText.match(/^(!?=~?"|")/)) {
|
|
|
|
suggestion = `"${suggestion}`;
|
|
|
|
}
|
2019-07-08 12:02:16 -05:00
|
|
|
if (DOMUtil.getNextCharacter() !== '"') {
|
2019-03-25 06:08:28 -05:00
|
|
|
suggestion = `${suggestion}"`;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return suggestion;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface CascaderOption {
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
children?: CascaderOption[];
|
|
|
|
disabled?: boolean;
|
|
|
|
}
|
|
|
|
|
2019-06-04 03:30:07 -05:00
|
|
|
export interface LokiQueryFieldFormProps extends ExploreQueryFieldProps<DataSourceApi<LokiQuery>, LokiQuery> {
|
2019-03-25 06:08:28 -05:00
|
|
|
history: HistoryItem[];
|
|
|
|
syntax: any;
|
|
|
|
logLabelOptions: any[];
|
|
|
|
syntaxLoaded: any;
|
2019-07-08 10:14:48 -05:00
|
|
|
absoluteRange: AbsoluteTimeRange;
|
2019-03-25 06:08:28 -05:00
|
|
|
onLoadOptions: (selectedOptions: CascaderOption[]) => void;
|
|
|
|
onLabelsRefresh?: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LokiQueryFieldForm extends React.PureComponent<LokiQueryFieldFormProps> {
|
|
|
|
plugins: any[];
|
|
|
|
modifiedSearch: string;
|
|
|
|
modifiedQuery: string;
|
|
|
|
|
2019-04-01 00:38:00 -05:00
|
|
|
constructor(props: LokiQueryFieldFormProps, context: React.Context<any>) {
|
2019-03-25 06:08:28 -05:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.plugins = [
|
|
|
|
BracesPlugin(),
|
|
|
|
PluginPrism({
|
2019-04-01 00:38:00 -05:00
|
|
|
onlyIn: (node: any) => node.type === 'code_block',
|
|
|
|
getSyntax: (node: any) => 'promql',
|
2019-03-25 06:08:28 -05:00
|
|
|
}),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
loadOptions = (selectedOptions: CascaderOption[]) => {
|
|
|
|
this.props.onLoadOptions(selectedOptions);
|
|
|
|
};
|
|
|
|
|
|
|
|
onChangeLogLabels = (values: string[], selectedOptions: CascaderOption[]) => {
|
|
|
|
if (selectedOptions.length === 2) {
|
|
|
|
const key = selectedOptions[0].value;
|
|
|
|
const value = selectedOptions[1].value;
|
|
|
|
const query = `{${key}="${value}"}`;
|
|
|
|
this.onChangeQuery(query, true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onChangeQuery = (value: string, override?: boolean) => {
|
|
|
|
// Send text change to parent
|
2019-05-10 07:00:39 -05:00
|
|
|
const { query, onChange, onRunQuery } = this.props;
|
|
|
|
if (onChange) {
|
2019-03-25 06:08:28 -05:00
|
|
|
const nextQuery = { ...query, expr: value };
|
2019-05-10 07:00:39 -05:00
|
|
|
onChange(nextQuery);
|
2019-03-25 06:08:28 -05:00
|
|
|
|
2019-05-10 07:00:39 -05:00
|
|
|
if (override && onRunQuery) {
|
|
|
|
onRunQuery();
|
2019-03-25 06:08:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onTypeahead = (typeahead: TypeaheadInput): TypeaheadOutput => {
|
|
|
|
const { datasource } = this.props;
|
|
|
|
if (!datasource.languageProvider) {
|
|
|
|
return { suggestions: [] };
|
|
|
|
}
|
|
|
|
|
2019-07-08 10:14:48 -05:00
|
|
|
const { history, absoluteRange } = this.props;
|
2019-03-25 06:08:28 -05:00
|
|
|
const { prefix, text, value, wrapperNode } = typeahead;
|
|
|
|
|
|
|
|
// Get DOM-dependent context
|
|
|
|
const wrapperClasses = Array.from(wrapperNode.classList);
|
2019-07-08 12:02:16 -05:00
|
|
|
const labelKeyNode = DOMUtil.getPreviousCousin(wrapperNode, '.attr-name');
|
2019-03-25 06:08:28 -05:00
|
|
|
const labelKey = labelKeyNode && labelKeyNode.textContent;
|
2019-07-08 12:02:16 -05:00
|
|
|
const nextChar = DOMUtil.getNextCharacter();
|
2019-03-25 06:08:28 -05:00
|
|
|
|
|
|
|
const result = datasource.languageProvider.provideCompletionItems(
|
|
|
|
{ text, value, prefix, wrapperClasses, labelKey },
|
2019-07-08 10:14:48 -05:00
|
|
|
{ history, absoluteRange }
|
2019-03-25 06:08:28 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
console.log('handleTypeahead', wrapperClasses, text, prefix, nextChar, labelKey, result.context);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
2019-05-10 07:00:39 -05:00
|
|
|
queryResponse,
|
2019-03-25 06:08:28 -05:00
|
|
|
query,
|
|
|
|
syntaxLoaded,
|
|
|
|
logLabelOptions,
|
|
|
|
onLoadOptions,
|
|
|
|
onLabelsRefresh,
|
|
|
|
datasource,
|
2019-04-01 00:38:00 -05:00
|
|
|
datasourceStatus,
|
2019-03-25 06:08:28 -05:00
|
|
|
} = this.props;
|
|
|
|
const cleanText = datasource.languageProvider ? datasource.languageProvider.cleanText : undefined;
|
|
|
|
const hasLogLabels = logLabelOptions && logLabelOptions.length > 0;
|
2019-04-01 00:38:00 -05:00
|
|
|
const chooserText = getChooserText(syntaxLoaded, hasLogLabels, datasourceStatus);
|
2019-04-04 11:30:15 -05:00
|
|
|
const buttonDisabled = !syntaxLoaded || datasourceStatus === DataSourceStatus.Disconnected;
|
2019-09-03 02:55:20 -05:00
|
|
|
const showError = queryResponse && queryResponse.error && queryResponse.error.refId === query.refId;
|
2019-03-25 06:08:28 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="gf-form-inline">
|
|
|
|
<div className="gf-form">
|
|
|
|
<Cascader
|
|
|
|
options={logLabelOptions}
|
|
|
|
onChange={this.onChangeLogLabels}
|
|
|
|
loadData={onLoadOptions}
|
2019-04-01 00:38:00 -05:00
|
|
|
onPopupVisibleChange={(isVisible: boolean) => {
|
2019-03-25 06:08:28 -05:00
|
|
|
if (isVisible && onLabelsRefresh) {
|
|
|
|
onLabelsRefresh();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2019-04-01 00:38:00 -05:00
|
|
|
<button className="gf-form-label gf-form-label--btn" disabled={buttonDisabled}>
|
2019-03-25 06:08:28 -05:00
|
|
|
{chooserText} <i className="fa fa-caret-down" />
|
|
|
|
</button>
|
|
|
|
</Cascader>
|
|
|
|
</div>
|
|
|
|
<div className="gf-form gf-form--grow">
|
|
|
|
<QueryField
|
|
|
|
additionalPlugins={this.plugins}
|
|
|
|
cleanText={cleanText}
|
|
|
|
initialQuery={query.expr}
|
|
|
|
onTypeahead={this.onTypeahead}
|
|
|
|
onWillApplySuggestion={willApplySuggestion}
|
2019-05-10 07:00:39 -05:00
|
|
|
onChange={this.onChangeQuery}
|
|
|
|
onRunQuery={this.props.onRunQuery}
|
2019-03-25 06:08:28 -05:00
|
|
|
placeholder="Enter a Loki query"
|
|
|
|
portalOrigin="loki"
|
|
|
|
syntaxLoaded={syntaxLoaded}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div>
|
2019-09-03 02:55:20 -05:00
|
|
|
{showError ? <div className="prom-query-field-info text-error">{queryResponse.error.message}</div> : null}
|
2019-03-25 06:08:28 -05:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|