|
| 1 | +import { IAlarm, IAlarmRule } from './alarm-base'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Enumeration indicates state of Alarm used in building Alarm Rule. |
| 5 | + */ |
| 6 | +export enum AlarmState { |
| 7 | + |
| 8 | + /** |
| 9 | + * State indicates resource is in ALARM |
| 10 | + */ |
| 11 | + ALARM = 'ALARM', |
| 12 | + |
| 13 | + /** |
| 14 | + * State indicates resource is not in ALARM |
| 15 | + */ |
| 16 | + OK = 'OK', |
| 17 | + |
| 18 | + /** |
| 19 | + * State indicates there is not enough data to determine is resource is in ALARM |
| 20 | + */ |
| 21 | + INSUFFICIENT_DATA = 'INSUFFICIENT_DATA', |
| 22 | + |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Enumeration of supported Composite Alarms operators. |
| 27 | + */ |
| 28 | +enum Operator { |
| 29 | + |
| 30 | + AND = 'AND', |
| 31 | + OR = 'OR', |
| 32 | + NOT = 'NOT', |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Class with static functions to build AlarmRule for Composite Alarms. |
| 38 | + */ |
| 39 | +export class AlarmRule { |
| 40 | + |
| 41 | + /** |
| 42 | + * function to join all provided AlarmRules with AND operator. |
| 43 | + * |
| 44 | + * @param operands IAlarmRules to be joined with AND operator. |
| 45 | + */ |
| 46 | + public static allOf(...operands: IAlarmRule[]): IAlarmRule { |
| 47 | + return this.concat(Operator.AND, ...operands); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * function to join all provided AlarmRules with OR operator. |
| 52 | + * |
| 53 | + * @param operands IAlarmRules to be joined with OR operator. |
| 54 | + */ |
| 55 | + public static anyOf(...operands: IAlarmRule[]): IAlarmRule { |
| 56 | + return this.concat(Operator.OR, ...operands); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * function to wrap provided AlarmRule in NOT operator. |
| 61 | + * |
| 62 | + * @param operand IAlarmRule to be wrapped in NOT operator. |
| 63 | + */ |
| 64 | + public static not(operand: IAlarmRule): IAlarmRule { |
| 65 | + // tslint:disable-next-line:new-parens |
| 66 | + return new class implements IAlarmRule { |
| 67 | + public renderAlarmRule(): string { |
| 68 | + return `(NOT (${operand.renderAlarmRule()}))`; |
| 69 | + } |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * function to build TRUE/FALSE intent for Rule Expression. |
| 75 | + * |
| 76 | + * @param value boolean value to be used in rule expression. |
| 77 | + */ |
| 78 | + public static fromBoolean(value: boolean): IAlarmRule { |
| 79 | + // tslint:disable-next-line:new-parens |
| 80 | + return new class implements IAlarmRule { |
| 81 | + public renderAlarmRule(): string { |
| 82 | + return `${String(value).toUpperCase()}`; |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * function to build Rule Expression for given IAlarm and AlarmState. |
| 89 | + * |
| 90 | + * @param alarm IAlarm to be used in Rule Expression. |
| 91 | + * @param alarmState AlarmState to be used in Rule Expression. |
| 92 | + */ |
| 93 | + public static fromAlarm(alarm: IAlarm, alarmState: AlarmState): IAlarmRule { |
| 94 | + // tslint:disable-next-line:new-parens |
| 95 | + return new class implements IAlarmRule { |
| 96 | + public renderAlarmRule(): string { |
| 97 | + return `${alarmState}(${alarm.alarmArn})`; |
| 98 | + } |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * function to build Rule Expression for given Alarm Rule string. |
| 104 | + * |
| 105 | + * @param alarmRule string to be used in Rule Expression. |
| 106 | + */ |
| 107 | + public static fromString(alarmRule: string): IAlarmRule { |
| 108 | + // tslint:disable-next-line:new-parens |
| 109 | + return new class implements IAlarmRule { |
| 110 | + public renderAlarmRule(): string { |
| 111 | + return alarmRule; |
| 112 | + } |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + private static concat(operator: Operator, ...operands: IAlarmRule[]): IAlarmRule { |
| 117 | + // tslint:disable-next-line:new-parens |
| 118 | + return new class implements IAlarmRule { |
| 119 | + public renderAlarmRule(): string { |
| 120 | + const expression = operands |
| 121 | + .map(operand => `${operand.renderAlarmRule()}`) |
| 122 | + .join(` ${operator} `); |
| 123 | + return `(${expression})`; |
| 124 | + } |
| 125 | + }; |
| 126 | + } |
| 127 | +} |
0 commit comments