reducers.ts 14 KB

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