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 { UpdateLocationAction } from 'app/core/actions/location';
  8. import { buildNavModel } from './navModel';
  9. import { DataSourceSettings } from '@grafana/ui/src/types';
  10. import { Plugin, StoreState } from 'app/types';
  11. import { actionCreatorFactory } from 'app/core/redux';
  12. import { ActionOf, noPayloadActionCreatorFactory } from 'app/core/redux/actionCreatorFactory';
  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. | UpdateLocationAction
  25. | UpdateNavIndexAction
  26. | ActionOf<DataSourceSettings>
  27. | ActionOf<DataSourceSettings[]>
  28. | ActionOf<Plugin>
  29. | ActionOf<Plugin[]>;
  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 getBackendSrv().get(`/api/plugins/${dataSource.type}/settings`);
  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. dispatch(updateLocation({ path: '/datasources' }));
  82. };
  83. }
  84. export function nameExits(dataSources, name) {
  85. return (
  86. dataSources.filter(dataSource => {
  87. return dataSource.name.toLowerCase() === name.toLowerCase();
  88. }).length > 0
  89. );
  90. }
  91. export function findNewName(dataSources, name) {
  92. // Need to loop through current data sources to make sure
  93. // the name doesn't exist
  94. while (nameExits(dataSources, name)) {
  95. // If there's a duplicate name that doesn't end with '-x'
  96. // we can add -1 to the name and be done.
  97. if (!nameHasSuffix(name)) {
  98. name = `${name}-1`;
  99. } else {
  100. // if there's a duplicate name that ends with '-x'
  101. // we can try to increment the last digit until the name is unique
  102. // remove the 'x' part and replace it with the new number
  103. name = `${getNewName(name)}${incrementLastDigit(getLastDigit(name))}`;
  104. }
  105. }
  106. return name;
  107. }
  108. function updateFrontendSettings() {
  109. return getBackendSrv()
  110. .get('/api/frontend/settings')
  111. .then(settings => {
  112. config.datasources = settings.datasources;
  113. config.defaultDatasource = settings.defaultDatasource;
  114. getDatasourceSrv().init();
  115. });
  116. }
  117. function nameHasSuffix(name) {
  118. return name.endsWith('-', name.length - 1);
  119. }
  120. function getLastDigit(name) {
  121. return parseInt(name.slice(-1), 10);
  122. }
  123. function incrementLastDigit(digit) {
  124. return isNaN(digit) ? 1 : digit + 1;
  125. }
  126. function getNewName(name) {
  127. return name.slice(0, name.length - 1);
  128. }