Skip to content

Commit

Permalink
Add more resource metrics on getUsage handler
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielseibel1 committed Sep 22, 2023
1 parent 0453a5b commit 34a787e
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions controllers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/gorilla/mux"

"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
Expand All @@ -15,24 +16,32 @@ import (

func serverHandlers(r *mux.Router) {
// r.HandleFunc("/api/server/addnetwork/{network}", securityCheckServer(true, http.HandlerFunc(addNetwork))).Methods(http.MethodPost)
r.HandleFunc("/api/server/health", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(http.StatusOK)
resp.Write([]byte("Server is up and running!!"))
}))
r.HandleFunc("/api/server/getconfig", allowUsers(http.HandlerFunc(getConfig))).Methods(http.MethodGet)
r.HandleFunc("/api/server/getserverinfo", Authorize(true, false, "node", http.HandlerFunc(getServerInfo))).Methods(http.MethodGet)
r.HandleFunc(
"/api/server/health",
http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(http.StatusOK)
resp.Write([]byte("Server is up and running!!"))
}),
)
r.HandleFunc("/api/server/getconfig", allowUsers(http.HandlerFunc(getConfig))).
Methods(http.MethodGet)
r.HandleFunc("/api/server/getserverinfo", Authorize(true, false, "node", http.HandlerFunc(getServerInfo))).
Methods(http.MethodGet)
r.HandleFunc("/api/server/status", http.HandlerFunc(getStatus)).Methods(http.MethodGet)
r.HandleFunc("/api/server/usage", Authorize(true, false, "user", http.HandlerFunc(getUsage))).Methods(http.MethodGet)
r.HandleFunc("/api/server/usage", Authorize(true, false, "user", http.HandlerFunc(getUsage))).
Methods(http.MethodGet)
}

func getUsage(w http.ResponseWriter, r *http.Request) {
func getUsage(w http.ResponseWriter, _ *http.Request) {
type usage struct {
Hosts int `json:"hosts"`
Clients int `json:"clients"`
Networks int `json:"networks"`
Users int `json:"users"`
Ingresses int `json:"ingresses"`
Egresses int `json:"egresses"`
Hosts int `json:"hosts"`
Clients int `json:"clients"`
Networks int `json:"networks"`
Users int `json:"users"`
Ingresses int `json:"ingresses"`
Egresses int `json:"egresses"`
Relays int `json:"relays"`
InternetGateways int `json:"internet_gateways"`
}
var serverUsage usage
hosts, err := logic.GetAllHosts()
Expand All @@ -51,6 +60,7 @@ func getUsage(w http.ResponseWriter, r *http.Request) {
if err == nil {
serverUsage.Networks = len(networks)
}
// TODO this part bellow can be optimized to get nodes just once
ingresses, err := logic.GetAllIngresses()
if err == nil {
serverUsage.Ingresses = len(ingresses)
Expand All @@ -59,12 +69,19 @@ func getUsage(w http.ResponseWriter, r *http.Request) {
if err == nil {
serverUsage.Egresses = len(egresses)
}
relays, err := logic.GetRelays()
if err == nil {
serverUsage.Relays = len(relays)
}
gateways, err := logic.GetInternetGateways()
if err == nil {
serverUsage.InternetGateways = len(gateways)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(models.SuccessResponse{
Code: http.StatusOK,
Response: serverUsage,
})

}

// swagger:route GET /api/server/status server getStatus
Expand Down Expand Up @@ -105,12 +122,12 @@ func getStatus(w http.ResponseWriter, r *http.Request) {
// allowUsers - allow all authenticated (valid) users - only used by getConfig, may be able to remove during refactor
func allowUsers(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var errorResponse = models.ErrorResponse{
errorResponse := models.ErrorResponse{
Code: http.StatusUnauthorized, Message: logic.Unauthorized_Msg,
}
bearerToken := r.Header.Get("Authorization")
var tokenSplit = strings.Split(bearerToken, " ")
var authToken = ""
tokenSplit := strings.Split(bearerToken, " ")
authToken := ""
if len(tokenSplit) < 2 {
logic.ReturnErrorResponse(w, r, errorResponse)
return
Expand Down Expand Up @@ -144,7 +161,7 @@ func getServerInfo(w http.ResponseWriter, r *http.Request) {
// get params

json.NewEncoder(w).Encode(servercfg.GetServerInfo())
//w.WriteHeader(http.StatusOK)
// w.WriteHeader(http.StatusOK)
}

// swagger:route GET /api/server/getconfig server getConfig
Expand All @@ -170,5 +187,5 @@ func getConfig(w http.ResponseWriter, r *http.Request) {
scfg.IsPro = "yes"
}
json.NewEncoder(w).Encode(scfg)
//w.WriteHeader(http.StatusOK)
// w.WriteHeader(http.StatusOK)
}

0 comments on commit 34a787e

Please sign in to comment.