added share list page

This commit is contained in:
Pijus Kamandulis 2025-05-27 22:56:44 +03:00
parent 1cc12afa16
commit 6f18e75688
7 changed files with 246 additions and 28 deletions

View File

@ -3,6 +3,7 @@ package database
import (
"fmt"
"log"
"pool-stats/models"
"time"
"github.com/ostafen/clover/v2"
@ -76,3 +77,25 @@ func PrintAllHashes(db *clover.DB) {
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
}

39
helpers/helpers.go Normal file
View File

@ -0,0 +1,39 @@
package helpers
import (
"fmt"
"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 ""
}

View File

@ -1,37 +1,15 @@
package stats
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/ostafen/clover/v2"
"pool-stats/database"
"pool-stats/helpers"
"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 {
@ -50,8 +28,8 @@ func GetStats(db *clover.DB) ([]models.ShareStat, error) {
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),
Diff: helpers.HumanDiff(doc.Get("SDiff").(float64)),
Time: helpers.ParseCreateDate(doc.Get("CreateDate").(string)).Format(time.RFC822),
})
}
@ -60,8 +38,8 @@ func GetStats(db *clover.DB) ([]models.ShareStat, error) {
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),
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: "-"})

View File

@ -26,10 +26,19 @@
tr:nth-child(even) {
background-color: #1a1a1a;
}
a {
color: #0af;
text-decoration: none;
}
li {
display: inline;
margin: 0 10px;
}
</style>
</head>
<body>
<h1>🌟 ckpool Share Stats</h1>
<h1>🌟 Pool Share Stats</h1>
<table>
<tr>
<th>Range</th>
@ -44,5 +53,10 @@
</tr>
{{end}}
</table>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/shares">View Shares</a></li>
</ul>
</body>
</html>

103
templates/shares.html Normal file
View File

@ -0,0 +1,103 @@
{{ define "share_list" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>ckpool Share Browser</title>
<style>
body {
font-family: sans-serif;
background: #111;
color: #eee;
padding: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th,
td {
padding: 8px 12px;
border: 1px solid #444;
text-align: left;
white-space: nowrap;
}
th {
background-color: #222;
}
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>
<thead>
<tr>
<th>Time</th>
<th>Worker</th>
<th>Address</th>
<th>SDiff</th>
<th>Result</th>
<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>
{{ 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>
<a class="page-link" href="?page={{ add .Page 1 }}">Next &raquo;</a>
{{ end }}
</div>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/shares">View Shares</a></li>
</ul>
</body>
</html>
{{ end }}

View File

@ -22,6 +22,8 @@ func NewWebServer(db *clover.DB, port int) *WebServer {
func (ws *WebServer) Start() error {
http.HandleFunc("/", ws.IndexHandler)
http.HandleFunc("/shares", ws.SharesHandler)
address := ":" + fmt.Sprint(ws.port)
println("Listening on", address)
return http.ListenAndServe(address, nil)

59
web/sharesHandler.go Normal file
View File

@ -0,0 +1,59 @@
package web
import (
"html/template"
"net/http"
"pool-stats/database"
"pool-stats/helpers"
"pool-stats/models"
"strconv"
)
type SharePageData struct {
Shares []models.ShareLog
Page int
HasMore bool
}
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
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,
}
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, "Failed to render template", 500)
return
}
}