ElasticsearchQueryField.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import _ from 'lodash';
  2. import React from 'react';
  3. import { SlatePrism } from '@grafana/ui';
  4. // dom also includes Element polyfills
  5. import QueryField from 'app/features/explore/QueryField';
  6. import { ExploreQueryFieldProps } from '@grafana/ui';
  7. import { ElasticDatasource } from '../datasource';
  8. import { ElasticsearchOptions, ElasticsearchQuery } from '../types';
  9. interface Props extends ExploreQueryFieldProps<ElasticDatasource, ElasticsearchQuery, ElasticsearchOptions> {}
  10. interface State {
  11. syntaxLoaded: boolean;
  12. }
  13. class ElasticsearchQueryField extends React.PureComponent<Props, State> {
  14. plugins: any[];
  15. constructor(props: Props, context: React.Context<any>) {
  16. super(props, context);
  17. this.plugins = [
  18. SlatePrism({
  19. onlyIn: (node: any) => node.type === 'code_block',
  20. getSyntax: (node: any) => 'lucene',
  21. }),
  22. ];
  23. this.state = {
  24. syntaxLoaded: false,
  25. };
  26. }
  27. componentDidMount() {
  28. if (!this.props.query.isLogsQuery) {
  29. this.onChangeQuery('', true);
  30. }
  31. }
  32. componentWillUnmount() {}
  33. componentDidUpdate(prevProps: Props) {
  34. // if query changed from the outside (i.e. cleared via explore toolbar)
  35. if (!this.props.query.isLogsQuery) {
  36. this.onChangeQuery('', true);
  37. }
  38. }
  39. onChangeQuery = (value: string, override?: boolean) => {
  40. // Send text change to parent
  41. const { query, onChange, onRunQuery } = this.props;
  42. if (onChange) {
  43. const nextQuery: ElasticsearchQuery = { ...query, query: value, isLogsQuery: true };
  44. onChange(nextQuery);
  45. if (override && onRunQuery) {
  46. onRunQuery();
  47. }
  48. }
  49. };
  50. render() {
  51. const { queryResponse, query } = this.props;
  52. const { syntaxLoaded } = this.state;
  53. return (
  54. <>
  55. <div className="gf-form-inline gf-form-inline--nowrap">
  56. <div className="gf-form gf-form--grow flex-shrink-1">
  57. <QueryField
  58. additionalPlugins={this.plugins}
  59. initialQuery={query.query}
  60. onChange={this.onChangeQuery}
  61. onRunQuery={this.props.onRunQuery}
  62. placeholder="Enter a Lucene query"
  63. portalOrigin="elasticsearch"
  64. syntaxLoaded={syntaxLoaded}
  65. />
  66. </div>
  67. </div>
  68. {queryResponse && queryResponse.error ? (
  69. <div className="prom-query-field-info text-error">{queryResponse.error.message}</div>
  70. ) : null}
  71. </>
  72. );
  73. }
  74. }
  75. export default ElasticsearchQueryField;