LokiQueryEditor.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. query={query}
  51. onQueryChange={this.onFieldChange}
  52. onExecuteQuery={this.onRunQuery}
  53. history={[]}
  54. />
  55. <div className="gf-form-inline">
  56. <div className="gf-form">
  57. <div className="gf-form-label">Format as</div>
  58. <Select
  59. isSearchable={false}
  60. options={formatOptions}
  61. onChange={this.onFormatChanged}
  62. value={currentFormat}
  63. />
  64. </div>
  65. <div className="gf-form gf-form--grow">
  66. <div className="gf-form-label gf-form-label--grow" />
  67. </div>
  68. </div>
  69. </div>
  70. );
  71. }
  72. }
  73. export default LokiQueryEditor;