-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstore_downloads_snapshot.py
More file actions
440 lines (385 loc) · 13.9 KB
/
Copy pathstore_downloads_snapshot.py
File metadata and controls
440 lines (385 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env python3
"""Generate store_downloads.json from live PostHog analytics.
This snapshot feeds wiki_sync.py so dashboard download/user sections stay populated.
When store console export data is unavailable, we use PostHog lifecycle events as
the source of truth for current growth reporting.
"""
from __future__ import annotations
import argparse
import datetime as dt
import json
import os
import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
_POSTHOG_RETRYABLE_STATUS = frozenset({429, 502, 503, 504})
LIVE_EVENTS_PREDICATE = """
(
(
lower(coalesce(properties.environment, '')) IN ('production', 'live')
OR lower(coalesce(properties.build_audience, '')) = 'live'
)
AND lower(coalesce(properties.build_type, 'release')) != 'debug'
AND lower(coalesce(properties.runtime_target, 'device')) NOT IN ('simulator', 'emulator')
AND coalesce(toString(properties.is_internal), 'false') != 'true'
AND coalesce(toString(properties.distribution_channel), 'legacy') NOT IN (
'testflight', 'non_play_install', 'dev', 'emulator', 'simulator', 'ui_test'
)
)
"""
def _requests_module():
try:
import requests
return requests
except ImportError:
return None
def _posthog_backoff(attempt: int) -> None:
time.sleep(min(8.0, 2.0**attempt))
def _posthog_execute_once(
requests: Any,
url: str,
headers: Dict[str, str],
payload: Dict[str, Any],
timeout: float,
) -> Tuple[Optional[Dict[str, Any]], Optional[str], bool]:
"""POST once; returns (json_body, error_message, should_retry)."""
try:
response = requests.post(url, headers=headers, json=payload, timeout=timeout)
except requests.RequestException as exc:
return None, f"request_error: {exc}", True
if response.status_code >= 300:
code = response.status_code
return None, f"http_{code}", code in _POSTHOG_RETRYABLE_STATUS
try:
return response.json(), None, False
except Exception as exc:
return None, f"invalid_json: {exc}", True
def posthog_query(
query: str,
api_key: str,
project_id: str,
errors: List[str],
*,
timeout: float = 90.0,
max_retries: int = 3,
) -> Optional[Dict[str, Any]]:
"""Execute a HogQL query and return JSON payload.
Retries on transient PostHog/network failures (504s, timeouts) with backoff.
"""
requests = _requests_module()
if requests is None:
errors.append("missing_dependency: requests")
return None
url = f"https://us.posthog.com/api/projects/{project_id}/query/"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload = {"query": {"kind": "HogQLQuery", "query": query}}
for attempt in range(max_retries):
data, err, should_retry = _posthog_execute_once(requests, url, headers, payload, timeout)
if data is not None:
return data
final_attempt = attempt >= max_retries - 1
if final_attempt or not should_retry:
if err:
errors.append(err)
return None
_posthog_backoff(attempt)
return None
def query_scalar(query: str, api_key: str, project_id: str, errors: List[str]) -> int:
result = posthog_query(query, api_key, project_id, errors)
if not result or not result.get("results"):
return 0
row = result["results"][0]
if not row:
return 0
try:
return int(row[0] or 0)
except (TypeError, ValueError):
return 0
def query_rows(query: str, api_key: str, project_id: str, errors: List[str]) -> List[List[Any]]:
result = posthog_query(query, api_key, project_id, errors)
if not result:
return []
rows = result.get("results")
if not isinstance(rows, list):
return []
return rows
def _load_existing_payload(path: Path) -> Dict[str, Any]:
if not path.exists():
return {}
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
return {}
return payload if isinstance(payload, dict) else {}
def _metric_definitions() -> Dict[str, Any]:
return {
"downloads_30d": {
"display_name": "Distinct install users (30d)",
"source": "posthog",
"semantic_type": "proxy",
"description": (
"Distinct live-device users with an 'Application Installed' event over the trailing window. "
"This is a PostHog proxy metric, not store download truth."
),
}
}
def _data_quality(
*,
generated_at: str,
status: str,
errors: List[str],
preserved_previous_metrics: bool,
last_good_generated_at: str = "",
) -> Dict[str, Any]:
is_stale = status in {"degraded", "skipped"} and preserved_previous_metrics
return {
"is_stale": is_stale,
"preserved_previous_metrics": preserved_previous_metrics,
"last_attempt_generated_at": generated_at,
"last_good_generated_at": last_good_generated_at,
"reason": errors[-1] if errors else "",
}
def _preserve_previous_metrics(
existing: Dict[str, Any],
*,
generated_at: str,
status: str,
reason: str,
errors: List[str],
) -> Optional[Dict[str, Any]]:
if not existing:
return None
payload = dict(existing)
payload["generated_at"] = generated_at
payload["status"] = status
payload["status_reason"] = reason
payload["metric_definitions"] = _metric_definitions()
payload["query_diagnostics"] = {"errors": errors}
payload["data_quality"] = _data_quality(
generated_at=generated_at,
status=status,
errors=errors,
preserved_previous_metrics=True,
last_good_generated_at=str(existing.get("generated_at", "")),
)
return payload
def _empty_snapshot(window_days: int, reason: str = "") -> Dict[str, Any]:
return {
"generated_at": dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat(),
"window_days": window_days,
"source": "posthog",
"status": "skipped" if reason else "ok",
"status_reason": reason,
"ios": {"downloads_30d": 0},
"android": {"downloads_30d": 0, "active_installs": 0},
"combined": {"downloads_30d": 0},
"active_users": {"dau": 0, "wau": 0, "mau": 0},
"metric_definitions": _metric_definitions(),
"data_quality": {
"is_stale": False,
"preserved_previous_metrics": False,
"last_attempt_generated_at": "",
"last_good_generated_at": "",
"reason": "",
},
"query_diagnostics": {"errors": []},
"snapshots": [],
}
def run(repo_root: Path, days: int = 30) -> Dict[str, Any]:
output_path = repo_root / "marketing" / "data" / "store_downloads.json"
output_path.parent.mkdir(parents=True, exist_ok=True)
existing = _load_existing_payload(output_path)
generated_at = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat()
key = (
os.getenv("POSTHOG_PERSONAL_API_KEY", "").strip()
or os.getenv("POSTHOG_API_KEY", "").strip()
or os.getenv("posthog_api_key", "").strip()
)
project_id = os.getenv("POSTHOG_PROJECT_ID", "").strip()
errors: List[str] = []
payload = _empty_snapshot(days)
if not key or not project_id:
payload["generated_at"] = generated_at
payload["status"] = "skipped"
payload["status_reason"] = "missing POSTHOG_PERSONAL_API_KEY/POSTHOG_API_KEY or POSTHOG_PROJECT_ID"
preserved = _preserve_previous_metrics(
existing,
generated_at=generated_at,
status="skipped",
reason=payload["status_reason"],
errors=[],
)
if preserved is not None:
output_path.write_text(json.dumps(preserved, indent=2) + "\n", encoding="utf-8")
return {
"status": "skipped",
"output": str(output_path),
"reason": payload["status_reason"],
"preserved_previous_metrics": True,
}
payload["data_quality"] = _data_quality(
generated_at=generated_at,
status="skipped",
errors=[],
preserved_previous_metrics=False,
)
output_path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
return {"status": "skipped", "output": str(output_path), "reason": payload["status_reason"]}
installs_by_os = query_rows(
f"""
SELECT coalesce(properties.$os, properties.$os_name, 'Unknown') AS os, count(DISTINCT person_id) AS users
FROM events
WHERE event = 'Application Installed'
AND timestamp > now() - interval {days} day
AND {LIVE_EVENTS_PREDICATE}
GROUP BY os
ORDER BY users DESC
""",
key,
project_id,
errors,
)
ios_downloads = 0
android_downloads = 0
for row in installs_by_os:
os_name = str(row[0] or "").lower()
users = int(row[1] or 0)
if "ios" in os_name:
ios_downloads += users
elif "android" in os_name:
android_downloads += users
android_active_installs = query_scalar(
f"""
SELECT count(DISTINCT person_id)
FROM events
WHERE event = 'Application Opened'
AND (properties.$os = 'Android' OR properties.$os_name = 'Android')
AND timestamp > now() - interval {days} day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
dau = query_scalar(
f"""
SELECT count(DISTINCT person_id)
FROM events
WHERE event = 'Application Opened'
AND timestamp > now() - interval 1 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
wau = query_scalar(
f"""
SELECT count(DISTINCT person_id)
FROM events
WHERE event = 'Application Opened'
AND timestamp > now() - interval 7 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
mau = query_scalar(
f"""
SELECT count(DISTINCT person_id)
FROM events
WHERE event = 'Application Opened'
AND timestamp > now() - interval {days} day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
if errors:
preserved = _preserve_previous_metrics(
existing,
generated_at=generated_at,
status="degraded",
reason="preserved last good metrics after degraded PostHog query",
errors=errors,
)
if preserved is not None:
output_path.write_text(json.dumps(preserved, indent=2) + "\n", encoding="utf-8")
return {
"status": "degraded",
"output": str(output_path),
"ios_downloads_30d": int(preserved.get("ios", {}).get("downloads_30d", 0) or 0),
"android_downloads_30d": int(preserved.get("android", {}).get("downloads_30d", 0) or 0),
"combined_downloads_30d": int(preserved.get("combined", {}).get("downloads_30d", 0) or 0),
"dau": int(preserved.get("active_users", {}).get("dau", 0) or 0),
"wau": int(preserved.get("active_users", {}).get("wau", 0) or 0),
"mau": int(preserved.get("active_users", {}).get("mau", 0) or 0),
"query_errors_count": len(errors),
"preserved_previous_metrics": True,
}
payload = {
"generated_at": generated_at,
"window_days": days,
"source": "posthog",
"status": "ok" if not errors else "degraded",
"status_reason": "",
"ios": {"downloads_30d": ios_downloads},
"android": {
"downloads_30d": android_downloads,
"active_installs": android_active_installs,
},
"combined": {"downloads_30d": ios_downloads + android_downloads},
"active_users": {"dau": dau, "wau": wau, "mau": mau},
"metric_definitions": _metric_definitions(),
"data_quality": _data_quality(
generated_at=generated_at,
status="ok" if not errors else "degraded",
errors=errors,
preserved_previous_metrics=False,
),
"query_diagnostics": {"errors": errors},
"snapshots": [],
}
snapshots = existing.get("snapshots", [])
if isinstance(snapshots, list):
payload["snapshots"] = snapshots
snapshot = {
"timestamp": payload["generated_at"],
"ios_downloads_30d": ios_downloads,
"android_downloads_30d": android_downloads,
"combined_downloads_30d": ios_downloads + android_downloads,
"android_active_installs": android_active_installs,
"dau": dau,
"wau": wau,
"mau": mau,
}
if not errors:
payload["snapshots"].append(snapshot)
payload["snapshots"] = payload["snapshots"][-90:]
output_path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
return {
"status": payload["status"],
"output": str(output_path),
"ios_downloads_30d": ios_downloads,
"android_downloads_30d": android_downloads,
"combined_downloads_30d": ios_downloads + android_downloads,
"dau": dau,
"wau": wau,
"mau": mau,
"query_errors_count": len(errors),
}
def main() -> int:
parser = argparse.ArgumentParser(description="Generate store downloads snapshot from PostHog")
parser.add_argument("--repo-root", default=".", help="Repository root path")
parser.add_argument("--days", type=int, default=30, help="Rolling lookback window")
args = parser.parse_args()
result = run(Path(args.repo_root).resolve(), days=args.days)
print(json.dumps(result, indent=2))
return 0
if __name__ == "__main__":
raise SystemExit(main())