export async function useDashboardQueryProcessor()

in src/hooks/useExpressionsProcessor.ts [36:206]


export async function useDashboardQueryProcessor(configList: Indexable[]) {
  function expressionsGraphql(config: Indexable, idx: number) {
    if (!(config.metrics && config.metrics[0])) {
      return;
    }
    const dashboardStore = useDashboardStore();
    const selectorStore = useSelectorStore();

    if (!selectorStore.currentService && dashboardStore.entity !== "All") {
      return;
    }
    const conditions: Recordable = {};
    const variables: string[] = [];
    const isRelation = ["ServiceRelation", "ServiceInstanceRelation", "EndpointRelation", "ProcessRelation"].includes(
      dashboardStore.entity,
    );
    if (isRelation && !selectorStore.currentDestService) {
      return;
    }
    if (idx === 0) {
      variables.push(`$entity: Entity!`);
      const entity = {
        serviceName: dashboardStore.entity === "All" ? undefined : selectorStore.currentService.value,
        normal: dashboardStore.entity === "All" ? undefined : selectorStore.currentService.normal,
        serviceInstanceName: ["ServiceInstance", "ServiceInstanceRelation", "ProcessRelation", "Process"].includes(
          dashboardStore.entity,
        )
          ? selectorStore.currentPod && selectorStore.currentPod.value
          : undefined,
        endpointName: dashboardStore.entity.includes("Endpoint")
          ? selectorStore.currentPod && selectorStore.currentPod.value
          : undefined,
        processName: dashboardStore.entity.includes("Process")
          ? selectorStore.currentProcess && selectorStore.currentProcess.value
          : undefined,
        destNormal: isRelation ? selectorStore.currentDestService.normal : undefined,
        destServiceName: isRelation ? selectorStore.currentDestService.value : undefined,
        destServiceInstanceName: ["ServiceInstanceRelation", "ProcessRelation"].includes(dashboardStore.entity)
          ? selectorStore.currentDestPod && selectorStore.currentDestPod.value
          : undefined,
        destEndpointName:
          dashboardStore.entity === "EndpointRelation"
            ? selectorStore.currentDestPod && selectorStore.currentDestPod.value
            : undefined,
        destProcessName: dashboardStore.entity.includes("ProcessRelation")
          ? selectorStore.currentDestProcess && selectorStore.currentDestProcess.value
          : undefined,
      };
      conditions[`entity`] = entity;
    }
    const fragment = config.metrics.map((name: string, index: number) => {
      variables.push(`$expression${idx}${index}: String!`);
      conditions[`expression${idx}${index}`] = name;

      return `expression${idx}${index}: execExpression(expression: $expression${idx}${index}, entity: $entity, duration: $duration)${RespFields.execExpression}`;
    });
    return {
      variables,
      fragment,
      conditions,
    };
  }
  function expressionsSource(config: Indexable, resp: { errors: string; data: Indexable | any }) {
    if (resp.errors) {
      ElMessage.error(resp.errors);
      return { source: {}, tips: [], typesOfMQE: [] };
    }
    if (!resp.data) {
      ElMessage.error("The query is wrong");
      return { source: {}, tips: [], typesOfMQE: [] };
    }
    if (resp.data.error) {
      ElMessage.error(resp.data.error);
      return { source: {}, tips: [], typesOfMQE: [] };
    }
    const tips: string[] = [];
    const source: Indexable<unknown> = {};
    const keys = Object.keys(resp.data);
    const typesOfMQE: string[] = [];

    for (let i = 0; i < config.metrics.length; i++) {
      const metricConfig: MetricConfigOpt = (config.metricConfig && config.metricConfig[i]) || {};
      const obj = resp.data[keys[i]] || {};
      const results = obj.results || [];
      const name = config.metrics[i];
      const type = obj.type;

      tips.push(obj.error);
      typesOfMQE.push(type);
      if (!obj.error) {
        if ([ExpressionResultType.SINGLE_VALUE, ExpressionResultType.TIME_SERIES_VALUES].includes(type)) {
          for (const item of results) {
            let label =
              item.metric &&
              item.metric.labels.map((d: { key: string; value: string }) => `${d.key}=${d.value}`).join(",");
            const values = item.values.map((d: { value: unknown }) => d.value) || [];
            if (results.length === 1) {
              // If the metrics label does not exist, use the configuration label or expression
              label = label ? `${metricConfig.label || name}, ${label}` : metricConfig.label || name;
            }
            source[label] = values;
          }
        }
        if (([ExpressionResultType.RECORD_LIST, ExpressionResultType.SORTED_LIST] as string[]).includes(type)) {
          source[name] = results[0].values;
        }
      }
    }

    return { source, tips, typesOfMQE };
  }
  async function fetchMetrics(configArr: any) {
    const appStore = useAppStoreWithOut();
    const variables: string[] = [`$duration: Duration!`];
    let fragments = "";
    let conditions: Recordable<unknown> = {
      duration: appStore.durationTime,
    };
    for (let i = 0; i < configArr.length; i++) {
      const params = await expressionsGraphql(configArr[i], i);
      if (params) {
        fragments += params?.fragment;
        conditions = { ...conditions, ...params.conditions };
        variables.push(...params.variables);
      }
    }
    if (!fragments) {
      return { 0: { source: {}, tips: [], typesOfMQE: [] } };
    }
    const queryStr = `query queryData(${variables}) {${fragments}}`;
    const dashboardStore = useDashboardStore();
    const json = await dashboardStore.fetchMetricValue({
      queryStr,
      conditions,
    });
    if (json.errors) {
      ElMessage.error(json.errors);
      return { 0: { source: {}, tips: [], typesOfMQE: [] } };
    }
    try {
      const pageData: Recordable = {};

      for (let i = 0; i < configArr.length; i++) {
        const resp: any = {};
        for (let m = 0; m < configArr[i].metrics.length; m++) {
          resp[`expression${i}${m}`] = json.data[`expression${i}${m}`];
        }
        const data = expressionsSource(configArr[i], { ...json, data: resp });
        const id = configArr[i].id;
        pageData[id] = data;
      }
      return pageData;
    } catch (error) {
      console.error(error);
      return { 0: { source: {}, tips: [], typesOfMQE: [] } };
    }
  }

  const partArr = chunkArray(configList, 6);
  const promiseArr = partArr.map((d: Array<Indexable>) => fetchMetrics(d));
  const responseList = await Promise.all(promiseArr);
  let resp = {};
  for (const item of responseList) {
    resp = {
      ...resp,
      ...item,
    };
  }

  return resp;
}