reducers.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 } = 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. };
  157. }
  158. case ActionTypes.UpdateDatasourceInstance: {
  159. const { datasourceInstance } = action.payload;
  160. return {
  161. ...state,
  162. datasourceInstance,
  163. datasourceName: datasourceInstance.name,
  164. };
  165. }
  166. case ActionTypes.LoadDatasourceFailure: {
  167. return { ...state, datasourceError: action.payload.error, datasourceLoading: false };
  168. }
  169. case ActionTypes.LoadDatasourceMissing: {
  170. return { ...state, datasourceMissing: true, datasourceLoading: false };
  171. }
  172. case ActionTypes.LoadDatasourcePending: {
  173. return { ...state, datasourceLoading: true, requestedDatasourceName: action.payload.requestedDatasourceName };
  174. }
  175. case ActionTypes.LoadDatasourceSuccess: {
  176. const { containerWidth, range } = state;
  177. const {
  178. StartPage,
  179. datasourceInstance,
  180. history,
  181. initialQueries,
  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. initialQueries,
  195. showingStartPage,
  196. supportsGraph,
  197. supportsLogs,
  198. supportsTable,
  199. datasourceLoading: false,
  200. datasourceMissing: false,
  201. datasourceError: null,
  202. logsHighlighterExpressions: undefined,
  203. modifiedQueries: initialQueries.slice(),
  204. queryTransactions: [],
  205. };
  206. }
  207. case ActionTypes.ModifyQueries: {
  208. const { initialQueries, modifiedQueries, queryTransactions } = state;
  209. const { modification, index, modifier } = action.payload as any;
  210. let nextQueries: DataQuery[];
  211. let nextQueryTransactions;
  212. if (index === undefined) {
  213. // Modify all queries
  214. nextQueries = initialQueries.map((query, i) => ({
  215. ...modifier(modifiedQueries[i], modification),
  216. ...generateEmptyQuery(i),
  217. }));
  218. // Discard all ongoing transactions
  219. nextQueryTransactions = [];
  220. } else {
  221. // Modify query only at index
  222. nextQueries = initialQueries.map((query, i) => {
  223. // Synchronize all queries with local query cache to ensure consistency
  224. // TODO still needed?
  225. return i === index
  226. ? {
  227. ...modifier(modifiedQueries[i], modification),
  228. ...generateEmptyQuery(i),
  229. }
  230. : query;
  231. });
  232. nextQueryTransactions = queryTransactions
  233. // Consume the hint corresponding to the action
  234. .map(qt => {
  235. if (qt.hints != null && qt.rowIndex === index) {
  236. qt.hints = qt.hints.filter(hint => hint.fix.action !== modification);
  237. }
  238. return qt;
  239. })
  240. // Preserve previous row query transaction to keep results visible if next query is incomplete
  241. .filter(qt => modification.preventSubmit || qt.rowIndex !== index);
  242. }
  243. return {
  244. ...state,
  245. initialQueries: nextQueries,
  246. modifiedQueries: nextQueries.slice(),
  247. queryTransactions: nextQueryTransactions,
  248. };
  249. }
  250. case ActionTypes.QueryTransactionFailure: {
  251. const { queryTransactions } = action.payload;
  252. return {
  253. ...state,
  254. queryTransactions,
  255. showingStartPage: false,
  256. };
  257. }
  258. case ActionTypes.QueryTransactionStart: {
  259. const { queryTransactions } = state;
  260. const { resultType, rowIndex, transaction } = action.payload;
  261. // Discarding existing transactions of same type
  262. const remainingTransactions = queryTransactions.filter(
  263. qt => !(qt.resultType === resultType && qt.rowIndex === rowIndex)
  264. );
  265. // Append new transaction
  266. const nextQueryTransactions: QueryTransaction[] = [...remainingTransactions, transaction];
  267. return {
  268. ...state,
  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, scanner: action.payload.scanner };
  322. }
  323. case ActionTypes.ScanStop: {
  324. const { queryTransactions } = state;
  325. const nextQueryTransactions = queryTransactions.filter(qt => qt.scanning && !qt.done);
  326. return {
  327. ...state,
  328. queryTransactions: nextQueryTransactions,
  329. scanning: false,
  330. scanRange: undefined,
  331. scanner: undefined,
  332. };
  333. }
  334. case ActionTypes.SetQueries: {
  335. const { queries } = action.payload;
  336. return { ...state, initialQueries: queries.slice(), modifiedQueries: queries.slice() };
  337. }
  338. case ActionTypes.ToggleGraph: {
  339. const showingGraph = !state.showingGraph;
  340. let nextQueryTransactions = state.queryTransactions;
  341. if (!showingGraph) {
  342. // Discard transactions related to Graph query
  343. nextQueryTransactions = state.queryTransactions.filter(qt => qt.resultType !== 'Graph');
  344. }
  345. return { ...state, queryTransactions: nextQueryTransactions, showingGraph };
  346. }
  347. case ActionTypes.ToggleLogs: {
  348. const showingLogs = !state.showingLogs;
  349. let nextQueryTransactions = state.queryTransactions;
  350. if (!showingLogs) {
  351. // Discard transactions related to Logs query
  352. nextQueryTransactions = state.queryTransactions.filter(qt => qt.resultType !== 'Logs');
  353. }
  354. return { ...state, queryTransactions: nextQueryTransactions, showingLogs };
  355. }
  356. case ActionTypes.ToggleTable: {
  357. const showingTable = !state.showingTable;
  358. if (showingTable) {
  359. return { ...state, showingTable, queryTransactions: state.queryTransactions };
  360. }
  361. // Toggle off needs discarding of table queries and results
  362. const nextQueryTransactions = state.queryTransactions.filter(qt => qt.resultType !== 'Table');
  363. const results = calculateResultsFromQueryTransactions(
  364. nextQueryTransactions,
  365. state.datasourceInstance,
  366. state.queryIntervals.intervalMs
  367. );
  368. return { ...state, ...results, queryTransactions: nextQueryTransactions, showingTable };
  369. }
  370. }
  371. return state;
  372. };
  373. /**
  374. * Global Explore reducer that handles multiple Explore areas (left and right).
  375. * Actions that have an `exploreId` get routed to the ExploreItemReducer.
  376. */
  377. export const exploreReducer = (state = initialExploreState, action: Action): ExploreState => {
  378. switch (action.type) {
  379. case ActionTypes.SplitClose: {
  380. return { ...state, split: false };
  381. }
  382. case ActionTypes.SplitOpen: {
  383. return { ...state, split: true, right: action.payload.itemState };
  384. }
  385. case ActionTypes.InitializeExploreSplit: {
  386. return { ...state, split: true };
  387. }
  388. case ActionTypes.ResetExplore: {
  389. return initialExploreState;
  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. };