# aadilsalimm/logspy architecture

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

- Interactive diagram: https://gitdiagram.com/aadilsalimm/logspy
- Repository: https://github.com/aadilsalimm/logspy

## Overview

1. **Overall shape**
   - LogSpy is a desktop app, not a server-only web app. The native window is provided by `pywebview`, while the backend is a Flask application with Socket.IO for live updates.
   - The repository is split into two main product areas: **live anomaly monitoring** (`log_spy/`) and **interactive troubleshooting** (`troubleshooter/`).

2. **Top-level runtime**
   - `app.py` is the entry point and likely wires together the Flask server, Socket.IO event bridge, and the desktop window.
   - The UI is rendered from Jinja templates in `templates/` and enhanced by page-specific JavaScript/CSS in `static/`.

3. **Anomaly detection pipeline boundary**
   - `log_spy/main.py` acts as the controller/orchestrator for the log-monitoring engine.
   - It coordinates subprocesses and queues rather than doing all work in one thread, which is the main architectural boundary for the monitoring path.

4. **Log ingestion and pre-processing**
   - `log_spy/log_shipper.py` continuously tails `journalctl` output from the local Linux journal.
   - `log_spy/log_filter.py` removes noisy or low-value system messages before they reach the classifier, reducing model load and false positives.

5. **Local LLM classification**
   - `log_spy/log_classifier.py` is the anomaly decision point.
   - It calls a local Ollama-hosted fine-tuned Phi-3.5-mini model to classify buffered log batches as normal or anomalous and to extract a reason/component summary.

6. **Persistence layer**
   - `log_spy/db_ops.py` provides SQLite-backed storage for anomaly history.
   - This is the durable state of the monitoring subsystem: detected events are written here for later viewing and CRUD operations.

7. **Real-time UI update path**
   - When the controller receives a classification result, it is pushed to the frontend through Socket.IO events.
   - This creates a live monitoring loop: journal logs → filter → batch → classify → persist → push to UI.

8. **Troubleshooter subsystem**
   - The `troubleshooter/` package is a separate analysis pipeline focused on user-described issues rather than continuous monitoring.
   - `troubleshooter/troubleShooter.py` is the main orchestrator, and `log_fetcher.py` handles targeted journal queries.

9. **Cloud LLM-assisted diagnosis**
   - Troubleshooting uses a 3-stage flow: intent identification, log retrieval, and root-cause analysis.
   - The prompts in `intent_sys_prompt.txt` and `rc_sys_prompt.txt` guide Groq-hosted models, so this subsystem depends on cloud API keys in `troubleshooter/.env`.

10. **Frontend split**
    - There are two distinct dashboards: `templates/log_spy.html` and `templates/troubleshooter.html`.
    - Their JS/CSS assets are separated under `static/log_spy/` and `static/troubleshooter/`, which mirrors the backend split and helps draw the UI as two entry points into the same app shell.

11. **Key external dependencies that affect architecture**
    - `journalctl` and systemd are mandatory for log access.
    - Ollama provides the local inference runtime for anomaly classification.
    - Groq provides remote inference for the troubleshooting workflow.
    - SQLite is the only persistent datastore visible in the repository.

12. **Primary architectural diagram boundaries**
    - A useful diagram should show: local journal source on the left, a multiprocessing monitoring pipeline in the center, SQLite plus Socket.IO as shared backend services, and two frontend panels on the right.
    - The troubleshooting path should be drawn as a separate request-driven workflow that starts from user input and ends in cloud-LLM analysis, distinct from the continuous monitoring loop.

