(add): Counts backend logic, placeholders
This commit is contained in:
@@ -27,6 +27,12 @@ class ReportByBasketLine:
|
||||
phone_number: str = ""
|
||||
pref: str = ""
|
||||
|
||||
@dataclass
|
||||
class ReportCountLine:
|
||||
prefix: str = ""
|
||||
unique_buyers: int = 0
|
||||
total_buys: int = 0
|
||||
|
||||
class ReportsRepo(RepoTemplate):
|
||||
def get_by_name_report(self, prefix: str):
|
||||
self.cur.execute("SELECT * FROM report_by_name WHERE prefix = ?", (prefix,))
|
||||
@@ -36,6 +42,10 @@ class ReportsRepo(RepoTemplate):
|
||||
self.cur.execute("SELECT * FROM report_by_basket WHERE prefix = ?", (prefix,))
|
||||
results = self.cur.fetchall()
|
||||
return [ReportByBasketLine(*r) for r in results]
|
||||
def get_counts_report(self):
|
||||
self.cur.execute("SELECT * FROM report_counts")
|
||||
results = self.cur.fetchall()
|
||||
return [ReportCountLine(*r) for r in results]
|
||||
|
||||
reports_router = APIRouter(prefix="/api/reports")
|
||||
|
||||
@@ -48,3 +58,8 @@ def get_report_by_name(prefix: str, tam_key: str = Header("")):
|
||||
def get_report_by_basket(prefix: str, tam_key: str = Header("")):
|
||||
AuthRepo().verify_key(tam_key)
|
||||
return ReportsRepo().get_by_basket_report(prefix)
|
||||
|
||||
@reports_router.get("/counts")
|
||||
def get_report_counts(tam_key: str = Header("")):
|
||||
AuthRepo().verify_key(tam_key)
|
||||
return ReportsRepo().get_counts_report()
|
||||
|
||||
@@ -33,6 +33,13 @@ def init_db():
|
||||
SELECT b.*, t.last_name, t.first_name, t.phone_number, t.pref
|
||||
FROM baskets b LEFT JOIN tickets t ON b.prefix = t.prefix AND b.winning_ticket = t.t_id
|
||||
ORDER BY b.prefix, b.b_id""")
|
||||
cur.execute("""CREATE VIEW IF NOT EXISTS report_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 'Totals', COUNT(DISTINCT(CONCAT(first_name, last_name, phone_number))), COUNT(*)
|
||||
FROM tickets""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user