|
7 | 7 | router = APIRouter() |
8 | 8 |
|
9 | 9 |
|
10 | | -# Endpoint to get unique values for specified fields |
11 | | -from fastapi import Query |
| 10 | +@router.get("/prospects") |
| 11 | +def root() -> dict: |
| 12 | + """Return a placeholder message for prospects endpoint.""" |
| 13 | + meta = make_meta("success", "Prospects placeholder") |
| 14 | + data = {"message": "This is a placeholder for the /prospects endpoint."} |
| 15 | + return {"meta": meta, "data": data} |
12 | 16 |
|
13 | | -@router.get("/prospects/unique") |
14 | | -def get_unique_fields(fields: list[str] = Query(..., description="List of field names to get unique values for")) -> dict: |
15 | | - """Return lists of unique values and their counts for specified fields in the prospects table.""" |
16 | | - conn_gen = get_db_connection() |
17 | | - conn = next(conn_gen) |
18 | | - cur = conn.cursor() |
19 | | - result = {} |
20 | | - errors = {} |
21 | | - try: |
22 | | - for field in fields: |
23 | | - try: |
24 | | - cur.execute(f'SELECT "{field}", COUNT(*) FROM prospects WHERE "{field}" IS NOT NULL GROUP BY "{field}" ORDER BY COUNT(*) DESC;') |
25 | | - values = [ |
26 | | - {"value": row[0], "count": row[1]} for row in cur.fetchall() |
27 | | - ] |
28 | | - result[field] = values |
29 | | - except Exception as e: |
30 | | - errors[field] = str(e) |
31 | | - meta = make_meta("success", f"Unique values and counts for fields: {fields}") |
32 | | - return {"meta": meta, "data": result, "errors": errors if errors else None} |
33 | | - finally: |
34 | | - cur.close() |
35 | | - conn.close() |
36 | 17 |
|
| 18 | +# New endpoint: /prospects/init |
37 | 19 |
|
38 | | -@router.get("/prospects") |
39 | | -def root() -> dict: |
40 | | - """Return all prospects table records""" |
41 | | - base_url = os.getenv("BASE_URL", "http://localhost:8000") |
| 20 | +@router.get("/prospects/init") |
| 21 | +def prospects_init() -> dict: |
| 22 | + """Initialize prospects and return real total count.""" |
| 23 | + meta = make_meta("success", "Initialized prospects") |
42 | 24 | conn_gen = get_db_connection() |
43 | 25 | conn = next(conn_gen) |
44 | 26 | cur = conn.cursor() |
45 | | - actions = [ |
46 | | - { |
47 | | - "name": "Seed prospects table", |
48 | | - "url": f"{base_url}/prospects/seed" |
49 | | - }, |
50 | | - { |
51 | | - "name": "Empty prospects table", |
52 | | - "url": f"{base_url}/prospects/empty" |
53 | | - }, |
54 | | - ] |
| 27 | + title = [] |
| 28 | + total_unique_title = 0 |
| 29 | + seniority = [] |
| 30 | + total_unique_seniority = 0 |
| 31 | + sub_departments = [] |
| 32 | + total_unique_sub_departments = 0 |
55 | 33 | try: |
56 | | - cur.execute('SELECT * FROM prospects LIMIT 200;') |
57 | | - if cur.description is None: |
58 | | - prospects = [] |
59 | | - else: |
60 | | - columns = [desc[0] for desc in cur.description] |
61 | | - prospects = [dict(zip(columns, row)) for row in cur.fetchall()] |
62 | | - meta = make_meta("success", "Prospects List (max 200)") |
63 | | - result = {"meta": meta, "data": prospects} |
64 | | - except Exception as e: |
65 | | - import psycopg2 |
66 | | - if isinstance(e, psycopg2.errors.UndefinedTable): |
67 | | - meta = make_meta("error", "prospects table does not exist.") |
68 | | - result = {"meta": meta, "data": actions} |
69 | | - else: |
70 | | - meta = make_meta("error", str(e)) |
71 | | - result = {"meta": meta, "data": actions} |
| 34 | + cur.execute('SELECT COUNT(*) FROM prospects;') |
| 35 | + row = cur.fetchone() |
| 36 | + total = row[0] if row is not None else 0 |
| 37 | + |
| 38 | + # Get unique titles and their counts (column is 'title') |
| 39 | + cur.execute('SELECT title, COUNT(*) FROM prospects WHERE title IS NOT NULL GROUP BY title ORDER BY COUNT(*) DESC;') |
| 40 | + title_rows = cur.fetchall() |
| 41 | + title = [ |
| 42 | + {"label": t[0], "count": t[1]} for t in title_rows if t[0] is not None |
| 43 | + ] |
| 44 | + total_unique_title = len(title) |
| 45 | + |
| 46 | + # Get unique seniority and their counts (column is 'seniority') |
| 47 | + cur.execute('SELECT seniority, COUNT(*) FROM prospects WHERE seniority IS NOT NULL GROUP BY seniority ORDER BY COUNT(*) DESC;') |
| 48 | + seniority_rows = cur.fetchall() |
| 49 | + seniority = [ |
| 50 | + {"label": s[0], "count": s[1]} for s in seniority_rows if s[0] is not None |
| 51 | + ] |
| 52 | + total_unique_seniority = len(seniority) |
| 53 | + |
| 54 | + # Get unique sub_departments and their counts (column is 'sub_departments') |
| 55 | + cur.execute('SELECT sub_departments, COUNT(*) FROM prospects WHERE sub_departments IS NOT NULL GROUP BY sub_departments ORDER BY COUNT(*) DESC;') |
| 56 | + sub_department_rows = cur.fetchall() |
| 57 | + sub_departments = [ |
| 58 | + {"label": sd[0], "count": sd[1]} for sd in sub_department_rows if sd[0] is not None |
| 59 | + ] |
| 60 | + total_unique_sub_departments = len(sub_departments) |
| 61 | + except Exception: |
| 62 | + total = 0 |
| 63 | + title = [] |
| 64 | + total_unique_title = 0 |
| 65 | + seniority = [] |
| 66 | + total_unique_seniority = 0 |
| 67 | + sub_departments = [] |
| 68 | + total_unique_sub_departments = 0 |
72 | 69 | finally: |
73 | 70 | cur.close() |
74 | 71 | conn.close() |
75 | | - return result |
76 | | - |
77 | | - |
78 | | -# New endpoint: /prospects/init |
79 | | -@router.get("/prospects/init") |
80 | | -def prospects_init() -> dict: |
81 | | - """Initialize prospects (placeholder endpoint)""" |
82 | | - meta = make_meta("success", "Initialized prospects (placeholder)") |
83 | | - data = {"message": "This is a placeholder for prospects/init."} |
| 72 | + data = { |
| 73 | + "total_prospects": total, |
| 74 | + "title": { |
| 75 | + "total_unique": total_unique_title, |
| 76 | + "values": title |
| 77 | + }, |
| 78 | + "seniority": { |
| 79 | + "total_unique": total_unique_seniority, |
| 80 | + "values": seniority |
| 81 | + }, |
| 82 | + "departments": { |
| 83 | + "total_unique": total_unique_sub_departments, |
| 84 | + "values": sub_departments |
| 85 | + } |
| 86 | + } |
84 | 87 | return {"meta": meta, "data": data} |
0 commit comments