Skip to content

The open alternative to Salesforce, designed for AI.

fix: prevent NaN in health indicator calculations (#19378)

eason7c2a9abed442
diff --git a/packages/twenty-server/src/engine/core-modules/admin-panel/indicators/database.health.ts b/packages/twenty-server/src/engine/core-modules/admin-panel/indicators/database.health.ts
index c79982bcce4..f2273f84b84 100644
--- a/packages/twenty-server/src/engine/core-modules/admin-panel/indicators/database.health.ts
+++ b/packages/twenty-server/src/engine/core-modules/admin-panel/indicators/database.health.ts
@@ -6,6 +6,7 @@ import {
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
+import { isDefined } from 'twenty-shared/utils';
import { HEALTH_ERROR_MESSAGES } from 'src/engine/core-modules/admin-panel/constants/health-error-messages.constants';
import { withHealthCheckTimeout } from 'src/engine/core-modules/admin-panel/utils/health-check-timeout.util';
@@ -89,7 +90,9 @@ export class DatabaseHealthIndicator {
},
databaseSize: databaseSize.size,
performance: {
- cacheHitRatio: Math.round(parseFloat(cacheHitRatio.ratio)) + '%',
+ cacheHitRatio: isDefined(cacheHitRatio.ratio)
+ ? Math.round(parseFloat(cacheHitRatio.ratio)) + '%'
+ : 'N/A',
deadlocks: parseInt(deadlocks.deadlocks),
slowQueries: parseInt(slowQueries.count),
},
diff --git a/packages/twenty-server/src/engine/core-modules/admin-panel/indicators/redis.health.ts b/packages/twenty-server/src/engine/core-modules/admin-panel/indicators/redis.health.ts
index 84034e0d833..1e4fed56e19 100644
--- a/packages/twenty-server/src/engine/core-modules/admin-panel/indicators/redis.health.ts
+++ b/packages/twenty-server/src/engine/core-modules/admin-panel/indicators/redis.health.ts
@@ -4,6 +4,8 @@ import {
HealthIndicatorService,
} from '@nestjs/terminus';
+import { isDefined } from 'twenty-shared/utils';
+
import { HEALTH_ERROR_MESSAGES } from 'src/engine/core-modules/admin-panel/constants/health-error-messages.constants';
import { withHealthCheckTimeout } from 'src/engine/core-modules/admin-panel/utils/health-check-timeout.util';
import { HealthStateManager } from 'src/engine/core-modules/admin-panel/utils/health-state-manager.util';
@@ -70,14 +72,19 @@ export class RedisHealthIndicator {
},
performance: {
opsPerSecond: parseInt(statsData.instantaneous_ops_per_sec),
- hitRate: statsData.keyspace_hits
- ? Math.round(
- (parseInt(statsData.keyspace_hits) /
- (parseInt(statsData.keyspace_hits) +
- parseInt(statsData.keyspace_misses))) *
- 100,
- ) + '%'
- : '0%',
+ hitRate:
+ isDefined(statsData.keyspace_hits) &&
+ isDefined(statsData.keyspace_misses)
+ ? (() => {
+ const hits = parseInt(statsData.keyspace_hits);
+ const misses = parseInt(statsData.keyspace_misses);
+ const total = hits + misses;
+
+ return total > 0
+ ? Math.round((hits / total) * 100) + '%'
+ : '0%';
+ })()
+ : '0%',
evictedKeys: parseInt(statsData.evicted_keys),
expiredKeys: parseInt(statsData.expired_keys),
},