Implement daily stats

This commit is contained in:
Pijus Kamandulis
2025-06-23 21:35:16 +03:00
parent 4ddd9abd2e
commit b89a1a2a7e
8 changed files with 318 additions and 0 deletions

View File

@@ -2,6 +2,9 @@ package helpers
import (
"fmt"
"math"
"pool-stats/models"
"sort"
"strconv"
"strings"
"time"
@@ -37,3 +40,41 @@ func FormatCreateDate(createdate string) string {
}
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])
}