helpers.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { combineReducers } from '@reduxjs/toolkit';
  2. import { LoadingState } from '@grafana/data';
  3. import { dashboardReducer } from 'app/features/dashboard/state/reducers';
  4. import { DashboardState, StoreState } from '../../../types';
  5. import { VariableAdapter } from '../adapters';
  6. import { NEW_VARIABLE_ID } from '../constants';
  7. import {
  8. DashboardVariableModel,
  9. initialVariableModelState,
  10. OrgVariableModel,
  11. UserVariableModel,
  12. VariableHide,
  13. VariableModel,
  14. } from '../types';
  15. import { keyedVariablesReducer, KeyedVariablesState } from './keyedVariablesReducer';
  16. import { getInitialTemplatingState, TemplatingState } from './reducers';
  17. import { VariablesState } from './types';
  18. export const getVariableState = (
  19. noOfVariables: number,
  20. inEditorIndex = -1,
  21. includeEmpty = false,
  22. includeSystem = false
  23. ): Record<string, VariableModel> => {
  24. const variables: Record<string, VariableModel> = {};
  25. if (includeSystem) {
  26. const dashboardModel: DashboardVariableModel = {
  27. ...initialVariableModelState,
  28. id: '__dashboard',
  29. name: '__dashboard',
  30. type: 'system',
  31. index: -3,
  32. skipUrlSync: true,
  33. hide: VariableHide.hideVariable,
  34. current: {
  35. value: {
  36. name: 'A dashboard title',
  37. uid: 'An dashboard UID',
  38. toString: () => 'A dashboard title',
  39. },
  40. },
  41. };
  42. const orgModel: OrgVariableModel = {
  43. ...initialVariableModelState,
  44. id: '__org',
  45. name: '__org',
  46. type: 'system',
  47. index: -2,
  48. skipUrlSync: true,
  49. hide: VariableHide.hideVariable,
  50. current: {
  51. value: {
  52. name: 'An org name',
  53. id: 1,
  54. toString: () => '1',
  55. },
  56. },
  57. };
  58. const userModel: UserVariableModel = {
  59. ...initialVariableModelState,
  60. id: '__user',
  61. name: '__user',
  62. type: 'system',
  63. index: -1,
  64. skipUrlSync: true,
  65. hide: VariableHide.hideVariable,
  66. current: {
  67. value: {
  68. login: 'admin',
  69. id: 1,
  70. email: 'admin@test',
  71. toString: () => '1',
  72. },
  73. },
  74. };
  75. variables[dashboardModel.id] = dashboardModel;
  76. variables[orgModel.id] = orgModel;
  77. variables[userModel.id] = userModel;
  78. }
  79. for (let index = 0; index < noOfVariables; index++) {
  80. variables[index] = {
  81. id: index.toString(),
  82. rootStateKey: 'key',
  83. type: 'query',
  84. name: `Name-${index}`,
  85. hide: VariableHide.dontHide,
  86. index,
  87. label: `Label-${index}`,
  88. skipUrlSync: false,
  89. global: false,
  90. state: LoadingState.NotStarted,
  91. error: null,
  92. description: null,
  93. };
  94. }
  95. if (includeEmpty) {
  96. variables[NEW_VARIABLE_ID] = {
  97. id: NEW_VARIABLE_ID,
  98. rootStateKey: 'key',
  99. type: 'query',
  100. name: `Name-${NEW_VARIABLE_ID}`,
  101. hide: VariableHide.dontHide,
  102. index: noOfVariables,
  103. label: `Label-${NEW_VARIABLE_ID}`,
  104. skipUrlSync: false,
  105. global: false,
  106. state: LoadingState.NotStarted,
  107. error: null,
  108. description: null,
  109. };
  110. }
  111. return variables;
  112. };
  113. export const getVariableTestContext = <Model extends VariableModel>(
  114. adapter: VariableAdapter<Model>,
  115. variableOverrides: Partial<Model> = {}
  116. ) => {
  117. const defaults: Partial<VariableModel> = {
  118. id: '0',
  119. rootStateKey: 'key',
  120. index: 0,
  121. name: '0',
  122. };
  123. const defaultVariable = {
  124. ...adapter.initialState,
  125. ...defaults,
  126. };
  127. const initialState: VariablesState = {
  128. '0': { ...defaultVariable, ...variableOverrides },
  129. };
  130. return { initialState };
  131. };
  132. export const getRootReducer = () =>
  133. combineReducers({
  134. dashboard: dashboardReducer,
  135. templating: keyedVariablesReducer,
  136. });
  137. export type RootReducerType = { dashboard: DashboardState; templating: KeyedVariablesState };
  138. export const getTemplatingRootReducer = () =>
  139. combineReducers({
  140. templating: keyedVariablesReducer,
  141. });
  142. export type TemplatingReducerType = { templating: KeyedVariablesState };
  143. export function getPreloadedState(
  144. key: string,
  145. templatingState: Partial<TemplatingState>
  146. ): Pick<StoreState, 'templating'> {
  147. return {
  148. templating: {
  149. lastKey: key,
  150. keys: {
  151. [key]: {
  152. ...getInitialTemplatingState(),
  153. ...templatingState,
  154. },
  155. },
  156. },
  157. };
  158. }