LokiQueryEditor.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Components
  4. import { Select, SelectOptionItem } from '@grafana/ui';
  5. // Types
  6. import { QueryEditorProps } from '@grafana/ui/src/types';
  7. import { LokiDatasource } from '../datasource';
  8. import { LokiQuery } from '../types';
  9. import { LokiQueryField } from './LokiQueryField';
  10. type Props = QueryEditorProps<LokiDatasource, LokiQuery>;
  11. interface State {
  12. query: LokiQuery;
  13. }
  14. export class LokiQueryEditor extends PureComponent<Props> {
  15. state: State = {
  16. query: this.props.query,
  17. };
  18. onRunQuery = () => {
  19. const { query } = this.state;
  20. this.props.onChange(query);
  21. this.props.onRunQuery();
  22. };
  23. onFieldChange = (query: LokiQuery, override?) => {
  24. this.setState({
  25. query: {
  26. ...this.state.query,
  27. expr: query.expr,
  28. }
  29. });
  30. };
  31. onFormatChanged = (option: SelectOptionItem) => {
  32. this.props.onChange({
  33. ...this.state.query,
  34. resultFormat: option.value,
  35. });
  36. };
  37. render() {
  38. const { query } = this.state;
  39. const { datasource } = this.props;
  40. const formatOptions: SelectOptionItem[] = [
  41. { label: 'Time Series', value: 'time_series' },
  42. { label: 'Table', value: 'table' },
  43. ];
  44. query.resultFormat = query.resultFormat || 'time_series';
  45. const currentFormat = formatOptions.find(item => item.value === query.resultFormat);
  46. return (
  47. <div>
  48. <LokiQueryField
  49. datasource={datasource}
  50. initialQuery={query}
  51. onQueryChange={this.onFieldChange}
  52. onPressEnter={this.onRunQuery}
  53. />
  54. <div className="gf-form-inline">
  55. <div className="gf-form">
  56. <div className="gf-form-label">Format as</div>
  57. <Select isSearchable={false} options={formatOptions} onChange={this.onFormatChanged} value={currentFormat} />
  58. </div>
  59. <div className="gf-form gf-form--grow">
  60. <div className="gf-form-label gf-form-label--grow" />
  61. </div>
  62. </div>
  63. </div>
  64. );
  65. }
  66. }
  67. export default LokiQueryEditor;