|
3 | 3 |
|
4 | 4 | from .db import get_pool |
5 | 5 |
|
| 6 | + |
| 7 | +def _map_changes_expr(alias: str = "st") -> str: |
| 8 | + return f""" |
| 9 | + {alias}.nodes_created + {alias}.nodes_modified + {alias}.nodes_deleted + |
| 10 | + {alias}.ways_created + {alias}.ways_modified + {alias}.ways_deleted + |
| 11 | + {alias}.rels_created + {alias}.rels_modified + {alias}.rels_deleted |
| 12 | + """ |
| 13 | + |
6 | 14 | _TAG_CTES = """, |
7 | 15 | tag_agg AS ( |
8 | 16 | SELECT |
@@ -136,6 +144,119 @@ def _user_stats_sql(*, filter_dates: bool, filter_hashtags: bool, include_tags: |
136 | 144 | """ |
137 | 145 |
|
138 | 146 |
|
| 147 | +def _changeset_filters_sql(*, filter_dates: bool, filter_hashtags: bool = False) -> tuple[str, int]: |
| 148 | + n = 1 |
| 149 | + filters: list[str] = [] |
| 150 | + if filter_dates: |
| 151 | + filters.append(f"cs.created_at >= ${n}") |
| 152 | + n += 1 |
| 153 | + filters.append(f"cs.created_at < ${n}") |
| 154 | + n += 1 |
| 155 | + if filter_hashtags: |
| 156 | + filters.append(f"cs.hashtags && ${n}::TEXT[]") |
| 157 | + n += 1 |
| 158 | + where_sql = f"WHERE {' AND '.join(filters)}" if filters else "" |
| 159 | + return where_sql, n |
| 160 | + |
| 161 | + |
| 162 | +def _hashtag_stats_sql(*, filter_dates: bool, filter_hashtags: bool) -> str: |
| 163 | + where_sql, n = _changeset_filters_sql(filter_dates=filter_dates, filter_hashtags=filter_hashtags) |
| 164 | + limit_param = f"${n}" |
| 165 | + offset_param = f"${n + 1}" |
| 166 | + map_changes = _map_changes_expr() |
| 167 | + return f""" |
| 168 | + WITH hashtag_scope AS ( |
| 169 | + SELECT |
| 170 | + ht.hashtag, |
| 171 | + st.uid, |
| 172 | + st.changeset_id, |
| 173 | + ({map_changes}) AS map_changes |
| 174 | + FROM changesets cs |
| 175 | + JOIN changeset_stats st ON st.changeset_id = cs.changeset_id |
| 176 | + CROSS JOIN LATERAL UNNEST(cs.hashtags) AS ht(hashtag) |
| 177 | + {where_sql} |
| 178 | + ), |
| 179 | + hashtag_totals AS ( |
| 180 | + SELECT |
| 181 | + hashtag, |
| 182 | + COUNT(DISTINCT changeset_id) AS changesets, |
| 183 | + COUNT(DISTINCT uid) AS users, |
| 184 | + COALESCE(SUM(map_changes), 0) AS map_changes |
| 185 | + FROM hashtag_scope |
| 186 | + GROUP BY hashtag |
| 187 | + ) |
| 188 | + SELECT |
| 189 | + hashtag, |
| 190 | + changesets, |
| 191 | + users, |
| 192 | + map_changes, |
| 193 | + ROW_NUMBER() OVER (ORDER BY map_changes DESC, hashtag ASC) AS rank |
| 194 | + FROM hashtag_totals |
| 195 | + ORDER BY map_changes DESC, hashtag ASC |
| 196 | + LIMIT {limit_param} OFFSET {offset_param} |
| 197 | + """ |
| 198 | + |
| 199 | + |
| 200 | +def _hashtag_trends_sql(*, filter_hashtags: bool) -> str: |
| 201 | + where_sql, n = _changeset_filters_sql(filter_dates=True, filter_hashtags=filter_hashtags) |
| 202 | + interval_param = f"${n}" |
| 203 | + limit_param = f"${n + 1}" |
| 204 | + offset_param = f"${n + 2}" |
| 205 | + map_changes = _map_changes_expr() |
| 206 | + return f""" |
| 207 | + SELECT |
| 208 | + DATE_TRUNC({interval_param}, cs.created_at) AS period_start, |
| 209 | + ht.hashtag, |
| 210 | + COUNT(DISTINCT st.changeset_id) AS changesets, |
| 211 | + COUNT(DISTINCT st.uid) AS users, |
| 212 | + COALESCE(SUM({map_changes}), 0) AS map_changes |
| 213 | + FROM changesets cs |
| 214 | + JOIN changeset_stats st ON st.changeset_id = cs.changeset_id |
| 215 | + CROSS JOIN LATERAL UNNEST(cs.hashtags) AS ht(hashtag) |
| 216 | + {where_sql} |
| 217 | + GROUP BY period_start, ht.hashtag |
| 218 | + ORDER BY period_start ASC, map_changes DESC, ht.hashtag ASC |
| 219 | + LIMIT {limit_param} OFFSET {offset_param} |
| 220 | + """ |
| 221 | + |
| 222 | + |
| 223 | +def _editor_stats_sql(*, filter_dates: bool) -> str: |
| 224 | + where_sql, n = _changeset_filters_sql(filter_dates=filter_dates) |
| 225 | + limit_param = f"${n}" |
| 226 | + offset_param = f"${n + 1}" |
| 227 | + map_changes = _map_changes_expr() |
| 228 | + return f""" |
| 229 | + WITH editor_scope AS ( |
| 230 | + SELECT |
| 231 | + COALESCE(NULLIF(cs.editor, ''), 'unknown') AS editor, |
| 232 | + st.uid, |
| 233 | + st.changeset_id, |
| 234 | + ({map_changes}) AS map_changes |
| 235 | + FROM changesets cs |
| 236 | + JOIN changeset_stats st ON st.changeset_id = cs.changeset_id |
| 237 | + {where_sql} |
| 238 | + ), |
| 239 | + editor_totals AS ( |
| 240 | + SELECT |
| 241 | + editor, |
| 242 | + COUNT(DISTINCT changeset_id) AS changesets, |
| 243 | + COUNT(DISTINCT uid) AS users, |
| 244 | + COALESCE(SUM(map_changes), 0) AS map_changes |
| 245 | + FROM editor_scope |
| 246 | + GROUP BY editor |
| 247 | + ) |
| 248 | + SELECT |
| 249 | + editor, |
| 250 | + changesets, |
| 251 | + users, |
| 252 | + map_changes, |
| 253 | + ROW_NUMBER() OVER (ORDER BY map_changes DESC, editor ASC) AS rank |
| 254 | + FROM editor_totals |
| 255 | + ORDER BY map_changes DESC, editor ASC |
| 256 | + LIMIT {limit_param} OFFSET {offset_param} |
| 257 | + """ |
| 258 | + |
| 259 | + |
139 | 260 | async def fetch_state() -> dict[str, Any] | None: |
140 | 261 | # last_ts/last_seq come from the worst-lagging source (slowest source bounds real freshness); |
141 | 262 | # updated_at is the most recent heartbeat across all sources (any tick proves the worker is alive). |
@@ -175,3 +296,66 @@ async def fetch_user_stats( |
175 | 296 | async with get_pool().acquire() as conn: |
176 | 297 | rows = await conn.fetch(sql, *params) |
177 | 298 | return [dict(row) for row in rows] |
| 299 | + |
| 300 | + |
| 301 | +async def fetch_hashtag_stats( |
| 302 | + *, |
| 303 | + start: datetime | None = None, |
| 304 | + end: datetime | None = None, |
| 305 | + hashtag: list[str] | None = None, |
| 306 | + limit: int = 100, |
| 307 | + offset: int = 0, |
| 308 | +) -> list[dict[str, Any]]: |
| 309 | + filter_dates = start is not None and end is not None |
| 310 | + filter_hashtags = bool(hashtag) |
| 311 | + sql = _hashtag_stats_sql(filter_dates=filter_dates, filter_hashtags=filter_hashtags) |
| 312 | + params: list[Any] = [] |
| 313 | + if filter_dates: |
| 314 | + params.extend([start, end]) |
| 315 | + if filter_hashtags: |
| 316 | + params.append(hashtag) |
| 317 | + params.extend([limit, offset]) |
| 318 | + |
| 319 | + async with get_pool().acquire() as conn: |
| 320 | + rows = await conn.fetch(sql, *params) |
| 321 | + return [dict(row) for row in rows] |
| 322 | + |
| 323 | + |
| 324 | +async def fetch_hashtag_trends( |
| 325 | + *, |
| 326 | + start: datetime, |
| 327 | + end: datetime, |
| 328 | + interval: str, |
| 329 | + hashtag: list[str] | None = None, |
| 330 | + limit: int = 1000, |
| 331 | + offset: int = 0, |
| 332 | +) -> list[dict[str, Any]]: |
| 333 | + filter_hashtags = bool(hashtag) |
| 334 | + sql = _hashtag_trends_sql(filter_hashtags=filter_hashtags) |
| 335 | + params: list[Any] = [start, end] |
| 336 | + if filter_hashtags: |
| 337 | + params.append(hashtag) |
| 338 | + params.extend([interval, limit, offset]) |
| 339 | + |
| 340 | + async with get_pool().acquire() as conn: |
| 341 | + rows = await conn.fetch(sql, *params) |
| 342 | + return [dict(row) for row in rows] |
| 343 | + |
| 344 | + |
| 345 | +async def fetch_editor_stats( |
| 346 | + *, |
| 347 | + start: datetime | None = None, |
| 348 | + end: datetime | None = None, |
| 349 | + limit: int = 100, |
| 350 | + offset: int = 0, |
| 351 | +) -> list[dict[str, Any]]: |
| 352 | + filter_dates = start is not None and end is not None |
| 353 | + sql = _editor_stats_sql(filter_dates=filter_dates) |
| 354 | + params: list[Any] = [] |
| 355 | + if filter_dates: |
| 356 | + params.extend([start, end]) |
| 357 | + params.extend([limit, offset]) |
| 358 | + |
| 359 | + async with get_pool().acquire() as conn: |
| 360 | + rows = await conn.fetch(sql, *params) |
| 361 | + return [dict(row) for row in rows] |
0 commit comments