From 09ab43fcefcd494b5ed7140f78909cf8881d4972 Mon Sep 17 00:00:00 2001 From: Matt Ray Date: Mon, 19 Jun 2023 18:41:52 +1000 Subject: [PATCH] Fixes for eslint "guard-for-in" Signed-off-by: Matt Ray --- .../components/AllocationChart/RangeChart.js | 40 ++++++++------ .../AllocationChart/SummaryChart.js | 52 +++++++++++-------- plugins/opencost/src/components/Details.js | 1 + .../OpenCostReport/OpenCostReport.tsx | 6 ++- 4 files changed, 58 insertions(+), 41 deletions(-) diff --git a/plugins/opencost/src/components/AllocationChart/RangeChart.js b/plugins/opencost/src/components/AllocationChart/RangeChart.js index 0c74cccbda..e5a7af8044 100644 --- a/plugins/opencost/src/components/AllocationChart/RangeChart.js +++ b/plugins/opencost/src/components/AllocationChart/RangeChart.js @@ -81,10 +81,12 @@ function toBarLabels(allocationRange) { const labels = [] for (const key in keyToFill) { - labels.push({ - dataKey: key, - fill: keyToFill[key], - }) + if (Object.hasOwn(keyToFill, key)) { + labels.push({ + dataKey: key, + fill: keyToFill[key], + }) + } } return reverse(labels) @@ -95,24 +97,30 @@ function toBar(datum) { const bar = {} for (const key in top) { - const allocation = top[key] - const start = new Date(allocation.start) - bar.start = `${start.getUTCFullYear()}-${start.getUTCMonth()+1}-${start.getUTCDate()}` - bar[allocation.name] = allocation.totalCost + if (Object.hasOwn(top, key)) { + const allocation = top[key] + const start = new Date(allocation.start) + bar.start = `${start.getUTCFullYear()}-${start.getUTCMonth()+1}-${start.getUTCDate()}` + bar[allocation.name] = allocation.totalCost + } } for (const key in other) { - const allocation = other[key] - const start = new Date(allocation.start) - bar.start = `${start.getUTCFullYear()}-${start.getUTCMonth()+1}-${start.getUTCDate()}` - bar[allocation.name] = allocation.totalCost + if (Object.hasOwn(other, key)) { + const allocation = other[key] + const start = new Date(allocation.start) + bar.start = `${start.getUTCFullYear()}-${start.getUTCMonth()+1}-${start.getUTCDate()}` + bar[allocation.name] = allocation.totalCost + } } for (const key in idle) { - const allocation = idle[key] - const start = new Date(allocation.start) - bar.start = `${start.getUTCFullYear()}-${start.getUTCMonth()+1}-${start.getUTCDate()}` - bar[allocation.name] = allocation.totalCost + if (Object.hasOwn(idle, key)) { + const allocation = idle[key] + const start = new Date(allocation.start) + bar.start = `${start.getUTCFullYear()}-${start.getUTCMonth()+1}-${start.getUTCDate()}` + bar[allocation.name] = allocation.totalCost + } } return bar diff --git a/plugins/opencost/src/components/AllocationChart/SummaryChart.js b/plugins/opencost/src/components/AllocationChart/SummaryChart.js index 7431c7984c..977ee395a3 100644 --- a/plugins/opencost/src/components/AllocationChart/SummaryChart.js +++ b/plugins/opencost/src/components/AllocationChart/SummaryChart.js @@ -23,36 +23,42 @@ function toPieData(top, other, idle) { const slices = [] for (const i in top) { - const allocation = top[i] - const fill = allocation.name === "__unallocated__" - ? "#212121" - : primary[i % primary.length] + if (Object.hasOwn(top, i)) { + const allocation = top[i] + const fill = allocation.name === "__unallocated__" + ? "#212121" + : primary[i % primary.length] - slices.push({ - name: allocation.name, - value: allocation.totalCost, - fill: fill, - }) + slices.push({ + name: allocation.name, + value: allocation.totalCost, + fill: fill, + }) + } } for (const i in other) { - const allocation = other[i] - const fill = browns[i % browns.length] - slices.push({ - name: allocation.name, - value: allocation.totalCost, - fill: fill, - }) + if (Object.hasOwn(other, i)) { + const allocation = other[i] + const fill = browns[i % browns.length] + slices.push({ + name: allocation.name, + value: allocation.totalCost, + fill: fill, + }) + } } for (const i in idle) { - const allocation = idle[i] - const fill = greyscale[i % greyscale.length] - slices.push({ - name: allocation.name, - value: allocation.totalCost, - fill: fill, - }) + if (Object.hasOwn(idle, i)) { + const allocation = idle[i] + const fill = greyscale[i % greyscale.length] + slices.push({ + name: allocation.name, + value: allocation.totalCost, + fill: fill, + }) + } } return slices diff --git a/plugins/opencost/src/components/Details.js b/plugins/opencost/src/components/Details.js index ff09336f97..8be2038cee 100644 --- a/plugins/opencost/src/components/Details.js +++ b/plugins/opencost/src/components/Details.js @@ -156,6 +156,7 @@ const Details = ({ setRows(data) } catch (e) { + /* eslint no-console: ["error", { allow: ["warn"] }] */ console.warn(`Error fetching details for (${controllerKind}, ${controller}):`, e) setErrors([{ primary: "Error fetching details", diff --git a/plugins/opencost/src/components/OpenCostReport/OpenCostReport.tsx b/plugins/opencost/src/components/OpenCostReport/OpenCostReport.tsx index 2e93e64d98..c84070aa2b 100644 --- a/plugins/opencost/src/components/OpenCostReport/OpenCostReport.tsx +++ b/plugins/opencost/src/components/OpenCostReport/OpenCostReport.tsx @@ -174,8 +174,10 @@ export const OpenCostReport = () => { if (resp.data && resp.data.length > 0) { const allocationRange = resp.data for (const i in allocationRange) { - // update cluster aggregations to use clusterName/clusterId names - allocationRange[i] = sortBy(allocationRange[i], a => a.totalCost) + if (Object.hasOwn(allocationRange, i)) { + // update cluster aggregations to use clusterName/clusterId names + allocationRange[i] = sortBy(allocationRange[i], a => a.totalCost) + } } setAllocationData(allocationRange) } else {