|
| 1 | +"""Optional telemetry for Gramps Web API.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import time |
| 7 | +import uuid |
| 8 | + |
| 9 | +import requests |
| 10 | +from flask import current_app |
| 11 | + |
| 12 | +from gramps_webapi.api.cache import persistent_cache |
| 13 | +from gramps_webapi.auth.passwords import hash_password_salt |
| 14 | +from gramps_webapi.const import ( |
| 15 | + TELEMETRY_ENDPOINT, |
| 16 | + TELEMETRY_SERVER_ID_KEY, |
| 17 | + TELEMETRY_TIMESTAMP_KEY, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def send_telemetry(data: dict[str, str | int | float]) -> None: |
| 22 | + """Send telemetry""" |
| 23 | + response = requests.post(TELEMETRY_ENDPOINT, json=data, timeout=30) |
| 24 | + response.raise_for_status() # Raise exception for HTTP errors |
| 25 | + |
| 26 | + |
| 27 | +def should_send_telemetry() -> bool: |
| 28 | + """Whether telemetry should be sent.""" |
| 29 | + if current_app.config.get("DISABLE_TELEMETRY"): |
| 30 | + return False |
| 31 | + if os.getenv("FLASK_RUN_FROM_CLI"): |
| 32 | + # Flask development server, not a production environment (hopefully!) |
| 33 | + return False |
| 34 | + if (os.environ.get("PYTEST_CURRENT_TEST") or current_app.testing) and not os.getenv( |
| 35 | + "MOCK_TELEMETRY" |
| 36 | + ): |
| 37 | + # do not send telemetry during tests unless MOCK_TELEMETRY is set |
| 38 | + return False |
| 39 | + # only send telemetry if it has not been sent in the last 24 hours |
| 40 | + if time.time() - telemetry_last_sent() < 24 * 60 * 60: |
| 41 | + return False |
| 42 | + return True |
| 43 | + |
| 44 | + |
| 45 | +def telemetry_last_sent() -> float: |
| 46 | + """Timestamp when telemetry was last sent successfully.""" |
| 47 | + return persistent_cache.get(TELEMETRY_TIMESTAMP_KEY) or 0.0 |
| 48 | + |
| 49 | + |
| 50 | +def update_telemetry_timestamp() -> None: |
| 51 | + """Update the telemetry timestamp.""" |
| 52 | + persistent_cache.set(TELEMETRY_TIMESTAMP_KEY, time.time()) |
| 53 | + |
| 54 | + |
| 55 | +def generate_server_uuid() -> str: |
| 56 | + """Generate a random, unique server UUID.""" |
| 57 | + return uuid.uuid4().hex |
| 58 | + |
| 59 | + |
| 60 | +def generate_tree_uuid(tree_id: str, server_uuid: str) -> str: |
| 61 | + """Generate a unique tree UUID for the given tree ID and server UUID. |
| 62 | +
|
| 63 | + The tree UUID is uniquely determined for a given tree ID and server |
| 64 | + UUID but does not allow reconstructing the tree ID. |
| 65 | + """ |
| 66 | + return hash_password_salt(password=tree_id, salt=server_uuid.encode()).hex() |
| 67 | + |
| 68 | + |
| 69 | +def get_telemetry_payload(tree_id: str) -> dict[str, str | int | float]: |
| 70 | + """Get the telemetry payload for the given tree ID.""" |
| 71 | + if not tree_id: |
| 72 | + raise ValueError("Tree ID must not be empty") |
| 73 | + server_uuid = persistent_cache.get(TELEMETRY_SERVER_ID_KEY) |
| 74 | + if not server_uuid: |
| 75 | + server_uuid = generate_server_uuid() |
| 76 | + persistent_cache.set(TELEMETRY_SERVER_ID_KEY, server_uuid) |
| 77 | + tree_uuid = generate_tree_uuid(tree_id=tree_id, server_uuid=server_uuid) |
| 78 | + return { |
| 79 | + "server_uuid": server_uuid, |
| 80 | + "tree_uuid": tree_uuid, |
| 81 | + "timestamp": time.time(), |
| 82 | + } |
0 commit comments