Compare commits

...

13 Commits

Author SHA1 Message Date
Pijus Kamandulis b8511f5ff8 Fix? 2025-07-05 08:49:01 +03:00
Pijus Kamandulis ac0285c2e6 Fix? 2025-07-05 08:44:52 +03:00
Pijus Kamandulis effe887b3b Implement control panel 2025-07-04 21:23:21 +03:00
Pijus Kamandulis ef247fc843 Added auth 2025-07-04 21:05:28 +03:00
Pijus Kamandulis b3c89a01d0 Show stats by worker in daily stats 2025-07-04 19:04:24 +03:00
Pijus Kamandulis 11cc168b3a Cache current day stats for 5m 2025-07-04 18:42:35 +03:00
Pijus Kamandulis edb17e825d Fix time window recalc 2025-07-02 14:24:32 +03:00
Pijus Kamandulis 844f7fa08b Recalculate yesterday's and today's stats 2025-06-24 09:41:24 +03:00
Pijus Kamandulis b89a1a2a7e Implement daily stats 2025-06-23 21:35:16 +03:00
Pijus Kamandulis 4ddd9abd2e Restructure 2025-06-23 19:38:02 +03:00
Pijus Kamandulis f66fbcc454 Extract page layout 2025-06-23 18:42:00 +03:00
Pijus Kamandulis be637f4540 Precalculate index stats 2025-06-23 17:52:20 +03:00
Pijus Kamandulis d801debaf6 Implement top shares page 2025-06-23 14:37:06 +03:00
30 changed files with 1336 additions and 282 deletions
+9 -6
View File
@@ -3,21 +3,24 @@ package config
import "flag" import "flag"
type Config struct { type Config struct {
Port int `json:"port"` Port int `json:"port"`
LogPath string `json:"logPath"` LogPath string `json:"logPath"`
DatabasePath string `json:"databasePath"` DatabasePath string `json:"databasePath"`
AdminPassword string `json:"adminPassword"`
} }
func ParseFlags() Config { func ParseFlags() Config {
port := flag.Int("Port", 8080, "Listen port") port := flag.Int("Port", 8080, "Listen port")
logPath := flag.String("LogPath", "logs", "Path to log files") logPath := flag.String("LogPath", "logs", "Path to log files")
databasePath := flag.String("DatabasePath", "badgerdb", "Path to the database directory") databasePath := flag.String("DatabasePath", "badgerdb", "Path to the database directory")
adminPassword := flag.String("AdminPassword", "", "Admin password for the web interface, disabled if empty")
flag.Parse() flag.Parse()
return Config{ return Config{
Port: *port, Port: *port,
LogPath: *logPath, LogPath: *logPath,
DatabasePath: *databasePath, DatabasePath: *databasePath,
AdminPassword: *adminPassword,
} }
} }
+26
View File
@@ -0,0 +1,26 @@
package constants
import "time"
// time.Duration constants
const (
// RecalculateTimeWindowHighSharesJob interval
RecalculateTimeWindowHighSharesJobInterval = 1 * time.Minute
// RecalculateTopSharesJob interval
RecalculateTopSharesJobInterval = 30 * time.Second
// IngestSharesJob interval
IngestSharesJobInterval = 30 * time.Second
// RecalculateCurrentDayStatsJob interval
RecalculateCurrentDayStatsJobInterval = 30 * time.Minute
)
// counts and stuff
const (
// TopSharesAmount is the number of top shares to keep
TopSharesAmount = 15
// DailyStatsPerPage is the number of daily stats per page
DailyStatsPerPage = 15
)
// EpochTime is the start time for daily stats
var EpochTime = time.Date(2025, 5, 1, 0, 0, 0, 0, time.UTC)
+250 -4
View File
@@ -3,7 +3,9 @@ package database
import ( import (
"fmt" "fmt"
"log" "log"
"pool-stats/helpers"
"pool-stats/models" "pool-stats/models"
"sort"
"time" "time"
"github.com/ostafen/clover/v2" "github.com/ostafen/clover/v2"
@@ -13,7 +15,10 @@ import (
) )
const ( const (
CollectionName = "shares" CollectionName = "shares"
TopSharesCollectionName = "TopShares"
TimeWindowHighShareCollectionName = "TimeWindowHighShareStat"
DailyStatsCollectionName = "DailyStats"
) )
func InitDatabase(path string) (*clover.DB, error) { func InitDatabase(path string) (*clover.DB, error) {
@@ -42,10 +47,59 @@ 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)
}
}
// Init TimeWindowHighShareStat collection
hasTimeWindowCollection, err := db.HasCollection(TimeWindowHighShareCollectionName)
if err != nil {
return nil, fmt.Errorf("failed to check TimeWindowHighShare collection: %v", err)
}
if !hasTimeWindowCollection {
if err := db.CreateCollection(TimeWindowHighShareCollectionName); err != nil {
return nil, fmt.Errorf("failed to create TimeWindowHighShare collection: %v", err)
}
if err := db.CreateIndex(TimeWindowHighShareCollectionName, "TimeWindowID"); err != nil {
return nil, fmt.Errorf("failed to create index for TimeWindowHighShare: %v", err)
}
}
// Init DailyStats collection
hasDailyStatsCollection, err := db.HasCollection(DailyStatsCollectionName)
if err != nil {
return nil, fmt.Errorf("failed to check DailyStats collection: %v", err)
}
if !hasDailyStatsCollection {
if err := db.CreateCollection(DailyStatsCollectionName); err != nil {
return nil, fmt.Errorf("failed to create DailyStats collection: %v", err)
}
if err := db.CreateIndex(DailyStatsCollectionName, "Date"); err != nil {
return nil, fmt.Errorf("failed to create index for DailyStats: %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 +112,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 +163,185 @@ 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
}
}
}
func GetTimeWindowHighShares(db *clover.DB) []models.TimeWindowHighShare {
results, err := db.FindAll(
c.NewQuery(TimeWindowHighShareCollectionName).
Sort(c.SortOption{Field: "TimeWindowID", Direction: 1}),
)
if err != nil {
log.Printf("failed to list time window high shares: %v", err)
return nil
}
timeWindowHighShares := make([]models.TimeWindowHighShare, len(results))
for idx, doc := range results {
var timeWindowHighShare models.TimeWindowHighShare
doc.Unmarshal(&timeWindowHighShare)
timeWindowHighShares[idx] = timeWindowHighShare
}
return timeWindowHighShares
}
func SetTimeWindowHighShare(db *clover.DB, share models.TimeWindowHighShare) error {
db.Delete(
c.NewQuery(TimeWindowHighShareCollectionName).
Where(c.Field("TimeWindowID").
Eq(share.TimeWindowID)))
doc := document.NewDocumentOf(&share)
db.InsertOne(TimeWindowHighShareCollectionName, doc)
return nil
}
func ListSharesInTimeRange(db *clover.DB, since time.Time, till time.Time) []models.ShareLog {
lower := since.Unix()
upper := till.Unix()
results, err := db.FindAll(c.NewQuery(CollectionName).
Where(c.Field("CreateDate").GtEq(fmt.Sprint(lower)).
And(c.Field("CreateDate").LtEq(fmt.Sprint(upper)))).
Sort(c.SortOption{Field: "CreateDate", Direction: -1}))
if err != nil {
log.Printf("failed to list shares in time range: %v", err)
return nil
}
shareLogs := make([]models.ShareLog, len(results))
for idx, doc := range results {
var shareLog models.ShareLog
doc.Unmarshal(&shareLog)
shareLogs[idx] = shareLog
}
return shareLogs
}
// GetStatsForDay retrieves daily statistics for a given date
// Tries to find from DailyStats collection, calculates on the fly if not found and stores
func GetDailyStats(db *clover.DB, date time.Time) (*models.DailyStats, error) {
dateStr := date.Format(time.DateOnly)
// Check if stats already exist
existingDoc, err := db.FindFirst(c.NewQuery(DailyStatsCollectionName).
Where(c.Field("Date").Eq(dateStr)))
if err == nil && existingDoc != nil {
expiresAt := existingDoc.ExpiresAt()
if expiresAt != nil && expiresAt.After(time.Now()) {
DeleteDailyStatsForDay(db, date)
} else {
var stats models.DailyStats
if err := existingDoc.Unmarshal(&stats); err != nil {
return nil, fmt.Errorf("failed to unmarshal daily stats: %v", err)
}
return &stats, nil
}
}
// Get shares in range
since := date.Truncate(24 * time.Hour)
till := since.Add(24 * time.Hour)
shares := ListSharesInTimeRange(db, since, till)
sort.Slice(shares, func(i, j int) bool {
return shares[i].SDiff > shares[j].SDiff
})
// Calculate daily stats
stats := &models.DailyStats{
Date: dateStr,
ShareCount: len(shares),
Workers: make(map[string]models.WorkerDailyStats),
}
if len(shares) > 0 {
stats.TopShare = shares[0]
stats.PoolHashrate = helpers.CalculateAverageHashrate(shares)
}
// Calculate worker stats
sharesByWorker := make(map[string][]models.ShareLog)
for _, share := range shares {
sharesByWorker[share.WorkerName] = append(sharesByWorker[share.WorkerName], share)
}
for workerName, workerShares := range sharesByWorker {
workerHashrate := helpers.CalculateAverageHashrate(workerShares)
sort.Slice(workerShares, func(i, j int) bool {
return workerShares[i].SDiff > workerShares[j].SDiff
})
workerTopShare := workerShares[0] // Already sorted by SDiff
stats.Workers[workerName] = models.WorkerDailyStats{
TopShare: workerTopShare,
Hashrate: workerHashrate,
Shares: len(workerShares),
}
}
// Insert or update the daily stats in the collection
doc := document.NewDocumentOf(stats)
isToday := dateStr == time.Now().UTC().Format(time.DateOnly)
if isToday {
doc.SetExpiresAt(time.Now().Add(5 * time.Minute))
}
if _, err := db.InsertOne(DailyStatsCollectionName, doc); err != nil {
return nil, fmt.Errorf("failed to insert daily stats: %v", err)
}
return stats, nil
}
func ClearDailyStats(db *clover.DB) error {
// Delete all documents in DailyStats collection
if err := db.Delete(c.NewQuery(DailyStatsCollectionName)); err != nil {
return fmt.Errorf("failed to clear DailyStats collection: %v", err)
}
return nil
}
func DeleteDailyStatsForDay(db *clover.DB, date time.Time) error {
dateStr := date.Format(time.DateOnly)
// Delete the document for the specific date
if err := db.Delete(c.NewQuery(DailyStatsCollectionName).
Where(c.Field("Date").Eq(dateStr))); err != nil {
return fmt.Errorf("failed to delete daily stats for %s: %v", dateStr, err)
}
return nil
}
+4 -2
View File
@@ -2,14 +2,16 @@ module pool-stats
go 1.24.3 go 1.24.3
require github.com/ostafen/clover/v2 v2.0.0-alpha.3.0.20250212110647-35f6fd38bde2 require (
github.com/gofrs/uuid/v5 v5.3.1
github.com/ostafen/clover/v2 v2.0.0-alpha.3.0.20250212110647-35f6fd38bde2
)
require ( require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgraph-io/badger/v4 v4.5.1 // indirect github.com/dgraph-io/badger/v4 v4.5.1 // indirect
github.com/dgraph-io/ristretto/v2 v2.1.0 // indirect github.com/dgraph-io/ristretto/v2 v2.1.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gofrs/uuid/v5 v5.3.1 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/flatbuffers v25.2.10+incompatible // indirect github.com/google/flatbuffers v25.2.10+incompatible // indirect
github.com/google/orderedcode v0.0.1 // indirect github.com/google/orderedcode v0.0.1 // indirect
+42 -1
View File
@@ -2,6 +2,9 @@ package helpers
import ( import (
"fmt" "fmt"
"math"
"pool-stats/models"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -35,5 +38,43 @@ func FormatCreateDate(createdate string) string {
t := time.Unix(sec, nsec) t := time.Unix(sec, nsec)
return t.Format(time.DateTime) return t.Format(time.DateTime)
} }
return "" return "-"
}
func CalculateAverageHashrate(shares []models.ShareLog) float64 {
if len(shares) == 0 {
return 0.0
}
sort.Slice(shares, func(i, j int) bool {
return shares[i].CreateDate < shares[j].CreateDate
})
first := ParseCreateDate(shares[0].CreateDate)
last := ParseCreateDate(shares[len(shares)-1].CreateDate)
timeSpan := last.Sub(first).Seconds()
if timeSpan <= 0 {
return 0.0
}
var totalAssignedDiff float64
for _, s := range shares {
totalAssignedDiff += s.Diff
}
avgAssignedDiff := totalAssignedDiff / float64(len(shares))
// Hashrate = avg diff * 2^32 / avg time per share
hashrate := (avgAssignedDiff * math.Pow(2, 32)) / (timeSpan / float64(len(shares)))
return hashrate
}
func FormatHashrate(hps float64) string {
units := []string{"H/s", "kH/s", "MH/s", "GH/s", "TH/s", "PH/s", "EH/s"}
i := 0
for hps >= 1000 && i < len(units)-1 {
hps /= 1000
i++
}
return fmt.Sprintf("%.2f %s", hps, units[i])
} }
+9 -8
View File
@@ -1,4 +1,4 @@
package ingest package jobs
import ( import (
"encoding/json" "encoding/json"
@@ -12,21 +12,22 @@ import (
"github.com/ostafen/clover/v2" "github.com/ostafen/clover/v2"
"github.com/ostafen/clover/v2/document" "github.com/ostafen/clover/v2/document"
"pool-stats/constants"
"pool-stats/database" "pool-stats/database"
"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(30 * time.Second) ticker := time.NewTicker(constants.IngestSharesJobInterval)
defer ticker.Stop() defer ticker.Stop()
for { for {
@@ -35,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)
@@ -66,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)
+39
View File
@@ -0,0 +1,39 @@
package jobs
import (
"pool-stats/constants"
"pool-stats/database"
"time"
"github.com/ostafen/clover/v2"
)
type RecalculateCurrentDayStatsJob struct {
DB *clover.DB
}
func NewRecalculateCurrentDayStatsJob(db *clover.DB) *RecalculateCurrentDayStatsJob {
return &RecalculateCurrentDayStatsJob{DB: db}
}
func (job *RecalculateCurrentDayStatsJob) Run() error {
ticker := time.NewTicker(constants.RecalculateCurrentDayStatsJobInterval)
defer ticker.Stop()
for {
<-ticker.C
job.recalculateCurrentDayStats()
}
}
func (job *RecalculateCurrentDayStatsJob) recalculateCurrentDayStats() {
today := time.Now()
yesterday := today.Add(-24 * time.Hour)
database.DeleteDailyStatsForDay(job.DB, today)
database.GetDailyStats(job.DB, today)
// Need to keep yesterday's stats cache updated
database.DeleteDailyStatsForDay(job.DB, yesterday)
database.GetDailyStats(job.DB, yesterday)
}
+104
View File
@@ -0,0 +1,104 @@
package jobs
import (
"pool-stats/constants"
"pool-stats/database"
"pool-stats/models"
"pool-stats/notlinq"
"sort"
"time"
"github.com/ostafen/clover/v2"
)
type RecalculateTimeWindowHighSharesJob struct {
DB *clover.DB
}
func NewRecalculateTimeWindowHighSharesJob(db *clover.DB) *RecalculateTimeWindowHighSharesJob {
return &RecalculateTimeWindowHighSharesJob{DB: db}
}
func (job *RecalculateTimeWindowHighSharesJob) Run() error {
ticker := time.NewTicker(constants.RecalculateTimeWindowHighSharesJobInterval)
defer ticker.Stop()
for {
<-ticker.C
job.recalculateTimeWindowHighShares()
}
}
func (job *RecalculateTimeWindowHighSharesJob) recalculateTimeWindowHighShares() {
topShares := database.ListTopShares(job.DB)
sort.Slice(topShares, func(i, j int) bool {
return topShares[i].SDiff > topShares[j].SDiff
})
// All time high share
if len(topShares) > 0 {
allTimeHighShare := topShares[0]
allTimeHighShareStat := &models.TimeWindowHighShare{
TimeWindowID: "0-all-time",
TimeWindowName: "All Time",
SDiff: allTimeHighShare.SDiff,
Time: allTimeHighShare.CreateDate,
}
database.SetTimeWindowHighShare(job.DB, *allTimeHighShareStat)
}
// Other ranges
timeWindows := []struct {
ID string
Name string
Since time.Time
}{
{"1-hour", "Past Hour", time.Now().Add(-1 * time.Hour)},
{"2-day", "Past 24h", time.Now().Add(-24 * time.Hour)},
{"3-week", "Past 7d", time.Now().Add(-7 * 24 * time.Hour)},
}
for _, tw := range timeWindows {
// Can use one of top shares if in range,
// otherwise get highest share in range
var highestShare models.ShareLog
topSharesInRange := notlinq.
Where(topShares, func(s models.ShareLog) bool {
shareTime, err := s.ParseCreateDate()
if err != nil {
return false
}
return shareTime.After(tw.Since)
})
sort.Slice(topSharesInRange, func(i, j int) bool {
return topSharesInRange[i].SDiff > topSharesInRange[j].SDiff
})
if len(topSharesInRange) > 0 {
highestShare = topSharesInRange[0]
} else {
highestShareDocs, _ := database.GetHighestSharesInRange(
job.DB, database.CollectionName, tw.Since, 1)
if len(highestShareDocs) > 0 {
highestShare = highestShareDocs[0]
}
}
var timeWindowStat models.TimeWindowHighShare
if highestShare.SDiff > 0 {
timeWindowStat = models.TimeWindowHighShare{
TimeWindowID: tw.ID,
TimeWindowName: tw.Name,
SDiff: highestShare.SDiff,
Time: highestShare.CreateDate,
}
} else {
timeWindowStat = models.TimeWindowHighShare{
TimeWindowID: tw.ID,
TimeWindowName: tw.Name,
SDiff: 0,
Time: "-",
}
}
database.SetTimeWindowHighShare(job.DB, timeWindowStat)
}
}
+60
View File
@@ -0,0 +1,60 @@
package jobs
import (
"pool-stats/constants"
"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(constants.RecalculateTopSharesJobInterval)
defer ticker.Stop()
for {
<-ticker.C
job.recalculateTopShares()
}
}
func (job *RecalculateTopSharesJob) recalculateTopShares() {
currentTopShares := database.ListTopShares(job.DB)
var newTopShares []models.ShareLog
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, constants.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) > constants.TopSharesAmount {
newTopShares = newTopShares[:constants.TopSharesAmount]
}
database.ReplaceTopShares(job.DB, newTopShares)
}
+12 -3
View File
@@ -9,7 +9,7 @@ import (
"pool-stats/config" "pool-stats/config"
"pool-stats/database" "pool-stats/database"
"pool-stats/ingest" "pool-stats/jobs"
"pool-stats/web" "pool-stats/web"
) )
@@ -22,10 +22,19 @@ 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()
webServer := web.NewWebServer(db, config.Port) topSharesRecalcJob := jobs.NewRecalculateTopSharesJob(db)
go topSharesRecalcJob.Run()
timeWindowHighSharesRecalcJob := jobs.NewRecalculateTimeWindowHighSharesJob(db)
go timeWindowHighSharesRecalcJob.Run()
currentDayStatsRecalcJob := jobs.NewRecalculateCurrentDayStatsJob(db)
go currentDayStatsRecalcJob.Run()
webServer := web.NewWebServer(db, config.Port, config.AdminPassword)
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)
} }
+19 -4
View File
@@ -29,10 +29,25 @@ type ShareLog struct {
Agent string `json:"agent"` // Miner agent string (e.g., bitaxe/BM1370) Agent string `json:"agent"` // Miner agent string (e.g., bitaxe/BM1370)
} }
type ShareStat struct { type TimeWindowHighShare struct {
Label string TimeWindowID string `json:"time_window_id"` // Unique ID for the time window
Diff string TimeWindowName string `json:"time_window_name"` // Name of the time window (e.g., "Past Hour")
Time string SDiff float64 `json:"share_diff"` // Difficulty of the highest share
Time string `json:"share_time"` // Time of the highest share
}
type DailyStats struct {
Date string `json:"date"` // Format: "2006-01-02" in UTC
ShareCount int `json:"sharecount"` // Total shares submitted that day
TopShare ShareLog `json:"topshare"` // Highest share (by SDiff)
PoolHashrate float64 `json:"poolhashrate"` // In H/s (averaged)
Workers map[string]WorkerDailyStats `json:"workers"` // key = workername
}
type WorkerDailyStats struct {
TopShare ShareLog `json:"topshare"` // Highest share by this worker
Hashrate float64 `json:"hashrate"` // avg hashrate in H/s
Shares int `json:"shares"` // shares submitted
} }
// ParseCreateDate can be used to convert ShareLog.CreateDate to time.Time // ParseCreateDate can be used to convert ShareLog.CreateDate to time.Time
+24
View File
@@ -0,0 +1,24 @@
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
}
func Where[T any](items []T, predicate func(T) bool) []T {
result := make([]T, 0)
for _, item := range items {
if predicate(item) {
result = append(result, item)
}
}
return result
}
-50
View File
@@ -1,50 +0,0 @@
package stats
import (
"time"
"github.com/ostafen/clover/v2"
"pool-stats/database"
"pool-stats/helpers"
"pool-stats/models"
)
func GetStats(db *clover.DB) ([]models.ShareStat, error) {
now := time.Now()
ranges := []struct {
Label string
Since time.Time
}{
{"Past Hour", now.Add(-1 * time.Hour)},
{"Past 24h", now.Add(-24 * time.Hour)},
{"Past 7d", now.Add(-7 * 24 * time.Hour)},
}
stats := []models.ShareStat{}
// All-time highest
doc, _ := database.GetHighestShareInRange(db, database.CollectionName, time.Unix(0, 0))
if doc != nil {
stats = append(stats, models.ShareStat{
Label: "All Time",
Diff: helpers.HumanDiff(doc.Get("SDiff").(float64)),
Time: helpers.ParseCreateDate(doc.Get("CreateDate").(string)).Format(time.RFC822),
})
}
for _, r := range ranges {
doc, _ := database.GetHighestShareInRange(db, database.CollectionName, r.Since)
if doc != nil {
stats = append(stats, models.ShareStat{
Label: r.Label,
Diff: helpers.HumanDiff(doc.Get("SDiff").(float64)),
Time: helpers.ParseCreateDate(doc.Get("CreateDate").(string)).Format(time.RFC822),
})
} else {
stats = append(stats, models.ShareStat{Label: r.Label, Diff: "-", Time: "-"})
}
}
return stats, nil
}
+24
View File
@@ -0,0 +1,24 @@
{{ define "title" }}Control Panel{{ end }} {{ define "header" }}⚙️ Control
Panel{{ end }} {{ define "content" }}
<div>
{{ if .Message }}
<strong>Info:</strong> {{ .Message }} {{ end }}
<table>
<thead>
<tr>
<th>Action</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/cp?action=ClearDailyStats">Clear Daily Stats</a></td>
<td>Clear Daily Stats cache</td>
</tr>
</tbody>
</table>
</div>
{{ end }} {{ template "layout" . }}
+98
View File
@@ -0,0 +1,98 @@
{{ define "title" }}Daily Stats{{ end }} {{ define "header" }}📊 Pool Daily
Stats{{ end }} {{ define "content" }}
<style>
.worker-rows {
display: none;
}
.expand-btn {
cursor: pointer;
}
.worker-table {
width: 100%;
}
.worker-table th,
.worker-table td {
font-size: 0.9em;
}
</style>
<table>
<thead>
<tr>
<th></th>
<th>Date (UTC)</th>
<th>Share Count</th>
<th>Top Share Diff</th>
<th>Pool Hashrate</th>
</tr>
</thead>
<tbody>
{{ range $i, $ds := .DailyStats }}
<tr>
<td>
<span class="expand-btn" onclick="toggleWorkers({{ $i }})"></span>
</td>
<td>{{ $ds.Date }}</td>
<td>{{ $ds.ShareCount }}</td>
<td>{{ humanDiff $ds.TopShare.SDiff }}</td>
<td>{{ formatHashrate $ds.PoolHashrate }}</td>
</tr>
<tr class="worker-rows" id="workers-{{ $i }}">
<td colspan="5">
<table class="worker-table">
<thead>
<tr>
<th>Shares</th>
<th>Top Share Diff</th>
<th>Hashrate</th>
</tr>
</thead>
<tbody>
{{ range $name, $w := $ds.Workers }}
<tr>
<td>{{ $w.Shares }}</td>
<td>{{ humanDiff $w.TopShare.SDiff }}</td>
<td>{{ formatHashrate $w.Hashrate }}</td>
</tr>
{{ end }}
</tbody>
</table>
</td>
</tr>
{{ else }}
<tr>
<td colspan="5">No stats found for this date range.</td>
</tr>
{{ end }}
</tbody>
</table>
<script>
function toggleWorkers(index) {
const row = document.getElementById(`workers-${index}`);
if (row.style.display === "none" || row.style.display === "") {
row.style.display = "table-row";
} else {
row.style.display = "none";
}
}
</script>
<div>
{{ if .PrevPageAvailable }}
<a class="page-link" href="?start={{ .PrevPageStart }}&end={{ .PrevPageEnd }}"
>&laquo; Prev</a
>
{{ end }}
<a class="page-link current" href="?start={{ .Start }}&end={{ .End }}"
>{{ .Start }} - {{ .End }}</a
>
{{ if .NextPageAvailable }}
<a class="page-link" href="?start={{ .NextPageStart }}&end={{ .NextPageEnd }}"
>Next &raquo;</a
>
{{ end }}
</div>
{{ end }} {{ template "layout" . }}
+19 -62
View File
@@ -1,62 +1,19 @@
<!DOCTYPE html> {{ define "title" }}Share Stats{{ end }} {{ define "header" }}🌟 Pool Share
<html> Stats{{ end }} {{ define "content" }}
<head> <table>
<meta charset="UTF-8" /> <tr>
<title>Share Stats</title> <th>Range</th>
<style> <th>Highest Share Diff</th>
body { <th>Time</th>
font-family: sans-serif; </tr>
background: #111; {{ range .Stats }}
color: #eee; <tr>
text-align: center; <td>{{ .TimeWindowName }}</td>
padding: 2em; <td>
} {{ if ne .SDiff 0.0 }} {{ humanDiff .SDiff }} {{ else }} - {{ end }}
table { </td>
margin: auto; <td>{{ formatCreateDate .Time }}</td>
border-collapse: collapse; </tr>
} {{ end }}
th, </table>
td { {{ end }} {{ template "layout" . }}
padding: 0.5em 1em;
border: 1px solid #444;
}
th {
background-color: #222;
}
tr:nth-child(even) {
background-color: #1a1a1a;
}
a {
color: #0af;
text-decoration: none;
}
li {
display: inline;
margin: 0 10px;
}
</style>
</head>
<body>
<h1>🌟 Pool Share Stats</h1>
<table>
<tr>
<th>Range</th>
<th>Highest Share Diff</th>
<th>Time</th>
</tr>
{{range .}}
<tr>
<td>{{.Label}}</td>
<td>{{.Diff}}</td>
<td>{{.Time}}</td>
</tr>
{{end}}
</table>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/shares">View Shares</a></li>
</ul>
</body>
</html>
+115
View File
@@ -0,0 +1,115 @@
{{ define "layout" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>{{ template "title" . }}</title>
<style>
body {
font-family: "Courier New", Courier, monospace;
background: #111;
color: #eee;
padding: 0;
margin: 0;
text-align: center;
}
nav {
background-color: #222;
border-bottom: 1px solid #444;
padding: 5px 0;
margin-bottom: 20px;
display: flex;
justify-content: space-between;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
display: inline-flex;
gap: 15px;
}
nav li {
display: inline;
}
nav a {
color: #0af;
text-decoration: none;
padding: 4px 8px;
border: 1px solid transparent;
font-weight: bold;
font-size: 14px;
}
nav a:hover {
background-color: #0af;
color: #111;
border-color: #0af;
border-radius: 3px;
}
h1 {
margin-top: 0;
}
table {
border-collapse: collapse;
margin: auto;
margin-bottom: 20px;
}
table.fw {
width: 100%;
}
th,
td {
padding: 8px 12px;
border: 1px solid #444;
text-align: left;
white-space: nowrap;
}
th {
background-color: #222;
}
tr:nth-child(even) {
background-color: #1a1a1a;
}
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;
}
</style>
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/shares">View Shares</a></li>
<li><a href="/top-shares">Top Shares</a></li>
<li><a href="/daily-stats">Daily Stats</a></li>
</ul>
{{ if .UserName }}
<ul>
<li>{{ .UserName }}</li>
<li><a href="/logout">Logout</a></li>
</ul>
{{ else }}
<ul>
<li><a href="/login">Login</a></li>
</ul>
{{ end }}
</nav>
<h1>{{ template "header" . }}</h1>
{{ template "content" . }}
</body>
</html>
{{ end }}
+61
View File
@@ -0,0 +1,61 @@
{{ define "title" }}Login{{ end }} {{ define "header" }}🔒 Login{{ end }} {{
define "content" }}
<style>
.login-form {
display: inline-block;
text-align: left;
padding: 20px 25px;
background-color: #1a1a1a;
border: 1px solid #444;
color: #eee;
margin: 0 auto;
}
.login-form label {
display: block;
margin-bottom: 8px;
color: #ccc;
font-weight: bold;
}
.login-form input[type="password"],
.login-form input[type="submit"] {
width: 100%;
padding: 10px;
margin-bottom: 12px;
border: 1px solid #444;
background-color: #222;
color: #eee;
border-radius: 4px;
box-sizing: border-box;
}
.login-form input[type="submit"] {
background-color: #0af;
color: #111;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s ease;
}
.login-form input[type="submit"]:hover {
background-color: #08c;
}
.error {
color: #ff6b6b;
font-size: 0.9em;
margin-top: -8px;
}
</style>
<form method="POST" action="/login" class="login-form">
<label for="password">Password</label><br />
<input type="password" id="password" name="password" required /><br /><br />
<input type="submit" value="Login" />
{{ if .ErrorMessage }}
<p class="error">{{ .ErrorMessage }}</p>
{{ end }}
</form>
{{ end }} {{ template "layout" . }}
+43 -100
View File
@@ -1,103 +1,46 @@
{{ define "share_list" }} {{ define "title" }}Share Browser{{ end }} {{ define "header" }}☀️ Pool Share
<!DOCTYPE html> Browser{{ end }} {{ define "content" }}
<html lang="en"> <table>
<head> <thead>
<meta charset="UTF-8" /> <tr>
<title>ckpool Share Browser</title> <th>Time</th>
<style> <th>Worker</th>
body { <th>SDiff</th>
font-family: sans-serif; <th>Result</th>
background: #111; <th>Hash</th>
color: #eee; </tr>
padding: 20px; </thead>
} <tbody>
table { {{ range .Shares }}
width: 100%; <tr>
border-collapse: collapse; <td>{{ formatCreateDate .CreateDate }}</td>
margin-bottom: 20px; <td>{{ .WorkerName }}</td>
} <td>{{ humanDiff .SDiff }}</td>
th, <td>{{ if .Result }}✔️{{ else }}❌{{ end }}</td>
td { <td><code style="font-size: small">{{ .Hash }}</code></td>
padding: 8px 12px; </tr>
border: 1px solid #444; {{ else }}
text-align: left; <tr>
white-space: nowrap; <td colspan="5">No shares found.</td>
} </tr>
th { {{ end }}
background-color: #222; </tbody>
} </table>
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 Share Browser</h1>
<table> <div>
<thead> {{ if gt .Page 1 }}
<tr> <a class="page-link" href="?page={{ sub .Page 1 }}">&laquo; Prev</a>
<th>Time</th> {{ end }} {{ if gt .Page 2 }}
<th>Worker</th> <a class="page-link" href="?page=1">1</a>
<th>Address</th> {{ if gt .Page 3 }}
<th>SDiff</th> <span class="page-link">...</span>
<th>Result</th> {{ end }} {{ end }}
<th>Hash</th>
</tr>
</thead>
<tbody>
{{ range .Shares }}
<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>
</tr>
{{ end }}
</tbody>
</table>
<div> <a class="page-link current" href="?page={{ .Page }}">{{ .Page }}</a>
{{ if gt .Page 1 }}
<a class="page-link" href="?page={{ sub .Page 1 }}">&laquo; Prev</a>
{{ end }} {{ if gt .Page 2 }}
<a class="page-link" href="?page=1">1</a>
{{ if gt .Page 3 }}
<span class="page-link">...</span>
{{ end }} {{ end }}
<a class="page-link current" href="?page={{ .Page }}">{{ .Page }}</a> {{ if .HasMore }}
<span class="page-link">...</span>
{{ if .HasMore }} <a class="page-link" href="?page={{ add .Page 1 }}">Next &raquo;</a>
<span class="page-link">...</span> {{ end }}
<a class="page-link" href="?page={{ add .Page 1 }}">Next &raquo;</a> </div>
{{ end }} {{ end }} {{ template "layout" . }}
</div>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/shares">View Shares</a></li>
</ul>
</body>
</html>
{{ end }}
+27
View File
@@ -0,0 +1,27 @@
{{ define "title" }}Top Shares{{ end }} {{ define "header" }}☀️ Pool Top
Shares{{ end }} {{ define "content" }}
<table>
<thead>
<tr>
<th>Time</th>
<th>Worker</th>
<th>SDiff</th>
<th>Hash</th>
</tr>
</thead>
<tbody>
{{ range .Shares }}
<tr>
<td>{{ formatCreateDate .CreateDate }}</td>
<td>{{ .WorkerName }}</td>
<td>{{ humanDiff .SDiff }}</td>
<td><code style="font-size: small">{{ .Hash }}</code></td>
</tr>
{{ else }}
<tr>
<td colspan="4">No shares found.</td>
</tr>
{{ end }}
</tbody>
</table>
{{ end }} {{ template "layout" . }}
+39
View File
@@ -0,0 +1,39 @@
package web
import (
"net/http"
"pool-stats/database"
)
type ControlPanelPageData struct {
PageDataBase
Message string
}
func (ws *WebServer) ControlPanelHandler(w http.ResponseWriter, r *http.Request) {
username := ws.getUser(r)
if username == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
data := ControlPanelPageData{}
action := r.URL.Query().Get("action")
if action != "" {
data.Message = ws.executeAction(action)
}
ws.renderTemplate(w, r, "templates/cp.html", &data)
}
func (ws *WebServer) executeAction(action string) string {
switch action {
case "ClearDailyStats":
database.ClearDailyStats(ws.db)
return "Daily stats cleared successfully"
default:
return "Unknown action"
}
}
+91
View File
@@ -0,0 +1,91 @@
package web
import (
"net/http"
"pool-stats/constants"
"pool-stats/database"
"pool-stats/models"
"time"
)
type DailyStatsPageData struct {
PageDataBase
DailyStats []models.DailyStats
Start string
End string
NextPageAvailable bool
NextPageStart string
NextPageEnd string
PrevPageAvailable bool
PrevPageStart string
PrevPageEnd string
}
func (ws *WebServer) DailyStatsHandler(w http.ResponseWriter, r *http.Request) {
startParam := r.URL.Query().Get("start")
endParam := r.URL.Query().Get("end")
var startTime, endTime time.Time
var err error
if startParam == "" || endParam == "" {
endTime = time.Now().Truncate(24 * time.Hour)
startTime = endTime.AddDate(0, 0, -constants.DailyStatsPerPage+1)
} else {
startTime, err = time.Parse(time.DateOnly, startParam)
if err != nil {
http.Error(w, "Invalid start time format", http.StatusBadRequest)
return
}
endTime, err = time.Parse(time.DateOnly, endParam)
if err != nil {
http.Error(w, "Invalid end time format", http.StatusBadRequest)
return
}
}
daysCount := int(endTime.Sub(startTime).Hours() / 24)
if daysCount < 0 {
http.Error(w, "End time must be after start time", http.StatusBadRequest)
return
}
if daysCount > constants.DailyStatsPerPage {
http.Error(w, "Too many days requested", http.StatusBadRequest)
return
}
dailyStats := make([]models.DailyStats, 0)
for t := endTime; !t.Before(startTime); t = t.AddDate(0, 0, -1) {
stats, err := database.GetDailyStats(ws.db, t)
if err != nil {
http.Error(w, "Failed to fetch daily stats", http.StatusInternalServerError)
return
}
dailyStats = append(dailyStats, *stats)
}
nextPageStart := endTime.AddDate(0, 0, 1)
nextPageEnd := endTime.AddDate(0, 0, constants.DailyStatsPerPage)
prevPageEnd := startTime.AddDate(0, 0, -1)
prevPageStart := startTime.AddDate(0, 0, -constants.DailyStatsPerPage)
data := DailyStatsPageData{
DailyStats: dailyStats,
Start: startTime.Format(time.DateOnly),
End: endTime.Format(time.DateOnly),
NextPageAvailable: nextPageStart.Before(time.Now()),
NextPageStart: nextPageStart.Format(time.DateOnly),
NextPageEnd: nextPageEnd.Format(time.DateOnly),
PrevPageAvailable: prevPageStart.After(constants.EpochTime),
PrevPageStart: prevPageStart.Format(time.DateOnly),
PrevPageEnd: prevPageEnd.Format(time.DateOnly),
}
ws.renderTemplate(w, r, "templates/daily_stats.html", &data)
}
-19
View File
@@ -1,19 +0,0 @@
package web
import (
"html/template"
"net/http"
"pool-stats/stats"
)
func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
shareStats, err := stats.GetStats(ws.db)
if err != nil {
http.Error(w, "Failed to load stats", 500)
return
}
tmpl := template.Must(template.ParseFiles("templates/index.html"))
tmpl.Execute(w, shareStats)
}
+28
View File
@@ -0,0 +1,28 @@
package web
import (
"net/http"
"pool-stats/database"
"pool-stats/models"
)
type IndexPageData struct {
PageDataBase
Stats []models.TimeWindowHighShare
}
func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
tws := database.GetTimeWindowHighShares(ws.db)
if tws == nil {
http.Error(w, "Failed to load time window high shares", 500)
return
}
indexData := IndexPageData{
Stats: tws,
}
ws.renderTemplate(w, r, "templates/index.html", &indexData)
}
+42
View File
@@ -0,0 +1,42 @@
package web
import (
"net/http"
)
type LoginPageData struct {
PageDataBase
ErrorMessage string
}
func (ws *WebServer) LoginHandler(w http.ResponseWriter, r *http.Request) {
loginData := LoginPageData{}
if r.Method == http.MethodPost {
if err := r.ParseForm(); err != nil {
loginData.ErrorMessage = "Failed to parse form data"
} else {
password := r.FormValue("password")
if password != ws.adminPassword || ws.adminPassword == "" {
loginData.ErrorMessage = "Invalid password"
} else {
sessionID := generateSessionID()
if sessionID == "" {
loginData.ErrorMessage = "Failed to generate session ID"
}
ws.sessions[sessionID] = "admin"
http.SetCookie(w, &http.Cookie{
Name: "session_id",
Value: sessionID,
Path: "/",
HttpOnly: true,
})
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}
}
ws.renderTemplate(w, r, "templates/login.html", &loginData)
}
+25
View File
@@ -0,0 +1,25 @@
package web
import (
"net/http"
"time"
)
func (ws *WebServer) LogoutHandler(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("session_id")
if err != nil {
http.Error(w, "No session found", http.StatusUnauthorized)
return
}
delete(ws.sessions, cookie.Value)
http.SetCookie(w, &http.Cookie{
Name: "session_id",
Value: "",
Path: "/",
HttpOnly: true,
Expires: <-time.After(time.Second * 1),
})
http.Redirect(w, r, "/", http.StatusSeeOther)
}
+18
View File
@@ -0,0 +1,18 @@
package web
type PageDataBase struct {
UserName string
}
func (pageData PageDataBase) GetUserName() string {
return pageData.UserName
}
func (pageData *PageDataBase) SetUserName(userName string) {
pageData.UserName = userName
}
type IPageDataBase interface {
GetUserName() string
SetUserName(userName string)
}
+77 -5
View File
@@ -1,30 +1,102 @@
package web package web
import ( import (
"html/template"
"net/http" "net/http"
"path/filepath"
"pool-stats/helpers"
"fmt" "fmt"
"github.com/gofrs/uuid/v5"
"github.com/ostafen/clover/v2" "github.com/ostafen/clover/v2"
) )
type WebServer struct { type WebServer struct {
db *clover.DB db *clover.DB
port int port int
templates *template.Template
sessions map[string]string
adminPassword string
} }
func NewWebServer(db *clover.DB, port int) *WebServer { func NewWebServer(db *clover.DB, port int, adminPassword string) *WebServer {
templates := template.New("base").Funcs(template.FuncMap{
"add": func(a, b int) int { return a + b },
"sub": func(a, b int) int { return a - b },
"humanDiff": helpers.HumanDiff,
"formatHashrate": helpers.FormatHashrate,
"formatCreateDate": helpers.FormatCreateDate,
})
templates = template.Must(templates.ParseFiles(
"templates/layout.html",
))
return &WebServer{ return &WebServer{
db: db, db: db,
port: port, port: port,
templates: templates,
sessions: make(map[string]string),
adminPassword: adminPassword,
} }
} }
func (ws *WebServer) Start() error { func (ws *WebServer) Start() error {
http.HandleFunc("/", ws.IndexHandler) http.HandleFunc("/", ws.IndexHandler)
http.HandleFunc("/cp", ws.ControlPanelHandler)
http.HandleFunc("/login", ws.LoginHandler)
http.HandleFunc("/logout", ws.LogoutHandler)
http.HandleFunc("/shares", ws.SharesHandler) http.HandleFunc("/shares", ws.SharesHandler)
http.HandleFunc("/top-shares", ws.TopSharesHandler)
http.HandleFunc("/daily-stats", ws.DailyStatsHandler)
address := ":" + fmt.Sprint(ws.port) address := ":" + fmt.Sprint(ws.port)
println("Listening on", address) println("Listening on", address)
return http.ListenAndServe(address, nil) return http.ListenAndServe(address, nil)
} }
func generateSessionID() string {
uuid, err := uuid.NewV4()
if err != nil {
fmt.Println("Error generating session ID:", err)
return ""
}
return uuid.String()
}
func (ws *WebServer) getUser(r *http.Request) string {
cookie, err := r.Cookie("session_id")
if err != nil {
return ""
}
if user, ok := ws.sessions[cookie.Value]; ok {
return user
}
return ""
}
func (ws *WebServer) renderTemplate(
w http.ResponseWriter,
r *http.Request,
templateFile string,
data IPageDataBase) {
data.SetUserName(ws.getUser(r))
tmpl, err := template.Must(ws.templates.Clone()).ParseFiles(templateFile)
if err != nil {
http.Error(w, "Failed to parse template", 500)
println("Error parsing template:", err.Error())
return
}
templateName := filepath.Base(templateFile)
if err := tmpl.ExecuteTemplate(w, templateName, data); err != nil {
http.Error(w, "Failed to render template", 500)
println("Error rendering template:", err.Error())
return
}
}
+4 -18
View File
@@ -1,33 +1,21 @@
package web package web
import ( import (
"html/template"
"net/http" "net/http"
"pool-stats/database" "pool-stats/database"
"pool-stats/helpers"
"pool-stats/models" "pool-stats/models"
"strconv" "strconv"
) )
type SharePageData struct { type SharePageData struct {
PageDataBase
Shares []models.ShareLog Shares []models.ShareLog
Page int Page int
HasMore bool HasMore bool
} }
func (ws *WebServer) SharesHandler(w http.ResponseWriter, r *http.Request) { func (ws *WebServer) SharesHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.New("share_list").Funcs(template.FuncMap{
"add": func(a, b int) int { return a + b },
"sub": func(a, b int) int { return a - b },
"humanDiff": helpers.HumanDiff,
"formatCreateDate": helpers.FormatCreateDate,
})
tmpl, err := tmpl.ParseFiles("templates/shares.html")
if err != nil {
http.Error(w, "Failed to load template", 500)
return
}
entriesPerPage := 10 entriesPerPage := 10
page := r.URL.Query().Get("page") page := r.URL.Query().Get("page")
if page == "" { if page == "" {
@@ -52,8 +40,6 @@ func (ws *WebServer) SharesHandler(w http.ResponseWriter, r *http.Request) {
Page: offset/entriesPerPage + 1, Page: offset/entriesPerPage + 1,
HasMore: len(shareLogs) == entriesPerPage, HasMore: len(shareLogs) == entriesPerPage,
} }
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, "Failed to render template", 500) ws.renderTemplate(w, r, "templates/shares.html", &data)
return
}
} }
+27
View File
@@ -0,0 +1,27 @@
package web
import (
"net/http"
"pool-stats/database"
"pool-stats/models"
)
type TopSharesPageData struct {
PageDataBase
Shares []models.ShareLog
}
func (ws *WebServer) TopSharesHandler(w http.ResponseWriter, r *http.Request) {
topShares := database.ListTopShares(ws.db)
if topShares == nil {
http.Error(w, "Failed to load top shares", 500)
return
}
data := TopSharesPageData{
Shares: topShares,
}
ws.renderTemplate(w, r, "templates/top_shares.html", &data)
}