refine Semi UI alert disposition
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { alertValue, canAct, formatAlertTime, ruleCondition, thresholdText } from './alert';
|
||||
import { alertDeltaText, alertValue, canAct, formatAlertTime, ruleCondition, thresholdText } from './alert';
|
||||
|
||||
describe('alert domain helpers', () => {
|
||||
it('keeps trigger evidence and duration explicit', () => {
|
||||
const event = { triggerValue: 96, threshold: 80, thresholdHigh: 0, operator: 'gt', unit: 'km/h', durationSec: 60 };
|
||||
expect(alertValue(event)).toBe('96 km/h');
|
||||
expect(thresholdText(event)).toBe('> 80 km/h,持续 60 秒');
|
||||
expect(alertDeltaText(event)).toBe('+16 km/h');
|
||||
});
|
||||
|
||||
it('describes low-bound and non-numeric trigger outcomes without inventing a delta', () => {
|
||||
expect(alertDeltaText({ triggerValue: 12, threshold: 20, operator: 'lt', unit: '%' })).toBe('+8 %');
|
||||
expect(alertDeltaText({ triggerValue: 1, threshold: 1, operator: 'changed', unit: '' })).toBe('规则已命中');
|
||||
});
|
||||
|
||||
it('enforces valid workflow transitions', () => {
|
||||
|
||||
@@ -35,6 +35,17 @@ export function thresholdText(event: Pick<AlertEvent, 'operator' | 'threshold' |
|
||||
return `${operatorLabels[event.operator] ?? event.operator} ${formatZhNumber(Number(event.threshold.toFixed(2)), 2)} ${event.unit}${duration}`.trim();
|
||||
}
|
||||
|
||||
export function alertDeltaText(event: Pick<AlertEvent, 'operator' | 'triggerValue' | 'threshold' | 'unit'>) {
|
||||
if (!Number.isFinite(event.triggerValue) || !Number.isFinite(event.threshold)) return '规则已命中';
|
||||
const delta = event.operator === 'gt' || event.operator === 'gte'
|
||||
? event.triggerValue - event.threshold
|
||||
: event.operator === 'lt' || event.operator === 'lte'
|
||||
? event.threshold - event.triggerValue
|
||||
: Number.NaN;
|
||||
if (!Number.isFinite(delta)) return '规则已命中';
|
||||
return `+${formatZhNumber(Number(Math.max(0, delta).toFixed(2)), 2)} ${event.unit}`.trim();
|
||||
}
|
||||
|
||||
export function ruleCondition(rule: AlertRule, labels: Record<string, string> = metricLabels) {
|
||||
const threshold = rule.operator === 'changed' ? '状态变化' : rule.operator === 'between' || rule.operator === 'outside' ? `${operatorLabels[rule.operator]} ${rule.threshold}–${rule.thresholdHigh}` : rule.valueType === 'boolean' ? (rule.booleanThreshold ? '是' : '否') : `${operatorLabels[rule.operator] ?? rule.operator} ${rule.threshold}`;
|
||||
return `${labels[rule.metric] ?? rule.metric} ${threshold}${rule.durationSec ? ` · ${rule.durationSec} 秒` : ''}`;
|
||||
|
||||
Reference in New Issue
Block a user