Compare commits

...

2 Commits

Author SHA1 Message Date
Pijus Kamandulis
6f18e75688 added share list page 2025-05-27 22:56:44 +03:00
Pijus Kamandulis
1cc12afa16 add configuration 2025-05-27 21:56:19 +03:00
11 changed files with 325 additions and 52 deletions

23
config/config.go Normal file
View File

@ -0,0 +1,23 @@
package config
import "flag"
type Config struct {
Port int `json:"port"`
LogPath string `json:"logPath"`
DatabasePath string `json:"databasePath"`
}
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")
flag.Parse()
return Config{
Port: *port,
LogPath: *logPath,
DatabasePath: *databasePath,
}
}

View File

@ -3,6 +3,7 @@ package database
import (
"fmt"
"log"
"pool-stats/models"
"time"
"github.com/ostafen/clover/v2"
@ -15,8 +16,8 @@ const (
CollectionName = "shares"
)
func InitDatabase() (*clover.DB, error) {
store, err := badgerstore.Open("badgerdb")
func InitDatabase(path string) (*clover.DB, error) {
store, err := badgerstore.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open BadgerDB store: %v", err)
}
@ -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

@ -18,17 +18,26 @@ import (
const logsDir = "/home/pk/pro/pkstats/logs"
func WatchAndIngest(db *clover.DB) {
type Ingestor struct {
db *clover.DB
logPath string
}
func NewIngestor(db *clover.DB, path string) *Ingestor {
return &Ingestor{db: db, logPath: path}
}
func (this *Ingestor) WatchAndIngest() {
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
<-ticker.C
IngestClosedBlocks(db)
this.ingestClosedBlocks()
}
}
func IngestClosedBlocks(db *clover.DB) {
func (this *Ingestor) ingestClosedBlocks() {
entries, err := os.ReadDir(logsDir)
if err != nil {
log.Println("Error reading logsDir:", err)
@ -54,12 +63,12 @@ func IngestClosedBlocks(db *clover.DB) {
// Ingest all except last (current block dir)
for _, dir := range blockDirs[:len(blockDirs)-1] {
IngestBlockDir(db, filepath.Join(logsDir, dir.Name()))
this.ingestBlockDir(this.db, filepath.Join(logsDir, dir.Name()))
_ = os.RemoveAll(filepath.Join(logsDir, dir.Name()))
}
}
func IngestBlockDir(db *clover.DB, dirPath string) {
func (this *Ingestor) ingestBlockDir(db *clover.DB, dirPath string) {
files, err := os.ReadDir(dirPath)
if err != nil {
log.Printf("Failed to read block dir %s: %v", dirPath, err)

19
main.go
View File

@ -3,31 +3,32 @@ package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"pool-stats/config"
"pool-stats/database"
"pool-stats/ingest"
"pool-stats/web"
)
func main() {
db, err := database.InitDatabase()
config := config.ParseFlags()
db, err := database.InitDatabase(config.DatabasePath)
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
defer db.Close()
go ingest.WatchAndIngest(db)
ingestor := ingest.NewIngestor(db, config.LogPath)
go ingestor.WatchAndIngest()
go func() {
handlers := web.Handlers{DB: db}
http.HandleFunc("/", handlers.IndexHandler)
fmt.Println("Listening on :8081")
log.Fatal(http.ListenAndServe(":8081", nil))
}()
webServer := web.NewWebServer(db, config.Port)
if err := webServer.Start(); err != nil {
log.Fatalf("Failed to start web server: %v", err)
}
fmt.Println("Waiting for ctrl-c")
sigs := make(chan os.Signal, 1)

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

@ -5,16 +5,10 @@ import (
"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)
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

30
web/server.go Normal file
View File

@ -0,0 +1,30 @@
package web
import (
"net/http"
"fmt"
"github.com/ostafen/clover/v2"
)
type WebServer struct {
db *clover.DB
port int
}
func NewWebServer(db *clover.DB, port int) *WebServer {
return &WebServer{
db: db,
port: port,
}
}
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
}
}