diff --git a/src/hooks/useListConfig.ts b/src/hooks/useListConfig.ts index a7f7ad59..c87fd39e 100644 --- a/src/hooks/useListConfig.ts +++ b/src/hooks/useListConfig.ts @@ -26,14 +26,18 @@ export function useListConfig(config: any, index: string) { config.metricConfig && config.metricConfig[i] && config.metricConfig[i].calculation; - const line = - config.metricTypes[i] === MetricQueryTypes.ReadMetricsValues && - !types.includes(calculation); + const isLinear = + [ + MetricQueryTypes.ReadMetricsValues, + MetricQueryTypes.ReadLabeledMetricsValues, + ].includes(config.metricTypes[i]) && !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, + isLinear, isAvg, }; } diff --git a/src/hooks/useProcessor.ts b/src/hooks/useProcessor.ts index d7301898..97b22e4d 100644 --- a/src/hooks/useProcessor.ts +++ b/src/hooks/useProcessor.ts @@ -154,6 +154,7 @@ export function useQueryProcessor(config: any) { } }); const queryStr = `query queryData(${variables}) {${fragment}}`; + return { queryStr, conditions, @@ -254,13 +255,19 @@ export function useSourceProcessor( export function useQueryPodsMetrics( pods: Array, - config: { metrics: string[]; metricTypes: string[] }, + config: { + metrics: string[]; + metricTypes: string[]; + metricConfig: MetricConfigOpt[]; + }, scope: string ) { - if (!(config.metrics && config.metrics[0])) { + const metricTypes = (config.metricTypes || []).filter((m: string) => m); + if (!metricTypes.length) { return; } - if (!(config.metricTypes && config.metricTypes[0])) { + const metrics = (config.metrics || []).filter((m: string) => m); + if (!metrics.length) { return; } const appStore = useAppStoreWithOut(); @@ -282,14 +289,24 @@ export function useQueryPodsMetrics( endpointName: scope === "Endpoint" ? d.label : undefined, normal: scope === "Service" ? d.normal : currentService.normal, }; - const f = config.metrics.map((name: string, idx: number) => { - const metricType = config.metricTypes[idx] || ""; + const f = metrics.map((name: string, idx: number) => { + const metricType = metricTypes[idx] || ""; + variables.push(`$condition${index}${idx}: MetricsCondition!`); conditions[`condition${index}${idx}`] = { name, entity: param, }; - variables.push(`$condition${index}${idx}: MetricsCondition!`); - return `${name}${index}${idx}: ${metricType}(condition: $condition${index}${idx}, duration: $duration)${RespFields[metricType]}`; + let labelStr = ""; + if (metricType === MetricQueryTypes.ReadLabeledMetricsValues) { + const c = config.metricConfig[idx] || {}; + variables.push(`$labels${index}${idx}: [String!]!`); + labelStr = `labels: $labels${index}${idx}, `; + const labels = (c.labelsIndex || "") + .split(",") + .map((item: string) => item.replace(/^\s*|\s*$/g, "")); + conditions[`labels${index}${idx}`] = labels; + } + return `${name}${index}${idx}: ${metricType}(condition: $condition${index}${idx}, ${labelStr}duration: $duration)${RespFields[metricType]}`; }); return f; } @@ -299,6 +316,7 @@ export function useQueryPodsMetrics( return { queryStr, conditions }; } + export function usePodsSource( pods: Array, resp: { errors: string; data: { [key: string]: any } }, @@ -312,12 +330,20 @@ export function usePodsSource( ElMessage.error(resp.errors); return {}; } + const names: string[] = []; + const metricConfigArr: MetricConfigOpt[] = []; + const metricTypesArr: 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); + metricConfigArr.push(c); + metricTypesArr.push(config.metricTypes[index]); + } } if (config.metricTypes[index] === MetricQueryTypes.ReadMetricsValues) { d[name] = {}; @@ -333,12 +359,56 @@ export function usePodsSource( d[name]["values"] = resp.data[key].values.values.map( (val: { value: number }) => aggregation(val.value, c) ); + if (idx === 0) { + names.push(name); + metricConfigArr.push(c); + metricTypesArr.push(config.metricTypes[index]); + } + } + 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, "")); + for (let i = 0; i < resVal.length; i++) { + const item = resVal[i]; + const values = item.values.values.map((d: { value: number }) => + aggregation(Number(d.value), c) + ); + const indexNum = labelsIdx.findIndex((d: string) => d === item.label); + let key = item.label; + if (labels[indexNum] && indexNum > -1) { + key = labels[indexNum]; + } + if (!d[key]) { + d[key] = {}; + } + if ( + [ + Calculations.Average, + Calculations.ApdexAvg, + Calculations.PercentageAvg, + ].includes(c.calculation) + ) { + d[key]["avg"] = calculateExp(item.values.values, c); + } + d[key]["values"] = values; + if (idx === 0) { + names.push(key); + metricConfigArr.push({ ...c, index: i }); + metricTypesArr.push(config.metricTypes[index]); + } + } } }); - return d; }); - return data; + return { data, names, metricConfigArr, metricTypesArr }; } export function useQueryTopologyMetrics(metrics: string[], ids: string[]) { const appStore = useAppStoreWithOut(); diff --git a/src/types/dashboard.d.ts b/src/types/dashboard.d.ts index b82afe9a..ceabab34 100644 --- a/src/types/dashboard.d.ts +++ b/src/types/dashboard.d.ts @@ -74,6 +74,7 @@ export type MetricConfigOpt = { labelsIndex: string; sortOrder: string; topN?: number; + index?: number; }; export interface WidgetConfig { diff --git a/src/views/dashboard/configuration/widget/metric/Index.vue b/src/views/dashboard/configuration/widget/metric/Index.vue index 233eb8a2..cdb31f0d 100644 --- a/src/views/dashboard/configuration/widget/metric/Index.vue +++ b/src/views/dashboard/configuration/widget/metric/Index.vue @@ -191,7 +191,10 @@ async function setMetricType(chart?: any) { states.metricList = (arr || []).filter( (d: { catalog: string; type: string }) => { if (states.isList) { - if (d.type === MetricsType.REGULAR_VALUE) { + if ( + d.type === MetricsType.REGULAR_VALUE || + d.type === MetricsType.LABELED_VALUE + ) { return d; } } else if (g.type === "Table") { @@ -239,7 +242,10 @@ async function setMetricType(chart?: any) { } function setDashboards(type?: string) { - const chart = type || dashboardStore.selectedGrid.graph || {}; + const chart = + type || + (dashboardStore.selectedGrid.graph && + dashboardStore.selectedGrid.graph.type); const list = JSON.parse(sessionStorage.getItem("dashboards") || "[]"); const arr = list.reduce( ( @@ -248,9 +254,9 @@ function setDashboards(type?: string) { ) => { if (d.layer === dashboardStore.layerId) { if ( - (d.entity === EntityType[0].value && chart.type === "ServiceList") || - (d.entity === EntityType[2].value && chart.type === "EndpointList") || - (d.entity === EntityType[3].value && chart.type === "InstanceList") + (d.entity === EntityType[0].value && chart === "ServiceList") || + (d.entity === EntityType[2].value && chart === "EndpointList") || + (d.entity === EntityType[3].value && chart === "InstanceList") ) { prev.push({ ...d, diff --git a/src/views/dashboard/configuration/widget/metric/Standard.vue b/src/views/dashboard/configuration/widget/metric/Standard.vue index 150c2204..1ba282c4 100644 --- a/src/views/dashboard/configuration/widget/metric/Standard.vue +++ b/src/views/dashboard/configuration/widget/metric/Standard.vue @@ -115,7 +115,9 @@ const currentMetric = ref({ topN: props.currentMetricConfig.topN || 10, }); const metricTypes = dashboardStore.selectedGrid.metricTypes || []; -const metricType = ref(metricTypes[props.index]); +const metricType = computed( + () => (dashboardStore.selectedGrid.metricTypes || [])[props.index] +); const hasLabel = computed(() => { const graph = dashboardStore.selectedGrid.graph || {}; return ( 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..138865c3 100644 --- a/src/views/dashboard/graphs/EndpointList.vue +++ b/src/views/dashboard/graphs/EndpointList.vue @@ -31,7 +31,7 @@ limitations under the License. -->
- +