modelRT/handler/circuit_diagram_load.go

139 lines
3.7 KiB
Go
Raw Normal View History

2024-12-31 16:18:51 +08:00
// Package handler provides HTTP handlers for various endpoints.
package handler
import (
2024-11-27 15:41:22 +08:00
"net/http"
"strconv"
"modelRT/database"
2024-11-27 15:41:22 +08:00
"modelRT/diagram"
2024-12-25 16:34:57 +08:00
"modelRT/logger"
2024-11-27 15:41:22 +08:00
"modelRT/network"
"github.com/gin-gonic/gin"
)
2024-11-27 16:48:11 +08:00
// CircuitDiagramLoadHandler define circuit diagram load process API
2024-12-06 16:13:11 +08:00
// @Summary load circuit diagram info
// @Description load circuit diagram info by page id
// @Tags load circuit_diagram
// @Accept json
// @Produce json
// @Param page_id path int64 true "page ID"
// @Success 200 {object} network.SuccessResponse "request process success"
// @Failure 400 {object} network.FailureResponse "request process failed"
// @Router /model/diagram_load/{page_id} [get]
2024-11-27 16:48:11 +08:00
func CircuitDiagramLoadHandler(c *gin.Context) {
pgClient := database.GetPostgresDBClient()
2024-11-27 15:41:22 +08:00
pageID, err := strconv.ParseInt(c.Query("page_id"), 10, 64)
if err != nil {
logger.Error(c, "get pageID from url param failed", "error", err)
2024-12-06 16:13:11 +08:00
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
2024-11-27 15:41:22 +08:00
PayLoad: map[string]interface{}{
"page_id": pageID,
},
}
c.JSON(http.StatusOK, resp)
return
2024-11-27 15:41:22 +08:00
}
topologicInfo, err := diagram.GetGraphMap(pageID)
if err != nil {
logger.Error(c, "get topologic data from set by pageID failed", "error", err)
2024-12-06 16:13:11 +08:00
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
2024-11-27 15:41:22 +08:00
PayLoad: map[string]interface{}{
"page_id": pageID,
},
}
c.JSON(http.StatusOK, resp)
return
2024-11-27 15:41:22 +08:00
}
payLoad := make(map[string]interface{})
payLoad["root_vertex"] = topologicInfo.RootVertex
payLoad["topologic"] = topologicInfo.VerticeLinks
2024-12-18 16:25:49 +08:00
componentParamMap := make(map[string]any)
2024-11-27 15:41:22 +08:00
for _, VerticeLink := range topologicInfo.VerticeLinks {
for _, componentUUID := range VerticeLink {
component, err := database.QueryComponentByUUID(c, pgClient, componentUUID)
if err != nil {
logger.Error(c, "get component id info from DB by uuid failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{
"uuid": componentUUID,
},
}
c.JSON(http.StatusOK, resp)
return
}
componentParams, err := diagram.GetComponentMap(component.GlobalUUID.String())
2024-11-27 15:41:22 +08:00
if err != nil {
logger.Error(c, "get component data from set by uuid failed", "error", err)
2024-12-06 16:13:11 +08:00
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
2024-11-27 15:41:22 +08:00
PayLoad: map[string]interface{}{
"uuid": componentUUID,
2024-11-27 15:41:22 +08:00
},
}
c.JSON(http.StatusOK, resp)
return
2024-11-27 15:41:22 +08:00
}
componentParamMap[componentUUID.String()] = componentParams
2024-11-27 15:41:22 +08:00
}
}
rootVertexUUID := topologicInfo.RootVertex.String()
rootComponent, err := database.QueryComponentByUUID(c, pgClient, topologicInfo.RootVertex)
if err != nil {
logger.Error(c, "get component id info from DB by uuid failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{
"uuid": topologicInfo.RootVertex,
},
}
c.JSON(http.StatusOK, resp)
return
}
rootComponentParam, err := diagram.GetComponentMap(rootComponent.GlobalUUID.String())
if err != nil {
logger.Error(c, "get component data from set by uuid failed", "error", err)
resp := network.FailureResponse{
Code: http.StatusBadRequest,
Msg: err.Error(),
PayLoad: map[string]interface{}{
"uuid": rootVertexUUID,
},
}
c.JSON(http.StatusOK, resp)
return
}
componentParamMap[rootVertexUUID] = rootComponentParam
2024-11-27 15:41:22 +08:00
payLoad["component_params"] = componentParamMap
2024-12-06 16:13:11 +08:00
resp := network.SuccessResponse{
Code: http.StatusOK,
Msg: "success",
2024-12-06 16:13:11 +08:00
PayLoad: payLoad,
2024-11-27 15:41:22 +08:00
}
c.JSON(http.StatusOK, resp)
}