reducers.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import {
  2. calculateResultsFromQueryTransactions,
  3. generateEmptyQuery,
  4. getIntervals,
  5. ensureQueries,
  6. } from 'app/core/utils/explore';
  7. import { ExploreItemState, ExploreState, QueryTransaction } from 'app/types/explore';
  8. import { DataQuery } from '@grafana/ui/src/types';
  9. import { Action, ActionTypes } from './actionTypes';
  10. export const DEFAULT_RANGE = {
  11. from: 'now-6h',
  12. to: 'now',
  13. };
  14. // Millies step for helper bar charts
  15. const DEFAULT_GRAPH_INTERVAL = 15 * 1000;
  16. /**
  17. * Returns a fresh Explore area state
  18. */
  19. export const makeExploreItemState = (): ExploreItemState => ({
  20. StartPage: undefined,
  21. containerWidth: 0,
  22. datasourceInstance: null,
  23. requestedDatasourceName: null,
  24. datasourceError: null,
  25. datasourceLoading: null,
  26. datasourceMissing: false,
  27. exploreDatasources: [],
  28. history: [],
  29. initialQueries: [],
  30. initialized: false,
  31. modifiedQueries: [],
  32. queryTransactions: [],
  33. queryIntervals: { interval: '15s', intervalMs: DEFAULT_GRAPH_INTERVAL },
  34. range: DEFAULT_RANGE,
  35. scanning: false,
  36. scanRange: null,
  37. showingGraph: true,
  38. showingLogs: true,
  39. showingTable: true,
  40. supportsGraph: null,
  41. supportsLogs: null,
  42. supportsTable: null,
  43. });
  44. /**
  45. * Global Explore state that handles multiple Explore areas and the split state
  46. */
  47. export const initialExploreState: ExploreState = {
  48. split: null,
  49. left: makeExploreItemState(),
  50. right: makeExploreItemState(),
  51. };
  52. /**
  53. * Reducer for an Explore area, to be used by the global Explore reducer.
  54. */
  55. export const itemReducer = (state, action: Action): ExploreItemState => {
  56. switch (action.type) {
  57. case ActionTypes.AddQueryRow: {
  58. const { initialQueries, modifiedQueries, queryTransactions } = state;
  59. const { index, query } = action.payload;
  60. // Add new query row after given index, keep modifications of existing rows
  61. const nextModifiedQueries = [
  62. ...modifiedQueries.slice(0, index + 1),
  63. { ...query },
  64. ...initialQueries.slice(index + 1),
  65. ];
  66. // Add to initialQueries, which will cause a new row to be rendered
  67. const nextQueries = [...initialQueries.slice(0, index + 1), { ...query }, ...initialQueries.slice(index + 1)];
  68. // Ongoing transactions need to update their row indices
  69. const nextQueryTransactions = queryTransactions.map(qt => {
  70. if (qt.rowIndex > index) {
  71. return {
  72. ...qt,
  73. rowIndex: qt.rowIndex + 1,
  74. };
  75. }
  76. return qt;
  77. });
  78. return {
  79. ...state,
  80. initialQueries: nextQueries,
  81. logsHighlighterExpressions: undefined,
  82. modifiedQueries: nextModifiedQueries,
  83. queryTransactions: nextQueryTransactions,
  84. };
  85. }
  86. case ActionTypes.ChangeQuery: {
  87. const { initialQueries, queryTransactions } = state;
  88. let { modifiedQueries } = state;
  89. const { query, index, override } = action.payload;
  90. // Fast path: only change modifiedQueries to not trigger an update
  91. modifiedQueries[index] = query;
  92. if (!override) {
  93. return {
  94. ...state,
  95. modifiedQueries,
  96. };
  97. }
  98. // Override path: queries are completely reset
  99. const nextQuery: DataQuery = {
  100. ...query,
  101. ...generateEmptyQuery(index),
  102. };
  103. const nextQueries = [...initialQueries];
  104. nextQueries[index] = nextQuery;
  105. modifiedQueries = [...nextQueries];
  106. // Discard ongoing transaction related to row query
  107. const nextQueryTransactions = queryTransactions.filter(qt => qt.rowIndex !== index);
  108. return {
  109. ...state,
  110. initialQueries: nextQueries,
  111. modifiedQueries: nextQueries.slice(),
  112. queryTransactions: nextQueryTransactions,
  113. };
  114. }
  115. case ActionTypes.ChangeSize: {
  116. const { range, datasourceInstance } = state;
  117. let interval = '1s';
  118. if (datasourceInstance && datasourceInstance.interval) {
  119. interval = datasourceInstance.interval;
  120. }
  121. const containerWidth = action.payload.width;
  122. const queryIntervals = getIntervals(range, interval, containerWidth);
  123. return { ...state, containerWidth, queryIntervals };
  124. }
  125. case ActionTypes.ChangeTime: {
  126. return {
  127. ...state,
  128. range: action.payload.range,
  129. };
  130. }
  131. case ActionTypes.ClearQueries: {
  132. const queries = ensureQueries();
  133. return {
  134. ...state,
  135. initialQueries: queries.slice(),
  136. modifiedQueries: queries.slice(),
  137. queryTransactions: [],
  138. showingStartPage: Boolean(state.StartPage),
  139. };
  140. }
  141. case ActionTypes.HighlightLogsExpression: {
  142. const { expressions } = action.payload;
  143. return { ...state, logsHighlighterExpressions: expressions };
  144. }
  145. case ActionTypes.InitializeExplore: {
  146. const { containerWidth, eventBridge, exploreDatasources, queries, range, ui } = action.payload;
  147. return {
  148. ...state,
  149. containerWidth,
  150. eventBridge,
  151. exploreDatasources,
  152. range,
  153. initialQueries: queries,
  154. initialized: true,
  155. modifiedQueries: queries.slice(),
  156. ...ui,
  157. };
  158. }
  159. case ActionTypes.UpdateDatasourceInstance: {
  160. const { datasourceInstance } = action.payload;
  161. return {
  162. ...state,
  163. datasourceInstance,
  164. datasourceName: datasourceInstance.name,
  165. };
  166. }
  167. case ActionTypes.LoadDatasourceFailure: {
  168. return { ...state, datasourceError: action.payload.error, datasourceLoading: false };
  169. }
  170. case ActionTypes.LoadDatasourceMissing: {
  171. return { ...state, datasourceMissing: true, datasourceLoading: false };
  172. }
  173. case ActionTypes.LoadDatasourcePending: {
  174. return { ...state, datasourceLoading: true, requestedDatasourceName: action.payload.requestedDatasourceName };
  175. }
  176. case ActionTypes.LoadDatasourceSuccess: {
  177. const { containerWidth, range } = state;
  178. const {
  179. StartPage,
  180. datasourceInstance,
  181. history,
  182. showingStartPage,
  183. supportsGraph,
  184. supportsLogs,
  185. supportsTable,
  186. } = action.payload;
  187. const queryIntervals = getIntervals(range, datasourceInstance.interval, containerWidth);
  188. return {
  189. ...state,
  190. queryIntervals,
  191. StartPage,
  192. datasourceInstance,
  193. history,
  194. showingStartPage,
  195. supportsGraph,
  196. supportsLogs,
  197. supportsTable,
  198. datasourceLoading: false,
  199. datasourceMissing: false,
  200. datasourceError: null,
  201. logsHighlighterExpressions: undefined,
  202. queryTransactions: [],
  203. };
  204. }
  205. case ActionTypes.ModifyQueries: {
  206. const { initialQueries, modifiedQueries, queryTransactions } = state;
  207. const { modification, index, modifier } = action.payload as any;
  208. let nextQueries: DataQuery[];
  209. let nextQueryTransactions;
  210. if (index === undefined) {
  211. // Modify all queries
  212. nextQueries = initialQueries.map((query, i) => ({
  213. ...modifier(modifiedQueries[i], modification),
  214. ...generateEmptyQuery(i),
  215. }));
  216. // Discard all ongoing transactions
  217. nextQueryTransactions = [];
  218. } else {
  219. // Modify query only at index
  220. nextQueries = initialQueries.map((query, i) => {
  221. // Synchronize all queries with local query cache to ensure consistency
  222. // TODO still needed?
  223. return i === index
  224. ? {
  225. ...modifier(modifiedQueries[i], modification),
  226. ...generateEmptyQuery(i),
  227. }
  228. : query;
  229. });
  230. nextQueryTransactions = queryTransactions
  231. // Consume the hint corresponding to the action
  232. .map(qt => {
  233. if (qt.hints != null && qt.rowIndex === index) {
  234. qt.hints = qt.hints.filter(hint => hint.fix.action !== modification);
  235. }
  236. return qt;
  237. })
  238. // Preserve previous row query transaction to keep results visible if next query is incomplete
  239. .filter(qt => modification.preventSubmit || qt.rowIndex !== index);
  240. }
  241. return {
  242. ...state,
  243. initialQueries: nextQueries,
  244. modifiedQueries: nextQueries.slice(),
  245. queryTransactions: nextQueryTransactions,
  246. };
  247. }
  248. case ActionTypes.QueryTransactionFailure: {
  249. const { queryTransactions } = action.payload;
  250. return {
  251. ...state,
  252. queryTransactions,
  253. showingStartPage: false,
  254. };
  255. }
  256. case ActionTypes.QueryTransactionStart: {
  257. const { queryTransactions } = state;
  258. const { resultType, rowIndex, transaction } = action.payload;
  259. // Discarding existing transactions of same type
  260. const remainingTransactions = queryTransactions.filter(
  261. qt => !(qt.resultType === resultType && qt.rowIndex === rowIndex)
  262. );
  263. // Append new transaction
  264. const nextQueryTransactions: QueryTransaction[] = [...remainingTransactions, transaction];
  265. return {
  266. ...state,
  267. queryTransactions: nextQueryTransactions,
  268. showingStartPage: false,
  269. };
  270. }
  271. case ActionTypes.QueryTransactionSuccess: {
  272. const { datasourceInstance, queryIntervals } = state;
  273. const { history, queryTransactions } = action.payload;
  274. const results = calculateResultsFromQueryTransactions(
  275. queryTransactions,
  276. datasourceInstance,
  277. queryIntervals.intervalMs
  278. );
  279. return {
  280. ...state,
  281. ...results,
  282. history,
  283. queryTransactions,
  284. showingStartPage: false,
  285. };
  286. }
  287. case ActionTypes.RemoveQueryRow: {
  288. const { datasourceInstance, initialQueries, queryIntervals, queryTransactions } = state;
  289. let { modifiedQueries } = state;
  290. const { index } = action.payload;
  291. modifiedQueries = [...modifiedQueries.slice(0, index), ...modifiedQueries.slice(index + 1)];
  292. if (initialQueries.length <= 1) {
  293. return state;
  294. }
  295. const nextQueries = [...initialQueries.slice(0, index), ...initialQueries.slice(index + 1)];
  296. // Discard transactions related to row query
  297. const nextQueryTransactions = queryTransactions.filter(qt => qt.rowIndex !== index);
  298. const results = calculateResultsFromQueryTransactions(
  299. nextQueryTransactions,
  300. datasourceInstance,
  301. queryIntervals.intervalMs
  302. );
  303. return {
  304. ...state,
  305. ...results,
  306. initialQueries: nextQueries,
  307. logsHighlighterExpressions: undefined,
  308. modifiedQueries: nextQueries.slice(),
  309. queryTransactions: nextQueryTransactions,
  310. };
  311. }
  312. case ActionTypes.RunQueriesEmpty: {
  313. return { ...state, queryTransactions: [] };
  314. }
  315. case ActionTypes.ScanRange: {
  316. return { ...state, scanRange: action.payload.range };
  317. }
  318. case ActionTypes.ScanStart: {
  319. return { ...state, scanning: true, scanner: action.payload.scanner };
  320. }
  321. case ActionTypes.ScanStop: {
  322. const { queryTransactions } = state;
  323. const nextQueryTransactions = queryTransactions.filter(qt => qt.scanning && !qt.done);
  324. return {
  325. ...state,
  326. queryTransactions: nextQueryTransactions,
  327. scanning: false,
  328. scanRange: undefined,
  329. scanner: undefined,
  330. };
  331. }
  332. case ActionTypes.SetQueries: {
  333. const { queries } = action.payload;
  334. return { ...state, initialQueries: queries.slice(), modifiedQueries: queries.slice() };
  335. }
  336. case ActionTypes.ToggleGraph: {
  337. const showingGraph = !state.showingGraph;
  338. let nextQueryTransactions = state.queryTransactions;
  339. if (!showingGraph) {
  340. // Discard transactions related to Graph query
  341. nextQueryTransactions = state.queryTransactions.filter(qt => qt.resultType !== 'Graph');
  342. }
  343. return { ...state, queryTransactions: nextQueryTransactions, showingGraph };
  344. }
  345. case ActionTypes.ToggleLogs: {
  346. const showingLogs = !state.showingLogs;
  347. let nextQueryTransactions = state.queryTransactions;
  348. if (!showingLogs) {
  349. // Discard transactions related to Logs query
  350. nextQueryTransactions = state.queryTransactions.filter(qt => qt.resultType !== 'Logs');
  351. }
  352. return { ...state, queryTransactions: nextQueryTransactions, showingLogs };
  353. }
  354. case ActionTypes.ToggleTable: {
  355. const showingTable = !state.showingTable;
  356. if (showingTable) {
  357. return { ...state, showingTable, queryTransactions: state.queryTransactions };
  358. }
  359. // Toggle off needs discarding of table queries and results
  360. const nextQueryTransactions = state.queryTransactions.filter(qt => qt.resultType !== 'Table');
  361. const results = calculateResultsFromQueryTransactions(
  362. nextQueryTransactions,
  363. state.datasourceInstance,
  364. state.queryIntervals.intervalMs
  365. );
  366. return { ...state, ...results, queryTransactions: nextQueryTransactions, showingTable };
  367. }
  368. case ActionTypes.QueriesImported: {
  369. return {
  370. ...state,
  371. initialQueries: action.payload.queries,
  372. modifiedQueries: action.payload.queries.slice(),
  373. };
  374. }
  375. }
  376. return state;
  377. };
  378. /**
  379. * Global Explore reducer that handles multiple Explore areas (left and right).
  380. * Actions that have an `exploreId` get routed to the ExploreItemReducer.
  381. */
  382. export const exploreReducer = (state = initialExploreState, action: Action): ExploreState => {
  383. switch (action.type) {
  384. case ActionTypes.SplitClose: {
  385. return { ...state, split: false };
  386. }
  387. case ActionTypes.SplitOpen: {
  388. return { ...state, split: true, right: action.payload.itemState };
  389. }
  390. case ActionTypes.InitializeExploreSplit: {
  391. return { ...state, split: true };
  392. }
  393. case ActionTypes.ResetExplore: {
  394. return initialExploreState;
  395. }
  396. }
  397. if (action.payload) {
  398. const { exploreId } = action.payload as any;
  399. if (exploreId !== undefined) {
  400. const exploreItemState = state[exploreId];
  401. return {
  402. ...state,
  403. [exploreId]: itemReducer(exploreItemState, action),
  404. };
  405. }
  406. }
  407. return state;
  408. };
  409. export default {
  410. explore: exploreReducer,
  411. };