|
| 1 | +"""`assembly telemetry` — inspect or change anonymous usage telemetry. |
| 2 | +
|
| 3 | +The collection itself lives in ``aai_cli/telemetry.py``; this command is the |
| 4 | +user-facing consent surface: see what would be sent and turn it off (or back |
| 5 | +on) persistently. The env kill-switches (``AAI_TELEMETRY_DISABLED=1``, |
| 6 | +``DO_NOT_TRACK=1``) always win over the persisted choice. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import typer |
| 12 | + |
| 13 | +from aai_cli import config, options, output, telemetry |
| 14 | +from aai_cli.context import AppState, run_command |
| 15 | +from aai_cli.help_text import examples_epilog |
| 16 | + |
| 17 | +app = typer.Typer(help="Anonymous usage telemetry: status, enable, disable.") |
| 18 | + |
| 19 | + |
| 20 | +def _consent_label() -> str: |
| 21 | + return "granted" if telemetry.consent_granted() else "denied" |
| 22 | + |
| 23 | + |
| 24 | +@app.command( |
| 25 | + epilog=examples_epilog( |
| 26 | + [ |
| 27 | + ("Show whether telemetry is active", "assembly telemetry status"), |
| 28 | + ("As JSON for scripting", "assembly telemetry status --json"), |
| 29 | + ] |
| 30 | + ) |
| 31 | +) |
| 32 | +def status( |
| 33 | + ctx: typer.Context, |
| 34 | + json_out: bool = options.json_option(), |
| 35 | +) -> None: |
| 36 | + """Show whether anonymous usage telemetry is active, and why.""" |
| 37 | + |
| 38 | + def body(_state: AppState, json_mode: bool) -> None: |
| 39 | + data: dict[str, object] = { |
| 40 | + "enabled": telemetry.is_enabled(), |
| 41 | + "consent": _consent_label(), |
| 42 | + "token_configured": bool(telemetry.client_token()), |
| 43 | + } |
| 44 | + |
| 45 | + def render(d: dict[str, object]) -> object: |
| 46 | + state_line = ( |
| 47 | + output.success("Telemetry is enabled.") |
| 48 | + if d["enabled"] |
| 49 | + else output.muted("Telemetry is disabled.") |
| 50 | + ) |
| 51 | + detail = output.muted( |
| 52 | + f"Consent: {d['consent']}. Intake token configured: " |
| 53 | + f"{'yes' if d['token_configured'] else 'no'}." |
| 54 | + ) |
| 55 | + hint = output.hint( |
| 56 | + "Opt out any time: 'assembly telemetry disable' or AAI_TELEMETRY_DISABLED=1." |
| 57 | + ) |
| 58 | + return output.stack(state_line, detail, hint) |
| 59 | + |
| 60 | + output.emit(data, render, json_mode=json_mode) |
| 61 | + |
| 62 | + run_command(ctx, body, json=json_out) |
| 63 | + |
| 64 | + |
| 65 | +@app.command( |
| 66 | + epilog=examples_epilog([("Re-enable telemetry", "assembly telemetry enable")]), |
| 67 | +) |
| 68 | +def enable( |
| 69 | + ctx: typer.Context, |
| 70 | + json_out: bool = options.json_option(), |
| 71 | +) -> None: |
| 72 | + """Re-enable anonymous usage telemetry for this machine.""" |
| 73 | + |
| 74 | + def body(_state: AppState, json_mode: bool) -> None: |
| 75 | + config.set_telemetry_enabled(enabled=True) |
| 76 | + output.emit( |
| 77 | + {"telemetry_enabled": True}, |
| 78 | + lambda _d: output.success("Telemetry enabled."), |
| 79 | + json_mode=json_mode, |
| 80 | + ) |
| 81 | + |
| 82 | + run_command(ctx, body, json=json_out) |
| 83 | + |
| 84 | + |
| 85 | +@app.command( |
| 86 | + hidden=True, |
| 87 | + epilog=examples_epilog( |
| 88 | + [ |
| 89 | + ( |
| 90 | + "Internal plumbing, spawned by the CLI itself", |
| 91 | + "assembly telemetry flush '<payload-json>'", |
| 92 | + ) |
| 93 | + ] |
| 94 | + ), |
| 95 | +) |
| 96 | +def flush( |
| 97 | + payload: str = typer.Argument(..., help="Serialized telemetry payload (internal)."), |
| 98 | +) -> None: |
| 99 | + """Deliver one serialized telemetry event to the intake (internal). |
| 100 | +
|
| 101 | + This is the detached flusher `telemetry.dispatch` spawns so user commands never |
| 102 | + wait on the network — an explicit, reviewable entry point rather than inline |
| 103 | + code. Hidden from help; runs with stdio discarded, so it neither needs nor |
| 104 | + produces output. |
| 105 | + """ |
| 106 | + telemetry.flush_payload(payload) |
| 107 | + |
| 108 | + |
| 109 | +@app.command( |
| 110 | + epilog=examples_epilog([("Opt out of telemetry", "assembly telemetry disable")]), |
| 111 | +) |
| 112 | +def disable( |
| 113 | + ctx: typer.Context, |
| 114 | + json_out: bool = options.json_option(), |
| 115 | +) -> None: |
| 116 | + """Opt out of anonymous usage telemetry for this machine.""" |
| 117 | + |
| 118 | + def body(_state: AppState, json_mode: bool) -> None: |
| 119 | + config.set_telemetry_enabled(enabled=False) |
| 120 | + output.emit( |
| 121 | + {"telemetry_enabled": False}, |
| 122 | + lambda _d: output.success("Telemetry disabled."), |
| 123 | + json_mode=json_mode, |
| 124 | + ) |
| 125 | + |
| 126 | + run_command(ctx, body, json=json_out) |
0 commit comments