Skip to content

Commit b7213aa

Browse files
authored
Merge branch 'sprint-july-4' into refactor/db-txn
2 parents ed6d659 + 1637690 commit b7213aa

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

code/go/0chain.net/validatorcore/storage/challenge_handler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ func challengeHandler(ctx context.Context, r *http.Request) (interface{}, error)
4949

5050
err = challengeRequest.VerifyChallenge(challengeObj, allocationObj)
5151
if err != nil {
52+
statsMutex.Lock()
53+
updateStats(false)
54+
statsMutex.Unlock()
5255
return InvalidValidationTicket(challengeObj, err)
5356
}
5457

58+
statsMutex.Lock()
59+
updateStats(false)
60+
statsMutex.Unlock()
61+
5562
return ValidValidationTicket(challengeObj, challengeRequest.ChallengeID, challengeHash)
5663
}
5764

code/go/0chain.net/validatorcore/storage/handler_main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ func SetupHandlers(r *mux.Router) {
1818
RateLimit(common.ToJSONResponse(SetupContext(ChallengeHandler))))
1919

2020
r.HandleFunc("/debug", common.ToJSONResponse(DumpGoRoutines))
21+
22+
r.HandleFunc("/stats", statsHandler)
2123
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package storage
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"sync"
7+
)
8+
9+
type Stats struct {
10+
TotalChallenges int
11+
SuccessfulChallenges int
12+
FailedChallenges int
13+
}
14+
15+
var (
16+
appStats Stats
17+
statsMutex sync.Mutex
18+
)
19+
20+
func init() {
21+
appStats = Stats{}
22+
23+
}
24+
25+
func updateStats(success bool) {
26+
statsMutex.Lock()
27+
defer statsMutex.Unlock()
28+
29+
appStats.TotalChallenges++
30+
31+
if success {
32+
appStats.SuccessfulChallenges++
33+
} else {
34+
appStats.FailedChallenges++
35+
}
36+
}
37+
38+
func getStats() Stats {
39+
statsMutex.Lock()
40+
defer statsMutex.Unlock()
41+
return appStats
42+
}
43+
44+
func statsHandler(w http.ResponseWriter, r *http.Request) {
45+
result := getStats()
46+
47+
statsHTML := `
48+
<!DOCTYPE html>
49+
<html>
50+
<head>
51+
<style>
52+
table {
53+
font-family: Arial, sans-serif;
54+
border-collapse: collapse;
55+
width: 50%;
56+
margin: auto;
57+
margin-top: 50px;
58+
}
59+
60+
th, td {
61+
border: 1px solid #dddddd;
62+
text-align: left;
63+
padding: 8px;
64+
}
65+
66+
th {
67+
background-color: #f2f2f2;
68+
}
69+
</style>
70+
</head>
71+
<body>
72+
<h1>Challenges Statistics</h1>
73+
<table>
74+
<tr>
75+
<th>Statistic</th>
76+
<th>Count</th>
77+
</tr>
78+
<tr>
79+
<td>Total Challenges</td>
80+
<td>` + fmt.Sprintf("%d", result.TotalChallenges) + `</td>
81+
</tr>
82+
<tr>
83+
<td>Successful Challenges</td>
84+
<td>` + fmt.Sprintf("%d", result.SuccessfulChallenges) + `</td>
85+
</tr>
86+
<tr>
87+
<td>Failed Challenges</td>
88+
<td>` + fmt.Sprintf("%d", result.FailedChallenges) + `</td>
89+
</tr>
90+
</table>
91+
</body>
92+
</html>
93+
`
94+
95+
w.Header().Set("Content-Type", "text/html")
96+
_, err := w.Write([]byte(statsHTML))
97+
if err != nil {
98+
return
99+
}
100+
}

0 commit comments

Comments
 (0)