From e98b4241684c284bcba39e7be5ae6faeecac4a16 Mon Sep 17 00:00:00 2001 From: Nawaz Dhandala Date: Thu, 2 Apr 2026 18:11:42 +0100 Subject: [PATCH] refactor: improve code readability by formatting and using optional chaining for req.body --- .../Exceptions/ExceptionsDashboard.tsx | 83 ++++---- .../Components/Profiles/ProfilesDashboard.tsx | 12 +- .../src/Components/Traces/TracesDashboard.tsx | 182 +++++++++--------- .../Middleware/OtelRequestMiddleware.ts | 8 +- .../Services/OtelLogsIngestService.ts | 2 +- .../Services/OtelMetricsIngestService.ts | 2 +- .../Services/OtelProfilesIngestService.ts | 2 +- .../Services/OtelTracesIngestService.ts | 2 +- 8 files changed, 135 insertions(+), 158 deletions(-) diff --git a/App/FeatureSet/Dashboard/src/Components/Exceptions/ExceptionsDashboard.tsx b/App/FeatureSet/Dashboard/src/Components/Exceptions/ExceptionsDashboard.tsx index 29d6d79b5b..392af433b4 100644 --- a/App/FeatureSet/Dashboard/src/Components/Exceptions/ExceptionsDashboard.tsx +++ b/App/FeatureSet/Dashboard/src/Components/Exceptions/ExceptionsDashboard.tsx @@ -33,9 +33,9 @@ const ExceptionsDashboard: FunctionComponent = (): ReactElement => { const [unresolvedCount, setUnresolvedCount] = useState(0); const [resolvedCount, setResolvedCount] = useState(0); const [archivedCount, setArchivedCount] = useState(0); - const [topExceptions, setTopExceptions] = useState< - Array - >([]); + const [topExceptions, setTopExceptions] = useState>( + [], + ); const [serviceSummaries, setServiceSummaries] = useState< Array >([]); @@ -274,9 +274,7 @@ const ExceptionsDashboard: FunctionComponent = (): ReactElement => { -

- Needs attention -

+

Needs attention

@@ -310,9 +308,7 @@ const ExceptionsDashboard: FunctionComponent = (): ReactElement => { -

- Fixed and verified -

+

Fixed and verified

@@ -397,10 +393,7 @@ const ExceptionsDashboard: FunctionComponent = (): ReactElement => { ] as Route, ) .toString() - .replace( - /\/$/, - `/${exception.fingerprint}`, - ), + .replace(/\/$/, `/${exception.fingerprint}`), ) : RouteUtil.populateRouteParams( RouteMap[ @@ -436,13 +429,9 @@ const ExceptionsDashboard: FunctionComponent = (): ReactElement => {

- {( - exception.occuranceCount || 0 - ).toLocaleString()} -

-

- occurrences + {(exception.occuranceCount || 0).toLocaleString()}

+

occurrences

@@ -475,40 +464,34 @@ const ExceptionsDashboard: FunctionComponent = (): ReactElement => {
- {serviceSummaries.map( - (summary: ServiceExceptionSummary) => { - return ( -
-
- -
-
-

- {summary.unresolvedCount} -

-

- unresolved -

-
-
-

- {summary.totalOccurrences.toLocaleString()} -

-

- total hits -

-
+ {serviceSummaries.map((summary: ServiceExceptionSummary) => { + return ( +
+
+ +
+
+

+ {summary.unresolvedCount} +

+

unresolved

+
+
+

+ {summary.totalOccurrences.toLocaleString()} +

+

total hits

- ); - }, - )} +
+ ); + })}
diff --git a/App/FeatureSet/Dashboard/src/Components/Profiles/ProfilesDashboard.tsx b/App/FeatureSet/Dashboard/src/Components/Profiles/ProfilesDashboard.tsx index 88aea5bca5..6115ac5d3e 100644 --- a/App/FeatureSet/Dashboard/src/Components/Profiles/ProfilesDashboard.tsx +++ b/App/FeatureSet/Dashboard/src/Components/Profiles/ProfilesDashboard.tsx @@ -122,7 +122,7 @@ const ProfilesDashboard: FunctionComponent = (): ReactElement => { for (const profile of profiles) { const serviceId: string = profile.serviceId?.toString() || ""; - let summary: ServiceProfileSummary | undefined = + const summary: ServiceProfileSummary | undefined = summaryMap.get(serviceId); if (!summary) { @@ -192,7 +192,7 @@ const ProfilesDashboard: FunctionComponent = (): ReactElement => { "functions" ] || []) as unknown as Array; setHotspots(functions); - } catch (_hotspotsErr) { + } catch { // Hotspots are optional - don't fail the whole page setHotspots([]); } @@ -334,9 +334,7 @@ const ProfilesDashboard: FunctionComponent = (): ReactElement => { to={RouteUtil.populateRouteParams( RouteMap[PageMap.SERVICE_VIEW_PROFILES] as Route, { - modelId: new ObjectID( - summary.service._id as string, - ), + modelId: new ObjectID(summary.service._id as string), }, )} > @@ -368,9 +366,7 @@ const ProfilesDashboard: FunctionComponent = (): ReactElement => { # Function Source File - - Own Time - + Own Time Total Time diff --git a/App/FeatureSet/Dashboard/src/Components/Traces/TracesDashboard.tsx b/App/FeatureSet/Dashboard/src/Components/Traces/TracesDashboard.tsx index 1768cf650b..3470be1216 100644 --- a/App/FeatureSet/Dashboard/src/Components/Traces/TracesDashboard.tsx +++ b/App/FeatureSet/Dashboard/src/Components/Traces/TracesDashboard.tsx @@ -52,16 +52,14 @@ const TracesDashboard: FunctionComponent = (): ReactElement => { const [recentErrorTraces, setRecentErrorTraces] = useState< Array >([]); - const [recentSlowTraces, setRecentSlowTraces] = useState< - Array - >([]); + const [recentSlowTraces, setRecentSlowTraces] = useState>( + [], + ); const [services, setServices] = useState>([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(""); - const formatDuration: (nanos: number) => string = ( - nanos: number, - ): string => { + const formatDuration: (nanos: number) => string = (nanos: number): string => { if (nanos >= 1_000_000_000) { return `${(nanos / 1_000_000_000).toFixed(2)}s`; } @@ -370,9 +368,7 @@ const TracesDashboard: FunctionComponent = (): ReactElement => { to={RouteUtil.populateRouteParams( RouteMap[PageMap.SERVICE_VIEW_TRACES] as Route, { - modelId: new ObjectID( - summary.service._id as string, - ), + modelId: new ObjectID(summary.service._id as string), }, )} > @@ -406,50 +402,48 @@ const TracesDashboard: FunctionComponent = (): ReactElement => { ) : (
- {recentErrorTraces.map( - (trace: RecentTrace, index: number) => { - return ( - -
-
- -
-

- {trace.name} -

-

- {getServiceName(trace.serviceId)} -

-
-
-
-

- {formatDuration(trace.durationNano)} + {recentErrorTraces.map((trace: RecentTrace, index: number) => { + return ( + +

+
+ +
+

+ {trace.name}

-

- {OneUptimeDate.getDateAsLocalFormattedString( - trace.startTime, - true, - )} +

+ {getServiceName(trace.serviceId)}

- - ); - }, - )} +
+

+ {formatDuration(trace.durationNano)} +

+

+ {OneUptimeDate.getDateAsLocalFormattedString( + trace.startTime, + true, + )} +

+
+
+ + ); + })}
)} @@ -474,57 +468,55 @@ const TracesDashboard: FunctionComponent = (): ReactElement => { ) : (
- {recentSlowTraces.map( - (trace: RecentTrace, index: number) => { - const maxDuration: number = - recentSlowTraces[0]?.durationNano || 1; - const barWidth: number = - (trace.durationNano / maxDuration) * 100; + {recentSlowTraces.map((trace: RecentTrace, index: number) => { + const maxDuration: number = + recentSlowTraces[0]?.durationNano || 1; + const barWidth: number = + (trace.durationNano / maxDuration) * 100; - return ( - -
-
- -
-

- {trace.name} -

-

- {getServiceName(trace.serviceId)} -

-
-
-
-

- {formatDuration(trace.durationNano)} + return ( + +

+
+ +
+

+ {trace.name} +

+

+ {getServiceName(trace.serviceId)}

-
-
-
-
+
+

+ {formatDuration(trace.durationNano)} +

- - ); - }, - )} +
+
+
+
+
+
+ + ); + })}
)} diff --git a/App/FeatureSet/Telemetry/Middleware/OtelRequestMiddleware.ts b/App/FeatureSet/Telemetry/Middleware/OtelRequestMiddleware.ts index 30efe60a9a..ede2a01ebb 100644 --- a/App/FeatureSet/Telemetry/Middleware/OtelRequestMiddleware.ts +++ b/App/FeatureSet/Telemetry/Middleware/OtelRequestMiddleware.ts @@ -13,7 +13,13 @@ import path from "path"; // Load proto file for OTel -const PROTO_DIR: string = path.resolve(__dirname, "..", "ProtoFiles", "OTel", "v1"); +const PROTO_DIR: string = path.resolve( + __dirname, + "..", + "ProtoFiles", + "OTel", + "v1", +); // Create a root namespace const LogsProto: protobuf.Root = protobuf.loadSync( diff --git a/App/FeatureSet/Telemetry/Services/OtelLogsIngestService.ts b/App/FeatureSet/Telemetry/Services/OtelLogsIngestService.ts index bc8d6d9f68..d1d0ffd76a 100644 --- a/App/FeatureSet/Telemetry/Services/OtelLogsIngestService.ts +++ b/App/FeatureSet/Telemetry/Services/OtelLogsIngestService.ts @@ -65,7 +65,7 @@ export default class OtelLogsIngestService extends OtelIngestBaseService { ); } - req.body = req.body.toJSON ? req.body.toJSON() : req.body; + req.body = req.body?.toJSON ? req.body.toJSON() : req.body; Response.sendEmptySuccessResponse(req, res); diff --git a/App/FeatureSet/Telemetry/Services/OtelMetricsIngestService.ts b/App/FeatureSet/Telemetry/Services/OtelMetricsIngestService.ts index 2bb0f9bec3..cad53d2b1c 100644 --- a/App/FeatureSet/Telemetry/Services/OtelMetricsIngestService.ts +++ b/App/FeatureSet/Telemetry/Services/OtelMetricsIngestService.ts @@ -73,7 +73,7 @@ export default class OtelMetricsIngestService extends OtelIngestBaseService { ); } - req.body = req.body.toJSON ? req.body.toJSON() : req.body; + req.body = req.body?.toJSON ? req.body.toJSON() : req.body; Response.sendEmptySuccessResponse(req, res); diff --git a/App/FeatureSet/Telemetry/Services/OtelProfilesIngestService.ts b/App/FeatureSet/Telemetry/Services/OtelProfilesIngestService.ts index 912953c8c7..695dec0be1 100644 --- a/App/FeatureSet/Telemetry/Services/OtelProfilesIngestService.ts +++ b/App/FeatureSet/Telemetry/Services/OtelProfilesIngestService.ts @@ -94,7 +94,7 @@ export default class OtelProfilesIngestService extends OtelIngestBaseService { ); } - req.body = req.body.toJSON ? req.body.toJSON() : req.body; + req.body = req.body?.toJSON ? req.body.toJSON() : req.body; Response.sendEmptySuccessResponse(req, res); diff --git a/App/FeatureSet/Telemetry/Services/OtelTracesIngestService.ts b/App/FeatureSet/Telemetry/Services/OtelTracesIngestService.ts index f1e9d2f0a7..c67935d65d 100644 --- a/App/FeatureSet/Telemetry/Services/OtelTracesIngestService.ts +++ b/App/FeatureSet/Telemetry/Services/OtelTracesIngestService.ts @@ -120,7 +120,7 @@ export default class OtelTracesIngestService extends OtelIngestBaseService { ); } - req.body = req.body.toJSON ? req.body.toJSON() : req.body; + req.body = req.body?.toJSON ? req.body.toJSON() : req.body; Response.sendEmptySuccessResponse(req, res);