Skip to content

Commit

Permalink
catch undefined and return default empty in normalizeMetrics
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
  • Loading branch information
Sebastian Malton committed Jun 15, 2020
1 parent 8f0c785 commit 2b90d13
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
14 changes: 14 additions & 0 deletions dashboard/client/api/endpoints/metrics.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions dashboard/client/components/+cluster/cluster.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,16 @@ export class ClusterStore extends KubeObjectStore<Cluster> {
this.liveMetrics = await this.loadMetrics({ start, end, step, range });
}

getMetricsValues(source: Partial<IClusterMetrics>) {
const metrics =
this.metricType === MetricType.CPU ? source.cpuUsage :
this.metricType === MetricType.MEMORY ? source.memoryUsage
: null;
if (!metrics) {
getMetricsValues(source: Partial<IClusterMetrics>): [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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export const IngressCharts = observer(() => {
if (!metrics) return null;
if (isMetricsEmpty(metrics)) return <NoMetrics/>;

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export const ContainerCharts = () => {
if (!metrics) return null;
if (isMetricsEmpty(metrics)) return <NoMetrics/>;

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,
Expand Down
6 changes: 3 additions & 3 deletions dashboard/client/components/+workloads-pods/pod-charts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export const PodCharts = observer(() => {
if (isMetricsEmpty(metrics)) return <NoMetrics/>;

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,
Expand Down

0 comments on commit 2b90d13

Please # to comment.