Main entry points: [`app.py`](https://github.com/aadilsalimm/logspy/blob/master/app.py) (App entry), [`log_spy/log_classifier.py`](https://github.com/aadilsalimm/logspy/blob/master/log_spy/log_classifier.py) (Log classifier), [`troubleshooter/troubleShooter.py`](https://github.com/aadilsalimm/logspy/blob/master/troubleshooter/troubleShooter.py) (Troubleshooter), [`log_spy/main.py`](https://github.com/aadilsalimm/logspy/blob/master/log_spy/main.py) (Monitor orchestrator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Desktop runtime"]
  node_app["App entry<br/>desktop shell<br/>[app.py]"]
end

subgraph group_monitoring["Live monitoring"]
  node_main_orchestrator["Monitor orchestrator<br/>pipeline controller<br/>[main.py]"]
  node_log_shipper["Log shipper<br/>journal tailer<br/>[log_shipper.py]"]
  node_log_filter["Log filter<br/>preprocessor<br/>[log_filter.py]"]
  node_log_classifier["Log classifier<br/>LLM analyzer<br/>[log_classifier.py]"]
end

subgraph group_troubleshoot["Troubleshooting"]
  node_ts_orchestrator["Troubleshooter<br/>analysis controller<br/>[troubleShooter.py]"]
  node_log_fetcher["Log fetcher<br/>journal query<br/>[log_fetcher.py]"]
  node_prompt_specs["Prompt specs<br/>LLM prompts"]
end

subgraph group_ui["Frontend"]
  node_log_spy_page["Log Spy UI<br/>dashboard view<br/>[log_spy.html]"]
  node_troubleshooter_page["Troubleshooter UI<br/>dashboard view"]
  node_log_spy_js["Spy script<br/>frontend logic<br/>[script.js]"]
  node_log_spy_css["Spy styles<br/>frontend style<br/>[style.css]"]
  node_ts_js["Troubleshoot script<br/>frontend logic<br/>[script.js]"]
  node_ts_css["Troubleshoot styles<br/>frontend style<br/>[style.css]"]
end

subgraph group_storage["Persistence"]
  node_db_ops[("Anomaly store<br/>SQLite persistence<br/>[db_ops.py]")]
end

node_journalctl[("System journal<br/>external source")]
node_ollama{{"Local model<br/>local LLM runtime"}}
node_groq{{"Groq API<br/>cloud LLM runtime"}}

node_app -->|"starts"| node_main_orchestrator
node_app -->|"exposes"| node_ts_orchestrator
node_app -->|"serves"| node_log_spy_page
node_app -->|"serves"| node_troubleshooter_page
node_log_spy_page -->|"uses"| node_log_spy_js
node_log_spy_page -->|"styles"| node_log_spy_css
node_troubleshooter_page -->|"uses"| node_ts_js
node_troubleshooter_page -->|"styles"| node_ts_css
node_journalctl -->|"streams"| node_log_shipper
node_main_orchestrator -->|"spawns"| node_log_shipper
node_main_orchestrator -->|"routes"| node_log_filter
node_main_orchestrator -->|"batches"| node_log_classifier
node_log_filter -->|"passes"| node_log_classifier
node_log_classifier -->|"queries"| node_ollama
node_log_classifier -->|"persists"| node_db_ops
node_log_classifier -.->|"emits"| node_app
node_db_ops -.->|"serves history"| node_app
node_ts_orchestrator -->|"requests"| node_log_fetcher
node_log_fetcher -->|"queries"| node_journalctl
node_ts_orchestrator -->|"guides"| node_prompt_specs
node_ts_orchestrator -->|"queries"| node_groq
node_ts_js -->|"submits"| node_ts_orchestrator
node_log_spy_js -.->|"receives events"| node_app
node_ts_js -.->|"receives results"| node_app

click node_app "https://github.com/aadilsalimm/logspy/blob/master/app.py"
click node_log_spy_page "https://github.com/aadilsalimm/logspy/blob/master/templates/log_spy.html"
click node_troubleshooter_page "https://github.com/aadilsalimm/logspy/blob/master/templates/troubleshooter.html"
click node_log_spy_js "https://github.com/aadilsalimm/logspy/blob/master/static/log_spy/script.js"
click node_log_spy_css "https://github.com/aadilsalimm/logspy/blob/master/static/log_spy/style.css"
click node_ts_js "https://github.com/aadilsalimm/logspy/blob/master/static/troubleshooter/script.js"
click node_ts_css "https://github.com/aadilsalimm/logspy/blob/master/static/troubleshooter/style.css"
click node_main_orchestrator "https://github.com/aadilsalimm/logspy/blob/master/log_spy/main.py"
click node_log_shipper "https://github.com/aadilsalimm/logspy/blob/master/log_spy/log_shipper.py"
click node_log_filter "https://github.com/aadilsalimm/logspy/blob/master/log_spy/log_filter.py"
click node_log_classifier "https://github.com/aadilsalimm/logspy/blob/master/log_spy/log_classifier.py"
click node_db_ops "https://github.com/aadilsalimm/logspy/blob/master/log_spy/db_ops.py"
click node_ts_orchestrator "https://github.com/aadilsalimm/logspy/blob/master/troubleshooter/troubleShooter.py"
click node_log_fetcher "https://github.com/aadilsalimm/logspy/blob/master/troubleshooter/log_fetcher.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app toneBlue
class node_main_orchestrator,node_log_shipper,node_log_filter,node_log_classifier toneAmber
class node_ts_orchestrator,node_log_fetcher,node_prompt_specs toneMint
class node_log_spy_page,node_troubleshooter_page,node_log_spy_js,node_log_spy_css,node_ts_js,node_ts_css toneRose
class node_db_ops toneIndigo
class node_journalctl,node_ollama,node_groq toneNeutral
```

## Components

### Desktop runtime

App shell, Flask/Socket.IO backend, and webview entry point.

- [App entry](https://github.com/aadilsalimm/logspy/blob/master/app.py): desktop shell · Starts the Flask app, Socket.IO bridge, and native window. · `app.py`

### Live monitoring

Continuous anomaly detection pipeline for journal logs.

- [Monitor orchestrator](https://github.com/aadilsalimm/logspy/blob/master/log_spy/main.py): pipeline controller · Coordinates the live log-monitoring subprocesses and queues. · `log_spy/main.py`
- [Log shipper](https://github.com/aadilsalimm/logspy/blob/master/log_spy/log_shipper.py): journal tailer · Streams journalctl output from the local system journal. · `log_spy/log_shipper.py`
- [Log filter](https://github.com/aadilsalimm/logspy/blob/master/log_spy/log_filter.py): preprocessor · Drops noisy or low-value messages before classification. · `log_spy/log_filter.py`
- [Log classifier](https://github.com/aadilsalimm/logspy/blob/master/log_spy/log_classifier.py): LLM analyzer · Classifies buffered log batches as normal or anomalous using local inference. · `log_spy/log_classifier.py`

### Troubleshooting

Request-driven diagnosis workflow for user-reported issues.

- [Troubleshooter](https://github.com/aadilsalimm/logspy/blob/master/troubleshooter/troubleShooter.py): analysis controller · Coordinates the issue-analysis flow from user input to diagnosis. · `troubleshooter/troubleShooter.py`
- [Log fetcher](https://github.com/aadilsalimm/logspy/blob/master/troubleshooter/log_fetcher.py): journal query · Retrieves targeted journal entries for a specific troubleshooting case. · `troubleshooter/log_fetcher.py`
- **Prompt specs**: LLM prompts · System prompts that shape intent detection and root-cause analysis.

### Frontend

Two dashboard entry points and their static assets.

- [Log Spy UI](https://github.com/aadilsalimm/logspy/blob/master/templates/log_spy.html): dashboard view · Template for the live anomaly monitoring dashboard. · `templates/log_spy.html`
- [Troubleshooter UI](https://github.com/aadilsalimm/logspy/blob/master/templates/troubleshooter.html): dashboard view · Template for the interactive troubleshooting dashboard. · `templates/troubleshooter.html`
- [Spy script](https://github.com/aadilsalimm/logspy/blob/master/static/log_spy/script.js): frontend logic · Client-side behavior for live monitoring updates. · `static/log_spy/script.js`
- [Spy styles](https://github.com/aadilsalimm/logspy/blob/master/static/log_spy/style.css): frontend style · Styling for the monitoring dashboard. · `static/log_spy/style.css`
- [Troubleshoot script](https://github.com/aadilsalimm/logspy/blob/master/static/troubleshooter/script.js): frontend logic · Client-side behavior for the troubleshooting workflow. · `static/troubleshooter/script.js`
- [Troubleshoot styles](https://github.com/aadilsalimm/logspy/blob/master/static/troubleshooter/style.css): frontend style · Styling for the troubleshooting dashboard. · `static/troubleshooter/style.css`

### Persistence

SQLite-backed history for detected anomalies.

- [Anomaly store](https://github.com/aadilsalimm/logspy/blob/master/log_spy/db_ops.py): SQLite persistence · Stores anomaly history for later viewing and CRUD operations. · `log_spy/db_ops.py`

### Other components

- **System journal**: external source · systemd journal access required for live monitoring and targeted retrieval.
- **Local model**: local LLM runtime · Hosts the Phi-3.5-mini model used for anomaly classification.
- **Groq API**: cloud LLM runtime · Remote inference backend used for troubleshooting analysis.

## Connections

- App entry → Monitor orchestrator: starts · Bootstraps the live monitoring pipeline.
- App entry → Troubleshooter: exposes · Makes the troubleshooting workflow available in the same app shell.
- App entry → Log Spy UI: serves · Renders the monitoring dashboard.
- App entry → Troubleshooter UI: serves · Renders the troubleshooting dashboard.
- Log Spy UI → Spy script: uses · Page behavior is driven by its client script.
- Log Spy UI → Spy styles: styles · Monitoring UI styling.
- Troubleshooter UI → Troubleshoot script: uses · Page behavior is driven by its client script.
- Troubleshooter UI → Troubleshoot styles: styles · Troubleshooting UI styling.
- System journal → Log shipper: streams · Live journal output feeds the monitoring pipeline.
- Monitor orchestrator → Log shipper: spawns · Runs the tailing worker as part of the monitoring loop.
- Monitor orchestrator → Log filter: routes · Sends batches through the noise filter.
- Monitor orchestrator → Log classifier: batches · Hands buffered logs to the classifier.
- Log filter → Log classifier: passes · Filtered logs become classification input.
- Log classifier → Local model: queries · Uses local inference for anomaly decisions.
- Log classifier → Anomaly store: persists · Writes anomaly results to SQLite history.
- Log classifier → App entry: emits · Sends live results back to the desktop/backend shell.
- Anomaly store → App entry: serves history · Provides stored anomaly records for UI actions.
- Troubleshooter → Log fetcher: requests · Fetches relevant logs as part of diagnosis.
- Log fetcher → System journal: queries · Pulls targeted entries from the system journal.
- Troubleshooter → Prompt specs: guides · Uses prompt templates for intent and root-cause reasoning.
- Troubleshooter → Groq API: queries · Sends analysis requests to the cloud model backend.
- Troubleshoot script → Troubleshooter: submits · User inputs trigger troubleshooting runs.
- Spy script → App entry: receives events · Consumes live monitoring updates from the backend.
- Troubleshoot script → App entry: receives results · Displays troubleshooting outcomes in the UI.
