T6-T8: web dashboard spec-list (§V10, I.web, I.db)
- dashboard.py: Flask read-only, paged 50/page, ORDER BY staked DESC - templates/index.html + static/style.css: DESIGN.md Bugatti austere tokens - test_dashboard.py: oracle (ordering, paging, read-only) - DESIGN.md style guide (was untracked)
This commit is contained in:
1 parent
c5e30a4785
commit
0b3a86aad2
8 files changed
+944
-4
No files matched your search
@@ -0,0 +1,80 @@
|
||||
"""Dashboard web pemilih BP databisnisid — baca `voters.db`, sajikan spec-list.
|
||||
|
||||
Read-only (V10): tidak pernah menulis ke DB. Data disegarkan oleh `get_voters.py`
|
||||
(run harian / cron). Styling mengikuti `DESIGN.md`.
|
||||
"""
|
||||
|
||||
import sqlite3
|
||||
|
||||
from flask import Flask, abort, render_template, request
|
||||
|
||||
from get_voters import DB_PATH, TARGET_BP
|
||||
|
||||
PAGE_SIZE = 50
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@app.template_filter('vex')
|
||||
def vex(value):
|
||||
"""Format jumlah VEX ala id-ID (koma ribuan, titik desimal), tanpa emoji."""
|
||||
return f'{value:,.4f}'.replace(',', '\u00a0').replace('.', ',').replace('\u00a0', '.')
|
||||
|
||||
|
||||
def _db_rows(page):
|
||||
"""V10: baca satu halaman, urut staked DESC lalu owner ASC."""
|
||||
offset = (page - 1) * PAGE_SIZE
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
try:
|
||||
return conn.execute(
|
||||
'SELECT owner, staked, weight, scanned_at FROM voters '
|
||||
'ORDER BY staked DESC, owner ASC '
|
||||
'LIMIT ? OFFSET ?',
|
||||
(PAGE_SIZE, offset),
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def _stats():
|
||||
"""Ringkasan agregat untuk band spec."""
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
try:
|
||||
total_voters, total_staked, top_staked = conn.execute(
|
||||
'SELECT COUNT(*), COALESCE(SUM(staked), 0), COALESCE(MAX(staked), 0) '
|
||||
'FROM voters'
|
||||
).fetchone()
|
||||
scanned = conn.execute(
|
||||
'SELECT COALESCE(MAX(scanned_at), \'\') FROM voters'
|
||||
).fetchone()[0]
|
||||
finally:
|
||||
conn.close()
|
||||
return total_voters, total_staked, top_staked, scanned
|
||||
|
||||
|
||||
@app.get('/')
|
||||
def index():
|
||||
try:
|
||||
page = max(1, int(request.args.get('page', 1)))
|
||||
except ValueError:
|
||||
abort(400)
|
||||
total_voters, total_staked, top_staked, scanned = _stats()
|
||||
total_pages = max(1, (total_voters + PAGE_SIZE - 1) // PAGE_SIZE)
|
||||
page = min(page, total_pages)
|
||||
rows = _db_rows(page)
|
||||
|
||||
return render_template(
|
||||
'index.html',
|
||||
bp=TARGET_BP,
|
||||
rows=rows,
|
||||
page=page,
|
||||
total_pages=total_pages,
|
||||
total_voters=total_voters,
|
||||
total_staked=total_staked,
|
||||
top_staked=top_staked,
|
||||
scanned=scanned,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='127.0.0.1', port=5000, debug=False)
|
||||
Reference in new issue
Block a user