51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
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
|
|
}
|