123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- import { css } from '@emotion/css';
- import { debounce, intersectionBy, unionBy } from 'lodash';
- import { LanguageMap, languages as prismLanguages } from 'prismjs';
- import React, { ReactNode } from 'react';
- import { Editor, Node, Plugin } from 'slate';
- import { AbsoluteTimeRange, QueryEditorProps, SelectableValue } from '@grafana/data';
- import {
- BracesPlugin,
- LegacyForms,
- MultiSelect,
- QueryField,
- SlatePrism,
- TypeaheadInput,
- TypeaheadOutput,
- } from '@grafana/ui';
- import { InputActionMeta } from '@grafana/ui/src/components/Select/types';
- import { notifyApp } from 'app/core/actions';
- import { createErrorNotification } from 'app/core/copy/appNotification';
- import { dispatch } from 'app/store/store';
- import { ExploreId } from 'app/types';
- // Utils & Services
- // dom also includes Element polyfills
- import { CloudWatchDatasource } from '../datasource';
- import { CloudWatchLanguageProvider } from '../language_provider';
- import syntax from '../syntax';
- import { CloudWatchJsonData, CloudWatchLogsQuery, CloudWatchQuery } from '../types';
- import { getStatsGroups } from '../utils/query/getStatsGroups';
- import { appendTemplateVariables } from '../utils/utils';
- import QueryHeader from './QueryHeader';
- export interface CloudWatchLogsQueryFieldProps
- extends QueryEditorProps<CloudWatchDatasource, CloudWatchQuery, CloudWatchJsonData> {
- absoluteRange: AbsoluteTimeRange;
- onLabelsRefresh?: () => void;
- ExtraFieldElement?: ReactNode;
- exploreId: ExploreId;
- allowCustomValue?: boolean;
- }
- const containerClass = css`
- flex-grow: 1;
- min-height: 35px;
- `;
- const rowGap = css`
- gap: 3px;
- `;
- interface State {
- selectedLogGroups: Array<SelectableValue<string>>;
- availableLogGroups: Array<SelectableValue<string>>;
- loadingLogGroups: boolean;
- invalidLogGroups: boolean;
- hint:
- | {
- message: string;
- fix: {
- label: string;
- action: () => void;
- };
- }
- | undefined;
- }
- export class CloudWatchLogsQueryField extends React.PureComponent<CloudWatchLogsQueryFieldProps, State> {
- state: State = {
- selectedLogGroups:
- (this.props.query as CloudWatchLogsQuery).logGroupNames?.map((logGroup) => ({
- value: logGroup,
- label: logGroup,
- })) ?? [],
- availableLogGroups: [],
- invalidLogGroups: false,
- loadingLogGroups: false,
- hint: undefined,
- };
- plugins: Plugin[];
- constructor(props: CloudWatchLogsQueryFieldProps, context: React.Context<any>) {
- super(props, context);
- this.plugins = [
- BracesPlugin(),
- SlatePrism(
- {
- onlyIn: (node: Node) => node.object === 'block' && node.type === 'code_block',
- getSyntax: (node: Node) => 'cloudwatch',
- },
- { ...(prismLanguages as LanguageMap), cloudwatch: syntax }
- ),
- ];
- }
- fetchLogGroupOptions = async (region: string, logGroupNamePrefix?: string) => {
- try {
- const logGroups: string[] = await this.props.datasource.describeLogGroups({
- refId: this.props.query.refId,
- region,
- logGroupNamePrefix,
- });
- return logGroups.map((logGroup) => ({
- value: logGroup,
- label: logGroup,
- }));
- } catch (err) {
- let errMessage = 'unknown error';
- if (typeof err !== 'string') {
- try {
- errMessage = JSON.stringify(err);
- } catch (e) {}
- } else {
- errMessage = err;
- }
- dispatch(notifyApp(createErrorNotification(errMessage)));
- return [];
- }
- };
- onLogGroupSearch = (searchTerm: string, region: string, actionMeta: InputActionMeta) => {
- if (actionMeta.action !== 'input-change') {
- return Promise.resolve();
- }
- // No need to fetch matching log groups if the search term isn't valid
- // This is also useful for preventing searches when a user is typing out a log group with template vars
- // See https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_LogGroup.html for the source of the pattern below
- const logGroupNamePattern = /^[\.\-_/#A-Za-z0-9]+$/;
- if (!logGroupNamePattern.test(searchTerm)) {
- return Promise.resolve();
- }
- this.setState({
- loadingLogGroups: true,
- });
- return this.fetchLogGroupOptions(region, searchTerm)
- .then((matchingLogGroups) => {
- this.setState((state) => ({
- availableLogGroups: unionBy(state.availableLogGroups, matchingLogGroups, 'value'),
- }));
- })
- .finally(() => {
- this.setState({
- loadingLogGroups: false,
- });
- });
- };
- onLogGroupSearchDebounced = debounce(this.onLogGroupSearch, 300);
- componentDidMount = () => {
- const { query, onChange } = this.props;
- this.setState({
- loadingLogGroups: true,
- });
- query.region &&
- this.fetchLogGroupOptions(query.region).then((logGroups) => {
- this.setState((state) => {
- const selectedLogGroups = state.selectedLogGroups;
- if (onChange) {
- const nextQuery = {
- ...query,
- logGroupNames: selectedLogGroups.map((group) => group.value!),
- };
- onChange(nextQuery);
- }
- return {
- loadingLogGroups: false,
- availableLogGroups: logGroups,
- selectedLogGroups,
- };
- });
- });
- };
- onChangeQuery = (value: string) => {
- // Send text change to parent
- const { query, onChange } = this.props;
- const { selectedLogGroups } = this.state;
- if (onChange) {
- const nextQuery = {
- ...query,
- expression: value,
- logGroupNames: selectedLogGroups?.map((logGroupName) => logGroupName.value!) ?? [],
- statsGroups: getStatsGroups(value),
- };
- onChange(nextQuery);
- }
- };
- setSelectedLogGroups = (selectedLogGroups: Array<SelectableValue<string>>) => {
- this.setState({
- selectedLogGroups,
- });
- const { onChange, query } = this.props;
- onChange?.({
- ...(query as CloudWatchLogsQuery),
- logGroupNames: selectedLogGroups.map((logGroupName) => logGroupName.value!) ?? [],
- });
- };
- setCustomLogGroups = (v: string) => {
- const customLogGroup: SelectableValue<string> = { value: v, label: v };
- const selectedLogGroups = [...this.state.selectedLogGroups, customLogGroup];
- this.setSelectedLogGroups(selectedLogGroups);
- };
- onRegionChange = async (v: string) => {
- this.setState({
- loadingLogGroups: true,
- });
- const logGroups = await this.fetchLogGroupOptions(v);
- this.setState((state) => {
- const selectedLogGroups = intersectionBy(state.selectedLogGroups, logGroups, 'value');
- const { onChange, query } = this.props;
- if (onChange) {
- const nextQuery = {
- ...query,
- logGroupNames: selectedLogGroups.map((group) => group.value!),
- };
- onChange(nextQuery);
- }
- return {
- availableLogGroups: logGroups,
- selectedLogGroups: selectedLogGroups,
- loadingLogGroups: false,
- };
- });
- };
- onTypeahead = async (typeahead: TypeaheadInput): Promise<TypeaheadOutput> => {
- const { datasource, query } = this.props;
- const { selectedLogGroups } = this.state;
- if (!datasource.languageProvider) {
- return { suggestions: [] };
- }
- const cloudwatchLanguageProvider = datasource.languageProvider as CloudWatchLanguageProvider;
- const { history, absoluteRange } = this.props;
- const { prefix, text, value, wrapperClasses, labelKey, editor } = typeahead;
- return await cloudwatchLanguageProvider.provideCompletionItems(
- { text, value, prefix, wrapperClasses, labelKey, editor },
- {
- history,
- absoluteRange,
- logGroupNames: selectedLogGroups.map((logGroup) => logGroup.value!),
- region: query.region,
- }
- );
- };
- onQueryFieldClick = (_event: Event, _editor: Editor, next: () => any) => {
- const { selectedLogGroups, loadingLogGroups } = this.state;
- const queryFieldDisabled = loadingLogGroups || selectedLogGroups.length === 0;
- if (queryFieldDisabled) {
- this.setState({
- invalidLogGroups: true,
- });
- }
- next();
- };
- onOpenLogGroupMenu = () => {
- this.setState({
- invalidLogGroups: false,
- });
- };
- render() {
- const { onRunQuery, onChange, ExtraFieldElement, data, query, datasource, allowCustomValue } = this.props;
- const { selectedLogGroups, availableLogGroups, loadingLogGroups, hint, invalidLogGroups } = this.state;
- const showError = data && data.error && data.error.refId === query.refId;
- const cleanText = datasource.languageProvider ? datasource.languageProvider.cleanText : undefined;
- const MAX_LOG_GROUPS = 20;
- return (
- <>
- <QueryHeader
- query={query}
- onRunQuery={onRunQuery}
- datasource={datasource}
- onChange={onChange}
- sqlCodeEditorIsDirty={false}
- onRegionChange={this.onRegionChange}
- />
- <div className={`gf-form gf-form--grow flex-grow-1 ${rowGap}`}>
- <LegacyForms.FormField
- label="Log Groups"
- labelWidth={6}
- className="flex-grow-1"
- inputEl={
- <MultiSelect
- aria-label="Log Groups"
- allowCustomValue={allowCustomValue}
- options={appendTemplateVariables(datasource, unionBy(availableLogGroups, selectedLogGroups, 'value'))}
- value={selectedLogGroups}
- onChange={(v) => {
- this.setSelectedLogGroups(v);
- }}
- onCreateOption={(v) => {
- this.setCustomLogGroups(v);
- }}
- onBlur={this.props.onRunQuery}
- className={containerClass}
- closeMenuOnSelect={false}
- isClearable={true}
- invalid={invalidLogGroups}
- isOptionDisabled={() => selectedLogGroups.length >= MAX_LOG_GROUPS}
- placeholder="Choose Log Groups"
- maxVisibleValues={4}
- noOptionsMessage="No log groups available"
- isLoading={loadingLogGroups}
- onOpenMenu={this.onOpenLogGroupMenu}
- onInputChange={(value, actionMeta) => {
- this.onLogGroupSearchDebounced(value, query.region, actionMeta);
- }}
- />
- }
- />
- </div>
- <div className="gf-form-inline gf-form-inline--nowrap flex-grow-1">
- <div className="gf-form gf-form--grow flex-shrink-1">
- <QueryField
- additionalPlugins={this.plugins}
- query={(query as CloudWatchLogsQuery).expression ?? ''}
- onChange={this.onChangeQuery}
- onClick={this.onQueryFieldClick}
- onRunQuery={this.props.onRunQuery}
- onTypeahead={this.onTypeahead}
- cleanText={cleanText}
- placeholder="Enter a CloudWatch Logs Insights query (run with Shift+Enter)"
- portalOrigin="cloudwatch"
- disabled={loadingLogGroups || selectedLogGroups.length === 0}
- />
- </div>
- {ExtraFieldElement}
- </div>
- {hint && (
- <div className="query-row-break">
- <div className="text-warning">
- {hint.message}
- <a className="text-link muted" onClick={hint.fix.action}>
- {hint.fix.label}
- </a>
- </div>
- </div>
- )}
- {showError ? (
- <div className="query-row-break">
- <div className="prom-query-field-info text-error">{data?.error?.message}</div>
- </div>
- ) : null}
- </>
- );
- }
- }
|