Skip to content

Commit 721561b

Browse files
committed
- changed ManagedAgentsConsumption component to only have a managedAgentFeature param
- added managedAgentFeature validation and stories for various failed states
1 parent 9d58c44 commit 721561b

File tree

3 files changed

+221
-46
lines changed

3 files changed

+221
-46
lines changed

site/src/pages/DeploymentSettingsPage/LicensesSettingsPage/LicensesSettingsPageView.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ const LicensesSettingsPageView: FC<Props> = ({
5151
}) => {
5252
const theme = useTheme();
5353
const { width, height } = useWindowSize();
54-
const managedAgentLimitStarts = managedAgentFeature?.usage_period?.start;
55-
const managedAgentLimitExpires = managedAgentFeature?.usage_period?.end;
56-
const managedAgentFeatureEnabled = managedAgentFeature?.enabled;
5754

5855
return (
5956
<>
@@ -158,15 +155,8 @@ const LicensesSettingsPageView: FC<Props> = ({
158155
/>
159156
)}
160157

161-
{licenses && licenses.length > 0 && managedAgentFeature && (
162-
<ManagedAgentsConsumption
163-
usage={managedAgentFeature.actual || 0}
164-
included={managedAgentFeature.soft_limit || 0}
165-
limit={managedAgentFeature.limit || 0}
166-
startDate={managedAgentLimitStarts || ""}
167-
endDate={managedAgentLimitExpires || ""}
168-
enabled={managedAgentFeatureEnabled}
169-
/>
158+
{licenses && licenses.length > 0 && (
159+
<ManagedAgentsConsumption managedAgentFeature={managedAgentFeature} />
170160
)}
171161
</div>
172162
</>

site/src/pages/DeploymentSettingsPage/LicensesSettingsPage/ManagedAgentsConsumption.stories.tsx

Lines changed: 156 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ const meta: Meta<typeof ManagedAgentsConsumption> = {
66
"pages/DeploymentSettingsPage/LicensesSettingsPage/ManagedAgentsConsumption",
77
component: ManagedAgentsConsumption,
88
args: {
9-
usage: 50000,
10-
included: 60000,
11-
limit: 120000,
12-
startDate: "February 27, 2025",
13-
endDate: "February 27, 2026",
9+
managedAgentFeature: {
10+
enabled: true,
11+
actual: 50000,
12+
soft_limit: 60000,
13+
limit: 120000,
14+
usage_period: {
15+
start: "February 27, 2025",
16+
end: "February 27, 2026",
17+
issued_at: "February 27, 2025",
18+
},
19+
entitlement: "entitled",
20+
},
1421
},
1522
};
1623

@@ -21,41 +28,169 @@ export const Default: Story = {};
2128

2229
export const NearLimit: Story = {
2330
args: {
24-
usage: 115000,
25-
included: 60000,
26-
limit: 120000,
31+
managedAgentFeature: {
32+
enabled: true,
33+
actual: 115000,
34+
soft_limit: 60000,
35+
limit: 120000,
36+
usage_period: {
37+
start: "February 27, 2025",
38+
end: "February 27, 2026",
39+
issued_at: "February 27, 2025",
40+
},
41+
entitlement: "entitled",
42+
},
2743
},
2844
};
2945

3046
export const OverIncluded: Story = {
3147
args: {
32-
usage: 80000,
33-
included: 60000,
34-
limit: 120000,
48+
managedAgentFeature: {
49+
enabled: true,
50+
actual: 80000,
51+
soft_limit: 60000,
52+
limit: 120000,
53+
usage_period: {
54+
start: "February 27, 2025",
55+
end: "February 27, 2026",
56+
issued_at: "February 27, 2025",
57+
},
58+
entitlement: "entitled",
59+
},
3560
},
3661
};
3762

3863
export const LowUsage: Story = {
3964
args: {
40-
usage: 25000,
41-
included: 60000,
42-
limit: 120000,
65+
managedAgentFeature: {
66+
enabled: true,
67+
actual: 25000,
68+
soft_limit: 60000,
69+
limit: 120000,
70+
usage_period: {
71+
start: "February 27, 2025",
72+
end: "February 27, 2026",
73+
issued_at: "February 27, 2025",
74+
},
75+
entitlement: "entitled",
76+
},
4377
},
4478
};
4579

4680
export const IncludedAtLimit: Story = {
4781
args: {
48-
usage: 25000,
49-
included: 30500,
50-
limit: 30500,
82+
managedAgentFeature: {
83+
enabled: true,
84+
actual: 25000,
85+
soft_limit: 30500,
86+
limit: 30500,
87+
usage_period: {
88+
start: "February 27, 2025",
89+
end: "February 27, 2026",
90+
issued_at: "February 27, 2025",
91+
},
92+
entitlement: "entitled",
93+
},
5194
},
5295
};
5396

5497
export const Disabled: Story = {
5598
args: {
56-
enabled: false,
57-
usage: Number.NaN,
58-
included: Number.NaN,
59-
limit: Number.NaN,
99+
managedAgentFeature: {
100+
enabled: false,
101+
actual: undefined,
102+
soft_limit: undefined,
103+
limit: undefined,
104+
usage_period: undefined,
105+
entitlement: "not_entitled",
106+
},
107+
},
108+
};
109+
110+
export const NoFeature: Story = {
111+
args: {
112+
managedAgentFeature: undefined,
113+
},
114+
};
115+
116+
// Error States for Validation
117+
export const ErrorMissingData: Story = {
118+
args: {
119+
managedAgentFeature: {
120+
enabled: true,
121+
actual: undefined,
122+
soft_limit: undefined,
123+
limit: undefined,
124+
usage_period: undefined,
125+
entitlement: "entitled",
126+
},
127+
},
128+
};
129+
130+
export const ErrorNegativeValues: Story = {
131+
args: {
132+
managedAgentFeature: {
133+
enabled: true,
134+
actual: -100,
135+
soft_limit: 60000,
136+
limit: 120000,
137+
usage_period: {
138+
start: "February 27, 2025",
139+
end: "February 27, 2026",
140+
issued_at: "February 27, 2025",
141+
},
142+
entitlement: "entitled",
143+
},
144+
},
145+
};
146+
147+
export const ErrorSoftLimitExceedsLimit: Story = {
148+
args: {
149+
managedAgentFeature: {
150+
enabled: true,
151+
actual: 50000,
152+
soft_limit: 150000,
153+
limit: 120000,
154+
usage_period: {
155+
start: "February 27, 2025",
156+
end: "February 27, 2026",
157+
issued_at: "February 27, 2025",
158+
},
159+
entitlement: "entitled",
160+
},
161+
},
162+
};
163+
164+
export const ErrorInvalidDates: Story = {
165+
args: {
166+
managedAgentFeature: {
167+
enabled: true,
168+
actual: 50000,
169+
soft_limit: 60000,
170+
limit: 120000,
171+
usage_period: {
172+
start: "invalid-date",
173+
end: "February 27, 2026",
174+
issued_at: "February 27, 2025",
175+
},
176+
entitlement: "entitled",
177+
},
178+
},
179+
};
180+
181+
export const ErrorEndBeforeStart: Story = {
182+
args: {
183+
managedAgentFeature: {
184+
enabled: true,
185+
actual: 50000,
186+
soft_limit: 60000,
187+
limit: 120000,
188+
usage_period: {
189+
start: "February 27, 2026",
190+
end: "February 27, 2025",
191+
issued_at: "February 27, 2025",
192+
},
193+
entitlement: "entitled",
194+
},
60195
},
61196
};

site/src/pages/DeploymentSettingsPage/LicensesSettingsPage/ManagedAgentsConsumption.tsx

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Interpolation, Theme } from "@emotion/react";
22
import MuiLink from "@mui/material/Link";
3+
import type { Feature } from "api/typesGenerated";
4+
import { ErrorAlert } from "components/Alert/ErrorAlert";
35
import { Button } from "components/Button/Button";
46
import {
57
Collapsible,
@@ -12,23 +14,65 @@ import { ChevronRightIcon } from "lucide-react";
1214
import type { FC } from "react";
1315

1416
interface ManagedAgentsConsumptionProps {
15-
usage: number;
16-
included: number;
17-
limit: number;
18-
startDate: string;
19-
endDate: string;
20-
enabled?: boolean;
17+
managedAgentFeature?: Feature;
2118
}
2219

20+
const validateFeature = (feature?: Feature): string | null => {
21+
if (!feature) {
22+
return null; // No feature is valid (will show disabled state)
23+
}
24+
25+
// If enabled, we need valid numeric data
26+
if (feature.enabled) {
27+
if (
28+
feature.actual === undefined ||
29+
feature.soft_limit === undefined ||
30+
feature.limit === undefined
31+
) {
32+
return "Managed agent feature is enabled but missing required usage data (actual, soft_limit, or limit).";
33+
}
34+
35+
if (
36+
feature.actual < 0 ||
37+
feature.soft_limit < 0 ||
38+
feature.limit < 0
39+
) {
40+
return "Managed agent feature contains invalid negative values for usage metrics.";
41+
}
42+
43+
if (feature.soft_limit > feature.limit) {
44+
return "Managed agent feature has invalid configuration: soft limit exceeds total limit.";
45+
}
46+
47+
// Validate usage period if present
48+
if (feature.usage_period) {
49+
const start = dayjs(feature.usage_period.start);
50+
const end = dayjs(feature.usage_period.end);
51+
52+
if (!start.isValid() || !end.isValid()) {
53+
return "Managed agent feature has invalid usage period dates.";
54+
}
55+
56+
if (end.isBefore(start)) {
57+
return "Managed agent feature has invalid usage period: end date is before start date.";
58+
}
59+
}
60+
}
61+
62+
return null; // Valid
63+
};
64+
2365
export const ManagedAgentsConsumption: FC<ManagedAgentsConsumptionProps> = ({
24-
usage,
25-
included,
26-
limit,
27-
startDate,
28-
endDate,
29-
enabled = true,
66+
managedAgentFeature,
3067
}) => {
31-
if (!enabled) {
68+
// Validate the feature data
69+
const validationError = validateFeature(managedAgentFeature);
70+
if (validationError) {
71+
return <ErrorAlert error={new Error(validationError)} />;
72+
}
73+
74+
// If no feature is provided or it's disabled, show disabled state
75+
if (!managedAgentFeature?.enabled) {
3276
return (
3377
<div css={styles.disabledRoot}>
3478
<Stack alignItems="center" spacing={1}>
@@ -48,6 +92,12 @@ export const ManagedAgentsConsumption: FC<ManagedAgentsConsumptionProps> = ({
4892
);
4993
}
5094

95+
const usage = managedAgentFeature.actual || 0;
96+
const included = managedAgentFeature.soft_limit || 0;
97+
const limit = managedAgentFeature.limit || 0;
98+
const startDate = managedAgentFeature.usage_period?.start || "";
99+
const endDate = managedAgentFeature.usage_period?.end || "";
100+
51101
const usagePercentage = Math.min((usage / limit) * 100, 100);
52102
const includedPercentage = Math.min((included / limit) * 100, 100);
53103
const remainingPercentage = Math.max(100 - includedPercentage, 0);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy