Explore.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import React from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import colors from 'app/core/utils/colors';
  4. import TimeSeries from 'app/core/time_series2';
  5. import ElapsedTime from './ElapsedTime';
  6. import Legend from './Legend';
  7. import QueryRows from './QueryRows';
  8. import Graph from './Graph';
  9. import Table from './Table';
  10. import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
  11. import { buildQueryOptions, ensureQueries, generateQueryKey, hasQuery } from './utils/query';
  12. import { decodePathComponent } from 'app/core/utils/location_util';
  13. function makeTimeSeriesList(dataList, options) {
  14. return dataList.map((seriesData, index) => {
  15. const datapoints = seriesData.datapoints || [];
  16. const alias = seriesData.target;
  17. const colorIndex = index % colors.length;
  18. const color = colors[colorIndex];
  19. const series = new TimeSeries({
  20. datapoints: datapoints,
  21. alias: alias,
  22. color: color,
  23. unit: seriesData.unit,
  24. });
  25. if (datapoints && datapoints.length > 0) {
  26. const last = datapoints[datapoints.length - 1][1];
  27. const from = options.range.from;
  28. if (last - from < -10000) {
  29. series.isOutsideRange = true;
  30. }
  31. }
  32. return series;
  33. });
  34. }
  35. function parseInitialQueries(initial) {
  36. if (!initial) {
  37. return [];
  38. }
  39. try {
  40. const parsed = JSON.parse(decodePathComponent(initial));
  41. return parsed.queries.map(q => q.query);
  42. } catch (e) {
  43. console.error(e);
  44. return [];
  45. }
  46. }
  47. interface IExploreState {
  48. datasource: any;
  49. datasourceError: any;
  50. datasourceLoading: any;
  51. graphResult: any;
  52. latency: number;
  53. loading: any;
  54. queries: any;
  55. requestOptions: any;
  56. showingGraph: boolean;
  57. showingTable: boolean;
  58. tableResult: any;
  59. }
  60. // @observer
  61. export class Explore extends React.Component<any, IExploreState> {
  62. datasourceSrv: DatasourceSrv;
  63. constructor(props) {
  64. super(props);
  65. const initialQueries = parseInitialQueries(props.routeParams.initial);
  66. this.state = {
  67. datasource: null,
  68. datasourceError: null,
  69. datasourceLoading: true,
  70. graphResult: null,
  71. latency: 0,
  72. loading: false,
  73. queries: ensureQueries(initialQueries),
  74. requestOptions: null,
  75. showingGraph: true,
  76. showingTable: true,
  77. tableResult: null,
  78. };
  79. }
  80. async componentDidMount() {
  81. const datasource = await this.props.datasourceSrv.get();
  82. const testResult = await datasource.testDatasource();
  83. if (testResult.status === 'success') {
  84. this.setState({ datasource, datasourceError: null, datasourceLoading: false }, () => this.handleSubmit());
  85. } else {
  86. this.setState({ datasource: null, datasourceError: testResult.message, datasourceLoading: false });
  87. }
  88. }
  89. handleAddQueryRow = index => {
  90. const { queries } = this.state;
  91. const nextQueries = [
  92. ...queries.slice(0, index + 1),
  93. { query: '', key: generateQueryKey() },
  94. ...queries.slice(index + 1),
  95. ];
  96. this.setState({ queries: nextQueries });
  97. };
  98. handleChangeQuery = (query, index) => {
  99. const { queries } = this.state;
  100. const nextQuery = {
  101. ...queries[index],
  102. query,
  103. };
  104. const nextQueries = [...queries];
  105. nextQueries[index] = nextQuery;
  106. this.setState({ queries: nextQueries });
  107. };
  108. handleClickGraphButton = () => {
  109. this.setState(state => ({ showingGraph: !state.showingGraph }));
  110. };
  111. handleClickTableButton = () => {
  112. this.setState(state => ({ showingTable: !state.showingTable }));
  113. };
  114. handleRemoveQueryRow = index => {
  115. const { queries } = this.state;
  116. if (queries.length <= 1) {
  117. return;
  118. }
  119. const nextQueries = [...queries.slice(0, index), ...queries.slice(index + 1)];
  120. this.setState({ queries: nextQueries }, () => this.handleSubmit());
  121. };
  122. handleSubmit = () => {
  123. const { showingGraph, showingTable } = this.state;
  124. if (showingTable) {
  125. this.runTableQuery();
  126. }
  127. if (showingGraph) {
  128. this.runGraphQuery();
  129. }
  130. };
  131. async runGraphQuery() {
  132. const { datasource, queries } = this.state;
  133. if (!hasQuery(queries)) {
  134. return;
  135. }
  136. this.setState({ latency: 0, loading: true, graphResult: null });
  137. const now = Date.now();
  138. const options = buildQueryOptions({
  139. format: 'time_series',
  140. interval: datasource.interval,
  141. instant: false,
  142. now,
  143. queries: queries.map(q => q.query),
  144. });
  145. try {
  146. const res = await datasource.query(options);
  147. const result = makeTimeSeriesList(res.data, options);
  148. const latency = Date.now() - now;
  149. this.setState({ latency, loading: false, graphResult: result, requestOptions: options });
  150. } catch (error) {
  151. console.error(error);
  152. this.setState({ loading: false, graphResult: error });
  153. }
  154. }
  155. async runTableQuery() {
  156. const { datasource, queries } = this.state;
  157. if (!hasQuery(queries)) {
  158. return;
  159. }
  160. this.setState({ latency: 0, loading: true, tableResult: null });
  161. const now = Date.now();
  162. const options = buildQueryOptions({
  163. format: 'table',
  164. interval: datasource.interval,
  165. instant: true,
  166. now,
  167. queries: queries.map(q => q.query),
  168. });
  169. try {
  170. const res = await datasource.query(options);
  171. const tableModel = res.data[0];
  172. const latency = Date.now() - now;
  173. this.setState({ latency, loading: false, tableResult: tableModel, requestOptions: options });
  174. } catch (error) {
  175. console.error(error);
  176. this.setState({ loading: false, tableResult: null });
  177. }
  178. }
  179. request = url => {
  180. const { datasource } = this.state;
  181. return datasource.metadataRequest(url);
  182. };
  183. render() {
  184. const {
  185. datasource,
  186. datasourceError,
  187. datasourceLoading,
  188. graphResult,
  189. latency,
  190. loading,
  191. queries,
  192. requestOptions,
  193. showingGraph,
  194. showingTable,
  195. tableResult,
  196. } = this.state;
  197. const showingBoth = showingGraph && showingTable;
  198. const graphHeight = showingBoth ? '200px' : null;
  199. const graphButtonClassName = showingBoth || showingGraph ? 'btn m-r-1' : 'btn btn-inverse m-r-1';
  200. const tableButtonClassName = showingBoth || showingTable ? 'btn m-r-1' : 'btn btn-inverse m-r-1';
  201. return (
  202. <div className="explore">
  203. <div className="page-body page-full">
  204. <h2 className="page-sub-heading">Explore</h2>
  205. {datasourceLoading ? <div>Loading datasource...</div> : null}
  206. {datasourceError ? <div title={datasourceError}>Error connecting to datasource.</div> : null}
  207. {datasource ? (
  208. <div className="m-r-3">
  209. <div className="nav m-b-1">
  210. <div className="pull-right">
  211. {loading || latency ? <ElapsedTime time={latency} className="" /> : null}
  212. <button type="submit" className="m-l-1 btn btn-primary" onClick={this.handleSubmit}>
  213. <i className="fa fa-return" /> Run Query
  214. </button>
  215. </div>
  216. <div>
  217. <button className={graphButtonClassName} onClick={this.handleClickGraphButton}>
  218. Graph
  219. </button>
  220. <button className={tableButtonClassName} onClick={this.handleClickTableButton}>
  221. Table
  222. </button>
  223. </div>
  224. </div>
  225. <QueryRows
  226. queries={queries}
  227. request={this.request}
  228. onAddQueryRow={this.handleAddQueryRow}
  229. onChangeQuery={this.handleChangeQuery}
  230. onExecuteQuery={this.handleSubmit}
  231. onRemoveQueryRow={this.handleRemoveQueryRow}
  232. />
  233. <main className="m-t-2">
  234. {showingGraph ? (
  235. <Graph data={graphResult} id="explore-1" options={requestOptions} height={graphHeight} />
  236. ) : null}
  237. {showingGraph ? <Legend data={graphResult} /> : null}
  238. {showingTable ? <Table data={tableResult} className="m-t-3" /> : null}
  239. </main>
  240. </div>
  241. ) : null}
  242. </div>
  243. </div>
  244. );
  245. }
  246. }
  247. export default hot(module)(Explore);