feat: Split topology metric query to avoid exceeding the maximum query complexity (#429)

This commit is contained in:
Fine0830 2024-11-14 11:38:13 +08:00 committed by GitHub
parent 64d4a2b59b
commit 536df8c052
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 58 additions and 51 deletions

View File

@ -112,3 +112,5 @@ export const LightChartColors = [
"#546570", "#546570",
"#c4ccd3", "#c4ccd3",
]; ];
export const MaxQueryLength = 120;

View File

@ -14,9 +14,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { RespFields, MaximumEntities } from "./data"; import { RespFields, MaximumEntities, MaxQueryLength } from "./data";
import { EntityType, ExpressionResultType } from "@/views/dashboard/data"; import { EntityType, ExpressionResultType } from "@/views/dashboard/data";
import { ElMessage } from "element-plus"; import { ElMessage } from "element-plus";
import { useTopologyStore } from "@/store/modules/topology";
import { useDashboardStore } from "@/store/modules/dashboard"; import { useDashboardStore } from "@/store/modules/dashboard";
import { useSelectorStore } from "@/store/modules/selectors"; import { useSelectorStore } from "@/store/modules/selectors";
import { useAppStoreWithOut } from "@/store/modules/app"; import { useAppStoreWithOut } from "@/store/modules/app";
@ -24,6 +25,14 @@ import type { MetricConfigOpt } from "@/types/dashboard";
import type { Instance, Endpoint, Service } from "@/types/selector"; import type { Instance, Endpoint, Service } from "@/types/selector";
import type { Node, Call } from "@/types/topology"; import type { Node, Call } from "@/types/topology";
function chunkArray(array: any[], chunkSize: number) {
const result = [];
for (let i = 0; i < array.length; i += chunkSize) {
result.push(array.slice(i, i + chunkSize));
}
return result;
}
export async function useDashboardQueryProcessor(configList: Indexable[]) { export async function useDashboardQueryProcessor(configList: Indexable[]) {
function expressionsGraphql(config: Indexable, idx: number) { function expressionsGraphql(config: Indexable, idx: number) {
if (!(config.metrics && config.metrics[0])) { if (!(config.metrics && config.metrics[0])) {
@ -181,13 +190,6 @@ export async function useDashboardQueryProcessor(configList: Indexable[]) {
return { 0: { source: {}, tips: [], typesOfMQE: [] } }; return { 0: { source: {}, tips: [], typesOfMQE: [] } };
} }
} }
function chunkArray(array: any[], chunkSize: number) {
const result = [];
for (let i = 0; i < array.length; i += chunkSize) {
result.push(array.slice(i, i + chunkSize));
}
return result;
}
const partArr = chunkArray(configList, 6); const partArr = chunkArray(configList, 6);
const promiseArr = partArr.map((d: Array<Indexable>) => fetchMetrics(d)); const promiseArr = partArr.map((d: Array<Indexable>) => fetchMetrics(d));
@ -394,7 +396,7 @@ export function useQueryTopologyExpressionsProcessor(metrics: string[], instance
const appStore = useAppStoreWithOut(); const appStore = useAppStoreWithOut();
const dashboardStore = useDashboardStore(); const dashboardStore = useDashboardStore();
function getExpressionQuery() { function getExpressionQuery(partMetrics?: string[]) {
const conditions: { [key: string]: unknown } = { const conditions: { [key: string]: unknown } = {
duration: appStore.durationTime, duration: appStore.durationTime,
}; };
@ -448,7 +450,7 @@ export function useQueryTopologyExpressionsProcessor(metrics: string[], instance
}; };
variables.push(`$entity${index}: Entity!`); variables.push(`$entity${index}: Entity!`);
conditions[`entity${index}`] = entity; conditions[`entity${index}`] = entity;
const f = metrics.map((name: string, idx: number) => { const f = (partMetrics || metrics).map((name: string, idx: number) => {
if (index === 0) { if (index === 0) {
variables.push(`$expression${idx}: String!`); variables.push(`$expression${idx}: String!`);
conditions[`expression${idx}`] = name; conditions[`expression${idx}`] = name;
@ -462,19 +464,19 @@ export function useQueryTopologyExpressionsProcessor(metrics: string[], instance
return { queryStr, conditions }; return { queryStr, conditions };
} }
function handleExpressionValues(resp: { [key: string]: any }) { function handleExpressionValues(partMetrics: string[], resp: { [key: string]: any }) {
const obj: any = {}; const obj: any = {};
for (let idx = 0; idx < instances.length; idx++) { for (let idx = 0; idx < instances.length; idx++) {
for (let index = 0; index < metrics.length; index++) { for (let index = 0; index < partMetrics.length; index++) {
const k = "expression" + idx + index; const k = "expression" + idx + index;
if (metrics[index]) { if (partMetrics[index]) {
if (!obj[metrics[index]]) { if (!obj[partMetrics[index]]) {
obj[metrics[index]] = { obj[partMetrics[index]] = {
values: [], values: [],
}; };
} }
obj[metrics[index]].values.push({ obj[partMetrics[index]].values.push({
value: resp[k].results[0] && resp[k].results[0].values[0].value, value: resp[k] && resp[k].results[0] && resp[k].results[0].values[0].value,
id: instances[idx].id, id: instances[idx].id,
}); });
} }
@ -482,6 +484,31 @@ export function useQueryTopologyExpressionsProcessor(metrics: string[], instance
} }
return obj; return obj;
} }
async function fetchMetrics(partMetrics: string[]) {
return { getExpressionQuery, handleExpressionValues }; const topologyStore = useTopologyStore();
const param = getExpressionQuery(partMetrics);
const res = await topologyStore.getTopologyExpressionValue(param);
if (res.errors) {
ElMessage.error(res.errors);
return;
}
return handleExpressionValues(partMetrics, res.data);
}
async function getMetrics() {
const count = Math.floor(MaxQueryLength / instances.length);
const metricsArr = chunkArray(metrics, count);
const promiseArr = metricsArr.map((d: string[]) => fetchMetrics(d));
const responseList = await Promise.all(promiseArr);
let resp = {};
for (const item of responseList) {
resp = {
...resp,
...item,
};
}
return resp;
}
return { getMetrics, getExpressionQuery };
} }

View File

@ -24,7 +24,6 @@ import { useAppStoreWithOut } from "@/store/modules/app";
import type { AxiosResponse } from "axios"; import type { AxiosResponse } from "axios";
import query from "@/graphql/fetch"; import query from "@/graphql/fetch";
import { useQueryTopologyExpressionsProcessor } from "@/hooks/useExpressionsProcessor"; import { useQueryTopologyExpressionsProcessor } from "@/hooks/useExpressionsProcessor";
import { ElMessage } from "element-plus";
interface MetricVal { interface MetricVal {
[key: string]: { values: { id: string; value: unknown }[] }; [key: string]: { values: { id: string; value: unknown }[] };
@ -443,7 +442,7 @@ export const topologyStore = defineStore({
return { calls, nodes }; return { calls, nodes };
}, },
async getNodeExpressionValue(param: { queryStr: string; conditions: { [key: string]: unknown } }) { async getTopologyExpressionValue(param: { queryStr: string; conditions: { [key: string]: unknown } }) {
const res: AxiosResponse = await query(param); const res: AxiosResponse = await query(param);
if (res.data.errors) { if (res.data.errors) {
@ -461,14 +460,8 @@ export const topologyStore = defineStore({
if (!calls.length) { if (!calls.length) {
return; return;
} }
const { getExpressionQuery, handleExpressionValues } = useQueryTopologyExpressionsProcessor(expressions, calls); const { getMetrics } = useQueryTopologyExpressionsProcessor(expressions, calls);
const param = getExpressionQuery(); const metrics = await getMetrics();
const res = await this.getNodeExpressionValue(param);
if (res.errors) {
ElMessage.error(res.errors);
return;
}
const metrics = handleExpressionValues(res.data);
if (type === "SERVER") { if (type === "SERVER") {
this.setLinkServerMetrics(metrics); this.setLinkServerMetrics(metrics);
} else { } else {
@ -484,17 +477,11 @@ export const topologyStore = defineStore({
this.setNodeMetricValue({}); this.setNodeMetricValue({});
return; return;
} }
const { getExpressionQuery, handleExpressionValues } = useQueryTopologyExpressionsProcessor( const { getMetrics } = useQueryTopologyExpressionsProcessor(
expressions, expressions,
this.nodes.filter((d: Node) => d.isReal), this.nodes.filter((d: Node) => d.isReal),
); );
const param = getExpressionQuery(); const metrics = await getMetrics();
const res = await this.getNodeExpressionValue(param);
if (res.errors) {
ElMessage.error(res.errors);
return;
}
const metrics = handleExpressionValues(res.data);
this.setNodeMetricValue(metrics); this.setNodeMetricValue(metrics);
}, },
async getHierarchyServiceTopology() { async getHierarchyServiceTopology() {
@ -550,17 +537,6 @@ export const topologyStore = defineStore({
this.setHierarchyInstanceTopology(res.data.data.hierarchyInstanceTopology || {}, levels); this.setHierarchyInstanceTopology(res.data.data.hierarchyInstanceTopology || {}, levels);
return res.data; return res.data;
}, },
async queryHierarchyExpressions(expressions: string[], nodes: Node[]) {
const { getExpressionQuery, handleExpressionValues } = useQueryTopologyExpressionsProcessor(expressions, nodes);
const param = getExpressionQuery();
const res = await this.getNodeExpressionValue(param);
if (res.errors) {
ElMessage.error(res.errors);
return;
}
const metrics = handleExpressionValues(res.data);
return metrics;
},
async queryHierarchyNodeExpressions(expressions: string[], layer: string) { async queryHierarchyNodeExpressions(expressions: string[], layer: string) {
const nodes = this.hierarchyServiceNodes.filter((n: HierarchyNode) => n.layer === layer); const nodes = this.hierarchyServiceNodes.filter((n: HierarchyNode) => n.layer === layer);
if (!nodes.length) { if (!nodes.length) {
@ -571,7 +547,8 @@ export const topologyStore = defineStore({
this.setHierarchyNodeMetricValue({}, layer); this.setHierarchyNodeMetricValue({}, layer);
return; return;
} }
const metrics = await this.queryHierarchyExpressions(expressions, nodes); const { getMetrics } = useQueryTopologyExpressionsProcessor(expressions, nodes);
const metrics = await getMetrics();
this.setHierarchyNodeMetricValue(metrics, layer); this.setHierarchyNodeMetricValue(metrics, layer);
}, },
async queryHierarchyInstanceNodeExpressions(expressions: string[], layer: string) { async queryHierarchyInstanceNodeExpressions(expressions: string[], layer: string) {
@ -585,7 +562,8 @@ export const topologyStore = defineStore({
this.setHierarchyInstanceNodeMetricValue({}, layer); this.setHierarchyInstanceNodeMetricValue({}, layer);
return; return;
} }
const metrics = await this.queryHierarchyExpressions(expressions, nodes); const { getMetrics } = useQueryTopologyExpressionsProcessor(expressions, nodes);
const metrics = await getMetrics();
this.setHierarchyInstanceNodeMetricValue(metrics, layer); this.setHierarchyInstanceNodeMetricValue(metrics, layer);
}, },
}, },

View File

@ -249,7 +249,7 @@ limitations under the License. -->
topologyStore.nodes.filter((d: Node) => d.isReal), topologyStore.nodes.filter((d: Node) => d.isReal),
); );
const param = getExpressionQuery(); const param = getExpressionQuery();
const res = await topologyStore.getNodeExpressionValue(param); const res = await topologyStore.getTopologyExpressionValue(param);
if (res.errors) { if (res.errors) {
ElMessage.error(res.errors); ElMessage.error(res.errors);
} else { } else {

View File

@ -290,7 +290,7 @@ limitations under the License. -->
topologyStore.nodes.filter((d: Node) => d.isReal), topologyStore.nodes.filter((d: Node) => d.isReal),
); );
const param = getExpressionQuery(); const param = getExpressionQuery();
const res = await topologyStore.getNodeExpressionValue(param); const res = await topologyStore.getTopologyExpressionValue(param);
if (res.errors) { if (res.errors) {
ElMessage.error(res.errors); ElMessage.error(res.errors);
} else { } else {