Restructure

This commit is contained in:
Pijus Kamandulis 2025-06-23 19:38:02 +03:00
parent f66fbcc454
commit 4ddd9abd2e
7 changed files with 25 additions and 28 deletions

View File

@ -8,6 +8,12 @@ const (
RecalculateTimeWindowHighSharesJobInterval = 1 * time.Minute
// RecalculateTopSharesJob interval
RecalculateTopSharesJobInterval = 30 * time.Second
// IngestorWatchInterval interval
IngestorWatchInterval = 30 * time.Second
// IngestSharesJob interval
IngestSharesJobInterval = 30 * time.Second
)
// counts and stuff
const (
// TopSharesAmount is the number of top shares to keep
TopSharesAmount = 15
)

View File

@ -13,11 +13,8 @@ import (
)
const (
CollectionName = "shares"
TopSharesCollectionName = "TopShares"
TopSharesAmount = 20
CollectionName = "shares"
TopSharesCollectionName = "TopShares"
TimeWindowHighShareCollectionName = "TimeWindowHighShareStat"
)
@ -179,8 +176,6 @@ func ReplaceTopShares(db *clover.DB, shares []models.ShareLog) {
return
}
}
log.Printf("Replaced TopShares with %d shares", len(shares))
}
func GetTimeWindowHighShares(db *clover.DB) []models.TimeWindowHighShare {

View File

@ -1,4 +1,4 @@
package ingest
package jobs
import (
"encoding/json"
@ -17,17 +17,17 @@ import (
"pool-stats/models"
)
type Ingestor struct {
type IngestSharesJob struct {
db *clover.DB
logPath string
}
func NewIngestor(db *clover.DB, path string) *Ingestor {
return &Ingestor{db: db, logPath: path}
func NewIngestSharesJob(db *clover.DB, path string) *IngestSharesJob {
return &IngestSharesJob{db: db, logPath: path}
}
func (this *Ingestor) WatchAndIngest() {
ticker := time.NewTicker(constants.IngestorWatchInterval)
func (this *IngestSharesJob) WatchAndIngest() {
ticker := time.NewTicker(constants.IngestSharesJobInterval)
defer ticker.Stop()
for {
@ -36,7 +36,7 @@ func (this *Ingestor) WatchAndIngest() {
}
}
func (this *Ingestor) ingestClosedBlocks() {
func (this *IngestSharesJob) ingestClosedBlocks() {
entries, err := os.ReadDir(this.logPath)
if err != nil {
log.Println("Error reading logsDir:", err)
@ -67,7 +67,7 @@ func (this *Ingestor) ingestClosedBlocks() {
}
}
func (this *Ingestor) ingestBlockDir(db *clover.DB, dirPath string) {
func (this *IngestSharesJob) ingestBlockDir(db *clover.DB, dirPath string) {
files, err := os.ReadDir(dirPath)
if err != nil {
log.Printf("Failed to read block dir %s: %v", dirPath, err)

View File

@ -31,19 +31,18 @@ func (job *RecalculateTopSharesJob) Run() error {
}
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)
if currentTopShares == nil || len(currentTopShares) < constants.TopSharesAmount {
newTopShares, _ = database.GetHighestSharesInRange(job.DB, database.CollectionName, time.Unix(0, 0), constants.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, _ = database.GetHighestSharesInRange(job.DB, database.CollectionName, lastTopShareDateTime, constants.TopSharesAmount)
}
newTopShares = append(newTopShares, currentTopShares...)
@ -53,8 +52,8 @@ func (job *RecalculateTopSharesJob) recalculateTopShares() {
newTopShares = notlinq.UniqueBy(newTopShares, func(s models.ShareLog) string {
return s.Hash
})
if len(newTopShares) > topSharesAmount {
newTopShares = newTopShares[:topSharesAmount]
if len(newTopShares) > constants.TopSharesAmount {
newTopShares = newTopShares[:constants.TopSharesAmount]
}
database.ReplaceTopShares(job.DB, newTopShares)

View File

@ -9,7 +9,6 @@ import (
"pool-stats/config"
"pool-stats/database"
"pool-stats/ingest"
"pool-stats/jobs"
"pool-stats/web"
)
@ -23,7 +22,7 @@ func main() {
}
defer db.Close()
ingestor := ingest.NewIngestor(db, config.LogPath)
ingestor := jobs.NewIngestSharesJob(db, config.LogPath)
go ingestor.WatchAndIngest()
topSharesRecalcJob := jobs.NewRecalculateTopSharesJob(db)

View File

@ -5,7 +5,6 @@ Browser{{ end }} {{ define "content" }}
<tr>
<th>Time</th>
<th>Worker</th>
<th>Address</th>
<th>SDiff</th>
<th>Result</th>
<th>Hash</th>
@ -16,14 +15,13 @@ Browser{{ end }} {{ define "content" }}
<tr>
<td>{{ formatCreateDate .CreateDate }}</td>
<td>{{ .WorkerName }}</td>
<td>{{ .Address }}</td>
<td>{{ humanDiff .SDiff }}</td>
<td>{{ if .Result }}✔️{{ else }}❌{{ end }}</td>
<td><code style="font-size: small">{{ .Hash }}</code></td>
</tr>
{{ else }}
<tr>
<td colspan="6">No shares found.</td>
<td colspan="5">No shares found.</td>
</tr>
{{ end }}
</tbody>