Skip to content

Commit 894e579

Browse files
shaktalspeterlimg
andauthored
Add support to json format for _stats API (#663)
Co-authored-by: peterlimg <54137706+peterlimg@users.noreply.github.com>
1 parent c3e8ee6 commit 894e579

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

code/go/0chain.net/blobbercore/handler/handler.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,47 @@ func UpdateAttributesHandler(ctx context.Context, r *http.Request) (interface{},
293293
return response, nil
294294
}
295295

296+
func writeResponse (w http.ResponseWriter, resp []byte) {
297+
_, err := w.Write(resp)
298+
299+
if err != nil {
300+
Logger.Error("Error sending StatsHandler response", zap.Error(err))
301+
}
302+
}
303+
296304
func StatsHandler(w http.ResponseWriter, r *http.Request) {
305+
isJSON := r.Header.Get("Accept") == "application/json"
306+
307+
if isJSON {
308+
blobberInfo := GetBlobberInfoJson()
309+
310+
ctx := datastore.GetStore().CreateTransaction(r.Context())
311+
blobberStats, err := stats.StatsJSONHandler(ctx, r)
312+
313+
if err != nil {
314+
Logger.Error("Error getting blobber JSON stats", zap.Error(err))
315+
316+
w.WriteHeader(http.StatusInternalServerError)
317+
writeResponse(w, []byte(err.Error()))
318+
return
319+
}
320+
321+
blobberInfo.Stats = blobberStats
322+
323+
statsJson, err := json.Marshal(blobberInfo)
324+
325+
if err != nil {
326+
Logger.Error("Error marshaling JSON stats", zap.Error(err))
327+
328+
w.WriteHeader(http.StatusInternalServerError)
329+
writeResponse(w, []byte(err.Error()))
330+
return
331+
}
332+
333+
writeResponse(w, statsJson)
334+
335+
return
336+
}
297337

298338
HTMLHeader(w, "Blobber Diagnostics")
299339
PrintCSS(w)

code/go/0chain.net/blobbercore/handler/handler_common.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@ func HomepageHandler(w http.ResponseWriter, r *http.Request) {
9494
fmt.Fprintf(w, "</br>")
9595
}
9696

97+
type BlobberInfo struct {
98+
ChainId string `json:"chain_id"`
99+
BlobberId string `json:"blobber_id"`
100+
BlobberPublicKey string `json:"public_key"`
101+
BuildTag string `json:"build_tag"`
102+
Stats interface{} `json:"stats"`
103+
}
104+
105+
func GetBlobberInfoJson() BlobberInfo {
106+
mc := chain.GetServerChain()
107+
108+
blobberInfo := BlobberInfo{
109+
ChainId: mc.ID,
110+
BlobberId: node.Self.ID,
111+
BlobberPublicKey: node.Self.PublicKey,
112+
BuildTag: build.BuildTag,
113+
}
114+
115+
return blobberInfo
116+
}
117+
97118
func WithStatusConnection(handler common.StatusCodeResponderF) common.StatusCodeResponderF {
98119
return func(ctx context.Context, r *http.Request) (resp interface{}, statusCode int, err error) {
99120
ctx = GetMetaDataStore().CreateTransaction(ctx)

0 commit comments

Comments
 (0)