actions.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { ThunkAction } from 'redux-thunk';
  2. import config from '../../../core/config';
  3. import { getBackendSrv } from 'app/core/services/backend_srv';
  4. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  5. import { LayoutMode } from 'app/core/components/LayoutSelector/LayoutSelector';
  6. import { updateLocation, updateNavIndex, UpdateNavIndexAction } from 'app/core/actions';
  7. import { buildNavModel } from './navModel';
  8. import { DataSourceSettings } from '@grafana/ui/src/types';
  9. import { Plugin, StoreState, LocationUpdate } from 'app/types';
  10. import { actionCreatorFactory } from 'app/core/redux';
  11. import { ActionOf, noPayloadActionCreatorFactory } from 'app/core/redux/actionCreatorFactory';
  12. import { getPluginSettings } from 'app/features/plugins/PluginSettingsCache';
  13. export const dataSourceLoaded = actionCreatorFactory<DataSourceSettings>('LOAD_DATA_SOURCE').create();
  14. export const dataSourcesLoaded = actionCreatorFactory<DataSourceSettings[]>('LOAD_DATA_SOURCES').create();
  15. export const dataSourceMetaLoaded = actionCreatorFactory<Plugin>('LOAD_DATA_SOURCE_META').create();
  16. export const dataSourceTypesLoad = noPayloadActionCreatorFactory('LOAD_DATA_SOURCE_TYPES').create();
  17. export const dataSourceTypesLoaded = actionCreatorFactory<Plugin[]>('LOADED_DATA_SOURCE_TYPES').create();
  18. export const setDataSourcesSearchQuery = actionCreatorFactory<string>('SET_DATA_SOURCES_SEARCH_QUERY').create();
  19. export const setDataSourcesLayoutMode = actionCreatorFactory<LayoutMode>('SET_DATA_SOURCES_LAYOUT_MODE').create();
  20. export const setDataSourceTypeSearchQuery = actionCreatorFactory<string>('SET_DATA_SOURCE_TYPE_SEARCH_QUERY').create();
  21. export const setDataSourceName = actionCreatorFactory<string>('SET_DATA_SOURCE_NAME').create();
  22. export const setIsDefault = actionCreatorFactory<boolean>('SET_IS_DEFAULT').create();
  23. export type Action =
  24. | UpdateNavIndexAction
  25. | ActionOf<DataSourceSettings>
  26. | ActionOf<DataSourceSettings[]>
  27. | ActionOf<Plugin>
  28. | ActionOf<Plugin[]>
  29. | ActionOf<LocationUpdate>;
  30. type ThunkResult<R> = ThunkAction<R, StoreState, undefined, Action>;
  31. export function loadDataSources(): ThunkResult<void> {
  32. return async dispatch => {
  33. const response = await getBackendSrv().get('/api/datasources');
  34. dispatch(dataSourcesLoaded(response));
  35. };
  36. }
  37. export function loadDataSource(id: number): ThunkResult<void> {
  38. return async dispatch => {
  39. const dataSource = await getBackendSrv().get(`/api/datasources/${id}`);
  40. const pluginInfo = await getPluginSettings(dataSource.type);
  41. dispatch(dataSourceLoaded(dataSource));
  42. dispatch(dataSourceMetaLoaded(pluginInfo));
  43. dispatch(updateNavIndex(buildNavModel(dataSource, pluginInfo)));
  44. };
  45. }
  46. export function addDataSource(plugin: Plugin): ThunkResult<void> {
  47. return async (dispatch, getStore) => {
  48. await dispatch(loadDataSources());
  49. const dataSources = getStore().dataSources.dataSources;
  50. const newInstance = {
  51. name: plugin.name,
  52. type: plugin.id,
  53. access: 'proxy',
  54. isDefault: dataSources.length === 0,
  55. };
  56. if (nameExits(dataSources, newInstance.name)) {
  57. newInstance.name = findNewName(dataSources, newInstance.name);
  58. }
  59. const result = await getBackendSrv().post('/api/datasources', newInstance);
  60. dispatch(updateLocation({ path: `/datasources/edit/${result.id}` }));
  61. };
  62. }
  63. export function loadDataSourceTypes(): ThunkResult<void> {
  64. return async dispatch => {
  65. dispatch(dataSourceTypesLoad());
  66. const result = await getBackendSrv().get('/api/plugins', { enabled: 1, type: 'datasource' });
  67. dispatch(dataSourceTypesLoaded(result));
  68. };
  69. }
  70. export function updateDataSource(dataSource: DataSourceSettings): ThunkResult<void> {
  71. return async dispatch => {
  72. await getBackendSrv().put(`/api/datasources/${dataSource.id}`, dataSource);
  73. await updateFrontendSettings();
  74. return dispatch(loadDataSource(dataSource.id));
  75. };
  76. }
  77. export function deleteDataSource(): ThunkResult<void> {
  78. return async (dispatch, getStore) => {
  79. const dataSource = getStore().dataSources.dataSource;
  80. await getBackendSrv().delete(`/api/datasources/${dataSource.id}`);
  81. await updateFrontendSettings();
  82. dispatch(updateLocation({ path: '/datasources' }));
  83. };
  84. }
  85. export function nameExits(dataSources, name) {
  86. return (
  87. dataSources.filter(dataSource => {
  88. return dataSource.name.toLowerCase() === name.toLowerCase();
  89. }).length > 0
  90. );
  91. }
  92. export function findNewName(dataSources, name) {
  93. // Need to loop through current data sources to make sure
  94. // the name doesn't exist
  95. while (nameExits(dataSources, name)) {
  96. // If there's a duplicate name that doesn't end with '-x'
  97. // we can add -1 to the name and be done.
  98. if (!nameHasSuffix(name)) {
  99. name = `${name}-1`;
  100. } else {
  101. // if there's a duplicate name that ends with '-x'
  102. // we can try to increment the last digit until the name is unique
  103. // remove the 'x' part and replace it with the new number
  104. name = `${getNewName(name)}${incrementLastDigit(getLastDigit(name))}`;
  105. }
  106. }
  107. return name;
  108. }
  109. function updateFrontendSettings() {
  110. return getBackendSrv()
  111. .get('/api/frontend/settings')
  112. .then(settings => {
  113. config.datasources = settings.datasources;
  114. config.defaultDatasource = settings.defaultDatasource;
  115. getDatasourceSrv().init();
  116. });
  117. }
  118. function nameHasSuffix(name) {
  119. return name.endsWith('-', name.length - 1);
  120. }
  121. function getLastDigit(name) {
  122. return parseInt(name.slice(-1), 10);
  123. }
  124. function incrementLastDigit(digit) {
  125. return isNaN(digit) ? 1 : digit + 1;
  126. }
  127. function getNewName(name) {
  128. return name.slice(0, name.length - 1);
  129. }