Skip to content

Commit eabd8ef

Browse files
committed
fix: handle null and undefined cases in flattenObjectProperty and ensure safe summation in AppStats
1 parent ca891e8 commit eabd8ef

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

justfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ bump-version level="patch":
458458

459459
level = "{{ level }}"
460460
valid_levels = ["major", "minor", "patch"]
461-
461+
462462
if level not in valid_levels:
463463
print(f"Error: Invalid level. Use 'major', 'minor', or 'patch'.", file=sys.stderr)
464464
sys.exit(1)
@@ -470,16 +470,16 @@ bump-version level="patch":
470470
"--format", "json",
471471
"--increment", level
472472
], capture_output=True, text=True, check=True)
473-
473+
474474
version_info = json.loads(result.stdout)
475475
current_version = version_info["current_version"]
476476
new_version = version_info["new_version"]
477-
477+
478478
print(f"Bumping version from {current_version} to {new_version}")
479-
479+
480480
# Bump version
481481
subprocess.run(["uvx", "bump-my-version", "bump", level], check=True)
482-
482+
483483
except subprocess.CalledProcessError as e:
484484
print(f"Error running bump-my-version: {e}", file=sys.stderr)
485485
sys.exit(e.returncode)

src-frontend/components/apps/app-stats.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,15 @@ function formatType(type: number): string {
7979
}
8080

8181
const flattenObjectProperty = <T extends object>(
82-
obj: T,
82+
obj: T | null | undefined,
8383
property: string,
8484
childrenKey: keyof T,
8585
): Array<unknown> => {
86+
// Handle null or undefined objects
87+
if (!obj) {
88+
return [];
89+
}
90+
8691
const getNestedValue = (obj: object, path: string): unknown => {
8792
return path
8893
.split(".")
@@ -312,13 +317,13 @@ export function AppStats({ appId }: { appId: string }) {
312317
stats,
313318
"cpuPercent",
314319
"children",
315-
).reduce((acc, curr) => acc + (curr as number), 0);
320+
).reduce((acc, curr) => acc + ((curr as number) || 0), 0);
316321

317322
const cumulativeNumThreads = flattenObjectProperty(
318323
stats,
319324
"numThreads",
320325
"children",
321-
).reduce((acc, curr) => acc + (curr as number), 0);
326+
).reduce((acc, curr) => acc + ((curr as number) || 0), 0);
322327

323328
const totalMemory =
324329
((stats.memoryInfo?.rss ?? 0) / stats.memoryPercent) * 100;
@@ -327,31 +332,33 @@ export function AppStats({ appId }: { appId: string }) {
327332
stats,
328333
"memoryPercent",
329334
"children",
330-
).reduce((acc, curr) => acc + (curr as number), 0);
335+
).reduce((acc, curr) => acc + ((curr as number) || 0), 0);
331336

332337
const cumulativeMemoryRSS = flattenObjectProperty(
333338
stats,
334339
"memoryInfo.rss",
335340
"children",
336-
).reduce((acc, curr) => acc + (curr as number), 0);
341+
).reduce((acc, curr) => acc + ((curr as number) || 0), 0);
337342

338343
const cumulativeMemoryVMS =
339344
flattenObjectProperty(stats, "memoryInfo.vms", "children").reduce(
340-
(acc, curr) => acc + (curr as number),
345+
(acc, curr) => acc + ((curr as number) || 0),
341346
0,
342347
) / 1000;
343348

344349
const cumulativeMemorySwap = flattenObjectProperty(
345350
stats,
346351
"memoryInfo.swap",
347352
"children",
348-
).reduce((acc, curr) => acc + (curr as number), 0);
353+
).reduce((acc, curr) => acc + ((curr as number) || 0), 0);
349354

350355
const allConnections: ConnectionStat[] = flattenObjectProperty(
351356
stats,
352357
"connections",
353358
"children",
354-
).flat() as ConnectionStat[];
359+
)
360+
.flat()
361+
.filter(Boolean) as ConnectionStat[];
355362

356363
return (
357364
<div className="flex h-full flex-col gap-2">

0 commit comments

Comments
 (0)