fix: get selectedGrid

This commit is contained in:
Qiuxia Fan 2022-01-23 20:25:31 +08:00
parent 400a1ae536
commit 51e6d0be27
4 changed files with 54 additions and 44 deletions

View File

@ -25,7 +25,6 @@ import { NewControl } from "../data";
import { Duration } from "@/types/app"; import { Duration } from "@/types/app";
import axios, { AxiosResponse } from "axios"; import axios, { AxiosResponse } from "axios";
import { cancelToken } from "@/utils/cancelToken"; import { cancelToken } from "@/utils/cancelToken";
import { Instance } from "@/types/selector";
interface DashboardState { interface DashboardState {
showConfig: boolean; showConfig: boolean;
layout: LayoutConfig[]; layout: LayoutConfig[];
@ -137,8 +136,8 @@ export const dashboardStore = defineStore({
setConfigPanel(show: boolean) { setConfigPanel(show: boolean) {
this.showConfig = show; this.showConfig = show;
}, },
selectWidget(widget: Nullable<LayoutConfig>) { selectWidget(item: Nullable<LayoutConfig>) {
this.selectedGrid = widget; this.selectedGrid = item;
}, },
setLayer(id: string) { setLayer(id: string) {
this.layerId = id; this.layerId = id;

View File

@ -16,21 +16,21 @@ limitations under the License. -->
<div class="widget-config flex-v"> <div class="widget-config flex-v">
<div class="graph" v-loading="loading"> <div class="graph" v-loading="loading">
<div class="header"> <div class="header">
<span>{{ selectedGrid.widget.title }}</span> <span>{{ dashboardStore.selectedGrid.widget.title }}</span>
<div class="tips" v-show="selectedGrid.widget.tips"> <div class="tips" v-show="dashboardStore.selectedGrid.widget.tips">
<el-tooltip :content="selectedGrid.widget.tips"> <el-tooltip :content="dashboardStore.selectedGrid.widget.tips">
<Icon iconName="info_outline" size="sm" /> <Icon iconName="info_outline" size="sm" />
</el-tooltip> </el-tooltip>
</div> </div>
</div> </div>
<div class="render-chart"> <div class="render-chart">
<component <component
:is="selectedGrid.graph.type" :is="dashboardStore.selectedGrid.graph.type"
:intervalTime="appStoreWithOut.intervalTime" :intervalTime="appStoreWithOut.intervalTime"
:data="states.source" :data="states.source"
:config="selectedGrid.graph" :config="dashboardStore.selectedGrid.graph"
/> />
<div v-show="!selectedGrid.graph.type" class="no-data"> <div v-show="!dashboardStore.selectedGrid.graph.type" class="no-data">
{{ t("noData") }} {{ t("noData") }}
</div> </div>
</div> </div>
@ -49,14 +49,16 @@ limitations under the License. -->
v-for="(type, index) in states.visType" v-for="(type, index) in states.visType"
:key="index" :key="index"
@click="changeChartType(type)" @click="changeChartType(type)"
:class="{ active: type.value === selectedGrid.graph.type }" :class="{
active: type.value === dashboardStore.selectedGrid.graph.type,
}"
> >
{{ type.label }} {{ type.label }}
</span> </span>
</div> </div>
</el-collapse-item> </el-collapse-item>
<el-collapse-item :title="t('graphStyles')" name="3"> <el-collapse-item :title="t('graphStyles')" name="3">
<component :is="`${selectedGrid.graph.type}Config`" /> <component :is="`${dashboardStore.selectedGrid.graph.type}Config`" />
</el-collapse-item> </el-collapse-item>
<el-collapse-item :title="t('widgetOptions')" name="4"> <el-collapse-item :title="t('widgetOptions')" name="4">
<WidgetOptions /> <WidgetOptions />
@ -109,7 +111,6 @@ export default defineComponent({
const { t } = useI18n(); const { t } = useI18n();
const dashboardStore = useDashboardStore(); const dashboardStore = useDashboardStore();
const appStoreWithOut = useAppStoreWithOut(); const appStoreWithOut = useAppStoreWithOut();
const { selectedGrid, entity } = dashboardStore;
const loading = ref<boolean>(false); const loading = ref<boolean>(false);
const states = reactive<{ const states = reactive<{
activeNames: string; activeNames: string;
@ -120,17 +121,19 @@ export default defineComponent({
}>({ }>({
activeNames: "1", activeNames: "1",
source: {}, source: {},
index: selectedGrid.i, index: dashboardStore.selectedGrid.i,
visType: [], visType: [],
isTable: false, isTable: false,
}); });
states.isTable = TableChartTypes.includes(selectedGrid.graph.type || ""); states.isTable = TableChartTypes.includes(
dashboardStore.selectedGrid.graph.type || ""
);
if (entity === EntityType[0].value) { if (dashboardStore.entity === EntityType[0].value) {
states.visType = ChartTypes.filter( states.visType = ChartTypes.filter(
(d: Option) => d.value !== "serviceList" (d: Option) => d.value !== "serviceList"
); );
} else if (entity === EntityType[1].value) { } else if (dashboardStore.entity === EntityType[1].value) {
states.visType = ChartTypes.filter( states.visType = ChartTypes.filter(
(d: Option) => !PodsChartTypes.includes(d.value) (d: Option) => !PodsChartTypes.includes(d.value)
); );
@ -141,12 +144,9 @@ export default defineComponent({
} }
function changeChartType(item: Option) { function changeChartType(item: Option) {
const graph = { const graph = DefaultGraphConfig[item.value];
...selectedGrid.graph, states.isTable = TableChartTypes.includes(graph.type);
...DefaultGraphConfig[item.value], dashboardStore.selectWidget({ ...dashboardStore.selectedGrid, graph });
};
states.isTable = TableChartTypes.includes(selectedGrid.graph.type);
dashboardStore.selectWidget({ ...selectedGrid, graph });
} }
function getSource(source: unknown) { function getSource(source: unknown) {
@ -169,7 +169,7 @@ export default defineComponent({
t, t,
appStoreWithOut, appStoreWithOut,
configHeight, configHeight,
selectedGrid, dashboardStore,
applyConfig, applyConfig,
getSource, getSource,
setLoading, setLoading,

View File

@ -16,7 +16,7 @@ limitations under the License. -->
<div v-show="states.isTable" class="ds-name"> <div v-show="states.isTable" class="ds-name">
<div>Dashboard</div> <div>Dashboard</div>
<Selector <Selector
:value="states.graph.dashboardName" :value="selectedGrid.graph.dashboardName"
:options="states.metricList" :options="states.metricList"
size="mini" size="mini"
placeholder="Select a dashboard" placeholder="Select a dashboard"
@ -24,12 +24,12 @@ limitations under the License. -->
class="selectors" class="selectors"
/> />
</div> </div>
<div>Metrics</div>
<div <div
v-for="(metric, index) in states.metrics" v-for="(metric, index) in states.metrics"
:key="index" :key="index"
class="metric-item" class="metric-item"
> >
<div>Metrics</div>
<Selector <Selector
:value="metric" :value="metric"
:options="states.metricList" :options="states.metricList"
@ -42,7 +42,7 @@ limitations under the License. -->
:value="states.metricTypes[index]" :value="states.metricTypes[index]"
:options="states.metricTypeList[index]" :options="states.metricTypeList[index]"
size="mini" size="mini"
:disabled="states.graph.type && !states.isTable && index !== 0" :disabled="selectedGrid.graph.type && !states.isTable && index !== 0"
@change="changeMetricType(index, $event)" @change="changeMetricType(index, $event)"
class="selectors" class="selectors"
/> />
@ -71,7 +71,6 @@ limitations under the License. -->
<script lang="ts" setup> <script lang="ts" setup>
import { reactive, watch } from "vue"; import { reactive, watch } from "vue";
import { Option } from "@/types/app"; import { Option } from "@/types/app";
import { GraphConfig } from "@/types/dashboard";
import { useDashboardStore } from "@/store/modules/dashboard"; import { useDashboardStore } from "@/store/modules/dashboard";
import { MetricTypes, TableChartTypes, MetricCatalog } from "../data"; import { MetricTypes, TableChartTypes, MetricCatalog } from "../data";
import { ElMessage } from "element-plus"; import { ElMessage } from "element-plus";
@ -90,7 +89,6 @@ const states = reactive<{
visType: Option[]; visType: Option[];
isTable: boolean; isTable: boolean;
metricList: (Option & { type: string })[]; metricList: (Option & { type: string })[];
graph: GraphConfig | any;
}>({ }>({
metrics: metrics && metrics.length ? metrics : [""], metrics: metrics && metrics.length ? metrics : [""],
metricTypes: metricTypes && metricTypes.length ? metricTypes : [""], metricTypes: metricTypes && metricTypes.length ? metricTypes : [""],
@ -98,20 +96,20 @@ const states = reactive<{
visType: [], visType: [],
isTable: false, isTable: false,
metricList: [], metricList: [],
graph: selectedGrid.graph,
}); });
states.isTable = TableChartTypes.includes(states.graph.type); states.isTable = TableChartTypes.includes(selectedGrid.graph.type);
setMetricType(); setMetricType();
async function setMetricType() { async function setMetricType(catalog?: string) {
catalog = catalog || entity;
const json = await dashboardStore.fetchMetricList(); const json = await dashboardStore.fetchMetricList();
if (json.errors) { if (json.errors) {
ElMessage.error(json.errors); ElMessage.error(json.errors);
return; return;
} }
states.metricList = (json.data.metrics || []).filter( states.metricList = (json.data.metrics || []).filter(
(d: { catalog: string }) => entity === (MetricCatalog as any)[d.catalog] (d: { catalog: string }) => catalog === (MetricCatalog as any)[d.catalog]
); );
const metrics: any = states.metricList.filter( const metrics: any = states.metricList.filter(
@ -196,7 +194,10 @@ async function queryMetrics() {
} }
function changeDashboard(item: Option[]) { function changeDashboard(item: Option[]) {
states.graph.dashboardName = item[0].value; dashboardStore.selectWidget({
...selectedGrid,
...{ dashboardName: item[0].value },
});
} }
function addMetric() { function addMetric() {
states.metrics.push(""); states.metrics.push("");
@ -212,9 +213,15 @@ function deleteMetric(index: number) {
states.metricTypes.splice(index, 1); states.metricTypes.splice(index, 1);
} }
watch( watch(
() => selectedGrid.graph, () => selectedGrid.graph.type,
(data: { type: string }) => { (type: string) => {
states.isTable = TableChartTypes.includes(data.type); states.isTable = TableChartTypes.includes(type);
const catalog: { [key: string]: string } = {
InstanceList: "ServiceInstance",
EndpointList: "Endpoint",
ServiceList: "Service",
};
setMetricType(catalog[type]);
} }
); );
</script> </script>

View File

@ -47,7 +47,7 @@ limitations under the License. -->
</template> </template>
</el-table-column> </el-table-column>
<el-table-column <el-table-column
v-for="(metric, index) in selectedGrid.metrics" v-for="(metric, index) in dashboardStore.selectedGrid.metrics"
:label="metric" :label="metric"
:key="metric + index" :key="metric + index"
> >
@ -88,9 +88,7 @@ import { useQueryPodsMetrics, usePodsSource } from "@/hooks/useProcessor";
/*global defineProps */ /*global defineProps */
defineProps({ defineProps({
config: { config: {
type: Object as PropType< type: Object as PropType<InstanceListConfig>,
InstanceListConfig & { metrics: string[]; metricTypes: string[] }
>,
default: () => ({ dashboardName: "", fontSize: 12 }), default: () => ({ dashboardName: "", fontSize: 12 }),
}, },
intervalTime: { type: Array as PropType<string[]>, default: () => [] }, intervalTime: { type: Array as PropType<string[]>, default: () => [] },
@ -102,7 +100,6 @@ const instances = ref<(Instance | any)[]>([]); // current instances
const searchInstances = ref<Instance[]>([]); // all instances const searchInstances = ref<Instance[]>([]); // all instances
const pageSize = 5; const pageSize = 5;
const searchText = ref<string>(""); const searchText = ref<string>("");
const selectedGrid = dashboardStore.selectedGrid;
queryInstance(); queryInstance();
@ -118,15 +115,22 @@ async function queryInstance() {
searchInstances.value = selectorStore.instances; searchInstances.value = selectorStore.instances;
const currentInstances = searchInstances.value.splice(0, pageSize); const currentInstances = searchInstances.value.splice(0, pageSize);
const params = await useQueryPodsMetrics(currentInstances, selectedGrid); const params = await useQueryPodsMetrics(
currentInstances,
dashboardStore.selectedGrid
);
const json = await dashboardStore.fetchMetricValue(params); const json = await dashboardStore.fetchMetricValue(params);
if (json.errors) { if (json.errors) {
ElMessage.error(json.errors); ElMessage.error(json.errors);
return; return;
} }
usePodsSource(currentInstances, json, selectedGrid); usePodsSource(currentInstances, json, dashboardStore.selectedGrid);
instances.value = usePodsSource(currentInstances, json, selectedGrid); instances.value = usePodsSource(
currentInstances,
json,
dashboardStore.selectedGrid
);
} }
function changePage(pageIndex: number) { function changePage(pageIndex: number) {