Skip to content

Commit

Permalink
Feature/validator diagnostics (#1205)
Browse files Browse the repository at this point in the history
* Added stats for validator

* Beautify

* Fix lint

* Added mutex

* Added mutex for getters

* Added mutex for getters
  • Loading branch information
Jayashsatolia403 authored Aug 12, 2023
1 parent 3a1a405 commit 1637690
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
7 changes: 7 additions & 0 deletions code/go/0chain.net/validatorcore/storage/challenge_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ func challengeHandler(ctx context.Context, r *http.Request) (interface{}, error)

err = challengeRequest.VerifyChallenge(challengeObj, allocationObj)
if err != nil {
statsMutex.Lock()
updateStats(false)
statsMutex.Unlock()
return InvalidValidationTicket(challengeObj, err)
}

statsMutex.Lock()
updateStats(false)
statsMutex.Unlock()

return ValidValidationTicket(challengeObj, challengeRequest.ChallengeID, challengeHash)
}

Expand Down
2 changes: 2 additions & 0 deletions code/go/0chain.net/validatorcore/storage/handler_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ func SetupHandlers(r *mux.Router) {
RateLimit(common.ToJSONResponse(SetupContext(ChallengeHandler))))

r.HandleFunc("/debug", common.ToJSONResponse(DumpGoRoutines))

r.HandleFunc("/stats", statsHandler)
}
100 changes: 100 additions & 0 deletions code/go/0chain.net/validatorcore/storage/stats_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package storage

import (
"fmt"
"net/http"
"sync"
)

type Stats struct {
TotalChallenges int
SuccessfulChallenges int
FailedChallenges int
}

var (
appStats Stats
statsMutex sync.Mutex
)

func init() {
appStats = Stats{}

}

func updateStats(success bool) {
statsMutex.Lock()
defer statsMutex.Unlock()

appStats.TotalChallenges++

if success {
appStats.SuccessfulChallenges++
} else {
appStats.FailedChallenges++
}
}

func getStats() Stats {
statsMutex.Lock()
defer statsMutex.Unlock()
return appStats
}

func statsHandler(w http.ResponseWriter, r *http.Request) {
result := getStats()

statsHTML := `
<!DOCTYPE html>
<html>
<head>
<style>
table {
font-family: Arial, sans-serif;
border-collapse: collapse;
width: 50%;
margin: auto;
margin-top: 50px;
}
th, td {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>Challenges Statistics</h1>
<table>
<tr>
<th>Statistic</th>
<th>Count</th>
</tr>
<tr>
<td>Total Challenges</td>
<td>` + fmt.Sprintf("%d", result.TotalChallenges) + `</td>
</tr>
<tr>
<td>Successful Challenges</td>
<td>` + fmt.Sprintf("%d", result.SuccessfulChallenges) + `</td>
</tr>
<tr>
<td>Failed Challenges</td>
<td>` + fmt.Sprintf("%d", result.FailedChallenges) + `</td>
</tr>
</table>
</body>
</html>
`

w.Header().Set("Content-Type", "text/html")
_, err := w.Write([]byte(statsHTML))
if err != nil {
return
}
}

0 comments on commit 1637690

Please sign in to comment.