From 2b90d130c2793fcfdb877ac9e27015d69af51bd1 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 12 Jun 2020 14:33:37 -0400 Subject: [PATCH] catch undefined and return default empty in normalizeMetrics Signed-off-by: Sebastian Malton --- dashboard/client/api/endpoints/metrics.api.ts | 14 ++++++++++++++ .../client/components/+cluster/cluster.store.ts | 15 ++++++++------- .../+network-ingresses/ingress-charts.tsx | 6 +++--- .../+workloads-pods/container-charts.tsx | 6 +++--- .../components/+workloads-pods/pod-charts.tsx | 6 +++--- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/dashboard/client/api/endpoints/metrics.api.ts b/dashboard/client/api/endpoints/metrics.api.ts index ed6260c225..eac3522a3f 100644 --- a/dashboard/client/api/endpoints/metrics.api.ts +++ b/dashboard/client/api/endpoints/metrics.api.ts @@ -56,7 +56,21 @@ export const metricsApi = { }; export function normalizeMetrics(metrics: IMetrics, frames = 60): IMetrics { + if (!metrics?.data?.result) { + return { + data: { + resultType: "", + result: [{ + metric: {}, + values: [] + } as IMetricsResult], + }, + status: "", + } + } + const { result } = metrics.data; + if (result.length) { if (frames > 0) { // fill the gaps diff --git a/dashboard/client/components/+cluster/cluster.store.ts b/dashboard/client/components/+cluster/cluster.store.ts index 066ecad2f2..f3ff8db12e 100644 --- a/dashboard/client/components/+cluster/cluster.store.ts +++ b/dashboard/client/components/+cluster/cluster.store.ts @@ -82,15 +82,16 @@ export class ClusterStore extends KubeObjectStore { this.liveMetrics = await this.loadMetrics({ start, end, step, range }); } - getMetricsValues(source: Partial) { - const metrics = - this.metricType === MetricType.CPU ? source.cpuUsage : - this.metricType === MetricType.MEMORY ? source.memoryUsage - : null; - if (!metrics) { + getMetricsValues(source: Partial): [number, string][] { + console.log(source) + switch (this.metricType) { + case MetricType.CPU: + return normalizeMetrics(source.cpuUsage).data.result[0].values + case MetricType.MEMORY: + return normalizeMetrics(source.memoryUsage).data.result[0].values + default: return []; } - return normalizeMetrics(metrics).data.result[0].values; } resetMetrics() { diff --git a/dashboard/client/components/+network-ingresses/ingress-charts.tsx b/dashboard/client/components/+network-ingresses/ingress-charts.tsx index a3aec31880..89ceb86b3d 100644 --- a/dashboard/client/components/+network-ingresses/ingress-charts.tsx +++ b/dashboard/client/components/+network-ingresses/ingress-charts.tsx @@ -18,9 +18,9 @@ export const IngressCharts = observer(() => { if (!metrics) return null; if (isMetricsEmpty(metrics)) return ; - const values = Object.values(metrics).map(metric => - normalizeMetrics(metric).data.result[0].values - ); + const values = Object.values(metrics) + .map(normalizeMetrics) + .map(({ data }) => data.result[0].values); const [ bytesSentSuccess, bytesSentFailure, diff --git a/dashboard/client/components/+workloads-pods/container-charts.tsx b/dashboard/client/components/+workloads-pods/container-charts.tsx index 138b659403..f879fa4662 100644 --- a/dashboard/client/components/+workloads-pods/container-charts.tsx +++ b/dashboard/client/components/+workloads-pods/container-charts.tsx @@ -17,9 +17,9 @@ export const ContainerCharts = () => { if (!metrics) return null; if (isMetricsEmpty(metrics)) return ; - const values = Object.values(metrics).map(metric => - normalizeMetrics(metric).data.result[0].values - ); + const values = Object.values(metrics) + .map(normalizeMetrics) + .map(({ data }) => data.result[0].values); const [ cpuUsage, cpuRequests, diff --git a/dashboard/client/components/+workloads-pods/pod-charts.tsx b/dashboard/client/components/+workloads-pods/pod-charts.tsx index b46a25d010..39439d9678 100644 --- a/dashboard/client/components/+workloads-pods/pod-charts.tsx +++ b/dashboard/client/components/+workloads-pods/pod-charts.tsx @@ -28,9 +28,9 @@ export const PodCharts = observer(() => { if (isMetricsEmpty(metrics)) return ; const options = tabId == 0 ? cpuOptions : memoryOptions; - const values = Object.values(metrics).map(metric => - normalizeMetrics(metric).data.result[0].values - ); + const values = Object.values(metrics) + .map(normalizeMetrics) + .map(({ data }) => data.result[0].values); const [ cpuUsage, cpuRequests,