2019-01-18 11:59:32 -06:00
|
|
|
// Libraries
|
2018-10-30 10:14:01 -05:00
|
|
|
import React from 'react';
|
|
|
|
import Cascader from 'rc-cascader';
|
|
|
|
import PluginPrism from 'slate-prism';
|
|
|
|
import Prism from 'prismjs';
|
|
|
|
|
2019-01-18 11:59:32 -06:00
|
|
|
// Components
|
|
|
|
import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField';
|
2018-10-30 10:14:01 -05:00
|
|
|
|
2019-01-18 11:59:32 -06:00
|
|
|
// Utils & Services
|
2018-10-30 10:14:01 -05:00
|
|
|
// dom also includes Element polyfills
|
|
|
|
import { getNextCharacter, getPreviousCousin } from 'app/features/explore/utils/dom';
|
|
|
|
import BracesPlugin from 'app/features/explore/slate-plugins/braces';
|
|
|
|
import RunnerPlugin from 'app/features/explore/slate-plugins/runner';
|
2019-01-18 11:59:32 -06:00
|
|
|
|
|
|
|
// Types
|
|
|
|
import { LokiQuery } from '../types';
|
2019-02-01 04:55:01 -06:00
|
|
|
import { TypeaheadOutput, HistoryItem } from 'app/types/explore';
|
2019-01-29 05:11:48 -06:00
|
|
|
import { makePromiseCancelable, CancelablePromise } from 'app/core/utils/CancelablePromise';
|
2019-02-01 04:55:01 -06:00
|
|
|
import { ExploreDataSourceApi, ExploreQueryFieldProps } from '@grafana/ui';
|
2018-10-30 10:14:01 -05:00
|
|
|
|
|
|
|
const PRISM_SYNTAX = 'promql';
|
|
|
|
|
2018-12-31 06:38:13 -06:00
|
|
|
function getChooserText(hasSytax, hasLogLabels) {
|
|
|
|
if (!hasSytax) {
|
|
|
|
return 'Loading labels...';
|
|
|
|
}
|
|
|
|
if (!hasLogLabels) {
|
|
|
|
return '(No labels found)';
|
|
|
|
}
|
|
|
|
return 'Log labels';
|
|
|
|
}
|
2018-12-09 16:39:25 -06:00
|
|
|
|
2018-10-30 10:14:01 -05:00
|
|
|
export function willApplySuggestion(suggestion: string, { typeaheadContext, typeaheadText }: QueryFieldState): string {
|
|
|
|
// Modify suggestion based on context
|
|
|
|
switch (typeaheadContext) {
|
|
|
|
case 'context-labels': {
|
|
|
|
const nextChar = getNextCharacter();
|
|
|
|
if (!nextChar || nextChar === '}' || nextChar === ',') {
|
|
|
|
suggestion += '=';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'context-label-values': {
|
|
|
|
// Always add quotes and remove existing ones instead
|
|
|
|
if (!typeaheadText.match(/^(!?=~?"|")/)) {
|
|
|
|
suggestion = `"${suggestion}`;
|
|
|
|
}
|
|
|
|
if (getNextCharacter() !== '"') {
|
|
|
|
suggestion = `${suggestion}"`;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return suggestion;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CascaderOption {
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
children?: CascaderOption[];
|
|
|
|
disabled?: boolean;
|
|
|
|
}
|
|
|
|
|
2019-02-01 04:55:01 -06:00
|
|
|
interface LokiQueryFieldProps extends ExploreQueryFieldProps<ExploreDataSourceApi, LokiQuery> {
|
|
|
|
history: HistoryItem[];
|
2018-10-30 10:14:01 -05:00
|
|
|
}
|
|
|
|
|
2018-12-05 16:13:57 -06:00
|
|
|
interface LokiQueryFieldState {
|
2018-10-30 10:14:01 -05:00
|
|
|
logLabelOptions: any[];
|
|
|
|
syntaxLoaded: boolean;
|
|
|
|
}
|
|
|
|
|
2019-01-23 10:44:22 -06:00
|
|
|
export class LokiQueryField extends React.PureComponent<LokiQueryFieldProps, LokiQueryFieldState> {
|
2018-10-30 10:14:01 -05:00
|
|
|
plugins: any[];
|
2018-12-09 11:44:59 -06:00
|
|
|
pluginsSearch: any[];
|
2018-10-30 10:14:01 -05:00
|
|
|
languageProvider: any;
|
2018-12-09 11:44:59 -06:00
|
|
|
modifiedSearch: string;
|
|
|
|
modifiedQuery: string;
|
2019-01-29 05:11:48 -06:00
|
|
|
languageProviderInitializationPromise: CancelablePromise<any>;
|
2018-10-30 10:14:01 -05:00
|
|
|
|
2018-12-05 16:13:57 -06:00
|
|
|
constructor(props: LokiQueryFieldProps, context) {
|
2018-10-30 10:14:01 -05:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
if (props.datasource.languageProvider) {
|
|
|
|
this.languageProvider = props.datasource.languageProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.plugins = [
|
|
|
|
BracesPlugin(),
|
2019-02-01 04:55:01 -06:00
|
|
|
RunnerPlugin({ handler: props.onExecuteQuery }),
|
2018-10-30 10:14:01 -05:00
|
|
|
PluginPrism({
|
|
|
|
onlyIn: node => node.type === 'code_block',
|
|
|
|
getSyntax: node => 'promql',
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
2019-02-01 04:55:01 -06:00
|
|
|
this.pluginsSearch = [RunnerPlugin({ handler: props.onExecuteQuery })];
|
2018-12-09 11:44:59 -06:00
|
|
|
|
2018-10-30 10:14:01 -05:00
|
|
|
this.state = {
|
|
|
|
logLabelOptions: [],
|
|
|
|
syntaxLoaded: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (this.languageProvider) {
|
2019-01-29 05:11:48 -06:00
|
|
|
this.languageProviderInitializationPromise = makePromiseCancelable(this.languageProvider.start());
|
|
|
|
|
|
|
|
this.languageProviderInitializationPromise.promise
|
2018-10-31 11:48:36 -05:00
|
|
|
.then(remaining => {
|
2018-11-01 03:36:09 -05:00
|
|
|
remaining.map(task => task.then(this.onUpdateLanguage).catch(() => {}));
|
2018-10-31 11:48:36 -05:00
|
|
|
})
|
2019-01-29 05:11:48 -06:00
|
|
|
.then(() => this.onUpdateLanguage())
|
|
|
|
.catch(({ isCanceled }) => {
|
|
|
|
if (isCanceled) {
|
|
|
|
console.warn('LokiQueryField has unmounted, language provider intialization was canceled');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.languageProviderInitializationPromise) {
|
|
|
|
this.languageProviderInitializationPromise.cancel();
|
2018-10-30 10:14:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 11:48:36 -05:00
|
|
|
loadOptions = (selectedOptions: CascaderOption[]) => {
|
|
|
|
const targetOption = selectedOptions[selectedOptions.length - 1];
|
|
|
|
|
|
|
|
this.setState(state => {
|
|
|
|
const nextOptions = state.logLabelOptions.map(option => {
|
|
|
|
if (option.value === targetOption.value) {
|
|
|
|
return {
|
|
|
|
...option,
|
|
|
|
loading: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return option;
|
|
|
|
});
|
|
|
|
return { logLabelOptions: nextOptions };
|
|
|
|
});
|
|
|
|
|
|
|
|
this.languageProvider
|
|
|
|
.fetchLabelValues(targetOption.value)
|
2018-11-01 03:36:09 -05:00
|
|
|
.then(this.onUpdateLanguage)
|
2018-10-31 11:48:36 -05:00
|
|
|
.catch(() => {});
|
|
|
|
};
|
|
|
|
|
2018-10-30 10:14:01 -05:00
|
|
|
onChangeLogLabels = (values: string[], selectedOptions: CascaderOption[]) => {
|
2018-10-31 11:48:36 -05:00
|
|
|
if (selectedOptions.length === 2) {
|
2018-10-30 10:14:01 -05:00
|
|
|
const key = selectedOptions[0].value;
|
|
|
|
const value = selectedOptions[1].value;
|
2018-10-31 11:48:36 -05:00
|
|
|
const query = `{${key}="${value}"}`;
|
|
|
|
this.onChangeQuery(query, true);
|
2018-10-30 10:14:01 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onChangeQuery = (value: string, override?: boolean) => {
|
2018-12-09 11:44:59 -06:00
|
|
|
// Send text change to parent
|
2019-02-04 23:19:40 -06:00
|
|
|
const { query, onQueryChange, onExecuteQuery } = this.props;
|
2018-12-09 11:44:59 -06:00
|
|
|
if (onQueryChange) {
|
2019-02-04 23:19:40 -06:00
|
|
|
const nextQuery = { ...query, expr: value };
|
|
|
|
onQueryChange(nextQuery);
|
2019-02-01 04:55:01 -06:00
|
|
|
|
|
|
|
if (override && onExecuteQuery) {
|
|
|
|
onExecuteQuery();
|
|
|
|
}
|
2018-10-30 10:14:01 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onClickHintFix = () => {
|
2019-02-01 05:54:16 -06:00
|
|
|
const { hint, onExecuteHint } = this.props;
|
|
|
|
if (onExecuteHint && hint && hint.fix) {
|
|
|
|
onExecuteHint(hint.fix.action);
|
|
|
|
}
|
2018-10-30 10:14:01 -05:00
|
|
|
};
|
|
|
|
|
2018-11-01 03:36:09 -05:00
|
|
|
onUpdateLanguage = () => {
|
2018-10-30 10:14:01 -05:00
|
|
|
Prism.languages[PRISM_SYNTAX] = this.languageProvider.getSyntax();
|
|
|
|
const { logLabelOptions } = this.languageProvider;
|
|
|
|
this.setState({
|
|
|
|
logLabelOptions,
|
|
|
|
syntaxLoaded: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onTypeahead = (typeahead: TypeaheadInput): TypeaheadOutput => {
|
|
|
|
if (!this.languageProvider) {
|
|
|
|
return { suggestions: [] };
|
|
|
|
}
|
|
|
|
|
|
|
|
const { history } = this.props;
|
|
|
|
const { prefix, text, value, wrapperNode } = typeahead;
|
|
|
|
|
|
|
|
// Get DOM-dependent context
|
|
|
|
const wrapperClasses = Array.from(wrapperNode.classList);
|
|
|
|
const labelKeyNode = getPreviousCousin(wrapperNode, '.attr-name');
|
|
|
|
const labelKey = labelKeyNode && labelKeyNode.textContent;
|
|
|
|
const nextChar = getNextCharacter();
|
|
|
|
|
|
|
|
const result = this.languageProvider.provideCompletionItems(
|
|
|
|
{ text, value, prefix, wrapperClasses, labelKey },
|
|
|
|
{ history }
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log('handleTypeahead', wrapperClasses, text, prefix, nextChar, labelKey, result.context);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2019-02-04 23:19:40 -06:00
|
|
|
const { error, hint, query } = this.props;
|
2018-10-30 10:14:01 -05:00
|
|
|
const { logLabelOptions, syntaxLoaded } = this.state;
|
|
|
|
const cleanText = this.languageProvider ? this.languageProvider.cleanText : undefined;
|
2018-12-31 06:38:13 -06:00
|
|
|
const hasLogLabels = logLabelOptions && logLabelOptions.length > 0;
|
|
|
|
const chooserText = getChooserText(syntaxLoaded, hasLogLabels);
|
2018-10-30 10:14:01 -05:00
|
|
|
|
|
|
|
return (
|
2019-01-29 01:14:07 -06:00
|
|
|
<>
|
|
|
|
<div className="gf-form-inline">
|
|
|
|
<div className="gf-form">
|
|
|
|
<Cascader options={logLabelOptions} onChange={this.onChangeLogLabels} loadData={this.loadOptions}>
|
|
|
|
<button className="gf-form-label gf-form-label--btn" disabled={!syntaxLoaded}>
|
|
|
|
{chooserText} <i className="fa fa-caret-down" />
|
|
|
|
</button>
|
|
|
|
</Cascader>
|
|
|
|
</div>
|
|
|
|
<div className="gf-form gf-form--grow">
|
|
|
|
<QueryField
|
|
|
|
additionalPlugins={this.plugins}
|
|
|
|
cleanText={cleanText}
|
2019-02-04 23:19:40 -06:00
|
|
|
initialQuery={query.expr}
|
2019-01-29 01:14:07 -06:00
|
|
|
onTypeahead={this.onTypeahead}
|
|
|
|
onWillApplySuggestion={willApplySuggestion}
|
2019-02-01 04:55:01 -06:00
|
|
|
onQueryChange={this.onChangeQuery}
|
|
|
|
onExecuteQuery={this.props.onExecuteQuery}
|
2019-01-29 01:14:07 -06:00
|
|
|
placeholder="Enter a Loki query"
|
|
|
|
portalOrigin="loki"
|
|
|
|
syntaxLoaded={syntaxLoaded}
|
|
|
|
/>
|
|
|
|
</div>
|
2018-10-30 10:14:01 -05:00
|
|
|
</div>
|
2019-01-29 01:14:07 -06:00
|
|
|
<div>
|
2018-10-30 10:14:01 -05:00
|
|
|
{error ? <div className="prom-query-field-info text-error">{error}</div> : null}
|
|
|
|
{hint ? (
|
|
|
|
<div className="prom-query-field-info text-warning">
|
|
|
|
{hint.label}{' '}
|
|
|
|
{hint.fix ? (
|
|
|
|
<a className="text-link muted" onClick={this.onClickHintFix}>
|
|
|
|
{hint.fix.label}
|
|
|
|
</a>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2019-01-29 01:14:07 -06:00
|
|
|
</>
|
2018-10-30 10:14:01 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 16:13:57 -06:00
|
|
|
export default LokiQueryField;
|