reducers.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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, requestedDatasourceName: action.payload.datasourceName };
  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. datasourceError: null,
  196. logsHighlighterExpressions: undefined,
  197. modifiedQueries: initialQueries.slice(),
  198. queryTransactions: [],
  199. };
  200. }
  201. case ActionTypes.ModifyQueries: {
  202. const { initialQueries, modifiedQueries, queryTransactions } = state;
  203. const { modification, index, modifier } = action.payload as any;
  204. let nextQueries: DataQuery[];
  205. let nextQueryTransactions;
  206. if (index === undefined) {
  207. // Modify all queries
  208. nextQueries = initialQueries.map((query, i) => ({
  209. ...modifier(modifiedQueries[i], modification),
  210. ...generateEmptyQuery(i),
  211. }));
  212. // Discard all ongoing transactions
  213. nextQueryTransactions = [];
  214. } else {
  215. // Modify query only at index
  216. nextQueries = initialQueries.map((query, i) => {
  217. // Synchronize all queries with local query cache to ensure consistency
  218. // TODO still needed?
  219. return i === index
  220. ? {
  221. ...modifier(modifiedQueries[i], modification),
  222. ...generateEmptyQuery(i),
  223. }
  224. : query;
  225. });
  226. nextQueryTransactions = queryTransactions
  227. // Consume the hint corresponding to the action
  228. .map(qt => {
  229. if (qt.hints != null && qt.rowIndex === index) {
  230. qt.hints = qt.hints.filter(hint => hint.fix.action !== modification);
  231. }
  232. return qt;
  233. })
  234. // Preserve previous row query transaction to keep results visible if next query is incomplete
  235. .filter(qt => modification.preventSubmit || qt.rowIndex !== index);
  236. }
  237. return {
  238. ...state,
  239. initialQueries: nextQueries,
  240. modifiedQueries: nextQueries.slice(),
  241. queryTransactions: nextQueryTransactions,
  242. };
  243. }
  244. case ActionTypes.QueryTransactionFailure: {
  245. const { queryTransactions } = action.payload;
  246. return {
  247. ...state,
  248. queryTransactions,
  249. showingStartPage: false,
  250. };
  251. }
  252. case ActionTypes.QueryTransactionStart: {
  253. const { datasourceInstance, queryIntervals, queryTransactions } = state;
  254. const { resultType, rowIndex, transaction } = action.payload;
  255. // Discarding existing transactions of same type
  256. const remainingTransactions = queryTransactions.filter(
  257. qt => !(qt.resultType === resultType && qt.rowIndex === rowIndex)
  258. );
  259. // Append new transaction
  260. const nextQueryTransactions: QueryTransaction[] = [...remainingTransactions, transaction];
  261. const results = calculateResultsFromQueryTransactions(
  262. nextQueryTransactions,
  263. datasourceInstance,
  264. queryIntervals.intervalMs
  265. );
  266. return {
  267. ...state,
  268. ...results,
  269. queryTransactions: nextQueryTransactions,
  270. showingStartPage: false,
  271. };
  272. }
  273. case ActionTypes.QueryTransactionSuccess: {
  274. const { datasourceInstance, queryIntervals } = state;
  275. const { history, queryTransactions } = action.payload;
  276. const results = calculateResultsFromQueryTransactions(
  277. queryTransactions,
  278. datasourceInstance,
  279. queryIntervals.intervalMs
  280. );
  281. return {
  282. ...state,
  283. ...results,
  284. history,
  285. queryTransactions,
  286. showingStartPage: false,
  287. };
  288. }
  289. case ActionTypes.RemoveQueryRow: {
  290. const { datasourceInstance, initialQueries, queryIntervals, queryTransactions } = state;
  291. let { modifiedQueries } = state;
  292. const { index } = action.payload;
  293. modifiedQueries = [...modifiedQueries.slice(0, index), ...modifiedQueries.slice(index + 1)];
  294. if (initialQueries.length <= 1) {
  295. return state;
  296. }
  297. const nextQueries = [...initialQueries.slice(0, index), ...initialQueries.slice(index + 1)];
  298. // Discard transactions related to row query
  299. const nextQueryTransactions = queryTransactions.filter(qt => qt.rowIndex !== index);
  300. const results = calculateResultsFromQueryTransactions(
  301. nextQueryTransactions,
  302. datasourceInstance,
  303. queryIntervals.intervalMs
  304. );
  305. return {
  306. ...state,
  307. ...results,
  308. initialQueries: nextQueries,
  309. logsHighlighterExpressions: undefined,
  310. modifiedQueries: nextQueries.slice(),
  311. queryTransactions: nextQueryTransactions,
  312. };
  313. }
  314. case ActionTypes.RunQueriesEmpty: {
  315. return { ...state, queryTransactions: [] };
  316. }
  317. case ActionTypes.ScanRange: {
  318. return { ...state, scanRange: action.payload.range };
  319. }
  320. case ActionTypes.ScanStart: {
  321. return { ...state, scanning: true };
  322. }
  323. case ActionTypes.ScanStop: {
  324. const { queryTransactions } = state;
  325. const nextQueryTransactions = queryTransactions.filter(qt => qt.scanning && !qt.done);
  326. return { ...state, queryTransactions: nextQueryTransactions, scanning: false, scanRange: undefined };
  327. }
  328. case ActionTypes.SetQueries: {
  329. const { queries } = action.payload;
  330. return { ...state, initialQueries: queries.slice(), modifiedQueries: queries.slice() };
  331. }
  332. case ActionTypes.ToggleGraph: {
  333. const showingGraph = !state.showingGraph;
  334. let nextQueryTransactions = state.queryTransactions;
  335. if (!showingGraph) {
  336. // Discard transactions related to Graph query
  337. nextQueryTransactions = state.queryTransactions.filter(qt => qt.resultType !== 'Graph');
  338. }
  339. return { ...state, queryTransactions: nextQueryTransactions, showingGraph };
  340. }
  341. case ActionTypes.ToggleLogs: {
  342. const showingLogs = !state.showingLogs;
  343. let nextQueryTransactions = state.queryTransactions;
  344. if (!showingLogs) {
  345. // Discard transactions related to Logs query
  346. nextQueryTransactions = state.queryTransactions.filter(qt => qt.resultType !== 'Logs');
  347. }
  348. return { ...state, queryTransactions: nextQueryTransactions, showingLogs };
  349. }
  350. case ActionTypes.ToggleTable: {
  351. const showingTable = !state.showingTable;
  352. if (showingTable) {
  353. return { ...state, showingTable, queryTransactions: state.queryTransactions };
  354. }
  355. // Toggle off needs discarding of table queries and results
  356. const nextQueryTransactions = state.queryTransactions.filter(qt => qt.resultType !== 'Table');
  357. const results = calculateResultsFromQueryTransactions(
  358. nextQueryTransactions,
  359. state.datasourceInstance,
  360. state.queryIntervals.intervalMs
  361. );
  362. return { ...state, ...results, queryTransactions: nextQueryTransactions, showingTable };
  363. }
  364. }
  365. return state;
  366. };
  367. /**
  368. * Global Explore reducer that handles multiple Explore areas (left and right).
  369. * Actions that have an `exploreId` get routed to the ExploreItemReducer.
  370. */
  371. export const exploreReducer = (state = initialExploreState, action: Action): ExploreState => {
  372. switch (action.type) {
  373. case ActionTypes.SplitClose: {
  374. return {
  375. ...state,
  376. split: false,
  377. };
  378. }
  379. case ActionTypes.SplitOpen: {
  380. return {
  381. ...state,
  382. split: true,
  383. right: action.payload.itemState,
  384. };
  385. }
  386. case ActionTypes.InitializeExploreSplit: {
  387. return {
  388. ...state,
  389. split: true,
  390. };
  391. }
  392. }
  393. if (action.payload) {
  394. const { exploreId } = action.payload as any;
  395. if (exploreId !== undefined) {
  396. const exploreItemState = state[exploreId];
  397. return {
  398. ...state,
  399. [exploreId]: itemReducer(exploreItemState, action),
  400. };
  401. }
  402. }
  403. return state;
  404. };
  405. export default {
  406. explore: exploreReducer,
  407. };