Restructure
This commit is contained in:
parent
f66fbcc454
commit
4ddd9abd2e
@ -8,6 +8,12 @@ const (
|
|||||||
RecalculateTimeWindowHighSharesJobInterval = 1 * time.Minute
|
RecalculateTimeWindowHighSharesJobInterval = 1 * time.Minute
|
||||||
// RecalculateTopSharesJob interval
|
// RecalculateTopSharesJob interval
|
||||||
RecalculateTopSharesJobInterval = 30 * time.Second
|
RecalculateTopSharesJobInterval = 30 * time.Second
|
||||||
// IngestorWatchInterval interval
|
// IngestSharesJob interval
|
||||||
IngestorWatchInterval = 30 * time.Second
|
IngestSharesJobInterval = 30 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
// counts and stuff
|
||||||
|
const (
|
||||||
|
// TopSharesAmount is the number of top shares to keep
|
||||||
|
TopSharesAmount = 15
|
||||||
)
|
)
|
||||||
|
@ -13,11 +13,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CollectionName = "shares"
|
CollectionName = "shares"
|
||||||
|
TopSharesCollectionName = "TopShares"
|
||||||
TopSharesCollectionName = "TopShares"
|
|
||||||
TopSharesAmount = 20
|
|
||||||
|
|
||||||
TimeWindowHighShareCollectionName = "TimeWindowHighShareStat"
|
TimeWindowHighShareCollectionName = "TimeWindowHighShareStat"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -179,8 +176,6 @@ func ReplaceTopShares(db *clover.DB, shares []models.ShareLog) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Replaced TopShares with %d shares", len(shares))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTimeWindowHighShares(db *clover.DB) []models.TimeWindowHighShare {
|
func GetTimeWindowHighShares(db *clover.DB) []models.TimeWindowHighShare {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package ingest
|
package jobs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -17,17 +17,17 @@ import (
|
|||||||
"pool-stats/models"
|
"pool-stats/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Ingestor struct {
|
type IngestSharesJob struct {
|
||||||
db *clover.DB
|
db *clover.DB
|
||||||
logPath string
|
logPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIngestor(db *clover.DB, path string) *Ingestor {
|
func NewIngestSharesJob(db *clover.DB, path string) *IngestSharesJob {
|
||||||
return &Ingestor{db: db, logPath: path}
|
return &IngestSharesJob{db: db, logPath: path}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Ingestor) WatchAndIngest() {
|
func (this *IngestSharesJob) WatchAndIngest() {
|
||||||
ticker := time.NewTicker(constants.IngestorWatchInterval)
|
ticker := time.NewTicker(constants.IngestSharesJobInterval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -36,7 +36,7 @@ func (this *Ingestor) WatchAndIngest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *Ingestor) ingestClosedBlocks() {
|
func (this *IngestSharesJob) ingestClosedBlocks() {
|
||||||
entries, err := os.ReadDir(this.logPath)
|
entries, err := os.ReadDir(this.logPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error reading logsDir:", err)
|
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)
|
files, err := os.ReadDir(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to read block dir %s: %v", dirPath, err)
|
log.Printf("Failed to read block dir %s: %v", dirPath, err)
|
@ -31,19 +31,18 @@ func (job *RecalculateTopSharesJob) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (job *RecalculateTopSharesJob) recalculateTopShares() {
|
func (job *RecalculateTopSharesJob) recalculateTopShares() {
|
||||||
topSharesAmount := database.TopSharesAmount
|
|
||||||
currentTopShares := database.ListTopShares(job.DB)
|
currentTopShares := database.ListTopShares(job.DB)
|
||||||
|
|
||||||
var newTopShares []models.ShareLog
|
var newTopShares []models.ShareLog
|
||||||
if currentTopShares == nil || len(currentTopShares) < topSharesAmount {
|
if currentTopShares == nil || len(currentTopShares) < constants.TopSharesAmount {
|
||||||
newTopShares, _ = database.GetHighestSharesInRange(job.DB, database.CollectionName, time.Unix(0, 0), topSharesAmount)
|
newTopShares, _ = database.GetHighestSharesInRange(job.DB, database.CollectionName, time.Unix(0, 0), constants.TopSharesAmount)
|
||||||
} else {
|
} else {
|
||||||
sort.Slice(currentTopShares, func(i, j int) bool {
|
sort.Slice(currentTopShares, func(i, j int) bool {
|
||||||
return currentTopShares[i].CreateDate > currentTopShares[j].CreateDate
|
return currentTopShares[i].CreateDate > currentTopShares[j].CreateDate
|
||||||
})
|
})
|
||||||
lastTopShareDate := currentTopShares[0].CreateDate
|
lastTopShareDate := currentTopShares[0].CreateDate
|
||||||
lastTopShareDateTime := helpers.ParseCreateDate(lastTopShareDate)
|
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...)
|
newTopShares = append(newTopShares, currentTopShares...)
|
||||||
@ -53,8 +52,8 @@ func (job *RecalculateTopSharesJob) recalculateTopShares() {
|
|||||||
newTopShares = notlinq.UniqueBy(newTopShares, func(s models.ShareLog) string {
|
newTopShares = notlinq.UniqueBy(newTopShares, func(s models.ShareLog) string {
|
||||||
return s.Hash
|
return s.Hash
|
||||||
})
|
})
|
||||||
if len(newTopShares) > topSharesAmount {
|
if len(newTopShares) > constants.TopSharesAmount {
|
||||||
newTopShares = newTopShares[:topSharesAmount]
|
newTopShares = newTopShares[:constants.TopSharesAmount]
|
||||||
}
|
}
|
||||||
|
|
||||||
database.ReplaceTopShares(job.DB, newTopShares)
|
database.ReplaceTopShares(job.DB, newTopShares)
|
||||||
|
3
main.go
3
main.go
@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"pool-stats/config"
|
"pool-stats/config"
|
||||||
"pool-stats/database"
|
"pool-stats/database"
|
||||||
"pool-stats/ingest"
|
|
||||||
"pool-stats/jobs"
|
"pool-stats/jobs"
|
||||||
"pool-stats/web"
|
"pool-stats/web"
|
||||||
)
|
)
|
||||||
@ -23,7 +22,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
ingestor := ingest.NewIngestor(db, config.LogPath)
|
ingestor := jobs.NewIngestSharesJob(db, config.LogPath)
|
||||||
go ingestor.WatchAndIngest()
|
go ingestor.WatchAndIngest()
|
||||||
|
|
||||||
topSharesRecalcJob := jobs.NewRecalculateTopSharesJob(db)
|
topSharesRecalcJob := jobs.NewRecalculateTopSharesJob(db)
|
||||||
|
@ -5,7 +5,6 @@ Browser{{ end }} {{ define "content" }}
|
|||||||
<tr>
|
<tr>
|
||||||
<th>Time</th>
|
<th>Time</th>
|
||||||
<th>Worker</th>
|
<th>Worker</th>
|
||||||
<th>Address</th>
|
|
||||||
<th>SDiff</th>
|
<th>SDiff</th>
|
||||||
<th>Result</th>
|
<th>Result</th>
|
||||||
<th>Hash</th>
|
<th>Hash</th>
|
||||||
@ -16,14 +15,13 @@ Browser{{ end }} {{ define "content" }}
|
|||||||
<tr>
|
<tr>
|
||||||
<td>{{ formatCreateDate .CreateDate }}</td>
|
<td>{{ formatCreateDate .CreateDate }}</td>
|
||||||
<td>{{ .WorkerName }}</td>
|
<td>{{ .WorkerName }}</td>
|
||||||
<td>{{ .Address }}</td>
|
|
||||||
<td>{{ humanDiff .SDiff }}</td>
|
<td>{{ humanDiff .SDiff }}</td>
|
||||||
<td>{{ if .Result }}✔️{{ else }}❌{{ end }}</td>
|
<td>{{ if .Result }}✔️{{ else }}❌{{ end }}</td>
|
||||||
<td><code style="font-size: small">{{ .Hash }}</code></td>
|
<td><code style="font-size: small">{{ .Hash }}</code></td>
|
||||||
</tr>
|
</tr>
|
||||||
{{ else }}
|
{{ else }}
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="6">No shares found.</td>
|
<td colspan="5">No shares found.</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user