2024-12-31 16:18:51 +08:00
|
|
|
// Package handler provides HTTP handlers for various endpoints.
|
2024-11-22 16:41:04 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
2024-11-27 15:41:22 +08:00
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
2024-11-22 16:41:04 +08:00
|
|
|
|
2025-01-21 16:35:44 +08:00
|
|
|
"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"
|
2024-11-22 16:41:04 +08:00
|
|
|
|
|
|
|
|
"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) {
|
2025-01-21 16:35:44 +08:00
|
|
|
pgClient := database.GetPostgresDBClient()
|
|
|
|
|
|
2024-11-27 15:41:22 +08:00
|
|
|
pageID, err := strconv.ParseInt(c.Query("page_id"), 10, 64)
|
|
|
|
|
if err != nil {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "get pageID from url param failed", "error", err)
|
2025-01-23 14:56:01 +08:00
|
|
|
|
2024-12-06 16:13:11 +08:00
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
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)
|
2024-11-29 16:34:03 +08:00
|
|
|
return
|
2024-11-27 15:41:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topologicInfo, err := diagram.GetGraphMap(pageID)
|
|
|
|
|
if err != nil {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "get topologic data from set by pageID failed", "error", err)
|
2025-01-23 14:56:01 +08:00
|
|
|
|
2024-12-06 16:13:11 +08:00
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
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)
|
2024-11-29 16:34:03 +08:00
|
|
|
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 {
|
2025-01-21 16:35:44 +08:00
|
|
|
component, err := database.QueryComponentByUUID(c, pgClient, componentUUID)
|
|
|
|
|
if err != nil {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "get component id info from DB by uuid failed", "error", err)
|
2025-01-21 16:35:44 +08:00
|
|
|
|
|
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-01-21 16:35:44 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"uuid": componentUUID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentParams, err := diagram.GetComponentMap(component.ID)
|
2024-11-27 15:41:22 +08:00
|
|
|
if err != nil {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "get component data from set by uuid failed", "error", err)
|
2025-01-23 14:56:01 +08:00
|
|
|
|
2024-12-06 16:13:11 +08:00
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2024-11-27 15:41:22 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
2025-01-21 16:35:44 +08:00
|
|
|
"uuid": componentUUID,
|
2024-11-27 15:41:22 +08:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
2024-11-29 16:34:03 +08:00
|
|
|
return
|
2024-11-27 15:41:22 +08:00
|
|
|
}
|
2025-01-21 16:35:44 +08:00
|
|
|
componentParamMap[componentUUID.String()] = componentParams
|
2024-11-27 15:41:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-07 16:45:52 +08:00
|
|
|
|
|
|
|
|
rootVertexUUID := topologicInfo.RootVertex.String()
|
2025-01-21 16:35:44 +08:00
|
|
|
rootComponent, err := database.QueryComponentByUUID(c, pgClient, topologicInfo.RootVertex)
|
|
|
|
|
if err != nil {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "get component id info from DB by uuid failed", "error", err)
|
2025-01-21 16:35:44 +08:00
|
|
|
|
|
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-01-21 16:35:44 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"uuid": topologicInfo.RootVertex,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rootComponentParam, err := diagram.GetComponentMap(rootComponent.ID)
|
2025-01-07 16:45:52 +08:00
|
|
|
if err != nil {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "get component data from set by uuid failed", "error", err)
|
2025-01-23 14:56:01 +08:00
|
|
|
|
2025-01-07 16:45:52 +08:00
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-01-07 16:45:52 +08:00
|
|
|
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{
|
2025-01-23 14:56:01 +08:00
|
|
|
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)
|
2024-11-22 16:41:04 +08:00
|
|
|
}
|