2018-09-28 04:05:34 -05:00
|
|
|
import { ThunkAction } from 'redux-thunk';
|
2018-10-03 09:04:30 -05:00
|
|
|
import { DataSource, Plugin, StoreState } from 'app/types';
|
2018-09-28 04:05:34 -05:00
|
|
|
import { getBackendSrv } from '../../../core/services/backend_srv';
|
|
|
|
import { LayoutMode } from '../../../core/components/LayoutSelector/LayoutSelector';
|
2018-10-03 02:56:15 -05:00
|
|
|
import { updateLocation } from '../../../core/actions';
|
|
|
|
import { UpdateLocationAction } from '../../../core/actions/location';
|
2018-09-28 04:05:34 -05:00
|
|
|
|
|
|
|
export enum ActionTypes {
|
|
|
|
LoadDataSources = 'LOAD_DATA_SOURCES',
|
2018-10-03 02:56:15 -05:00
|
|
|
LoadDataSourceTypes = 'LOAD_DATA_SOURCE_TYPES',
|
2018-09-28 04:05:34 -05:00
|
|
|
SetDataSourcesSearchQuery = 'SET_DATA_SOURCES_SEARCH_QUERY',
|
|
|
|
SetDataSourcesLayoutMode = 'SET_DATA_SOURCES_LAYOUT_MODE',
|
2018-10-04 04:42:17 -05:00
|
|
|
SetDataSourceTypeSearchQuery = 'SET_DATA_SOURCE_TYPE_SEARCH_QUERY',
|
2018-09-28 04:05:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface LoadDataSourcesAction {
|
|
|
|
type: ActionTypes.LoadDataSources;
|
|
|
|
payload: DataSource[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface SetDataSourcesSearchQueryAction {
|
|
|
|
type: ActionTypes.SetDataSourcesSearchQuery;
|
|
|
|
payload: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface SetDataSourcesLayoutModeAction {
|
|
|
|
type: ActionTypes.SetDataSourcesLayoutMode;
|
|
|
|
payload: LayoutMode;
|
|
|
|
}
|
|
|
|
|
2018-10-03 02:56:15 -05:00
|
|
|
export interface LoadDataSourceTypesAction {
|
|
|
|
type: ActionTypes.LoadDataSourceTypes;
|
2018-10-03 09:04:30 -05:00
|
|
|
payload: Plugin[];
|
2018-10-03 02:56:15 -05:00
|
|
|
}
|
|
|
|
|
2018-10-04 04:42:17 -05:00
|
|
|
export interface SetDataSourceTypeSearchQueryAction {
|
|
|
|
type: ActionTypes.SetDataSourceTypeSearchQuery;
|
|
|
|
payload: string;
|
|
|
|
}
|
|
|
|
|
2018-09-28 04:05:34 -05:00
|
|
|
const dataSourcesLoaded = (dataSources: DataSource[]): LoadDataSourcesAction => ({
|
|
|
|
type: ActionTypes.LoadDataSources,
|
|
|
|
payload: dataSources,
|
|
|
|
});
|
|
|
|
|
2018-10-03 09:04:30 -05:00
|
|
|
const dataSourceTypesLoaded = (dataSourceTypes: Plugin[]): LoadDataSourceTypesAction => ({
|
2018-10-03 02:56:15 -05:00
|
|
|
type: ActionTypes.LoadDataSourceTypes,
|
|
|
|
payload: dataSourceTypes,
|
|
|
|
});
|
|
|
|
|
2018-09-28 04:05:34 -05:00
|
|
|
export const setDataSourcesSearchQuery = (searchQuery: string): SetDataSourcesSearchQueryAction => ({
|
|
|
|
type: ActionTypes.SetDataSourcesSearchQuery,
|
|
|
|
payload: searchQuery,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const setDataSourcesLayoutMode = (layoutMode: LayoutMode): SetDataSourcesLayoutModeAction => ({
|
|
|
|
type: ActionTypes.SetDataSourcesLayoutMode,
|
|
|
|
payload: layoutMode,
|
|
|
|
});
|
|
|
|
|
2018-10-04 04:42:17 -05:00
|
|
|
export const setDataSourceTypeSearchQuery = (query: string): SetDataSourceTypeSearchQueryAction => ({
|
|
|
|
type: ActionTypes.SetDataSourceTypeSearchQuery,
|
|
|
|
payload: query,
|
|
|
|
});
|
|
|
|
|
2018-10-03 02:56:15 -05:00
|
|
|
export type Action =
|
|
|
|
| LoadDataSourcesAction
|
|
|
|
| SetDataSourcesSearchQueryAction
|
|
|
|
| SetDataSourcesLayoutModeAction
|
|
|
|
| UpdateLocationAction
|
2018-10-04 04:42:17 -05:00
|
|
|
| LoadDataSourceTypesAction
|
|
|
|
| SetDataSourceTypeSearchQueryAction;
|
2018-09-28 04:05:34 -05:00
|
|
|
|
|
|
|
type ThunkResult<R> = ThunkAction<R, StoreState, undefined, Action>;
|
|
|
|
|
|
|
|
export function loadDataSources(): ThunkResult<void> {
|
|
|
|
return async dispatch => {
|
|
|
|
const response = await getBackendSrv().get('/api/datasources');
|
|
|
|
dispatch(dataSourcesLoaded(response));
|
|
|
|
};
|
|
|
|
}
|
2018-10-02 09:50:34 -05:00
|
|
|
|
2018-10-05 04:33:41 -05:00
|
|
|
export function addDataSource(plugin: Plugin): ThunkResult<void> {
|
|
|
|
return async (dispatch, getStore) => {
|
2018-10-06 12:22:16 -05:00
|
|
|
await dispatch(loadDataSources());
|
2018-10-05 04:33:41 -05:00
|
|
|
|
2018-10-06 12:22:16 -05:00
|
|
|
const dataSources = getStore().dataSources.dataSources;
|
2018-10-05 04:33:41 -05:00
|
|
|
|
2018-10-06 12:22:16 -05:00
|
|
|
const newInstance = {
|
|
|
|
name: plugin.name,
|
|
|
|
type: plugin.id,
|
|
|
|
access: 'proxy',
|
|
|
|
isDefault: dataSources.length === 0,
|
|
|
|
};
|
2018-10-05 04:33:41 -05:00
|
|
|
|
2018-10-06 12:22:16 -05:00
|
|
|
if (nameExits(dataSources, newInstance.name)) {
|
|
|
|
newInstance.name = findNewName(dataSources, newInstance.name);
|
2018-10-05 04:33:41 -05:00
|
|
|
}
|
|
|
|
|
2018-10-06 12:22:16 -05:00
|
|
|
const result = await getBackendSrv().post('/api/datasources', newInstance);
|
2018-10-03 02:56:15 -05:00
|
|
|
dispatch(updateLocation({ path: `/datasources/edit/${result.id}` }));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loadDataSourceTypes(): ThunkResult<void> {
|
|
|
|
return async dispatch => {
|
|
|
|
const result = await getBackendSrv().get('/api/plugins', { enabled: 1, type: 'datasource' });
|
|
|
|
dispatch(dataSourceTypesLoaded(result));
|
2018-10-02 09:50:34 -05:00
|
|
|
};
|
|
|
|
}
|
2018-10-05 04:33:41 -05:00
|
|
|
|
|
|
|
export function nameExits(dataSources, name) {
|
|
|
|
return (
|
|
|
|
dataSources.filter(dataSource => {
|
|
|
|
return dataSource.name === name;
|
|
|
|
}).length > 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findNewName(dataSources, name) {
|
|
|
|
// Need to loop through current data sources to make sure
|
|
|
|
// the name doesn't exist
|
|
|
|
while (nameExits(dataSources, name)) {
|
|
|
|
// If there's a duplicate name that doesn't end with '-x'
|
|
|
|
// we can add -1 to the name and be done.
|
|
|
|
if (!nameHasSuffix(name)) {
|
|
|
|
name = `${name}-1`;
|
|
|
|
} else {
|
|
|
|
// if there's a duplicate name that ends with '-x'
|
|
|
|
// we can try to increment the last digit until the name is unique
|
|
|
|
|
|
|
|
// remove the 'x' part and replace it with the new number
|
|
|
|
name = `${getNewName(name)}${incrementLastDigit(getLastDigit(name))}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
function nameHasSuffix(name) {
|
|
|
|
return name.endsWith('-', name.length - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLastDigit(name) {
|
|
|
|
return parseInt(name.slice(-1), 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
function incrementLastDigit(digit) {
|
|
|
|
return isNaN(digit) ? 1 : digit + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNewName(name) {
|
|
|
|
return name.slice(0, name.length - 1);
|
|
|
|
}
|