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 | # --- Panneau 2 : Population & territoire -------------------------------------- | 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 | def _panel_population(con: sqlite3.Connection) -> dict | None: | 137 | def _panel_population(con: sqlite3.Connection) -> dict | None: | ||
| 129 | qc = _ro("quartier.db") | 138 | qc = _ro("quartier.db") | ||
| 130 | if qc is None: | 139 | if qc is None: | ||
| 131 | return None | 140 | return None | ||
| 141 | + aw = _active_where(con) | ||||
| 132 | try: | 142 | try: | ||
| 133 | # aires de diffusion couvertes par au moins une annonce active | 143 | # aires de diffusion couvertes par au moins une annonce active | ||
| 134 | dauids = [r[0] for r in con.execute( | 144 | dauids = [r[0] for r in con.execute( | ||
| 135 | − "SELECT DISTINCT dauid FROM listings WHERE active=1 " | 145 | + f"SELECT DISTINCT dauid FROM listings WHERE {aw} " | ||
| 136 | − "AND dup_of IS NULL AND dauid IS NOT NULL AND dauid<>''")] | 146 | + "AND dauid IS NOT NULL AND dauid<>''")] | ||
| 137 | except sqlite3.Error: | 147 | except sqlite3.Error: | ||
| 138 | dauids = [] | 148 | dauids = [] | ||
| 139 | if len(dauids) < 20: | 149 | if len(dauids) < 20: | ||
@@ -200,15 +210,14 @@ def _panel_population(con: sqlite3.Connection) -> dict | None: | |||||
| 200 | try: | 210 | try: | ||
| 201 | # villes avec le plus d'annonces actives | 211 | # villes avec le plus d'annonces actives | ||
| 202 | top_cities = [r["city"] for r in con.execute( | 212 | top_cities = [r["city"] for r in con.execute( | ||
| 203 | − "SELECT city, COUNT(*) n FROM listings WHERE active=1 " | 213 | + f"SELECT city, COUNT(*) n FROM listings WHERE {aw} " | ||
| 204 | − "AND dup_of IS NULL AND city<>'' GROUP BY city ORDER BY n DESC " | 214 | + "AND city<>'' GROUP BY city ORDER BY n DESC LIMIT 15")] | ||
| 205 | − "LIMIT 15")] | ||||
| 206 | st = {r["dauid"]: r for r in qc2.execute("SELECT * FROM da_stats")} | 215 | st = {r["dauid"]: r for r in qc2.execute("SELECT * FROM da_stats")} | ||
| 207 | trows = [] | 216 | trows = [] | ||
| 208 | for city in top_cities: | 217 | for city in top_cities: | ||
| 209 | das = [r["dauid"] for r in con.execute( | 218 | das = [r["dauid"] for r in con.execute( | ||
| 210 | − "SELECT DISTINCT dauid FROM listings WHERE active=1 " | 219 | + f"SELECT DISTINCT dauid FROM listings WHERE {aw} " | ||
| 211 | − "AND dup_of IS NULL AND city=? AND dauid IS NOT NULL", (city,))] | 220 | + "AND city=? AND dauid IS NOT NULL", (city,))] | ||
| 212 | recs = [st[d] for d in das if d in st] | 221 | recs = [st[d] for d in das if d in st] | ||
| 213 | if len(recs) < 3: | 222 | if len(recs) < 3: | ||
| 214 | continue | 223 | continue | ||
| 215 | 224 | ||||