remove, add, update templates

This commit is contained in:
Qiuxia Fan 2022-03-15 18:19:09 +08:00
parent c38a147e30
commit 08e770e0a4
11 changed files with 207 additions and 89 deletions

View File

@ -174,7 +174,7 @@ export const AllLayout = {
export const ServiceLayout = { export const ServiceLayout = {
id: "1", id: "1",
configuration: { configuration: {
name: "Service Layout", name: "Service Root Layout",
layer: "GENERAL", layer: "GENERAL",
entity: "Service", entity: "Service",
isRoot: true, isRoot: true,

View File

@ -26,41 +26,3 @@ export const OAPTimeInfo = {
export const OAPVersion = { export const OAPVersion = {
query: `version { version }`, query: `version { version }`,
}; };
export const getAllTemplates = {
query: `
getAllTemplates {
id,
configuration,
}
`,
};
export const addTemplate = {
variable: "$setting: NewDashboardSetting!",
query: `
addTemplate(setting: $setting) {
id
status
message
}`,
};
export const changeTemplate = {
variable: "$setting: DashboardSetting!",
query: `
changeTemplate(setting: $setting) {
id
status
message
}`,
};
export const deleteTemplate = {
variable: "$id: String!",
query: `
disableTemplate(id: $id) {
id
status
message
}`,
};

View File

@ -30,3 +30,41 @@ export const listMetrics = {
} }
`, `,
}; };
export const getAllTemplates = {
query: `
getAllTemplates {
id,
configuration,
}
`,
};
export const addTemplate = {
variable: "$setting: NewDashboardSetting!",
query: `
addTemplate(setting: $setting) {
id
status
message
}`,
};
export const changeTemplate = {
variable: "$setting: DashboardSetting!",
query: `
changeTemplate(setting: $setting) {
id
status
message
}`,
};
export const deleteTemplate = {
variable: "$id: String!",
query: `
disableTemplate(id: $id) {
id
status
message
}`,
};

View File

@ -14,23 +14,8 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { import { OAPTimeInfo, OAPVersion } from "../fragments/app";
OAPTimeInfo,
OAPVersion,
getAllTemplates,
addTemplate,
changeTemplate,
deleteTemplate,
} from "../fragments/app";
export const queryOAPTimeInfo = `query queryOAPTimeInfo {${OAPTimeInfo.query}}`; export const queryOAPTimeInfo = `query queryOAPTimeInfo {${OAPTimeInfo.query}}`;
export const queryOAPVersion = `query ${OAPVersion.query}`; export const queryOAPVersion = `query ${OAPVersion.query}`;
export const getTemplates = `query queryTemplates {${getAllTemplates.query}}`;
export const addNewTemplate = `query queryAlarms(${addTemplate.variable}) {${addTemplate.query}}`;
export const updateTemplate = `query queryAlarms(${changeTemplate.variable}) {${changeTemplate.query}}`;
export const removeTemplate = `query queryAlarms(${deleteTemplate.variable}) {${deleteTemplate.query}}`;

View File

@ -14,8 +14,23 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { TypeOfMetrics, listMetrics } from "../fragments/dashboard"; import {
TypeOfMetrics,
listMetrics,
getAllTemplates,
addTemplate,
changeTemplate,
deleteTemplate,
} from "../fragments/dashboard";
export const queryTypeOfMetrics = `query typeOfMetrics(${TypeOfMetrics.variable}) {${TypeOfMetrics.query}}`; export const queryTypeOfMetrics = `query typeOfMetrics(${TypeOfMetrics.variable}) {${TypeOfMetrics.query}}`;
export const queryMetrics = `query queryData(${listMetrics.variable}) {${listMetrics.query}}`; export const queryMetrics = `query queryData(${listMetrics.variable}) {${listMetrics.query}}`;
export const addNewTemplate = `mutation template(${addTemplate.variable}) {${addTemplate.query}}`;
export const updateTemplate = `mutation template(${changeTemplate.variable}) {${changeTemplate.query}}`;
export const removeTemplate = `mutation template(${deleteTemplate.variable}) {${deleteTemplate.query}}`;
export const getTemplates = `query templates {${getAllTemplates.query}}`;

View File

@ -19,15 +19,15 @@ import { store } from "@/store";
import { LayoutConfig } from "@/types/dashboard"; import { LayoutConfig } from "@/types/dashboard";
import graphql from "@/graphql"; import graphql from "@/graphql";
import query from "@/graphql/fetch"; import query from "@/graphql/fetch";
import { // import {
ServiceLayout, // ServiceLayout,
AllLayout, // AllLayout,
EndpointLayout, // EndpointLayout,
InstanceLayout, // InstanceLayout,
ServiceRelationLayout, // ServiceRelationLayout,
InstanceRelationLayout, // InstanceRelationLayout,
EndpointRelationLayout, // EndpointRelationLayout,
} from "@/constants/templates"; // } from "@/constants/templates";
import { useAppStoreWithOut } from "@/store/modules/app"; import { useAppStoreWithOut } from "@/store/modules/app";
import { useSelectorStore } from "@/store/modules/selectors"; import { useSelectorStore } from "@/store/modules/selectors";
import { NewControl } from "../data"; import { NewControl } from "../data";
@ -45,13 +45,19 @@ interface DashboardState {
selectorStore: any; selectorStore: any;
showTopology: boolean; showTopology: boolean;
currentTabItems: LayoutConfig[]; currentTabItems: LayoutConfig[];
dashboards: { name: string; layer: string; entity: string }[]; dashboards: { name: string; layer: string; entity: string; id: string }[];
currentDashboard: Nullable<{
name: string;
layer: string;
entity: string;
id: string;
}>;
} }
export const dashboardStore = defineStore({ export const dashboardStore = defineStore({
id: "dashboard", id: "dashboard",
state: (): DashboardState => ({ state: (): DashboardState => ({
layout: ServiceLayout.configuration.children, layout: [],
showConfig: false, showConfig: false,
selectedGrid: null, selectedGrid: null,
entity: "", entity: "",
@ -62,11 +68,21 @@ export const dashboardStore = defineStore({
showTopology: false, showTopology: false,
currentTabItems: [], currentTabItems: [],
dashboards: [], dashboards: [],
currentDashboard: null,
}), }),
actions: { actions: {
setLayout(data: LayoutConfig[]) { setLayout(data: LayoutConfig[]) {
this.layout = data; this.layout = data;
}, },
setCurrentDashboard(item: {
name: string;
layer: string;
entity: string;
id: string;
}) {
console.log(item);
this.currentDashboard = item;
},
addControl(type: string) { addControl(type: string) {
const newItem: LayoutConfig = { const newItem: LayoutConfig = {
...NewControl, ...NewControl,
@ -290,18 +306,19 @@ export const dashboardStore = defineStore({
if (res.data.errors) { if (res.data.errors) {
return res.data; return res.data;
} }
const data = [ // const data = [
ServiceLayout, // ServiceLayout,
AllLayout, // AllLayout,
EndpointLayout, // EndpointLayout,
InstanceLayout, // InstanceLayout,
ServiceRelationLayout, // ServiceRelationLayout,
InstanceRelationLayout, // InstanceRelationLayout,
EndpointRelationLayout, // EndpointRelationLayout,
].map((t: any) => { // ].map((t: any) => {
t.configuration = JSON.stringify(t.configuration); // t.configuration = JSON.stringify(t.configuration);
return t; // return t;
}); // });
const data = res.data.data.getAllTemplates;
const list = []; const list = [];
for (const t of data) { for (const t of data) {
const c = JSON.parse(t.configuration); const c = JSON.parse(t.configuration);
@ -332,6 +349,94 @@ export const dashboardStore = defineStore({
sessionStorage.getItem("dashboards") || "[]" sessionStorage.getItem("dashboards") || "[]"
); );
}, },
async saveDashboard() {
if (!this.currentDashboard.name) {
ElMessage.error("The dashboard name is needed.");
return;
}
const c = {
...this.currentDashboard,
isRoot: false,
children: this.layout,
};
let res: AxiosResponse;
let json;
if (this.currentDashboard.id) {
res = await graphql.query("updateTemplate").params({
setting: {
id: this.currentDashboard.id,
configuration: JSON.stringify(c),
},
});
json = res.data.data.changeTemplate;
} else {
const index = this.dashboards.findIndex(
(d: { name: string; entity: string; layer: string; id: string }) =>
d.name === this.currentDashboard.name &&
d.entity === this.currentDashboard.entity &&
d.layer === this.currentDashboard.layerId
);
if (index > -1) {
ElMessage.error("The dashboard name cannot be duplicate.");
return;
}
res = await graphql
.query("addNewTemplate")
.params({ setting: { configuration: JSON.stringify(c) } });
json = res.data.data.addTemplate;
}
if (res.data.errors) {
ElMessage.error(res.data.errors);
return res.data;
}
if (!json.status) {
ElMessage.error(json.message);
return;
}
ElMessage.success("Saved successfully");
this.dashboards.push({
id: json.id,
name,
layer: this.layerId,
entity: this.entity,
isRoot: true,
});
const key = [
this.layer,
this.entity,
this.currentDashboard.name.split(" ").join("-"),
].join("_");
const l = { id: json.id, configuration: c };
sessionStorage.setItem(key, JSON.stringify(l));
sessionStorage.setItem("dashboards", JSON.stringify(this.dashboards));
},
async deleteDashbaord() {
const res: AxiosResponse = await graphql
.query("removeTemplate")
.params({ id: this.currentDashboard.id });
if (res.data.errors) {
ElMessage.error(res.data.errors);
return res.data;
}
const json = res.data.data.disableTemplate;
if (!json.status) {
ElMessage.error(json.message);
return res.data;
}
this.dashboards = this.dashboards.filter(
(d: any) => d.id !== this.currentDashboard.id
);
const key = [
this.currentDashboard.layer,
this.currentDashboard.entity,
this.currentDashboard.name.split(" ").join("-"),
].join("_");
sessionStorage.removeItem(key);
},
}, },
}); });

View File

@ -63,6 +63,7 @@ async function setTemplate() {
sessionStorage.getItem(layoutKey) || "{}" sessionStorage.getItem(layoutKey) || "{}"
); );
const layout = JSON.parse(c.configuration || "{}"); const layout = JSON.parse(c.configuration || "{}");
dashboardStore.setLayout(layout.children || []); dashboardStore.setLayout(layout.children || []);
} }
function handleClick(e: any) { function handleClick(e: any) {

View File

@ -51,7 +51,7 @@ limitations under the License. -->
</el-button> </el-button>
<el-popconfirm <el-popconfirm
title="Are you sure to delete this?" title="Are you sure to delete this?"
@confirm="handleDelete(scope.$index, scope.row)" @confirm="handleDelete(scope.row)"
> >
<template #reference> <template #reference>
<el-button size="small" type="danger"> <el-button size="small" type="danger">
@ -98,20 +98,24 @@ async function setList() {
dashboards.value = dashboardStore.dashboards; dashboards.value = dashboardStore.dashboards;
} }
const handleEdit = (row: { name: string; layer: string; entity: string }) => { const handleEdit = (row: { name: string; layer: string; entity: string }) => {
dashboardStore.setCurrentDashboard(row);
router.push( router.push(
`/dashboard/${row.layer}/${row.entity}/${row.name.split(" ").join("-")}` `/dashboard/${row.layer}/${row.entity}/${row.name.split(" ").join("-")}`
); );
}; };
const handleDelete = ( async function handleDelete(row: {
index: number, name: string;
row: { name: string; layer: string; entity: string } layer: string;
) => { entity: string;
dashboards.value.splice(index, 1); }) {
dashboardStore.setCurrentDashboard(row);
await dashboardStore.deleteDashbaord();
dashboards.value = dashboardStore.dashboards;
sessionStorage.setItem("dashboards", JSON.stringify(dashboards.value)); sessionStorage.setItem("dashboards", JSON.stringify(dashboards.value));
sessionStorage.removeItem( sessionStorage.removeItem(
`${row.layer}_${row.entity}_${row.name.split(" ").join("-")}` `${row.layer}_${row.entity}_${row.name.split(" ").join("-")}`
); );
}; }
function searchDashboards() { function searchDashboards() {
const list = JSON.parse(sessionStorage.getItem("dashboards") || "[]"); const list = JSON.parse(sessionStorage.getItem("dashboards") || "[]");
dashboards.value = list.filter((d: { name: string }) => dashboards.value = list.filter((d: { name: string }) =>

View File

@ -85,6 +85,11 @@ const onCreate = () => {
ElMessage.error("The dashboard name cannot be duplicate."); ElMessage.error("The dashboard name cannot be duplicate.");
return; return;
} }
dashboardStore.setCurrentDashboard({
name: states.name,
entity: states.entity,
layer: states.selectedLayer,
});
const name = states.name.split(" ").join("-"); const name = states.name.split(" ").join("-");
const path = `/dashboard/${states.selectedLayer}/${states.entity}/${name}`; const path = `/dashboard/${states.selectedLayer}/${states.entity}/${name}`;
router.push(path); router.push(path);

View File

@ -19,7 +19,6 @@ limitations under the License. -->
:row-height="10" :row-height="10"
:is-draggable="true" :is-draggable="true"
:is-resizable="true" :is-resizable="true"
@layout-updated="layoutUpdatedEvent"
> >
<grid-item <grid-item
v-for="item in dashboardStore.layout" v-for="item in dashboardStore.layout"
@ -49,6 +48,7 @@ export default defineComponent({
setup() { setup() {
const dashboardStore = useDashboardStore(); const dashboardStore = useDashboardStore();
function layoutUpdatedEvent(newLayout: LayoutConfig[]) { function layoutUpdatedEvent(newLayout: LayoutConfig[]) {
console.log(newLayout);
dashboardStore.setLayout(newLayout); dashboardStore.setLayout(newLayout);
} }
function clickGrid(item: LayoutConfig) { function clickGrid(item: LayoutConfig) {

View File

@ -316,6 +316,9 @@ function setTabControls(id: string) {
case "addTopology": case "addTopology":
dashboardStore.addTabControls("Topology"); dashboardStore.addTabControls("Topology");
break; break;
case "apply":
dashboardStore.saveDashboard(params.name);
break;
default: default:
ElMessage.info("Don't support this control"); ElMessage.info("Don't support this control");
break; break;
@ -342,8 +345,8 @@ function setControls(id: string) {
case "addTopology": case "addTopology":
dashboardStore.addControl("Topology"); dashboardStore.addControl("Topology");
break; break;
case "settings": case "apply":
dashboardStore.setConfigPanel(true); dashboardStore.saveDashboard(params.name);
break; break;
default: default:
dashboardStore.addControl("Widget"); dashboardStore.addControl("Widget");