set root dashboards

This commit is contained in:
Qiuxia Fan 2022-03-16 11:49:05 +08:00
parent 1cdd65f49c
commit bdcaba8828
4 changed files with 147 additions and 49 deletions

View File

@ -98,6 +98,8 @@ const msg = {
editTab: "Enable editing tab names", editTab: "Enable editing tab names",
label: "Name", label: "Name",
id: "ID", id: "ID",
setRoot: "Set this to root",
setNormal: "Set this to normal",
hourTip: "Select Hour", hourTip: "Select Hour",
minuteTip: "Select Minute", minuteTip: "Select Minute",
secondTip: "Select Second", secondTip: "Select Second",

View File

@ -97,7 +97,9 @@ const msg = {
sampledTraces: "采样的追踪", sampledTraces: "采样的追踪",
editTab: "开启编辑Tab的名称", editTab: "开启编辑Tab的名称",
label: "名称", label: "名称",
id: "ID", id: "编号",
setRoot: "设置成为根",
setNormal: "设置成为普通",
hourTip: "选择小时", hourTip: "选择小时",
minuteTip: "选择分钟", minuteTip: "选择分钟",
secondTip: "选择秒数", secondTip: "选择秒数",

View File

@ -74,6 +74,18 @@ export const dashboardStore = defineStore({
setLayout(data: LayoutConfig[]) { setLayout(data: LayoutConfig[]) {
this.layout = data; this.layout = data;
}, },
resetDashboards(
list: {
name: string;
layer: string;
entity: string;
id: string;
isRoot: boolean;
}[]
) {
this.dashboards = list;
sessionStorage.setItem("dashboards", JSON.stringify(list));
},
setCurrentDashboard(item: { setCurrentDashboard(item: {
name: string; name: string;
layer: string; layer: string;
@ -349,23 +361,7 @@ export const dashboardStore = defineStore({
sessionStorage.getItem("dashboards") || "[]" sessionStorage.getItem("dashboards") || "[]"
); );
}, },
async updateDashboard(param: { name: string }) { async updateDashboard(setting: { id: string; configuration: string }) {
const key = [
this.currentDashboard.layer,
this.currentDashboard.entity,
this.currentDashboard.name.split(" ").join("-"),
].join("_");
const layout = sessionStorage.getItem(key);
const c = {
isRoot: false,
children: layout,
...this.currentDashboard,
name: param.name,
};
const setting = {
id: this.currentDashboard.id,
configuration: JSON.stringify(c),
};
const res: AxiosResponse = await graphql.query("updateTemplate").params({ const res: AxiosResponse = await graphql.query("updateTemplate").params({
setting, setting,
}); });
@ -376,24 +372,10 @@ export const dashboardStore = defineStore({
const json = res.data.data.changeTemplate; const json = res.data.data.changeTemplate;
if (!json.status) { if (!json.status) {
ElMessage.error(json.message); ElMessage.error(json.message);
return; return res.data;
} }
ElMessage.success("Saved successfully"); ElMessage.success("Saved successfully");
this.currentDashboard.name = param.name; return res.data;
this.dashboards = this.dashboards.map((d: any) => {
if (d.id === this.currentDashboard.id) {
d = this.currentDashboard;
}
return d;
});
sessionStorage.setItem("dashboards", JSON.stringify(this.dashboards));
sessionStorage.removeItem(key);
const str = [
this.currentDashboard.layer,
this.currentDashboard.entity,
param.name.split(" ").join("-"),
].join("_");
sessionStorage.setItem(str, JSON.stringify(setting));
}, },
async saveDashboard() { async saveDashboard() {
if (!this.currentDashboard.name) { if (!this.currentDashboard.name) {
@ -405,17 +387,17 @@ export const dashboardStore = defineStore({
children: this.layout, children: this.layout,
...this.currentDashboard, ...this.currentDashboard,
}; };
let res: AxiosResponse; let res: any;
let json; let json;
if (this.currentDashboard.id) { if (this.currentDashboard.id) {
res = await graphql.query("updateTemplate").params({ res = await this.updateDashboard({
setting: { setting: {
id: this.currentDashboard.id, id: this.currentDashboard.id,
configuration: JSON.stringify(c), configuration: JSON.stringify(c),
}, },
}); });
json = res.data.data.changeTemplate; json = res.data.changeTemplate;
} else { } else {
const index = this.dashboards.findIndex( const index = this.dashboards.findIndex(
(d: { name: string; entity: string; layer: string; id: string }) => (d: { name: string; entity: string; layer: string; id: string }) =>
@ -433,7 +415,7 @@ export const dashboardStore = defineStore({
json = res.data.data.addTemplate; json = res.data.data.addTemplate;
} }
if (res.data.errors) { if (res.data.errors || res.errors) {
ElMessage.error(res.data.errors); ElMessage.error(res.data.errors);
return res.data; return res.data;
} }

View File

@ -47,15 +47,25 @@ limitations under the License. -->
<el-table-column prop="entity" label="Entity" /> <el-table-column prop="entity" label="Entity" />
<el-table-column label="Operations"> <el-table-column label="Operations">
<template #default="scope"> <template #default="scope">
<el-button size="small" @click="handleView(scope.$index)"> <el-button size="small" @click="handleView(scope.row)">
{{ t("view") }} {{ t("view") }}
</el-button> </el-button>
<el-button size="small" @click="handleEdit(scope.$index)"> <el-button size="small" @click="handleEdit(scope.row)">
{{ t("edit") }} {{ t("edit") }}
</el-button> </el-button>
<el-popconfirm
title="Are you sure to set this?"
@confirm="setRoot(scope.row)"
>
<template #reference>
<el-button size="small">
{{ scope.row.isRoot ? t("setRoot") : t("setNormal") }}
</el-button>
</template>
</el-popconfirm>
<el-popconfirm <el-popconfirm
title="Are you sure to delete this?" title="Are you sure to delete this?"
@confirm="handleDelete(scope.$index)" @confirm="handleDelete(scope.row)"
> >
<template #reference> <template #reference>
<el-button size="small" type="danger"> <el-button size="small" type="danger">
@ -110,17 +120,75 @@ const handleView = (index: number) => {
`/dashboard/${d.layer}/${d.entity}/${d.name.split(" ").join("-")}` `/dashboard/${d.layer}/${d.entity}/${d.name.split(" ").join("-")}`
); );
}; };
function handleEdit(index: number) { async function setRoot(row: {
const d = dashboards.value[index]; entity: string;
layer: string;
name: string;
id: string;
isRoot: boolean;
}) {
const items = dashboardStore.dashboards.map(async (d: any) => {
if (d.id === row.id) {
d.isRoot = !row.isRoot;
const key = [d.layer, d.entity, d.name.split(" ").join("-")].join("_");
const layout = sessionStorage.getItem(key) || "{}";
const c = {
...JSON.parse(layout).configuration,
...d,
};
const setting = {
id: d.id,
configuration: JSON.stringify(c),
};
const res = await dashboardStore.updateDashboard(setting);
if (!res.data.changeTemplate.id) {
return d;
}
sessionStorage.setItem(key, JSON.stringify(setting));
return d;
}
if (
d.layer === row.layer &&
d.entity === row.entity &&
!row.isRoot &&
d.isRoot
) {
d.isRoot = false;
const key = [d.layer, d.entity, d.name.split(" ").join("-")].join("_");
const layout = sessionStorage.getItem(key) || "{}";
const c = {
...JSON.parse(layout).configuration,
...d,
};
const setting = {
id: d.id,
configuration: JSON.stringify(c),
};
const res = await dashboardStore.updateDashboard(setting);
if (!res.data.changeTemplate.id) {
return d;
}
sessionStorage.setItem(key, JSON.stringify(setting));
return d;
}
return d;
});
dashboardStore.resetDashboards(items);
searchDashboards();
}
function handleEdit(row: {
entity: string;
layer: string;
name: string;
id: string;
}) {
ElMessageBox.prompt("Please input dashboard name", "Edit", { ElMessageBox.prompt("Please input dashboard name", "Edit", {
confirmButtonText: "OK", confirmButtonText: "OK",
cancelButtonText: "Cancel", cancelButtonText: "Cancel",
inputValue: d.name, inputValue: row.name,
}) })
.then(({ value }) => { .then(({ value }) => {
dashboardStore.setCurrentDashboard(d); updateName(row, value);
dashboardStore.updateDashboard({ name: value });
}) })
.catch(() => { .catch(() => {
ElMessage({ ElMessage({
@ -129,8 +197,52 @@ function handleEdit(index: number) {
}); });
}); });
} }
async function handleDelete(index: number) { async function updateName(
const row = dashboards.value[index]; d: { entity: string; layer: string; name: string; id: string },
value: string
) {
const key = [d.layer, d.entity, d.name.split(" ").join("-")].join("_");
const layout = sessionStorage.getItem(key) || "{}";
const c = {
...JSON.parse(layout).configuration,
...d,
name: value,
};
const setting = {
id: d.id,
configuration: JSON.stringify(c),
};
const res = await dashboardStore.updateDashboard(setting);
if (!res.data.changeTemplate.id) {
return;
}
dashboardStore.setCurrentDashboard({
...d,
name: value,
});
dashboards.value = dashboardStore.dashboards.map((item: any) => {
if (dashboardStore.currentDashboard.id === item.id) {
item = dashboardStore.currentDashboard;
}
return item;
});
dashboardStore.resetDashboards(dashboards.value);
sessionStorage.setItem("dashboards", JSON.stringify(dashboards.value));
sessionStorage.removeItem(key);
const str = [
dashboardStore.currentDashboard.layer,
dashboardStore.currentDashboard.entity,
dashboardStore.currentDashboard.name.split(" ").join("-"),
].join("_");
sessionStorage.setItem(str, JSON.stringify(setting));
searchText.value = "";
}
async function handleDelete(row: {
entity: string;
layer: string;
name: string;
id: string;
}) {
dashboardStore.setCurrentDashboard(row); dashboardStore.setCurrentDashboard(row);
loading.value = true; loading.value = true;
await dashboardStore.deleteDashboard(); await dashboardStore.deleteDashboard();