stats : filtre annonces actives adaptatif au schéma (_active_where) dans les panneaux population
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 changed file +16 −7
modified
louka/statsextra.py
+16 −7
@@ -125,15 +125,25 @@ def _panel_rdl() -> dict | None: | ||
| 125 | 125 | |
| 126 | 126 | |
| 127 | 127 | # --- Panneau 2 : Population & territoire -------------------------------------- |
| 128 | +def _active_where(con: sqlite3.Connection) -> str: | |
| 129 | + """Filtre « annonces actives » adapté au schéma (lou-ka a dup_of, pas immo).""" | |
| 130 | + cols = {r[1] for r in con.execute("PRAGMA table_info(listings)")} | |
| 131 | + w = "active=1" if "active" in cols else "1=1" | |
| 132 | + if "dup_of" in cols: | |
| 133 | + w += " AND dup_of IS NULL" | |
| 134 | + return w | |
| 135 | + | |
| 136 | + | |
| 128 | 137 | def _panel_population(con: sqlite3.Connection) -> dict | None: |
| 129 | 138 | qc = _ro("quartier.db") |
| 130 | 139 | if qc is None: |
| 131 | 140 | return None |
| 141 | + aw = _active_where(con) | |
| 132 | 142 | try: |
| 133 | 143 | # aires de diffusion couvertes par au moins une annonce active |
| 134 | 144 | dauids = [r[0] for r in con.execute( |
| 135 | − "SELECT DISTINCT dauid FROM listings WHERE active=1 " | |
| 136 | − "AND dup_of IS NULL AND dauid IS NOT NULL AND dauid<>''")] | |
| 145 | + f"SELECT DISTINCT dauid FROM listings WHERE {aw} " | |
| 146 | + "AND dauid IS NOT NULL AND dauid<>''")] | |
| 137 | 147 | except sqlite3.Error: |
| 138 | 148 | dauids = [] |
| 139 | 149 | if len(dauids) < 20: |
@@ -200,15 +210,14 @@ def _panel_population(con: sqlite3.Connection) -> dict | None: | ||
| 200 | 210 | try: |
| 201 | 211 | # villes avec le plus d'annonces actives |
| 202 | 212 | top_cities = [r["city"] for r in con.execute( |
| 203 | − "SELECT city, COUNT(*) n FROM listings WHERE active=1 " | |
| 204 | − "AND dup_of IS NULL AND city<>'' GROUP BY city ORDER BY n DESC " | |
| 205 | − "LIMIT 15")] | |
| 213 | + f"SELECT city, COUNT(*) n FROM listings WHERE {aw} " | |
| 214 | + "AND city<>'' GROUP BY city ORDER BY n DESC LIMIT 15")] | |
| 206 | 215 | st = {r["dauid"]: r for r in qc2.execute("SELECT * FROM da_stats")} |
| 207 | 216 | trows = [] |
| 208 | 217 | for city in top_cities: |
| 209 | 218 | das = [r["dauid"] for r in con.execute( |
| 210 | − "SELECT DISTINCT dauid FROM listings WHERE active=1 " | |
| 211 | − "AND dup_of IS NULL AND city=? AND dauid IS NOT NULL", (city,))] | |
| 219 | + f"SELECT DISTINCT dauid FROM listings WHERE {aw} " | |
| 220 | + "AND city=? AND dauid IS NOT NULL", (city,))] | |
| 212 | 221 | recs = [st[d] for d in das if d in st] |
| 213 | 222 | if len(recs) < 3: |
| 214 | 223 | continue |
| 215 | 224 | |