浏览代码

Loki query editor is starting to work, had to make changes to explore query field in order to update query from the outside without unmount between

Torkel Ödegaard 7 年之前
父节点
当前提交
02083d71c8

+ 1 - 1
packages/grafana-ui/src/types/datasource.ts

@@ -3,7 +3,7 @@ import { TimeSeries } from './series';
 import { PluginMeta } from './plugin';
 import { PluginMeta } from './plugin';
 
 
 export interface DataQueryResponse {
 export interface DataQueryResponse {
-  data: TimeSeries[];
+  data: TimeSeries[] | any;
 }
 }
 
 
 export interface DataQuery {
 export interface DataQuery {

+ 2 - 2
packages/grafana-ui/src/types/plugin.ts

@@ -44,8 +44,8 @@ export interface DataSourceApi<TQuery extends DataQuery = DataQuery> {
 export interface QueryEditorProps<DSType extends DataSourceApi, TQuery extends DataQuery> {
 export interface QueryEditorProps<DSType extends DataSourceApi, TQuery extends DataQuery> {
   datasource: DSType;
   datasource: DSType;
   query: TQuery;
   query: TQuery;
-  onExecuteQuery?: () => void;
-  onQueryChange?: (value: TQuery) => void;
+  onRunQuery: () => void;
+  onChange: (value: TQuery) => void;
 }
 }
 
 
 export interface PluginExports {
 export interface PluginExports {

+ 3 - 4
public/app/features/dashboard/panel_editor/QueryEditorRow.tsx

@@ -107,10 +107,9 @@ export class QueryEditorRow extends PureComponent<Props, State> {
 
 
   onQueryChange = (query: DataQuery) => {
   onQueryChange = (query: DataQuery) => {
     Object.assign(this.props.query, query);
     Object.assign(this.props.query, query);
-    this.onExecuteQuery();
   };
   };
 
 
-  onExecuteQuery = () => {
+  onRunQuery = () => {
     this.props.panel.refresh();
     this.props.panel.refresh();
   };
   };
 
 
@@ -128,8 +127,8 @@ export class QueryEditorRow extends PureComponent<Props, State> {
         <QueryEditor
         <QueryEditor
           query={query}
           query={query}
           datasource={datasource}
           datasource={datasource}
-          onQueryChange={this.onQueryChange}
-          onExecuteQuery={this.onExecuteQuery}
+          onChange={this.onQueryChange}
+          onRunQuery={this.onRunQuery}
         />
         />
       );
       );
     }
     }

+ 13 - 4
public/app/features/explore/QueryField.tsx

@@ -101,11 +101,20 @@ export class QueryField extends React.PureComponent<QueryFieldProps, QueryFieldS
   }
   }
 
 
   componentDidUpdate(prevProps: QueryFieldProps, prevState: QueryFieldState) {
   componentDidUpdate(prevProps: QueryFieldProps, prevState: QueryFieldState) {
+    const { initialQuery, syntax } = this.props;
+    const { value, suggestions } = this.state;
+
+    // if query changed from the outside
+    if (initialQuery !== prevProps.initialQuery) {
+      // and we have a version that differs
+      if (initialQuery !== Plain.serialize(value)) {
+        this.placeholdersBuffer = new PlaceholdersBuffer(initialQuery || '');
+        this.setState({ value: makeValue(this.placeholdersBuffer.toString(), syntax) });
+      }
+    }
+
     // Only update menu location when suggestion existence or text/selection changed
     // Only update menu location when suggestion existence or text/selection changed
-    if (
-      this.state.value !== prevState.value ||
-      hasSuggestions(this.state.suggestions) !== hasSuggestions(prevState.suggestions)
-    ) {
+    if (value !== prevState.value || hasSuggestions(suggestions) !== hasSuggestions(prevState.suggestions)) {
       this.updateMenu();
       this.updateMenu();
     }
     }
   }
   }

+ 4 - 0
public/app/features/explore/QueryRow.tsx

@@ -65,6 +65,10 @@ export class QueryRow extends PureComponent<QueryRowProps> {
     }
     }
   };
   };
 
 
