Implement top shares page
This commit is contained in:
parent
260d2ec24b
commit
d801debaf6
@ -14,6 +14,9 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
CollectionName = "shares"
|
CollectionName = "shares"
|
||||||
|
|
||||||
|
TopSharesCollectionName = "TopShares"
|
||||||
|
TopSharesAmount = 20
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitDatabase(path string) (*clover.DB, error) {
|
func InitDatabase(path string) (*clover.DB, error) {
|
||||||
@ -42,10 +45,30 @@ func InitDatabase(path string) (*clover.DB, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Init TopShares collection
|
||||||
|
hasTopSharesCollection, err := db.HasCollection(TopSharesCollectionName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to check TopShares collection: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasTopSharesCollection {
|
||||||
|
if err := db.CreateCollection(TopSharesCollectionName); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create TopShares collection: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.CreateIndex(TopSharesCollectionName, "CreateDate"); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create index for TopShares: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.CreateIndex(TopSharesCollectionName, "SDiff"); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create index for TopShares SDiff: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetHighestShareInRange(db *clover.DB, collection string, since time.Time) (*document.Document, error) {
|
func GetHighestSharesInRange(db *clover.DB, collection string, since time.Time, count int) ([]models.ShareLog, error) {
|
||||||
// Convert `since` to the format in `createdate`
|
// Convert `since` to the format in `createdate`
|
||||||
lower := since.Unix()
|
lower := since.Unix()
|
||||||
upper := time.Now().Unix()
|
upper := time.Now().Unix()
|
||||||
@ -58,12 +81,22 @@ func GetHighestShareInRange(db *clover.DB, collection string, since time.Time) (
|
|||||||
results, err := db.FindAll(c.NewQuery(collection).
|
results, err := db.FindAll(c.NewQuery(collection).
|
||||||
Where(criteria).
|
Where(criteria).
|
||||||
Sort(c.SortOption{Field: "SDiff", Direction: -1}).
|
Sort(c.SortOption{Field: "SDiff", Direction: -1}).
|
||||||
Limit(1))
|
Limit(count))
|
||||||
|
|
||||||
if err != nil || len(results) == 0 {
|
if err != nil || len(results) == 0 {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return results[0], nil
|
|
||||||
|
var shares []models.ShareLog
|
||||||
|
for _, doc := range results {
|
||||||
|
var s models.ShareLog
|
||||||
|
if err := doc.Unmarshal(&s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
shares = append(shares, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return shares, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrintAllHashes(db *clover.DB) {
|
func PrintAllHashes(db *clover.DB) {
|
||||||
@ -99,3 +132,36 @@ func ListShares(db *clover.DB, offset int, count int) []models.ShareLog {
|
|||||||
|
|
||||||
return shareLogs
|
return shareLogs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ListTopShares(db *clover.DB) []models.ShareLog {
|
||||||
|
results, err := db.FindAll(
|
||||||
|
c.NewQuery(TopSharesCollectionName).
|
||||||
|
Sort(c.SortOption{Field: "SDiff", Direction: -1}),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to list top shares: %v", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
topShares := make([]models.ShareLog, len(results))
|
||||||
|
for idx, doc := range results {
|
||||||
|
var shareLog models.ShareLog
|
||||||
|
doc.Unmarshal(&shareLog)
|
||||||
|
topShares[idx] = shareLog
|
||||||
|
}
|
||||||
|
|
||||||
|
return topShares
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReplaceTopShares(db *clover.DB, shares []models.ShareLog) {
|
||||||
|
db.Delete(c.NewQuery(TopSharesCollectionName))
|
||||||
|
|
||||||
|
for _, share := range shares {
|
||||||
|
doc := document.NewDocumentOf(&share)
|
||||||
|
if _, err := db.InsertOne(TopSharesCollectionName, doc); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Replaced TopShares with %d shares", len(shares))
|
||||||
|
}
|
||||||
|
60
jobs/recalculateTopShares.go
Normal file
60
jobs/recalculateTopShares.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package jobs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"pool-stats/database"
|
||||||
|
"pool-stats/helpers"
|
||||||
|
"pool-stats/models"
|
||||||
|
"pool-stats/notlinq"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ostafen/clover/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RecalculateTopSharesJob struct {
|
||||||
|
DB *clover.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRecalculateTopSharesJob(db *clover.DB) *RecalculateTopSharesJob {
|
||||||
|
return &RecalculateTopSharesJob{DB: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (job *RecalculateTopSharesJob) Run() error {
|
||||||
|
ticker := time.NewTicker(10 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
<-ticker.C
|
||||||
|
job.recalculateTopShares()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (job *RecalculateTopSharesJob) recalculateTopShares() {
|
||||||
|
topSharesAmount := database.TopSharesAmount
|
||||||
|
currentTopShares := database.ListTopShares(job.DB)
|
||||||
|
|
||||||
|
var newTopShares []models.ShareLog
|
||||||
|
if currentTopShares == nil || len(currentTopShares) < topSharesAmount {
|
||||||
|
newTopShares, _ = database.GetHighestSharesInRange(job.DB, database.CollectionName, time.Unix(0, 0), topSharesAmount)
|
||||||
|
} else {
|
||||||
|
sort.Slice(currentTopShares, func(i, j int) bool {
|
||||||
|
return currentTopShares[i].CreateDate > currentTopShares[j].CreateDate
|
||||||
|
})
|
||||||
|
lastTopShareDate := currentTopShares[0].CreateDate
|
||||||
|
lastTopShareDateTime := helpers.ParseCreateDate(lastTopShareDate)
|
||||||
|
newTopShares, _ = database.GetHighestSharesInRange(job.DB, database.CollectionName, lastTopShareDateTime, topSharesAmount)
|
||||||
|
}
|
||||||
|
|
||||||
|
newTopShares = append(newTopShares, currentTopShares...)
|
||||||
|
sort.Slice(newTopShares, func(i, j int) bool {
|
||||||
|
return newTopShares[i].SDiff > newTopShares[j].SDiff
|
||||||
|
})
|
||||||
|
newTopShares = notlinq.UniqueBy(newTopShares, func(s models.ShareLog) string {
|
||||||
|
return s.Hash
|
||||||
|
})
|
||||||
|
if len(newTopShares) > topSharesAmount {
|
||||||
|
newTopShares = newTopShares[:topSharesAmount]
|
||||||
|
}
|
||||||
|
|
||||||
|
database.ReplaceTopShares(job.DB, newTopShares)
|
||||||
|
}
|
4
main.go
4
main.go
@ -10,6 +10,7 @@ import (
|
|||||||
"pool-stats/config"
|
"pool-stats/config"
|
||||||
"pool-stats/database"
|
"pool-stats/database"
|
||||||
"pool-stats/ingest"
|
"pool-stats/ingest"
|
||||||
|
"pool-stats/jobs"
|
||||||
"pool-stats/web"
|
"pool-stats/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,6 +26,9 @@ func main() {
|
|||||||
ingestor := ingest.NewIngestor(db, config.LogPath)
|
ingestor := ingest.NewIngestor(db, config.LogPath)
|
||||||
go ingestor.WatchAndIngest()
|
go ingestor.WatchAndIngest()
|
||||||
|
|
||||||
|
topSharesRecalcJob := jobs.NewRecalculateTopSharesJob(db)
|
||||||
|
go topSharesRecalcJob.Run()
|
||||||
|
|
||||||
webServer := web.NewWebServer(db, config.Port)
|
webServer := web.NewWebServer(db, config.Port)
|
||||||
if err := webServer.Start(); err != nil {
|
if err := webServer.Start(); err != nil {
|
||||||
log.Fatalf("Failed to start web server: %v", err)
|
log.Fatalf("Failed to start web server: %v", err)
|
||||||
|
14
notlinq/notlinq.go
Normal file
14
notlinq/notlinq.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package notlinq
|
||||||
|
|
||||||
|
func UniqueBy[T any, K comparable](items []T, keySelector func(T) K) []T {
|
||||||
|
seen := make(map[K]struct{})
|
||||||
|
result := make([]T, 0, len(items))
|
||||||
|
for _, item := range items {
|
||||||
|
key := keySelector(item)
|
||||||
|
if _, exists := seen[key]; !exists {
|
||||||
|
seen[key] = struct{}{}
|
||||||
|
result = append(result, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
@ -24,22 +24,22 @@ func GetStats(db *clover.DB) ([]models.ShareStat, error) {
|
|||||||
stats := []models.ShareStat{}
|
stats := []models.ShareStat{}
|
||||||
|
|
||||||
// All-time highest
|
// All-time highest
|
||||||
doc, _ := database.GetHighestShareInRange(db, database.CollectionName, time.Unix(0, 0))
|
doc, _ := database.GetHighestSharesInRange(db, database.CollectionName, time.Unix(0, 0), 1)
|
||||||
if doc != nil {
|
if doc != nil {
|
||||||
stats = append(stats, models.ShareStat{
|
stats = append(stats, models.ShareStat{
|
||||||
Label: "All Time",
|
Label: "All Time",
|
||||||
Diff: helpers.HumanDiff(doc.Get("SDiff").(float64)),
|
Diff: helpers.HumanDiff(doc[0].SDiff),
|
||||||
Time: helpers.ParseCreateDate(doc.Get("CreateDate").(string)).Format(time.RFC822),
|
Time: helpers.ParseCreateDate(doc[0].CreateDate).Format(time.RFC822),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range ranges {
|
for _, r := range ranges {
|
||||||
doc, _ := database.GetHighestShareInRange(db, database.CollectionName, r.Since)
|
doc, _ := database.GetHighestSharesInRange(db, database.CollectionName, r.Since, 1)
|
||||||
if doc != nil {
|
if doc != nil {
|
||||||
stats = append(stats, models.ShareStat{
|
stats = append(stats, models.ShareStat{
|
||||||
Label: r.Label,
|
Label: r.Label,
|
||||||
Diff: helpers.HumanDiff(doc.Get("SDiff").(float64)),
|
Diff: helpers.HumanDiff(doc[0].SDiff),
|
||||||
Time: helpers.ParseCreateDate(doc.Get("CreateDate").(string)).Format(time.RFC822),
|
Time: helpers.ParseCreateDate(doc[0].CreateDate).Format(time.RFC822),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
stats = append(stats, models.ShareStat{Label: r.Label, Diff: "-", Time: "-"})
|
stats = append(stats, models.ShareStat{Label: r.Label, Diff: "-", Time: "-"})
|
||||||
|
@ -57,6 +57,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<li><a href="/">Home</a></li>
|
<li><a href="/">Home</a></li>
|
||||||
<li><a href="/shares">View Shares</a></li>
|
<li><a href="/shares">View Shares</a></li>
|
||||||
|
<li><a href="/top-shares">Top Shares</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -97,6 +97,7 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<li><a href="/">Home</a></li>
|
<li><a href="/">Home</a></li>
|
||||||
<li><a href="/shares">View Shares</a></li>
|
<li><a href="/shares">View Shares</a></li>
|
||||||
|
<li><a href="/top-shares">Top Shares</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
84
templates/top_shares.html
Normal file
84
templates/top_shares.html
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
{{ define "top_shares" }}
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>ckpool Top Shares</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: sans-serif;
|
||||||
|
background: #111;
|
||||||
|
color: #eee;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
th,
|
||||||
|
td {
|
||||||
|
padding: 8px 12px;
|
||||||
|
border: 1px solid #444;
|
||||||
|
text-align: left;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
th {
|
||||||
|
background-color: #222;
|
||||||
|
}
|
||||||
|
a.page-link {
|
||||||
|
margin: 0 5px;
|
||||||
|
text-decoration: none;
|
||||||
|
color: #0af;
|
||||||
|
}
|
||||||
|
a.page-link.current {
|
||||||
|
font-weight: bold;
|
||||||
|
color: #fff;
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
color: #0af;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
li {
|
||||||
|
display: inline;
|
||||||
|
margin: 0 10px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>☀️ Pool Top Shares</h1>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Time</th>
|
||||||
|
<th>Worker</th>
|
||||||
|
<th>Address</th>
|
||||||
|
<th>SDiff</th>
|
||||||
|
<th>Hash</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{{ range .Shares }}
|
||||||
|
<tr>
|
||||||
|
<td>{{ formatCreateDate .CreateDate }}</td>
|
||||||
|
<td>{{ .WorkerName }}</td>
|
||||||
|
<td>{{ .Address }}</td>
|
||||||
|
<td>{{ humanDiff .SDiff }}</td>
|
||||||
|
<td><code style="font-size: small">{{ .Hash }}</code></td>
|
||||||
|
</tr>
|
||||||
|
{{ else }}
|
||||||
|
<tr>
|
||||||
|
<td colspan="6">No shares found.</td>
|
||||||
|
</tr>
|
||||||
|
{{ end }}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<ul>
|
||||||
|
<li><a href="/">Home</a></li>
|
||||||
|
<li><a href="/shares">View Shares</a></li>
|
||||||
|
<li><a href="/top-shares">Top Shares</a></li>
|
||||||
|
</ul>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
{{ end }}
|
@ -23,6 +23,7 @@ func NewWebServer(db *clover.DB, port int) *WebServer {
|
|||||||
func (ws *WebServer) Start() error {
|
func (ws *WebServer) Start() error {
|
||||||
http.HandleFunc("/", ws.IndexHandler)
|
http.HandleFunc("/", ws.IndexHandler)
|
||||||
http.HandleFunc("/shares", ws.SharesHandler)
|
http.HandleFunc("/shares", ws.SharesHandler)
|
||||||
|
http.HandleFunc("/top-shares", ws.TopSharesHandler)
|
||||||
|
|
||||||
address := ":" + fmt.Sprint(ws.port)
|
address := ":" + fmt.Sprint(ws.port)
|
||||||
println("Listening on", address)
|
println("Listening on", address)
|
||||||
|
39
web/topSharesHandler.go
Normal file
39
web/topSharesHandler.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
"pool-stats/database"
|
||||||
|
"pool-stats/helpers"
|
||||||
|
"pool-stats/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TopSharesPageData struct {
|
||||||
|
Shares []models.ShareLog
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *WebServer) TopSharesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
tmpl := template.New("top_shares").Funcs(template.FuncMap{
|
||||||
|
"humanDiff": helpers.HumanDiff,
|
||||||
|
"formatCreateDate": helpers.FormatCreateDate,
|
||||||
|
})
|
||||||
|
tmpl, err := tmpl.ParseFiles("templates/top_shares.html")
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Failed to load template", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
topShares := database.ListTopShares(ws.db)
|
||||||
|
if topShares == nil {
|
||||||
|
http.Error(w, "Failed to load top shares", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := TopSharesPageData{
|
||||||
|
Shares: topShares,
|
||||||
|
}
|
||||||
|
if err := tmpl.Execute(w, data); err != nil {
|
||||||
|
http.Error(w, "Failed to render template", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user