actions.ts 5.9 KB

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