Implement top shares page
This commit is contained in:
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user