Counts view on server and tests

This commit is contained in:
2026-05-02 17:04:29 -04:00
parent 5e135c786b
commit bf576376e7
7 changed files with 102 additions and 5 deletions
+9 -3
View File
@@ -1,14 +1,15 @@
import sqlite3
from ..core.settings import get_data_path
from .settings import get_data_path, get_config
def create_session():
path = get_data_path() / "./tam.db"
conn = sqlite3.connect(path)
conn = sqlite3.connect(path, timeout=10.0)
cur = conn.cursor()
return conn, cur
def init_db():
config = get_config()
conn, cur = create_session()
cur.execute("""CREATE TABLE IF NOT EXISTS auth (auth_key TEXT PRIMARY KEY, description TEXT)""")
cur.execute("""CREATE TABLE IF NOT EXISTS prefixes (
@@ -53,8 +54,13 @@ def init_db():
ORDER BY b.prefix, t.last_name, t.first_name, t.phone_number, b.basket_id""")
cur.execute("""CREATE VIEW IF NOT EXISTS counts AS
SELECT prefix, COUNT(DISTINCT(CONCAT(first_name, last_name, phone_number))) AS unique_buyers, COUNT(*) AS total_buys
FROM tickets
GROUP BY prefix
UNION ALL
SELECT 'Total', COUNT(DISTINCT(CONCAT(first_name, last_name, phone_number))), COUNT(*)""")
SELECT 'Total', COUNT(DISTINCT(CONCAT(first_name, last_name, phone_number))), COUNT(*)
FROM tickets""")
if config["mode"] != "prod":
cur.execute("""REPLACE INTO auth VALUES ('2RO2T7GET9S7X64JUFN67OAV', 'Testing')""")
conn.commit()
conn.close()
print("DB initiated.")
+2
View File
@@ -7,6 +7,8 @@ from .db import create_session
class RepoTemplate:
def __init__(self):
self.conn, self.cur = create_session()
def __del__(self):
self.conn.close()
choose_from = string.ascii_uppercase + string.digits
+6
View File
@@ -15,6 +15,12 @@ class Ticket:
phone_number: str = ""
pref: str = ""
@dataclass
class Count:
prefix: str = ""
unique_buyers: int = 0
total_buys: int = 0
@dataclass
class Basket:
prefix: str = ""
+9 -1
View File
@@ -1,5 +1,5 @@
from ..core.models import RepoTemplate
from .models import Prefix, Ticket
from .models import Prefix, Ticket, Count
class PrefixRepo(RepoTemplate):
@@ -73,3 +73,11 @@ class TicketRepo(RepoTemplate):
pref = EXCLUDED.pref""", (t.prefix, t.ticket_id, t.first_name, t.last_name, t.phone_number, t.pref))
self.conn.commit()
return {"detail": "Tickets posted successfully."}
class CountsRepo(RepoTemplate):
"""Repo that controls the counts system."""
def get_counts(self):
self.cur.execute("SELECT * FROM counts")
results = self.cur.fetchall()
return [Count(*r) for r in results]
+8 -1
View File
@@ -1,6 +1,6 @@
from fastapi import APIRouter, Header, status, HTTPException
from .repos import PrefixRepo, TicketRepo
from .repos import PrefixRepo, TicketRepo, CountsRepo
from .models import Prefix, Ticket
from ..core.auth import AuthRepo
@@ -61,3 +61,10 @@ def get_ticket_scope(prefix: str, s_id: str, tam_auth_key: str = Header("")) ->
def post_tickets(ts: list[Ticket], tam_auth_key: str = Header("")):
AuthRepo().verify_key(tam_auth_key)
return TicketRepo().post_tickets(ts)
counts_router = APIRouter(prefix="/api/counts")
@counts_router.get("")
def get_counts(tam_auth_key: str = Header("")):
AuthRepo().verify_key(tam_auth_key)
return CountsRepo().get_counts()
+1
View File
@@ -7,3 +7,4 @@ def append_routers(app: FastAPI):
app.include_router(auth_router)
app.include_router(routers.prefix_router)
app.include_router(routers.ticket_router)
app.include_router(routers.counts_router)