From 3968c2c13fd21dd360aa37c12b3916e517fb01a9 Mon Sep 17 00:00:00 2001 From: Qiuxia Fan Date: Sat, 22 Jan 2022 14:57:51 +0800 Subject: [PATCH] refactor: update selectors structure --- src/hooks/useProcessor.ts | 14 ++++----- src/store/modules/dashboard.ts | 4 --- src/store/modules/selectors.ts | 33 +++++++++++++-------- src/types/selector.d.ts | 24 +++++++++++++++ src/views/dashboard/graphs/EndpointList.vue | 2 +- src/views/dashboard/graphs/InstanceList.vue | 2 +- src/views/dashboard/graphs/ServiceList.vue | 2 +- src/views/dashboard/panel/Tool.vue | 12 +++++--- 8 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 src/types/selector.d.ts diff --git a/src/hooks/useProcessor.ts b/src/hooks/useProcessor.ts index 568e6026..c3d74941 100644 --- a/src/hooks/useProcessor.ts +++ b/src/hooks/useProcessor.ts @@ -33,7 +33,7 @@ export function useQueryProcessor(config: any) { const variables: string[] = [`$duration: Duration!`]; const { currentPod, currentService, currentDestPod, currentDestService } = selectorStore; - const { normal, destNormal, entity } = dashboardStore; + const { entity } = dashboardStore; const isRelation = [ "ServiceRelation", "ServiceInstanceRelation", @@ -53,8 +53,8 @@ export function useQueryProcessor(config: any) { name, parentService: ["Service", "All"].includes(entity) ? null - : currentService, - normal: normal, + : currentService.value, + normal: currentService.normal, scope: entity, topN: 10, order: "DES", @@ -69,14 +69,14 @@ export function useQueryProcessor(config: any) { name, entity: { scope: entity, - serviceName: entity === "All" ? undefined : currentService, - normal: entity === "All" ? undefined : normal, + serviceName: entity === "All" ? undefined : currentService.value, + normal: entity === "All" ? undefined : currentService.normal, serviceInstanceName: entity.includes("ServiceInstance") ? currentPod : undefined, endpointName: entity.includes("Endpoint") ? currentPod : undefined, - destNormal: entity === "All" ? undefined : undefined, - destServiceName: isRelation ? currentDestService : undefined, + destNormal: isRelation ? currentDestService.normal : undefined, + destServiceName: isRelation ? currentDestService.value : undefined, destServiceInstanceName: entity === "ServiceInstanceRelation" ? currentDestPod : undefined, destEndpointName: diff --git a/src/store/modules/dashboard.ts b/src/store/modules/dashboard.ts index fc1f1c6d..732bfa33 100644 --- a/src/store/modules/dashboard.ts +++ b/src/store/modules/dashboard.ts @@ -34,8 +34,6 @@ interface DashboardState { activedGridItem: string; durationTime: Duration; selectorStore: any; - normal: boolean; - destNormal: boolean; } export const dashboardStore = defineStore({ @@ -49,8 +47,6 @@ export const dashboardStore = defineStore({ activedGridItem: "", durationTime: useAppStoreWithOut().durationTime, selectorStore: useSelectorStore(), - normal: true, - destNormal: true, }), actions: { setLayout(data: LayoutConfig[]) { diff --git a/src/store/modules/selectors.ts b/src/store/modules/selectors.ts index ed9632e8..24971ff2 100644 --- a/src/store/modules/selectors.ts +++ b/src/store/modules/selectors.ts @@ -16,19 +16,20 @@ */ import { defineStore } from "pinia"; import { Option, Duration } from "@/types/app"; +import { Service } from "@/types/selector"; import { store } from "@/store"; import graph from "@/graph"; import { AxiosResponse } from "axios"; import { useAppStoreWithOut } from "@/store/modules/app"; interface SelectorState { - services: Option[]; + services: Service[]; instances: Option[]; pods: Option[]; endpoints: Option[]; - currentService: string; + currentService: Nullable; currentPod: string; - currentDestService: string; + currentDestService: Nullable; currentDestPod: string; durationTime: Duration; } @@ -40,14 +41,14 @@ export const selectorStore = defineStore({ instances: [], pods: [], endpoints: [], - currentService: "", + currentService: null, currentPod: "", - currentDestService: "", + currentDestService: null, currentDestPod: "", durationTime: useAppStoreWithOut().durationTime, }), actions: { - setCurrentService(service: string) { + setCurrentService(service: Service) { this.currentService = service; }, setCurrentPod(pod: string) { @@ -65,17 +66,19 @@ export const selectorStore = defineStore({ if (!res.data.errors) { this.services = res.data.data.services || []; - this.currentService = this.services.length - ? this.services[0].value - : ""; + this.currentService = this.services.length ? this.services[0] : null; } return res.data; }, async getServiceInstances(param?: { serviceId: string; - }): Promise { + }): Promise> { + const serviceId = param ? param.serviceId : this.currentService?.id; + if (!serviceId) { + return null; + } const res: AxiosResponse = await graph.query("queryInstances").params({ - serviceId: param ? param.serviceId : this.currentService, + serviceId, duration: this.durationTime, }); if (!res.data.errors) { @@ -88,15 +91,19 @@ export const selectorStore = defineStore({ async getEndpoints(params?: { keyword?: string; serviceId?: string; - }): Promise { + }): Promise> { if (!params) { params = {}; } if (!params.keyword) { params.keyword = ""; } + const serviceId = params.serviceId || this.currentService?.id; + if (!serviceId) { + return null; + } const res: AxiosResponse = await graph.query("queryEndpoints").params({ - serviceId: params.serviceId || this.currentService, + serviceId, duration: this.durationTime, keyword: params.keyword, }); diff --git a/src/types/selector.d.ts b/src/types/selector.d.ts new file mode 100644 index 00000000..f3727ffd --- /dev/null +++ b/src/types/selector.d.ts @@ -0,0 +1,24 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export type Service = { + id: string; + label: string; + value: string; + layers: string[]; + normal: boolean; + group: string; +}; diff --git a/src/views/dashboard/graphs/EndpointList.vue b/src/views/dashboard/graphs/EndpointList.vue index e15cb3e2..2dd7a162 100644 --- a/src/views/dashboard/graphs/EndpointList.vue +++ b/src/views/dashboard/graphs/EndpointList.vue @@ -39,7 +39,7 @@ limitations under the License. --> {{ scope.row.label }} diff --git a/src/views/dashboard/graphs/InstanceList.vue b/src/views/dashboard/graphs/InstanceList.vue index 0d798d2e..0d05251b 100644 --- a/src/views/dashboard/graphs/InstanceList.vue +++ b/src/views/dashboard/graphs/InstanceList.vue @@ -39,7 +39,7 @@ limitations under the License. --> {{ scope.row.label }} diff --git a/src/views/dashboard/graphs/ServiceList.vue b/src/views/dashboard/graphs/ServiceList.vue index 9e533811..c4d71efc 100644 --- a/src/views/dashboard/graphs/ServiceList.vue +++ b/src/views/dashboard/graphs/ServiceList.vue @@ -39,7 +39,7 @@ limitations under the License. --> {{ scope.row.label }} diff --git a/src/views/dashboard/panel/Tool.vue b/src/views/dashboard/panel/Tool.vue index 12b571e2..a10b4834 100644 --- a/src/views/dashboard/panel/Tool.vue +++ b/src/views/dashboard/panel/Tool.vue @@ -18,7 +18,7 @@ limitations under the License. -->
$Service