Skip to content

Commit

Permalink
Add support to json format for _stats API
Browse files Browse the repository at this point in the history
  • Loading branch information
shaktals committed Apr 27, 2022
1 parent 4f1c80e commit eee0667
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
38 changes: 38 additions & 0 deletions code/go/0chain.net/blobbercore/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,45 @@ func UpdateAttributesHandler(ctx context.Context, r *http.Request) (interface{},
return response, nil
}

func writeResponse (w http.ResponseWriter, resp []byte) {
_, err := w.Write(resp)

if err != nil {
Logger.Error("Error sending StatsHandler response", zap.Error(err))
}
}

func StatsHandler(w http.ResponseWriter, r *http.Request) {
isJSON := r.Header.Get("Accept") == "application/json"

if isJSON {
blobberInfo := GetBlobberInfoJson()

ctx := datastore.GetStore().CreateTransaction(r.Context())
blobberStats, err := stats.StatsJSONHandler(ctx, r)

if err != nil {
Logger.Error("Error getting blobber JSON stats", zap.Error(err))

w.WriteHeader(http.StatusInternalServerError)
writeResponse(w, []byte(err.Error()))
}

blobberInfo.Stats = blobberStats

statsJson, err := json.Marshal(blobberInfo)

if err != nil {
Logger.Error("Error marshaling JSON stats", zap.Error(err))

w.WriteHeader(http.StatusInternalServerError)
writeResponse(w, []byte(err.Error()))
}

writeResponse(w, statsJson)

return
}

HTMLHeader(w, "Blobber Diagnostics")
PrintCSS(w)
Expand Down
21 changes: 21 additions & 0 deletions code/go/0chain.net/blobbercore/handler/handler_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ func HomepageHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "</br>")
}

type BlobberInfo struct {
ChainId string `json:"chainId"`
BlobberId string `json:"blobberId"`
BlobberPublicKey string `json:"blobberPubKey"`
BuildTag string `json:"buildTag"`
Stats interface{} `json:"stats"`
}

func GetBlobberInfoJson() BlobberInfo {
mc := chain.GetServerChain()

blobberInfo := BlobberInfo{
ChainId: mc.ID,
BlobberId: node.Self.ID,
BlobberPublicKey: node.Self.PublicKey,
BuildTag: build.BuildTag,
}

return blobberInfo
}

func WithStatusConnection(handler common.StatusCodeResponderF) common.StatusCodeResponderF {
return func(ctx context.Context, r *http.Request) (resp interface{}, statusCode int, err error) {
ctx = GetMetaDataStore().CreateTransaction(ctx)
Expand Down

0 comments on commit eee0667

Please sign in to comment.