2020-03-16 07:45:51 -05:00
|
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
2021-04-21 02:38:00 -05:00
|
|
|
import { map } from 'lodash';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-02-17 23:06:04 -06:00
|
|
|
import { getInstanceState } from '../state/selectors';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { initialVariablesState, VariablePayload, VariablesState } from '../state/types';
|
|
|
|
import { initialVariableModelState, IntervalVariableModel, VariableOption, VariableRefresh } from '../types';
|
2020-03-16 07:45:51 -05:00
|
|
|
|
|
|
|
export const initialIntervalVariableModelState: IntervalVariableModel = {
|
2020-10-02 00:02:06 -05:00
|
|
|
...initialVariableModelState,
|
2020-03-16 07:45:51 -05:00
|
|
|
type: 'interval',
|
|
|
|
auto_count: 30,
|
|
|
|
auto_min: '10s',
|
|
|
|
options: [],
|
|
|
|
auto: false,
|
|
|
|
query: '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d',
|
|
|
|
refresh: VariableRefresh.onTimeRangeChanged,
|
|
|
|
current: {} as VariableOption,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const intervalVariableSlice = createSlice({
|
|
|
|
name: 'templating/interval',
|
|
|
|
initialState: initialVariablesState,
|
|
|
|
reducers: {
|
|
|
|
createIntervalOptions: (state: VariablesState, action: PayloadAction<VariablePayload>) => {
|
2022-08-10 10:06:49 -05:00
|
|
|
const instanceState = getInstanceState(state, action.payload.id);
|
|
|
|
if (instanceState.type !== 'interval') {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-21 02:38:00 -05:00
|
|
|
const options: VariableOption[] = map(instanceState.query.match(/(["'])(.*?)\1|\w+/g), (text) => {
|
2020-03-16 07:45:51 -05:00
|
|
|
text = text.replace(/["']+/g, '');
|
|
|
|
return { text: text.trim(), value: text.trim(), selected: false };
|
|
|
|
});
|
|
|
|
|
|
|
|
if (instanceState.auto) {
|
|
|
|
// add auto option if missing
|
|
|
|
if (options.length && options[0].text !== 'auto') {
|
|
|
|
options.unshift({
|
|
|
|
text: 'auto',
|
|
|
|
value: '$__auto_interval_' + instanceState.name,
|
|
|
|
selected: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceState.options = options;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const intervalVariableReducer = intervalVariableSlice.reducer;
|
|
|
|
|
|
|
|
export const { createIntervalOptions } = intervalVariableSlice.actions;
|