mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-27 13:12:35 +08:00
add graph api:
This commit is contained in:
@@ -594,6 +594,24 @@ func GetAllNodesAPI(nodes []models.Node) []models.ApiNode {
|
|||||||
return apiNodes[:]
|
return apiNodes[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllNodesAPI - get all nodes for api usage
|
||||||
|
func GetAllNodesAPIWithLocation(nodes []models.Node) []models.ApiNode {
|
||||||
|
apiNodes := []models.ApiNode{}
|
||||||
|
for i := range nodes {
|
||||||
|
node := nodes[i]
|
||||||
|
newApiNode := node.ConvertToAPINode()
|
||||||
|
if node.IsStatic {
|
||||||
|
newApiNode.Location = node.StaticNode.Location
|
||||||
|
} else {
|
||||||
|
host, _ := GetHost(node.HostID.String())
|
||||||
|
newApiNode.Location = host.Location
|
||||||
|
}
|
||||||
|
|
||||||
|
apiNodes = append(apiNodes, *newApiNode)
|
||||||
|
}
|
||||||
|
return apiNodes[:]
|
||||||
|
}
|
||||||
|
|
||||||
// GetNodesStatusAPI - gets nodes status
|
// GetNodesStatusAPI - gets nodes status
|
||||||
func GetNodesStatusAPI(nodes []models.Node) map[string]models.ApiNodeStatus {
|
func GetNodesStatusAPI(nodes []models.Node) map[string]models.ApiNodeStatus {
|
||||||
apiStatusNodesMap := make(map[string]models.ApiNodeStatus)
|
apiStatusNodesMap := make(map[string]models.ApiNodeStatus)
|
||||||
|
@@ -62,6 +62,7 @@ type ApiNode struct {
|
|||||||
IsUserNode bool `json:"is_user_node"`
|
IsUserNode bool `json:"is_user_node"`
|
||||||
StaticNode ExtClient `json:"static_node"`
|
StaticNode ExtClient `json:"static_node"`
|
||||||
Status NodeStatus `json:"status"`
|
Status NodeStatus `json:"status"`
|
||||||
|
Location string `json:"location"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApiNode.ConvertToServerNode - converts an api node to a server node
|
// ApiNode.ConvertToServerNode - converts an api node to a server node
|
||||||
|
@@ -20,6 +20,7 @@ func MetricHandlers(r *mux.Router) {
|
|||||||
r.HandleFunc("/api/metrics/{network}", logic.SecurityCheck(true, http.HandlerFunc(getNetworkNodesMetrics))).Methods(http.MethodGet)
|
r.HandleFunc("/api/metrics/{network}", logic.SecurityCheck(true, http.HandlerFunc(getNetworkNodesMetrics))).Methods(http.MethodGet)
|
||||||
r.HandleFunc("/api/metrics", logic.SecurityCheck(true, http.HandlerFunc(getAllMetrics))).Methods(http.MethodGet)
|
r.HandleFunc("/api/metrics", logic.SecurityCheck(true, http.HandlerFunc(getAllMetrics))).Methods(http.MethodGet)
|
||||||
r.HandleFunc("/api/metrics-ext/{network}", logic.SecurityCheck(true, http.HandlerFunc(getNetworkExtMetrics))).Methods(http.MethodGet)
|
r.HandleFunc("/api/metrics-ext/{network}", logic.SecurityCheck(true, http.HandlerFunc(getNetworkExtMetrics))).Methods(http.MethodGet)
|
||||||
|
r.HandleFunc("/api/v1/graph/{network}", logic.SecurityCheck(true, http.HandlerFunc(graph))).Methods(http.MethodGet)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the metrics of a given node
|
// get the metrics of a given node
|
||||||
@@ -165,3 +166,21 @@ func getAllMetrics(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(networkMetrics)
|
json.NewEncoder(w).Encode(networkMetrics)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func graph(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
var params = mux.Vars(r)
|
||||||
|
network := params["network"]
|
||||||
|
networkNodes, err := logic.GetNetworkNodes(network)
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(1, r.Header.Get("user"), "failed to get network nodes", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
networkNodes = logic.AddStaticNodestoList(networkNodes)
|
||||||
|
// return all the nodes in JSON/API format
|
||||||
|
apiNodes := logic.GetAllNodesAPIWithLocation(networkNodes[:])
|
||||||
|
logic.SortApiNodes(apiNodes[:])
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode(apiNodes)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user