diff --git a/constants/constants.go b/constants/constants.go index 1b3e115..a59cce5 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -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 ) diff --git a/database/db.go b/database/db.go index 14ae07c..901ad1c 100644 --- a/database/db.go +++ b/database/db.go @@ -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 { diff --git a/ingest/ingest.go b/jobs/ingestSharesJob.go similarity index 83% rename from ingest/ingest.go rename to jobs/ingestSharesJob.go index a7cb233..dd871e4 100644 --- a/ingest/ingest.go +++ b/jobs/ingestSharesJob.go @@ -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) diff --git a/jobs/recalculateTopShares.go b/jobs/recalculateTopShares.go index 633e474..069c473 100644 --- a/jobs/recalculateTopShares.go +++ b/jobs/recalculateTopShares.go @@ -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) diff --git a/main.go b/main.go index 3c69130..99dcbae 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/models/share_log.go b/models/models.go similarity index 100% rename from models/share_log.go rename to models/models.go diff --git a/templates/shares.html b/templates/shares.html index 8d75160..3418333 100644 --- a/templates/shares.html +++ b/templates/shares.html @@ -5,7 +5,6 @@ Browser{{ end }} {{ define "content" }} Time Worker - Address SDiff Result Hash @@ -16,14 +15,13 @@ Browser{{ end }} {{ define "content" }} {{ formatCreateDate .CreateDate }} {{ .WorkerName }} - {{ .Address }} {{ humanDiff .SDiff }} {{ if .Result }}✔️{{ else }}❌{{ end }} {{ .Hash }} {{ else }} - No shares found. + No shares found. {{ end }}