mirror of
https://github.com/grafana/grafana.git
synced 2025-02-16 18:34:52 -06:00
* Refactor: Adds Redux Toolkit package * Refactor: Uses configureStore from Redux Toolkit * Refactor: Migrates applicationReducer * Refactor: Migrates appNotificationsReducer * Refactor: Migrates locationReducer * Refactor: Migrates navModelReducer * Refactor: Migrates teamsReducer and teamReducer * Refactor: Migrates cleanUpAction * Refactor: Migrates alertRulesReducer * Refactor: Cleans up recursiveCleanState * Refactor: Switched to Angular compatible reducers * Refactor: Migrates folderReducer * Refactor: Migrates dashboardReducer * Migrates panelEditorReducer * Refactor: Migrates dataSourcesReducer * Refactor: Migrates usersReducer * Refactor: Migrates organizationReducer * Refactor: Migrates pluginsReducer * Refactor: Migrates ldapReducer and ldapUserReducer * Refactor: Migrates apiKeysReducer * Refactor: Migrates exploreReducer and itemReducer * Refactor: Removes actionCreatorFactory and reducerFactory * Refactor: Moves mocks to test section * Docs: Removes sections about home grown framework * Update contribute/style-guides/redux.md Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Refactor: Cleans up some code * Refactor: Adds state typings * Refactor: Cleans up typings * Refactor: Adds comment about ImmerJs autoFreeze Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { AlertRule, AlertRuleDTO, AlertRulesState } from 'app/types';
|
|
import alertDef from './alertDef';
|
|
import { dateTime } from '@grafana/data';
|
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
export const initialState: AlertRulesState = { items: [], searchQuery: '', isLoading: false };
|
|
|
|
function convertToAlertRule(dto: AlertRuleDTO, state: string): AlertRule {
|
|
const stateModel = alertDef.getStateDisplayModel(state);
|
|
|
|
const rule: AlertRule = {
|
|
...dto,
|
|
stateText: stateModel.text,
|
|
stateIcon: stateModel.iconClass,
|
|
stateClass: stateModel.stateClass,
|
|
stateAge: dateTime(dto.newStateDate).fromNow(true),
|
|
};
|
|
|
|
if (rule.state !== 'paused') {
|
|
if (rule.executionError) {
|
|
rule.info = 'Execution Error: ' + rule.executionError;
|
|
}
|
|
if (rule.evalData && rule.evalData.noData) {
|
|
rule.info = 'Query returned no data';
|
|
}
|
|
}
|
|
|
|
return rule;
|
|
}
|
|
|
|
const alertRulesSlice = createSlice({
|
|
name: 'alertRules',
|
|
initialState,
|
|
reducers: {
|
|
loadAlertRules: state => {
|
|
return { ...state, isLoading: true };
|
|
},
|
|
loadedAlertRules: (state, action: PayloadAction<AlertRuleDTO[]>): AlertRulesState => {
|
|
const alertRules: AlertRuleDTO[] = action.payload;
|
|
|
|
const alertRulesViewModel: AlertRule[] = alertRules.map(rule => {
|
|
return convertToAlertRule(rule, rule.state);
|
|
});
|
|
|
|
return { ...state, items: alertRulesViewModel, isLoading: false };
|
|
},
|
|
setSearchQuery: (state, action: PayloadAction<string>): AlertRulesState => {
|
|
return { ...state, searchQuery: action.payload };
|
|
},
|
|
},
|
|
});
|
|
|
|
export const { loadAlertRules, loadedAlertRules, setSearchQuery } = alertRulesSlice.actions;
|
|
|
|
export const alertRulesReducer = alertRulesSlice.reducer;
|
|
|
|
export default {
|
|
alertRules: alertRulesReducer,
|
|
};
|