actions.ts 5.5 KB

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