-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/validator diagnostics (#1205)
* Added stats for validator * Beautify * Fix lint * Added mutex * Added mutex for getters * Added mutex for getters
- Loading branch information
1 parent
3a1a405
commit 1637690
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
code/go/0chain.net/validatorcore/storage/stats_handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |