From 5bca58b00d31668c3682f06a881f9dec07f2e89b Mon Sep 17 00:00:00 2001 From: Qiuxia Fan Date: Wed, 13 Apr 2022 11:44:16 +0800 Subject: [PATCH] add calculation exp --- src/hooks/data.ts | 1 + src/hooks/useProcessor.ts | 29 ++++++++++++++----- .../configuration/widget/metric/Index.vue | 2 +- src/views/dashboard/data.ts | 5 ++-- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/hooks/data.ts b/src/hooks/data.ts index 00160b42..1909362d 100644 --- a/src/hooks/data.ts +++ b/src/hooks/data.ts @@ -33,6 +33,7 @@ export enum Calculations { ConvertSeconds = "convertSeconds", ConvertMilliseconds = "convertMilliseconds", MsTos = "msTos", + Average = "average", } export enum sizeEnum { XS = "XS", diff --git a/src/hooks/useProcessor.ts b/src/hooks/useProcessor.ts index 74d4f954..09d014a8 100644 --- a/src/hooks/useProcessor.ts +++ b/src/hooks/useProcessor.ts @@ -150,9 +150,7 @@ export function useSourceProcessor( const c = (config.metricConfig && config.metricConfig[index]) || {}; if (type === MetricQueryTypes.ReadMetricsValues) { - source[m] = resp.data[keys[index]].values.values.map( - (d: { value: number }) => aggregation(d.value, c) - ); + source[m] = calculateExp(resp.data[keys[index]].values.values, c); } if (type === MetricQueryTypes.ReadLabeledMetricsValues) { const resVal = Object.values(resp.data)[0] || []; @@ -166,7 +164,6 @@ export function useSourceProcessor( const values = item.values.values.map((d: { value: number }) => aggregation(Number(d.value), c) ); - const indexNum = labelsIdx.findIndex((d: string) => d === item.label); if (labels[indexNum] && indexNum > -1) { source[labels[indexNum]] = values; @@ -287,9 +284,7 @@ export function usePodsSource( d[name] = aggregation(resp.data[key], c); } if (config.metricTypes[index] === MetricQueryTypes.ReadMetricsValues) { - d[name] = resp.data[key].values.values.map((d: { value: number }) => - aggregation(d.value, c) - ); + d[name] = calculateExp(resp.data[key].values.values, c); } }); @@ -322,8 +317,26 @@ export function useQueryTopologyMetrics(metrics: string[], ids: string[]) { return { queryStr, conditions }; } +function calculateExp( + arr: number[], + config: { calculation: string } +): (number | string)[] { + let data: (number | string)[] = arr; + switch (config.calculation) { + case Calculations.Average: + data = [arr.reduce((a, b) => a + b) / arr.length]; + break; + default: + data = arr.map((d) => aggregation(d, config)); + break; + } + return data; +} -export function aggregation(val: number, config: any): number | string { +function aggregation( + val: number, + config: { calculation: string } +): number | string { let data: number | string = Number(val); switch (config.calculation) { diff --git a/src/views/dashboard/configuration/widget/metric/Index.vue b/src/views/dashboard/configuration/widget/metric/Index.vue index 14cb8363..e76324fa 100644 --- a/src/views/dashboard/configuration/widget/metric/Index.vue +++ b/src/views/dashboard/configuration/widget/metric/Index.vue @@ -466,7 +466,7 @@ function setMetricConfig(index: number) { .chart-types { span { display: inline-block; - padding: 5px 10px; + padding: 2px 10px; border: 1px solid #ccc; background-color: #fff; border-right: 0; diff --git a/src/views/dashboard/data.ts b/src/views/dashboard/data.ts index 50026a1c..5b80ca3c 100644 --- a/src/views/dashboard/data.ts +++ b/src/views/dashboard/data.ts @@ -266,11 +266,12 @@ export const CalculationOpts = [ { label: "Byte to KB", value: "byteToKB" }, { label: "Byte to MB", value: "byteToMB" }, { label: "Byte to GB", value: "byteToGB" }, + { label: "Average", value: "average" }, { - label: "Convert milliseconds to YYYY-MM-DD HH:mm:ss", + label: "Milliseconds to YYYY-MM-DD HH:mm:ss", value: "convertMilliseconds", }, - { label: "Convert seconds to YYYY-MM-DD HH:mm:ss", value: "convertSeconds" }, + { label: "Seconds to YYYY-MM-DD HH:mm:ss", value: "convertSeconds" }, { label: "Precision is 2", value: "precision" }, { label: "Milliseconds to seconds", value: "msTos" }, ];