mirror of https://github.com/grafana/grafana
parent
7b06800295
commit
2a64d19f5b
@ -0,0 +1,26 @@ |
||||
import { Dispatch } from 'redux'; |
||||
import { getBackendSrv } from 'app/core/services/backend_srv'; |
||||
import { AlertRule } from 'app/types'; |
||||
|
||||
export interface LoadAlertRulesAction { |
||||
type: 'LOAD_ALERT_RULES'; |
||||
payload: AlertRule[]; |
||||
} |
||||
|
||||
export const loadAlertRules = (rules: AlertRule[]): LoadAlertRulesAction => ({ |
||||
type: 'LOAD_ALERT_RULES', |
||||
payload: rules, |
||||
}); |
||||
|
||||
export type Action = LoadAlertRulesAction; |
||||
|
||||
export const getAlertRulesAsync = () => async (dispatch: Dispatch<Action>): Promise<AlertRule[]> => { |
||||
try { |
||||
const rules = await getBackendSrv().get('/api/alerts', {}); |
||||
dispatch(loadAlertRules(rules)); |
||||
return rules; |
||||
} catch (error) { |
||||
console.error(error); |
||||
throw error; |
||||
} |
||||
}; |
||||
@ -1,52 +0,0 @@ |
||||
import { getBackendSrv } from 'app/core/services/backend_srv'; |
||||
import alertDef from './alertDef'; |
||||
import moment from 'moment'; |
||||
|
||||
export interface AlertRule { |
||||
id: number; |
||||
dashboardId: number; |
||||
panelId: number; |
||||
name: string; |
||||
state: string; |
||||
stateText: string; |
||||
stateIcon: string; |
||||
stateClass: string; |
||||
stateAge: string; |
||||
info?: string; |
||||
url: string; |
||||
} |
||||
|
||||
export function setStateFields(rule, state) { |
||||
const stateModel = alertDef.getStateDisplayModel(state); |
||||
rule.state = state; |
||||
rule.stateText = stateModel.text; |
||||
rule.stateIcon = stateModel.iconClass; |
||||
rule.stateClass = stateModel.stateClass; |
||||
rule.stateAge = moment(rule.newStateDate) |
||||
.fromNow() |
||||
.replace(' ago', ''); |
||||
} |
||||
|
||||
export const getAlertRules = async (): Promise<AlertRule[]> => { |
||||
try { |
||||
const rules = await getBackendSrv().get('/api/alerts', {}); |
||||
|
||||
for (const rule of rules) { |
||||
setStateFields(rule, rule.state); |
||||
|
||||
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 rules; |
||||
} catch (error) { |
||||
console.error(error); |
||||
throw error; |
||||
} |
||||
}; |
||||
@ -0,0 +1,46 @@ |
||||
import { Action } from './actions'; |
||||
import { AlertRule } from 'app/types'; |
||||
import alertDef from './alertDef'; |
||||
import moment from 'moment'; |
||||
|
||||
export const initialState: AlertRule[] = []; |
||||
|
||||
export function setStateFields(rule, state) { |
||||
const stateModel = alertDef.getStateDisplayModel(state); |
||||
rule.state = state; |
||||
rule.stateText = stateModel.text; |
||||
rule.stateIcon = stateModel.iconClass; |
||||
rule.stateClass = stateModel.stateClass; |
||||
rule.stateAge = moment(rule.newStateDate) |
||||
.fromNow() |
||||
.replace(' ago', ''); |
||||
} |
||||
|
||||
export const alertRulesReducer = (state = initialState, action: Action): AlertRule[] => { |
||||
switch (action.type) { |
||||
case 'LOAD_ALERT_RULES': { |
||||
const alertRules = action.payload; |
||||
|
||||
for (const rule of alertRules) { |
||||
setStateFields(rule, rule.state); |
||||
|
||||
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 alertRules; |
||||
} |
||||
} |
||||
|
||||
return state; |
||||
}; |
||||
|
||||
export default { |
||||
alertRules: alertRulesReducer, |
||||
}; |
||||
Loading…
Reference in new issue