# waishnav/watcher architecture

> Architecture diagram of the GitHub repository waishnav/watcher, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/waishnav/watcher
- Repository: https://github.com/waishnav/watcher

## Overview

##### Project type and purpose
- This is a minimal open‑source, cross‑platform CLI application called “Watcher” that tracks screen‑time by polling the operating system for idle time (`xprintidle`) and active window events (`xdotool`), logs them, and provides summaries (day/week).

##### File structure
- Top level:
- install / uninstall / update scripts – OS‐level bootstrap and teardown.
- src/Watcher – Python package containing core logic.
- src/bin/watcher – CLI entry point.
- misc/export_to_2.0 – migration utilities for version 2.0 logfile format.
- src/Watcher sub‑modules:
- get_windows.py: invokes external tools to capture current window and idle time.
- time_operations.py: computes durations from timestamps.
- watch_log.py: appends events to a local logfile.
- analysis.py: reads logfile and aggregates data into day/week summaries.
- afk.py: (planned) filters out AFK time.
- commands.py: parses CLI arguments and dispatches to recording vs reporting.

##### Insights from README
- External dependencies: `xprintidle` and `xdotool`.
- Outputs: CLI reports with ASCII tables or simple text summaries.
- No database—logfile is the only persistence.
- Future: Go migration branch, GUI planned for stars.

##### Main components & interactions
a. Components:
1. User (CLI invocation)
2. CLI Frontend (`src/bin/watcher` + `commands.py`)
3. Core Processor (Watcher package modules)
- get_windows → polls OS
- time_operations → computes durations
- watch_log → writes to logfile
- analysis → reads logfile to produce reports
- afk → filters AFK time (future)
4. Local Logfile (e.g., `~/.watcher/log.txt`)
5. OS-level External Services (`xprintidle`, `xdotool`)
6. Bootstrap Scripts (`install`, `uninstall`, `update`)

b. Relationships & data flow:
- CLI Frontend → Core Processor modules (invoke record or report flows).
- get_windows ↔ External Tools (shell calls to gather window/idle time).
- get_windows → time_operations → watch_log → Logfile (persist events).
- analysis → Logfile (read persisted data) → commands → CLI (render summary).
- afk (in the pipeline) intercepts get_windows/time_operations to filter out idle.

c. Architectural patterns/design principles
- Modular separation of concerns: I/O (get_windows), processing (time_operations), persistence (watch_log), reporting (analysis), CLI (commands).
- Single source of truth: logfile as append‑only event store.
- CLI Front Controller: commands.py routes user actions to modules.

d. Key technologies/frameworks
- Python3 standard library
- External OS utilities: `xprintidle`, `xdotool`
- Bash scripts for install/uninstall/update

##### Detailed capturing of architecture
- Container “Watcher Python Package” containing: get_windows, time_operations, watch_log, analysis, afk.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User and Bootstrap
    User(["User"]):::external
    subgraph "Bootstrap Scripts"
        Install["install"]:::script
        Uninstall["uninstall"]:::script
        Update["update"]:::script
    end

    %% Migration Utilities
    subgraph "Migration Utilities"
        Migration["export_to_2.0"]:::script
    end

    %% CLI Frontend
    CLI("watcher CLI"):::frontend

    %% Watcher Python Package
    subgraph "Watcher Python Package"
        Commands["commands.py"]:::backend
        get_windows["get_windows.py"]:::backend
        TimeOps["time_operations.py"]:::backend
        WatchLog["watch_log.py"]:::backend
        Analysis["analysis.py"]:::backend
        AFK["afk.py"]:::future
    end

    %% External Tools and Persistence
    ExternalTools(("External Tools\nxprintidle, xdotool")):::external
    Logfile(("Local Logfile\n~/.watcher/log.txt")):::persistence

    %% Data Flows
    User -->|invoke CLI| CLI
    User -->|bootstrap| Install
    User -->|teardown| Uninstall
    User -->|upgrade| Update
    User -->|migrate logs| Migration

    CLI -->|parse & dispatch| Commands
    Commands -->|record flow| get_windows
    Commands -->|report flow| Analysis

    get_windows <-->|shell calls| ExternalTools
    get_windows -->|poll window+idle time| AFK
    AFK -->|filtered events| TimeOps
    TimeOps -->|compute duration| WatchLog
    WatchLog -->|write event| Logfile
    Analysis -->|read events| Logfile
    Analysis -->|aggregate data| Commands
    Commands -->|render summary| CLI

    %% Click Events
    click CLI "https://github.com/waishnav/watcher/tree/main/src/bin/watcher"
    click Commands "https://github.com/waishnav/watcher/blob/main/src/Watcher/commands.py"
    click get_windows "https://github.com/waishnav/watcher/blob/main/src/Watcher/get_windows.py"
    click TimeOps "https://github.com/waishnav/watcher/blob/main/src/Watcher/time_operations.py"
    click WatchLog "https://github.com/waishnav/watcher/blob/main/src/Watcher/watch_log.py"
    click Analysis "https://github.com/waishnav/watcher/blob/main/src/Watcher/analysis.py"
    click AFK "https://github.com/waishnav/watcher/blob/main/src/Watcher/afk.py"
    click Install "https://github.com/waishnav/watcher/tree/main/install"
    click Uninstall "https://github.com/waishnav/watcher/tree/main/uninstall"
    click Update "https://github.com/waishnav/watcher/tree/main/update"
    click Migration "https://github.com/waishnav/watcher/blob/main/misc/export_to_2.0"

    %% Styles
    classDef frontend fill:#D6EAF8,stroke:#3498DB;
    classDef backend fill:#D5F5E3,stroke:#27AE60;
    classDef external fill:#F2F3F4,stroke:#7F8C8D;
    classDef persistence fill:#FCF3CF,stroke:#F1C40F;
    classDef script fill:#FDEBD0,stroke:#E67E22;
    classDef future fill:#FADBD8,stroke:#E74C3C,stroke-dasharray:5 5;
```
