Revamp dashboard and persist pool stats across restarts

Dashboard now renders 10 tiles in a 5x2 overview: hashrate, best
share, miners, network hashrate, and expected block on the top row;
difficulty, block height, block reward, total work, and the
difficulty-adjustment countdown on the bottom row. Difficulty is
rendered with T/P suffixes instead of scientific notation, the main
hashrate card shows the 1-minute value, and the block-height tile
pulses orange when the network tip advances.

Added a 24-hour hashrate area chart below the overview, sampled
once per minute. Samples are persisted to a new hashrate_samples
SQLite table and restored on startup so the chart doesn't reset
every time kamado-api is restarted.

Cumulative pool work (sum of accepted diff-1-normalized shares) is
now tracked across ckpool restarts. The aggregator integrates only
positive deltas on pool.Shares — a regression means ckpool's
counter reset to zero and the baseline is refreshed without losing
the running total. A hasPoolSharesBaseline flag prevents double-
counting on the first refresh after a kamado-api restart. The
value is persisted to a new kv table once per minute.

Next-block reward (subsidy + fees) is fetched from bitcoind
getblocktemplate at most once per minute and surfaced as a tile.

Header's block-height badge now reads prevHeight via untrack() so
the effect doesn't form a dependency cycle with its own write.
This commit is contained in:
satoshi
2026-04-22 21:07:22 +03:00
parent 8de767646d
commit e881bc930d
10 changed files with 2180 additions and 14 deletions
+73
View File
@@ -39,6 +39,16 @@ CREATE TABLE IF NOT EXISTS blocks (
share_diff REAL NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS blocks_found_at_idx ON blocks(found_at);
CREATE TABLE IF NOT EXISTS hashrate_samples (
t INTEGER PRIMARY KEY,
v REAL NOT NULL
);
CREATE TABLE IF NOT EXISTS kv (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
`
// migrations additive only; safe to run every startup. SQLite ignores
@@ -103,6 +113,69 @@ func (s *BlockStore) InsertBlock(b Block) error {
return err
}
// HashratePoint is one persisted hashrate sample.
type HashratePoint struct {
T int64
V float64
}
// InsertHashrateSample appends a sample; duplicate timestamps are ignored.
func (s *BlockStore) InsertHashrateSample(t int64, v float64) error {
_, err := s.db.Exec(
`INSERT OR IGNORE INTO hashrate_samples(t, v) VALUES (?, ?)`,
t, v,
)
return err
}
// PruneHashrateBefore drops samples older than the given unix timestamp.
func (s *BlockStore) PruneHashrateBefore(cutoff int64) error {
_, err := s.db.Exec(`DELETE FROM hashrate_samples WHERE t < ?`, cutoff)
return err
}
// HashrateSince returns all samples with t >= from, oldest first.
func (s *BlockStore) HashrateSince(from int64) ([]HashratePoint, error) {
rows, err := s.db.Query(
`SELECT t, v FROM hashrate_samples WHERE t >= ? ORDER BY t ASC`,
from,
)
if err != nil {
return nil, err
}
defer rows.Close()
var out []HashratePoint
for rows.Next() {
var p HashratePoint
if err := rows.Scan(&p.T, &p.V); err != nil {
return nil, err
}
out = append(out, p)
}
return out, rows.Err()
}
// GetKV reads a string value by key. Returns ("", nil) when the key is
// absent so callers can distinguish "no value yet" from a real error.
func (s *BlockStore) GetKV(key string) (string, error) {
var v string
err := s.db.QueryRow(`SELECT value FROM kv WHERE key = ?`, key).Scan(&v)
if errors.Is(err, sql.ErrNoRows) {
return "", nil
}
return v, err
}
// SetKV writes or replaces a key's value.
func (s *BlockStore) SetKV(key, value string) error {
_, err := s.db.Exec(
`INSERT INTO kv(key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
key, value,
)
return err
}
// Recent returns up to limit blocks, newest first.
func (s *BlockStore) Recent(limit int) ([]Block, error) {
if limit <= 0 {