Skip to content

Commit

Permalink
upcoming: [DI-22132] - Add factories and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
vmangalr committed Jan 6, 2025
1 parent e8b29e4 commit fc7ce1a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 48 deletions.
6 changes: 5 additions & 1 deletion packages/api-v4/src/cloudpulse/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ export type AlertSeverityType = 0 | 1 | 2 | 3;
export type MetricAggregationType = 'avg' | 'sum' | 'min' | 'max' | 'count';
export type MetricOperatorType = 'eq' | 'gt' | 'lt' | 'gte' | 'lte';
export type AlertServiceType = 'linode' | 'dbaas';
type DimensionFilterOperatorType = 'eq' | 'neq' | 'startswith' | 'endswith';
export type DimensionFilterOperatorType =
| 'eq'
| 'neq'
| 'startswith'
| 'endswith';
export type AlertDefinitionType = 'system' | 'user';
export type AlertStatusType = 'enabled' | 'disabled';
export type CriteriaConditionType = 'ALL';
Expand Down
72 changes: 28 additions & 44 deletions packages/manager/src/factories/cloudpulse/alerts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
import Factory from 'src/factories/factoryProxy';
import { pickRandom } from 'src/utilities/random';

import type { DimensionFilter } from '@linode/api-v4';
import type { AlertDefinitionMetricCriteria } from '@linode/api-v4';
import type { Alert } from '@linode/api-v4';

export const alertDimensionsFactory = Factory.Sync.makeFactory<DimensionFilter>(
{
dimension_label: 'operating_system',
label: 'Operating System',
operator: Factory.each(() => pickRandom(['neq', 'eq'])),
value: Factory.each(() => pickRandom(['Windows', 'Linux'])),
}
);

export const alertRulesFactory = Factory.Sync.makeFactory<AlertDefinitionMetricCriteria>(
{
aggregation_type: Factory.each(() => pickRandom(['avg', 'sum'])),
dimension_filters: alertDimensionsFactory.buildList(1),
label: Factory.each(() => pickRandom(['CPU Usage', 'Memory Usage'])),
metric: Factory.each(() => pickRandom(['CPU Usage', 'Memory Usage'])),
operator: Factory.each(() => pickRandom(['eq', 'gt'])),
threshold: 60,
unit: Factory.each(() => pickRandom(['Bytes', 'Percentage'])),
}
);

export const alertFactory = Factory.Sync.makeFactory<Alert>({
channels: [],
created: new Date().toISOString(),
Expand All @@ -13,50 +37,10 @@ export const alertFactory = Factory.Sync.makeFactory<Alert>({
label: Factory.each((id) => `Alert-${id}`),
rule_criteria: {
rules: [
{
aggregation_type: 'avg',
dimension_filters: [
{
dimension_label: 'Test',
label: 'Test',
operator: 'eq',
value: '40',
},
],
label: 'CPU Usage',
metric: 'CPU Usage',
operator: 'gt',
threshold: 60,
unit: 'Bytes',
},
{
aggregation_type: 'avg',
dimension_filters: [
{
dimension_label: 'OperatingSystem',
label: 'OperatingSystem',
operator: 'eq',
value: 'MacOS',
},
{
dimension_label: 'OperatingSystem',
label: 'OperatingSystem',
operator: 'eq',
value: 'Windows',
},
{
dimension_label: 'Test',
label: 'Test',
operator: 'neq',
value: '40',
},
],
label: 'CPU Usage',
metric: 'CPU Usage',
operator: 'gt',
threshold: 50,
unit: 'Percentage',
},
...alertRulesFactory.buildList(2, {
dimension_filters: alertDimensionsFactory.buildList(2),
}),
...alertRulesFactory.buildList(1, { dimension_filters: [] }),
],
},
service_type: 'linode',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';

import NullComponent from 'src/components/NullComponent';

import { aggregationTypes, operators } from '../constants';
import { aggregationTypes, operatorLabel, operators } from '../constants';
import { DisplayAlertDetailChips } from './DisplayAlertDetailChips';

import type { AlertDefinitionMetricCriteria } from '@linode/api-v4';
Expand Down Expand Up @@ -59,7 +59,7 @@ export const RenderAlertMetricsAndDimensions = React.memo(
<DisplayAlertDetailChips
values={dimensionFilters.map(({ label, operator, value }) => [
label,
operator,
operatorLabel[operator],
value,
])}
label="Dimension Filter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const getAlertBoxStyles = (theme: Theme) => ({

/**
* Converts seconds into a human-readable minutes and seconds format.
*
* @param seconds The seconds that need to be converted into minutes.
* @returns A string representing the time in minutes and seconds.
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/manager/src/features/CloudPulse/Alerts/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
AlertSeverityType,
AlertStatusType,
DimensionFilterOperatorType,
MetricAggregationType,
MetricOperatorType,
} from '@linode/api-v4';
Expand Down Expand Up @@ -101,3 +102,10 @@ export const aggregationTypes: Record<MetricAggregationType, string> = {
min: 'Minimum',
sum: 'Sum',
};

export const operatorLabel: Record<DimensionFilterOperatorType, string> = {
endswith: 'ends with',
eq: 'equals',
neq: 'not equals',
startswith: 'starts with',
};

0 comments on commit fc7ce1a

Please sign in to comment.