actions.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, DataSourcePluginMeta } from '@grafana/ui';
  9. import { 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<DataSourcePluginMeta>('LOAD_DATA_SOURCE_META').create();
  16. export const dataSourceTypesLoad = noPayloadActionCreatorFactory('LOAD_DATA_SOURCE_TYPES').create();
  17. export const dataSourceTypesLoaded = actionCreatorFactory<DataSourcePluginMeta[]>('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<DataSourcePluginMeta>
  28. | ActionOf<DataSourcePluginMeta[]>
  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)) as DataSourcePluginMeta;
  41. dispatch(dataSourceLoaded(dataSource));
  42. dispatch(dataSourceMetaLoaded(pluginInfo));
  43. dispatch(updateNavIndex(buildNavModel(dataSource, pluginInfo)));
  44. };
  45. }
  46. export function addDataSource(plugin: DataSourcePluginMeta): 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 as DataSourcePluginMeta[]));
  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. interface ItemWithName {
  86. name: string;
  87. }
  88. export function nameExits(dataSources: ItemWithName[], name: string) {
  89. return (
  90. dataSources.filter(dataSource => {
  91. return dataSource.name.toLowerCase() === name.toLowerCase();
  92. }).length > 0
  93. );
  94. }
  95. export function findNewName(dataSources: ItemWithName[], name: string) {
  96. // Need to loop through current data sources to make sure
  97. // the name doesn't exist
  98. while (nameExits(dataSources, name)) {
  99. // If there's a duplicate name that doesn't end with '-x'
  100. // we can add -1 to the name and be done.
  101. if (!nameHasSuffix(name)) {
  102. name = `${name}-1`;
  103. } else {
  104. // if there's a duplicate name that ends with '-x'
  105. // we can try to increment the last digit until the name is unique
  106. // remove the 'x' part and replace it with the new number
  107. name = `${getNewName(name)}${incrementLastDigit(getLastDigit(name))}`;
  108. }
  109. }
  110. return name;
  111. }
  112. function updateFrontendSettings() {
  113. return getBackendSrv()
  114. .get('/api/frontend/settings')
  115. .then(settings => {
  116. config.datasources = settings.datasources;
  117. config.defaultDatasource = settings.defaultDatasource;
  118. getDatasourceSrv().init();
  119. });
  120. }
  121. function nameHasSuffix(name: string) {
  122. return name.endsWith('-', name.length - 1);
  123. }
  124. function getLastDigit(name: string) {
  125. return parseInt(name.slice(-1), 10);
  126. }
  127. function incrementLastDigit(digit: number) {
  128. return isNaN(digit) ? 1 : digit + 1;
  129. }
  130. function getNewName(name: string) {
  131. return name.slice(0, name.length - 1);
  132. }