diff --git a/src/hooks/useListConfig.ts b/src/hooks/useListConfig.ts index a7f7ad59..f000bf43 100644 --- a/src/hooks/useListConfig.ts +++ b/src/hooks/useListConfig.ts @@ -30,8 +30,10 @@ export function useListConfig(config: any, index: string) { config.metricTypes[i] === MetricQueryTypes.ReadMetricsValues && !types.includes(calculation); const isAvg = - config.metricTypes[i] === MetricQueryTypes.ReadMetricsValues && - types.includes(calculation); + [ + MetricQueryTypes.ReadMetricsValues, + MetricQueryTypes.ReadLabeledMetricsValues, + ].includes(config.metricTypes[i]) && types.includes(calculation); return { isLinear: line, isAvg, diff --git a/src/hooks/useProcessor.ts b/src/hooks/useProcessor.ts index b09bd43b..eb164da9 100644 --- a/src/hooks/useProcessor.ts +++ b/src/hooks/useProcessor.ts @@ -298,10 +298,12 @@ export function useQueryPodsMetrics( }; let labelStr = ""; if (metricType === MetricQueryTypes.ReadLabeledMetricsValues) { + console.log(config); variables.push(`$labels${index}${idx}: [String!]!`); labelStr = `labels: $labels${index}${idx}, `; conditions[`labels${index}${idx}`] = ( - config.metricConfig[idx].label || "" + (config.metricConfig[idx] && config.metricConfig[idx].label) || + "" ).split(","); } return `${name}${index}${idx}: ${metricType}(condition: $condition${index}${idx}, ${labelStr}duration: $duration)${RespFields[metricType]}`; @@ -314,6 +316,7 @@ export function useQueryPodsMetrics( return { queryStr, conditions }; } + export function usePodsSource( pods: Array, resp: { errors: string; data: { [key: string]: any } }, @@ -327,13 +330,16 @@ export function usePodsSource( ElMessage.error(resp.errors); return {}; } - console.log(config); + const names: string[] = []; const data = pods.map((d: Instance | any, idx: number) => { config.metrics.map((name: string, index: number) => { const c: any = (config.metricConfig && config.metricConfig[index]) || {}; const key = name + idx + index; if (config.metricTypes[index] === MetricQueryTypes.ReadMetricsValue) { d[name] = aggregation(resp.data[key], c); + if (idx === 0) { + names.push(name); + } } if (config.metricTypes[index] === MetricQueryTypes.ReadMetricsValues) { d[name] = {}; @@ -344,36 +350,73 @@ export function usePodsSource( Calculations.PercentageAvg, ].includes(c.calculation) ) { - if (Array.isArray(d[resp.data[key]])) { - for (const item of d[resp.data[key]]) { - d[item.label]["avg"] = calculateExp( - resp.data[key][item.label].values.values, - c - ); + d[name]["avg"] = calculateExp(resp.data[key].values.values, c); + } + d[name]["values"] = resp.data[key].values.values.map( + (val: { value: number }) => aggregation(val.value, c) + ); + if (idx === 0) { + names.push(name); + } + } + if ( + config.metricTypes[index] === MetricQueryTypes.ReadLabeledMetricsValues + ) { + const resVal = resp.data[key] || []; + const labels = (c.label || "") + .split(",") + .map((item: string) => item.replace(/^\s*|\s*$/g, "")); + const labelsIdx = (c.labelsIndex || "") + .split(",") + .map((item: string) => item.replace(/^\s*|\s*$/g, "")); + console.log(resVal); + for (const item of resVal) { + const values = item.values.values.map((d: { value: number }) => + aggregation(Number(d.value), c) + ); + const indexNum = labelsIdx.findIndex((d: string) => d === item.label); + if ( + [ + Calculations.Average, + Calculations.ApdexAvg, + Calculations.PercentageAvg, + ].includes(c.calculation) + ) { + if (labels[indexNum] && indexNum > -1) { + if (!d[labels[indexNum]]) { + d[labels[indexNum]] = {}; + } + d[labels[indexNum]]["avg"] = calculateExp(item.values.values, c); + } else { + if (!d[item.label]) { + d[item.label] = {}; + } + d[item.label]["avg"] = calculateExp(item.values.values, c); + } + } + if (labels[indexNum] && indexNum > -1) { + if (!d[labels[indexNum]]) { + d[labels[indexNum]] = {}; + } + d[labels[indexNum]]["values"] = values; + if (idx === 0) { + names.push(labels[indexNum]); } } else { - d[name]["avg"] = calculateExp(resp.data[key].values.values, c); + if (!d[item.label]) { + d[item.label] = {}; + } + d[item.label]["values"] = values; + if (idx === 0) { + names.push(item.label); + } } } - if (Array.isArray(d[resp.data[key]])) { - for (const item of d[resp.data[key]]) { - d[item.label]["values"] = resp.data[key][ - item.label - ].values.values.map((val: { value: number }) => - aggregation(val.value, c) - ); - } - } else { - d[name]["values"] = resp.data[key].values.values.map( - (val: { value: number }) => aggregation(val.value, c) - ); - } } }); - return d; }); - return data; + return { data, names }; } export function useQueryTopologyMetrics(metrics: string[], ids: string[]) { const appStore = useAppStoreWithOut(); diff --git a/src/views/dashboard/controls/Widget.vue b/src/views/dashboard/controls/Widget.vue index f723984d..8593d434 100644 --- a/src/views/dashboard/controls/Widget.vue +++ b/src/views/dashboard/controls/Widget.vue @@ -62,7 +62,7 @@ limitations under the License. --> metricTypes: data.metricTypes || [''], i: data.i, id: data.id, - metricConfig: data.metricConfig, + metricConfig: data.metricConfig || [], filters: data.filters || {}, relatedTrace: data.relatedTrace || {}, }" diff --git a/src/views/dashboard/graphs/EndpointList.vue b/src/views/dashboard/graphs/EndpointList.vue index c4654b22..18c621b5 100644 --- a/src/views/dashboard/graphs/EndpointList.vue +++ b/src/views/dashboard/graphs/EndpointList.vue @@ -143,7 +143,7 @@ async function queryEndpointMetrics(currentPods: Endpoint[]) { endpoints.value = usePodsSource(currentPods, json, { ...props.config, metricConfig: metricConfig, - }); + }).data; return; } endpoints.value = currentPods; diff --git a/src/views/dashboard/graphs/InstanceList.vue b/src/views/dashboard/graphs/InstanceList.vue index 04ae5412..6c468e26 100644 --- a/src/views/dashboard/graphs/InstanceList.vue +++ b/src/views/dashboard/graphs/InstanceList.vue @@ -172,7 +172,7 @@ async function queryInstanceMetrics(currentInstances: Instance[]) { instances.value = usePodsSource(currentInstances, json, { ...props.config, metricConfig, - }); + }).data; return; } instances.value = currentInstances; diff --git a/src/views/dashboard/graphs/ServiceList.vue b/src/views/dashboard/graphs/ServiceList.vue index e193e0da..b79129bd 100644 --- a/src/views/dashboard/graphs/ServiceList.vue +++ b/src/views/dashboard/graphs/ServiceList.vue @@ -75,7 +75,7 @@ limitations under the License. -->