import * as React from 'react'; import type { ICloudMetricStatistics } from '@spinnaker/core'; import { NumberInput, ReactSelectInput, usePrevious } from '@spinnaker/core'; import { MetricSelector } from './MetricSelector'; import { MetricAlarmChart } from '../../chart/MetricAlarmChart'; import type { IAmazonServerGroup, IScalingPolicyAlarm, IStepAdjustment } from '../../../../../domain'; import './AlarmConfigurer.less'; export interface IAlarmConfigurerProps { alarm: IScalingPolicyAlarm; multipleAlarms: boolean; serverGroup: IAmazonServerGroup; stepAdjustments: IStepAdjustment[]; stepsChanged: (steps: IStepAdjustment[]) => void; updateAlarm: (alarm: IScalingPolicyAlarm) => void; } const STATISTICS = ['Average', 'Maximum', 'Minimum', 'SampleCount', 'Sum']; export const COMPARATORS = [ { label: '>=', value: 'GreaterThanOrEqualToThreshold' }, { label: '>', value: 'GreaterThanThreshold' }, { label: '<=', value: 'LessThanOrEqualToThreshold' }, { label: '<', value: 'LessThanThreshold' }, ]; const PERIODS = [ { label: '1 minute', value: 60 }, { label: '5 minutes', value: 60 * 5 }, { label: '15 minutes', value: 60 * 15 }, { label: '1 hour', value: 60 * 60 }, { label: '4 hours', value: 60 * 60 * 4 }, { label: '1 day', value: 60 * 60 * 24 }, ]; export const AlarmConfigurer = ({ alarm, multipleAlarms, serverGroup, stepAdjustments, stepsChanged, updateAlarm, }: IAlarmConfigurerProps) => { const comparatorBound = alarm.comparisonOperator?.indexOf('Greater') === 0 ? 'max' : 'min'; const [unit, setUnit] = React.useState(alarm?.unit); const prevComparator = usePrevious(comparatorBound); React.useEffect(() => { if (stepAdjustments && prevComparator !== undefined) { const source = comparatorBound === 'max' ? 'metricIntervalLowerBound' : 'metricIntervalUpperBound'; const newStep: IStepAdjustment = { scalingAdjustment: 1, [source]: alarm.threshold, }; stepsChanged([newStep]); } }, [comparatorBound]); React.useEffect(() => { const source = comparatorBound === 'max' ? 'metricIntervalLowerBound' : 'metricIntervalUpperBound'; if (stepAdjustments?.length) { const updatedStepAdjustments = [...stepAdjustments]; // Always set the first step at the alarm threshold updatedStepAdjustments[0][source] = alarm.threshold; stepsChanged(updatedStepAdjustments); } }, [alarm.threshold]); const onChartLoaded = (stats: ICloudMetricStatistics) => setUnit(stats.unit); const onAlarmChange = (key: string, value: any) => { const newAlarm = { ...alarm, [key]: value, }; updateAlarm(newAlarm); }; const onMetricChange = (newAlarm: IScalingPolicyAlarm) => { updateAlarm(newAlarm); }; return (
{multipleAlarms && (

This scaling policy is configured with multiple alarms. You are only editing the first alarm.

To edit or remove the additional alarms, you will need to use the AWS console.

)} {alarm.alarmActionArns?.length > 1 && (

This alarm is used in multiple scaling policies. Any changes here will affect those other scaling policies.

)}
Whenever
onAlarmChange('statistic', e.target.value)} stringOptions={STATISTICS} clearable={false} inputClassName="sp-margin-xs-right configurer-field-lg" /> of
is
onAlarmChange('comparisonOperator', e.target.value)} options={COMPARATORS} clearable={false} inputClassName="sp-margin-s-right configurer-field-small" />
onAlarmChange('threshold', Number.parseInt(e.target.value))} inputClassName="sp-margin-xs-right configurer-field-lg" />
{unit}
for at least
onAlarmChange('evaluationPeriods', Number.parseInt(e.target.value))} inputClassName="configurer-field-med number-input-field" /> consecutive period(s) of onAlarmChange('period', e.target.value)} options={PERIODS} clearable={false} inputClassName="sp-margin-xs-right configurer-field-lg" />
{alarm && (
)}
); };