Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/validator diagnostics #1205

Merged
merged 6 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading