Compare commits
16 Commits
d836830f45
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| b8511f5ff8 | |||
| ac0285c2e6 | |||
| effe887b3b | |||
| ef247fc843 | |||
| b3c89a01d0 | |||
| 11cc168b3a | |||
| edb17e825d | |||
| 844f7fa08b | |||
| b89a1a2a7e | |||
| 4ddd9abd2e | |||
| f66fbcc454 | |||
| be637f4540 | |||
| d801debaf6 | |||
| 260d2ec24b | |||
| 6f18e75688 | |||
| 1cc12afa16 |
@@ -0,0 +1,26 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "flag"
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Port int `json:"port"`
|
||||||
|
LogPath string `json:"logPath"`
|
||||||
|
DatabasePath string `json:"databasePath"`
|
||||||
|
AdminPassword string `json:"adminPassword"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseFlags() Config {
|
||||||
|
port := flag.Int("Port", 8080, "Listen port")
|
||||||
|
logPath := flag.String("LogPath", "logs", "Path to log files")
|
||||||
|
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()
|
||||||
|
|
||||||
|
return Config{
|
||||||
|
Port: *port,
|
||||||
|
LogPath: *logPath,
|
||||||
|
DatabasePath: *databasePath,
|
||||||
|
AdminPassword: *adminPassword,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
+275
-6
@@ -3,6 +3,9 @@ package database
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"pool-stats/helpers"
|
||||||
|
"pool-stats/models"
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ostafen/clover/v2"
|
"github.com/ostafen/clover/v2"
|
||||||
@@ -12,11 +15,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CollectionName = "shares"
|
CollectionName = "shares"
|
||||||
|
TopSharesCollectionName = "TopShares"
|
||||||
|
TimeWindowHighShareCollectionName = "TimeWindowHighShareStat"
|
||||||
|
DailyStatsCollectionName = "DailyStats"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitDatabase() (*clover.DB, error) {
|
func InitDatabase(path string) (*clover.DB, error) {
|
||||||
store, err := badgerstore.Open("badgerdb")
|
store, err := badgerstore.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open BadgerDB store: %v", err)
|
return nil, fmt.Errorf("failed to open BadgerDB store: %v", err)
|
||||||
}
|
}
|
||||||
@@ -41,10 +47,59 @@ func InitDatabase() (*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()
|
||||||
@@ -57,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) {
|
||||||
@@ -76,3 +141,207 @@ func PrintAllHashes(db *clover.DB) {
|
|||||||
fmt.Println(hash)
|
fmt.Println(hash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ListShares(db *clover.DB, offset int, count int) []models.ShareLog {
|
||||||
|
results, err := db.FindAll(
|
||||||
|
c.NewQuery(CollectionName).
|
||||||
|
Sort(c.SortOption{Field: "CreateDate", Direction: -1}).
|
||||||
|
Skip(offset).
|
||||||
|
Limit(count),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to list shares: %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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package helpers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"pool-stats/models"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HumanDiff(diff float64) string {
|
||||||
|
units := []string{"", "K", "M", "G", "T"}
|
||||||
|
i := 0
|
||||||
|
for diff >= 1000 && i < len(units)-1 {
|
||||||
|
diff /= 1000
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%.2f%s", diff, units[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseCreateDate(createdate string) time.Time {
|
||||||
|
parts := strings.Split(createdate, ",")
|
||||||
|
if len(parts) == 2 {
|
||||||
|
sec, _ := strconv.ParseInt(parts[0], 10, 64)
|
||||||
|
nsec, _ := strconv.ParseInt(parts[1], 10, 64)
|
||||||
|
return time.Unix(sec, nsec)
|
||||||
|
}
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func FormatCreateDate(createdate string) string {
|
||||||
|
parts := strings.Split(createdate, ",")
|
||||||
|
if len(parts) == 2 {
|
||||||
|
sec, _ := strconv.ParseInt(parts[0], 10, 64)
|
||||||
|
nsec, _ := strconv.ParseInt(parts[1], 10, 64)
|
||||||
|
t := time.Unix(sec, nsec)
|
||||||
|
return t.Format(time.DateTime)
|
||||||
|
}
|
||||||
|
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])
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package ingest
|
package jobs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -12,24 +12,32 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logsDir = "/home/pk/pro/pkstats/logs"
|
type IngestSharesJob struct {
|
||||||
|
db *clover.DB
|
||||||
|
logPath string
|
||||||
|
}
|
||||||
|
|
||||||
func WatchAndIngest(db *clover.DB) {
|
func NewIngestSharesJob(db *clover.DB, path string) *IngestSharesJob {
|
||||||
ticker := time.NewTicker(30 * time.Second)
|
return &IngestSharesJob{db: db, logPath: path}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *IngestSharesJob) WatchAndIngest() {
|
||||||
|
ticker := time.NewTicker(constants.IngestSharesJobInterval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
<-ticker.C
|
<-ticker.C
|
||||||
IngestClosedBlocks(db)
|
this.ingestClosedBlocks()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func IngestClosedBlocks(db *clover.DB) {
|
func (this *IngestSharesJob) ingestClosedBlocks() {
|
||||||
entries, err := os.ReadDir(logsDir)
|
entries, err := os.ReadDir(this.logPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error reading logsDir:", err)
|
log.Println("Error reading logsDir:", err)
|
||||||
return
|
return
|
||||||
@@ -54,12 +62,12 @@ func IngestClosedBlocks(db *clover.DB) {
|
|||||||
|
|
||||||
// Ingest all except last (current block dir)
|
// Ingest all except last (current block dir)
|
||||||
for _, dir := range blockDirs[:len(blockDirs)-1] {
|
for _, dir := range blockDirs[:len(blockDirs)-1] {
|
||||||
IngestBlockDir(db, filepath.Join(logsDir, dir.Name()))
|
this.ingestBlockDir(this.db, filepath.Join(this.logPath, dir.Name()))
|
||||||
_ = os.RemoveAll(filepath.Join(logsDir, dir.Name()))
|
_ = os.RemoveAll(filepath.Join(this.logPath, dir.Name()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func 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)
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -3,31 +3,41 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"pool-stats/config"
|
||||||
"pool-stats/database"
|
"pool-stats/database"
|
||||||
"pool-stats/ingest"
|
"pool-stats/jobs"
|
||||||
"pool-stats/web"
|
"pool-stats/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
db, err := database.InitDatabase()
|
config := config.ParseFlags()
|
||||||
|
|
||||||
|
db, err := database.InitDatabase(config.DatabasePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to initialize database: %v", err)
|
log.Fatalf("Failed to initialize database: %v", err)
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
go ingest.WatchAndIngest(db)
|
ingestor := jobs.NewIngestSharesJob(db, config.LogPath)
|
||||||
|
go ingestor.WatchAndIngest()
|
||||||
|
|
||||||
go func() {
|
topSharesRecalcJob := jobs.NewRecalculateTopSharesJob(db)
|
||||||
handlers := web.Handlers{DB: db}
|
go topSharesRecalcJob.Run()
|
||||||
http.HandleFunc("/", handlers.IndexHandler)
|
|
||||||
fmt.Println("Listening on :8081")
|
timeWindowHighSharesRecalcJob := jobs.NewRecalculateTimeWindowHighSharesJob(db)
|
||||||
log.Fatal(http.ListenAndServe(":8081", nil))
|
go timeWindowHighSharesRecalcJob.Run()
|
||||||
}()
|
|
||||||
|
currentDayStatsRecalcJob := jobs.NewRecalculateCurrentDayStatsJob(db)
|
||||||
|
go currentDayStatsRecalcJob.Run()
|
||||||
|
|
||||||
|
webServer := web.NewWebServer(db, config.Port, config.AdminPassword)
|
||||||
|
if err := webServer.Start(); err != nil {
|
||||||
|
log.Fatalf("Failed to start web server: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println("Waiting for ctrl-c")
|
fmt.Println("Waiting for ctrl-c")
|
||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
package stats
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ostafen/clover/v2"
|
|
||||||
|
|
||||||
"pool-stats/database"
|
|
||||||
"pool-stats/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func parseCreatedate(createdate string) time.Time {
|
|
||||||
parts := strings.Split(createdate, ",")
|
|
||||||
if len(parts) == 2 {
|
|
||||||
sec, _ := strconv.ParseInt(parts[0], 10, 64)
|
|
||||||
nsec, _ := strconv.ParseInt(parts[1], 10, 64)
|
|
||||||
return time.Unix(sec, nsec)
|
|
||||||
}
|
|
||||||
return time.Time{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func humanDiff(diff float64) string {
|
|
||||||
units := []string{"", "K", "M", "G", "T"}
|
|
||||||
i := 0
|
|
||||||
for diff >= 1000 && i < len(units)-1 {
|
|
||||||
diff /= 1000
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%.2f%s", diff, units[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
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: humanDiff(doc.Get("SDiff").(float64)),
|
|
||||||
Time: 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: humanDiff(doc.Get("SDiff").(float64)),
|
|
||||||
Time: parseCreatedate(doc.Get("CreateDate").(string)).Format(time.RFC822),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
stats = append(stats, models.ShareStat{Label: r.Label, Diff: "-", Time: "-"})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return stats, nil
|
|
||||||
}
|
|
||||||
@@ -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" . }}
|
||||||
@@ -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 }}"
|
||||||
|
>« 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 »</a
|
||||||
|
>
|
||||||
|
{{ end }}
|
||||||
|
</div>
|
||||||
|
{{ end }} {{ template "layout" . }}
|
||||||
+19
-48
@@ -1,48 +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;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>🌟 ckpool 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>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|||||||
@@ -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 }}
|
||||||
@@ -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" . }}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
{{ define "title" }}Share Browser{{ end }} {{ define "header" }}☀️ Pool Share
|
||||||
|
Browser{{ end }} {{ define "content" }}
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Time</th>
|
||||||
|
<th>Worker</th>
|
||||||
|
<th>SDiff</th>
|
||||||
|
<th>Result</th>
|
||||||
|
<th>Hash</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{{ range .Shares }}
|
||||||
|
<tr>
|
||||||
|
<td>{{ formatCreateDate .CreateDate }}</td>
|
||||||
|
<td>{{ .WorkerName }}</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="5">No shares found.</td>
|
||||||
|
</tr>
|
||||||
|
{{ end }}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
{{ if gt .Page 1 }}
|
||||||
|
<a class="page-link" href="?page={{ sub .Page 1 }}">« 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>
|
||||||
|
<a class="page-link" href="?page={{ add .Page 1 }}">Next »</a>
|
||||||
|
{{ end }}
|
||||||
|
</div>
|
||||||
|
{{ end }} {{ template "layout" . }}
|
||||||
@@ -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" . }}
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package web
|
|
||||||
|
|
||||||
import (
|
|
||||||
"html/template"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"pool-stats/stats"
|
|
||||||
|
|
||||||
"github.com/ostafen/clover/v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Handlers struct {
|
|
||||||
DB *clover.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h Handlers) IndexHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
shareStats, err := stats.GetStats(h.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)
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
+102
@@ -0,0 +1,102 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
"pool-stats/helpers"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gofrs/uuid/v5"
|
||||||
|
"github.com/ostafen/clover/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WebServer struct {
|
||||||
|
db *clover.DB
|
||||||
|
port int
|
||||||
|
templates *template.Template
|
||||||
|
sessions map[string]string
|
||||||
|
adminPassword string
|
||||||
|
}
|
||||||
|
|
||||||
|
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{
|
||||||
|
db: db,
|
||||||
|
port: port,
|
||||||
|
templates: templates,
|
||||||
|
sessions: make(map[string]string),
|
||||||
|
adminPassword: adminPassword,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *WebServer) Start() error {
|
||||||
|
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("/top-shares", ws.TopSharesHandler)
|
||||||
|
http.HandleFunc("/daily-stats", ws.DailyStatsHandler)
|
||||||
|
|
||||||
|
address := ":" + fmt.Sprint(ws.port)
|
||||||
|
println("Listening on", address)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"pool-stats/database"
|
||||||
|
"pool-stats/models"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SharePageData struct {
|
||||||
|
PageDataBase
|
||||||
|
|
||||||
|
Shares []models.ShareLog
|
||||||
|
Page int
|
||||||
|
HasMore bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *WebServer) SharesHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
entriesPerPage := 10
|
||||||
|
page := r.URL.Query().Get("page")
|
||||||
|
if page == "" {
|
||||||
|
page = "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
offset, err := strconv.Atoi(page)
|
||||||
|
if err != nil || offset < 1 {
|
||||||
|
http.Error(w, "Invalid page number", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
offset = (offset - 1) * entriesPerPage
|
||||||
|
shareLogs := database.ListShares(ws.db, offset, entriesPerPage)
|
||||||
|
if shareLogs == nil {
|
||||||
|
http.Error(w, "Failed to load shares", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := SharePageData{
|
||||||
|
Shares: shareLogs,
|
||||||
|
Page: offset/entriesPerPage + 1,
|
||||||
|
HasMore: len(shareLogs) == entriesPerPage,
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.renderTemplate(w, r, "templates/shares.html", &data)
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user