reducers.ts 14 KB

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