adding statistics page for admin
This commit is contained in:
41
web/db.py
41
web/db.py
@@ -783,3 +783,44 @@ def change_keyword_color(keyword_id: int, new_color: str) -> bool:
|
||||
except Exception:
|
||||
session.rollback()
|
||||
return False
|
||||
|
||||
|
||||
def stats_overview() -> Dict[str, Any]:
|
||||
"""Return an overview of job DB statistics.
|
||||
|
||||
Returns a dict with keys:
|
||||
- total_jobs: int
|
||||
- total_keywords: int (distinct keywords in listings)
|
||||
- total_regions: int (distinct regions in listings)
|
||||
- jobs_per_keyword: List[{"keyword": str, "count": int}]
|
||||
- jobs_per_region: List[{"region": str, "count": int}]
|
||||
"""
|
||||
with _ensure_session() as session:
|
||||
total_jobs = session.execute(text(
|
||||
"SELECT COUNT(*) FROM job_listings l INNER JOIN job_descriptions d ON l.job_id = d.job_id AND l.url = d.url"
|
||||
)).scalar_one()
|
||||
total_keywords = session.execute(text(
|
||||
"SELECT COUNT(DISTINCT keyword) FROM job_listings WHERE keyword IS NOT NULL AND keyword != ''"
|
||||
)).scalar_one()
|
||||
total_regions = session.execute(text(
|
||||
"SELECT COUNT(DISTINCT region) FROM job_listings WHERE region IS NOT NULL AND region != ''"
|
||||
)).scalar_one()
|
||||
|
||||
rows = session.execute(text(
|
||||
"SELECT COALESCE(keyword, '') AS keyword, COUNT(*) as cnt FROM job_listings l INNER JOIN job_descriptions d ON l.job_id = d.job_id AND l.url = d.url GROUP BY keyword ORDER BY cnt DESC"
|
||||
)).fetchall()
|
||||
jobs_per_keyword = [
|
||||
{"keyword": r[0], "count": int(r[1])} for r in rows]
|
||||
|
||||
rows = session.execute(text(
|
||||
"SELECT COALESCE(region, '') AS region, COUNT(*) as cnt FROM job_listings l INNER JOIN job_descriptions d ON l.job_id = d.job_id AND l.url = d.url GROUP BY region ORDER BY cnt DESC"
|
||||
)).fetchall()
|
||||
jobs_per_region = [{"region": r[0], "count": int(r[1])} for r in rows]
|
||||
|
||||
return {
|
||||
"total_jobs": int(total_jobs or 0),
|
||||
"total_keywords": int(total_keywords or 0),
|
||||
"total_regions": int(total_regions or 0),
|
||||
"jobs_per_keyword": jobs_per_keyword,
|
||||
"jobs_per_region": jobs_per_region,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user