+  componentWillUnmount() {
+    console.log('QueryRow will unmount');
+  }
+
   onClickAddButton = () => {
   onClickAddButton = () => {
     const { exploreId, index } = this.props;
     const { exploreId, index } = this.props;
     this.props.addQueryRow(exploreId, index);
     this.props.addQueryRow(exploreId, index);

+ 47 - 0
public/app/plugins/datasource/loki/components/LokiQueryEditor.tsx

@@ -0,0 +1,47 @@
+// Libraries
+import React, { PureComponent } from 'react';
+
+// Types
+import { QueryEditorProps } from '@grafana/ui/src/types';
+import { LokiDatasource } from '../datasource';
+import { LokiQuery } from '../types';
+import { LokiQueryField } from './LokiQueryField';
+
+type Props = QueryEditorProps<LokiDatasource, LokiQuery>;
+
+interface State {
+  query: LokiQuery;
+}
+
+export class LokiQueryEditor extends PureComponent<Props> {
+
+  state: State = {
+    query: this.props.query
+  };
+
+  onRunQuery = () => {
+    const { query  } = this.state;
+
+    this.props.onChange(query);
+    this.props.onRunQuery();
+  };
+
+  onFieldChange = (query: LokiQuery, override?) => {
+    this.setState({
+      query:  query
+    });
+  };
+
+  render() {
+    const { query  } = this.state;
+    const { datasource } = this.props;
+
+    return (
+      <div>
+        <LokiQueryField datasource={datasource} initialQuery={query} onQueryChange={this.onFieldChange} onPressEnter={this.onRunQuery} />
+      </div>
+    );
+  }
+}
+
+export default LokiQueryEditor;

+ 3 - 2
public/app/plugins/datasource/loki/components/LokiQueryField.tsx

@@ -12,6 +12,7 @@ import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explor
 import { getNextCharacter, getPreviousCousin } from 'app/features/explore/utils/dom';
 import { getNextCharacter, getPreviousCousin } from 'app/features/explore/utils/dom';
 import BracesPlugin from 'app/features/explore/slate-plugins/braces';
 import BracesPlugin from 'app/features/explore/slate-plugins/braces';
 import RunnerPlugin from 'app/features/explore/slate-plugins/runner';
 import RunnerPlugin from 'app/features/explore/slate-plugins/runner';
+import LokiDatasource from '../datasource';
 
 
 // Types
 // Types
 import { LokiQuery } from '../types';
 import { LokiQuery } from '../types';
@@ -64,7 +65,7 @@ interface CascaderOption {
 }
 }
 
 
 interface LokiQueryFieldProps {
 interface LokiQueryFieldProps {
-  datasource: any;
+  datasource: LokiDatasource;
   error?: string | JSX.Element;
   error?: string | JSX.Element;
   hint?: any;
   hint?: any;
   history?: any[];
   history?: any[];
@@ -79,7 +80,7 @@ interface LokiQueryFieldState {
   syntaxLoaded: boolean;
   syntaxLoaded: boolean;
 }
 }
 
 
-class LokiQueryField extends React.PureComponent<LokiQueryFieldProps, LokiQueryFieldState> {
+export class LokiQueryField extends React.PureComponent<LokiQueryFieldProps, LokiQueryFieldState> {
   plugins: any[];
   plugins: any[];
   pluginsSearch: any[];
   pluginsSearch: any[];
   languageProvider: any;
   languageProvider: any;

+ 3 - 1
public/app/plugins/datasource/loki/datasource.ts

@@ -32,7 +32,7 @@ function serializeParams(data: any) {
     .join('&');
     .join('&');
 }
 }
 
 
-export default class LokiDatasource {
+export class LokiDatasource {
   languageProvider: LanguageProvider;
   languageProvider: LanguageProvider;
   maxLines: number;
   maxLines: number;
 
 
@@ -173,3 +173,5 @@ export default class LokiDatasource {
       });
       });
   }
   }
 }
 }
+
+export default LokiDatasource;

+ 2 - 0
public/app/plugins/datasource/loki/module.ts

@@ -2,6 +2,7 @@ import Datasource from './datasource';
 
 
 import LokiStartPage from './components/LokiStartPage';
 import LokiStartPage from './components/LokiStartPage';
 import LokiQueryField from './components/LokiQueryField';
 import LokiQueryField from './components/LokiQueryField';
+import LokiQueryEditor from './components/LokiQueryEditor';
 
 
 export class LokiConfigCtrl {
 export class LokiConfigCtrl {
   static templateUrl = 'partials/config.html';
   static templateUrl = 'partials/config.html';
@@ -9,6 +10,7 @@ export class LokiConfigCtrl {
 
 
 export {
 export {
   Datasource,
   Datasource,
+  LokiQueryEditor as QueryEditor,
   LokiConfigCtrl as ConfigCtrl,
   LokiConfigCtrl as ConfigCtrl,
   LokiQueryField as ExploreQueryField,
   LokiQueryField as ExploreQueryField,
   LokiStartPage as ExploreStartPage,
   LokiStartPage as ExploreStartPage,

+ 3 - 1
public/app/plugins/datasource/loki/plugin.json

@@ -2,12 +2,14 @@
   "type": "datasource",
   "type": "datasource",
   "name": "Loki",
   "name": "Loki",
   "id": "loki",
   "id": "loki",
-  "metrics": false,
+
+  "metrics": true,
   "alerting": false,
   "alerting": false,
   "annotations": false,
   "annotations": false,
   "logs": true,
   "logs": true,
   "explore": true,
   "explore": true,
   "tables": false,
   "tables": false,
+
   "info": {
   "info": {
     "description": "Loki Logging Data Source for Grafana",
     "description": "Loki Logging Data Source for Grafana",
     "author": {
     "author": {

+ 2 - 2
public/app/plugins/datasource/testdata/QueryEditor.tsx

@@ -41,9 +41,9 @@ export class QueryEditor extends PureComponent<Props> {
   }
   }
 
 
   onScenarioChange = (item: SelectOptionItem) => {
   onScenarioChange = (item: SelectOptionItem) => {
-    this.props.onQueryChange({
+    this.props.onChange({
+      ...this.props.query,
       scenarioId: item.value,
       scenarioId: item.value,
-      ...this.props.query
     });
     });
   }
   }
 
 

+ 11 - 13
public/sass/pages/_explore.scss

@@ -239,22 +239,20 @@
 
 
 // Prometheus-specifics, to be extracted to datasource soon
 // Prometheus-specifics, to be extracted to datasource soon
 
 
-.explore {
-  .prom-query-field {
-    display: flex;
-  }
+.prom-query-field {
+  display: flex;
+}
 
 
-  .prom-query-field-wrapper {
-    width: 100%;
-  }
+.prom-query-field-wrapper {
+  width: 100%;
+}
 
 
-  .prom-query-field-info {
-    margin: 0.25em 0.5em 0.5em;
-    display: flex;
+.prom-query-field-info {
+  margin: 0.25em 0.5em 0.5em;
+  display: flex;
 
 
-    details {
-      margin-left: 1em;
-    }
+  details {
+    margin-left: 1em;
   }
   }
 }
 }