feat: apply dashboard config

This commit is contained in:
Qiuxia Fan 2022-01-07 11:34:41 +08:00
parent e72368b6fc
commit edda37078a
6 changed files with 75 additions and 23 deletions

View File

@ -63,7 +63,7 @@ export const dashboardStore = defineStore({
this.showConfig = show; this.showConfig = show;
}, },
selectWidget(widget: Nullable<LayoutConfig>) { selectWidget(widget: Nullable<LayoutConfig>) {
this.selectedWidget = ConfigData || widget; this.selectedWidget = ConfigData || widget; //todo
}, },
setLayer(id: string) { setLayer(id: string) {
this.layerId = id; this.layerId = id;
@ -71,6 +71,13 @@ export const dashboardStore = defineStore({
setEntity(type: string) { setEntity(type: string) {
this.entity = type; this.entity = type;
}, },
setConfigs(param: { [key: string]: unknown }) {
const index = this.layout.findIndex((d: LayoutConfig) => d.i === param.i);
this.layout[index] = {
...this.layout[index],
...param,
};
},
async fetchMetricType(item: string) { async fetchMetricType(item: string) {
const res: AxiosResponse = await graph const res: AxiosResponse = await graph
.query("queryTypeOfMetrics") .query("queryTypeOfMetrics")

View File

@ -25,8 +25,8 @@ export const ConfigData: LayoutConfig = {
queryMetricType: "readMetricsValues", queryMetricType: "readMetricsValues",
chart: "Line", chart: "Line",
widget: { widget: {
title: "Title", title: "Title123",
tips: "Tooltip", tips: "Tooltip123",
}, },
graph: { graph: {
showBackground: true, showBackground: true,

View File

@ -82,7 +82,7 @@ limitations under the License. -->
<el-button size="mini"> <el-button size="mini">
{{ t("cancel") }} {{ t("cancel") }}
</el-button> </el-button>
<el-button size="mini" type="primary"> <el-button size="mini" type="primary" @click="applyConfig">
{{ t("apply") }} {{ t("apply") }}
</el-button> </el-button>
</div> </div>
@ -120,6 +120,7 @@ export default defineComponent({
const dashboardStore = useDashboardStore(); const dashboardStore = useDashboardStore();
const appStoreWithOut = useAppStoreWithOut(); const appStoreWithOut = useAppStoreWithOut();
const { loading } = Loading(); const { loading } = Loading();
const { selectedWidget } = dashboardStore;
const states = reactive<{ const states = reactive<{
metrics: string[]; metrics: string[];
valueTypes: Option[]; valueTypes: Option[];
@ -127,19 +128,27 @@ export default defineComponent({
metricQueryType: string; metricQueryType: string;
chartType: string; chartType: string;
activeNames: string; activeNames: string;
tips: string;
source: any; source: any;
title: string; index: string;
}>({ }>({
metrics: dashboardStore.selectedWidget.metrics || [], metrics: selectedWidget.metrics || [],
valueTypes: [], valueTypes: [],
valueType: "", valueType: "",
metricQueryType: "", metricQueryType: "",
chartType: dashboardStore.selectedWidget.chart, chartType: selectedWidget.chart,
activeNames: "1", activeNames: "1",
source: {}, source: {},
tips: "", index: selectedWidget.i,
title: "", });
const widgetOpt = reactive<
| {
title: string;
tips: string;
}
| any
>({
tips: selectedWidget.widget.tips,
title: selectedWidget.widget.title,
}); });
queryMetricType(states.metrics[0]); queryMetricType(states.metrics[0]);
@ -164,7 +173,6 @@ export default defineComponent({
const { typeOfMetrics } = resp.data; const { typeOfMetrics } = resp.data;
states.valueTypes = ValuesTypes[typeOfMetrics]; states.valueTypes = ValuesTypes[typeOfMetrics];
states.valueType = ValuesTypes[typeOfMetrics][0].value; states.valueType = ValuesTypes[typeOfMetrics][0].value;
console.log(states.valueType);
} }
function changeValueType(val: Option[]) { function changeValueType(val: Option[]) {
@ -190,13 +198,11 @@ export default defineComponent({
]; ];
const configHeight = document.documentElement.clientHeight - 520; const configHeight = document.documentElement.clientHeight - 520;
function updateWidgetOptions(param: any) { function updateWidgetOptions(param: { label: string; value: string }) {
if (param.title !== undefined) { if (widgetOpt[param.label] === undefined) {
states.title = param.title; return;
}
if (param.tips !== undefined) {
states.tips = param.tips;
} }
widgetOpt[param.label] = param.value;
} }
async function queryMetrics() { async function queryMetrics() {
@ -208,7 +214,7 @@ export default defineComponent({
return; return;
} }
const metricVal = json.data.readMetricsValues.values.values.map( const metricVal = json.data.readMetricsValues.values.values.map(
(d: any) => d.value (d: { value: number }, index: number) => d.value + index // todo
); );
const m = const m =
dashboardStore.selectedWidget.metrics && dashboardStore.selectedWidget.metrics &&
@ -223,7 +229,30 @@ export default defineComponent({
queryMetrics(); queryMetrics();
function applyConfig() {
const opts = {
...dashboardStore.selectedWidget,
metrics: states.metrics,
queryMetricType: states.valueType,
chart: states.chartType,
widget: {
...dashboardStore.selectedWidget.widget,
title: widgetOpt.title,
tips: widgetOpt.tips,
},
graph: {
...dashboardStore.selectedWidget.graph,
},
standard: {
...dashboardStore.selectedWidget.standard,
},
};
dashboardStore.setConfigs(opts);
dashboardStore.setConfigPanel(false);
}
return { return {
...toRefs(widgetOpt),
...toRefs(states), ...toRefs(states),
changeChartType, changeChartType,
changeValueType, changeValueType,
@ -235,6 +264,7 @@ export default defineComponent({
updateWidgetOptions, updateWidgetOptions,
configHeight, configHeight,
dashboardStore, dashboardStore,
applyConfig,
}; };
}, },
}); });

View File

@ -45,10 +45,10 @@ const title = ref<string>("");
const tooltip = ref<string>(""); const tooltip = ref<string>("");
function updateTitle(value: string) { function updateTitle(value: string) {
emits("update", { title: value }); emits("update", { value, label: "title" });
} }
function updateTips(value: string) { function updateTips(value: string) {
emits("update", { tips: value }); emits("update", { value, label: "tips" });
} }
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -47,11 +47,11 @@ export const ValuesTypes: {
[key: string]: Array<{ label: string; value: string }>; [key: string]: Array<{ label: string; value: string }>;
} = { } = {
REGULAR_VALUE: [ REGULAR_VALUE: [
{ label: "read all values in the duration", value: "readMetricsValues" },
{ {
label: "read the single value in the duration", label: "read the single value in the duration",
value: "readMetricsValue", value: "readMetricsValue",
}, },
{ label: "read all values in the duration", value: "readMetricsValues" },
{ label: "get sorted top N values", value: "sortMetrics" }, { label: "get sorted top N values", value: "sortMetrics" },
], ],
LABELED_VALUE: [ LABELED_VALUE: [

View File

@ -15,7 +15,7 @@ limitations under the License. -->
<template> <template>
<div class="widget"> <div class="widget">
<div class="header flex-h"> <div class="header flex-h">
<div>Title</div> <div>{{ item.widget.title || "" }}</div>
<div class="operations"> <div class="operations">
<Icon <Icon
class="mr-5" class="mr-5"
@ -42,14 +42,23 @@ import { LayoutConfig } from "@/types/dashboard";
import { useDashboardStore } from "@/store/modules/dashboard"; import { useDashboardStore } from "@/store/modules/dashboard";
import { useAppStoreWithOut } from "@/store/modules/app"; import { useAppStoreWithOut } from "@/store/modules/app";
import graphs from "../graphs"; import graphs from "../graphs";
import { ElMessage } from "element-plus";
import Loading from "@/utils/loading";
import { useI18n } from "vue-i18n";
const props = { const props = {
item: { type: Object as PropType<LayoutConfig>, default: () => ({}) }, item: {
type: Object as PropType<LayoutConfig>,
default: () => ({ widget: {} }),
},
}; };
export default defineComponent({ export default defineComponent({
name: "Widget", name: "Widget",
components: { ...graphs }, components: { ...graphs },
props, props,
setup(props) { setup(props) {
const { t } = useI18n();
const { loading } = Loading();
const state = reactive({ const state = reactive({
source: {}, source: {},
}); });
@ -58,9 +67,15 @@ export default defineComponent({
const dashboardStore = useDashboardStore(); const dashboardStore = useDashboardStore();
queryMetrics(); queryMetrics();
async function queryMetrics() { async function queryMetrics() {
const loadingInstance = loading({
text: t("loading"),
fullscreen: false,
});
const json = await dashboardStore.fetchMetricValue(props.item); const json = await dashboardStore.fetchMetricValue(props.item);
loadingInstance.close();
if (json.error) { if (json.error) {
ElMessage.error(json.error);
return; return;
} }
const metricVal = json.data.readMetricsValues.values.values.map( const metricVal = json.data.readMetricsValues.values.values.map(