# vedanta/dfo architecture

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

- Interactive diagram: https://gitdiagram.com/vedanta/dfo
- Repository: https://github.com/vedanta/dfo

## Overview

1. **Overall shape**
   - `dfo` is a Python CLI for FinOps optimization, centered on a pipeline: `discover → analyze → report → execute`.
   - The architecture is layered and modular: Azure-specific data collection sits behind provider wrappers, then domain analysis and rules operate on local data, and execution writes audit state back to DuckDB.

2. **Entry points and command surface**
   - The CLI entrypoint is `src/dfo/cli.py`, with command groups implemented under `src/dfo/cmd/`.
   - Major user-facing areas are `azure`, `config`, `db`, `rules`, `status`, `version`, plus Azure subcommands for discovery, reporting, execution, and logs.

3. **Current provider boundary**
   - `src/dfo/providers/azure/` is the cloud adapter layer for Azure SDK access.
   - It is split by concern: compute inventory, monitor metrics, advisor, pricing, cost, resource graph, and a client wrapper.
   - This is the only implemented cloud provider in the repo, but the structure is clearly meant to be extensible to AWS/GCP later.

4. **Discovery and inventory**
   - `src/dfo/discover/vms.py` collects VM inventory and related telemetry from Azure.
   - `src/dfo/inventory/` holds query and formatting logic for browsing/filtering/searching inventory results.
   - Discovery feeds normalized resource data into the local database so later analysis and reporting do not need to hit Azure again.

5. **Analysis layer**
   - `src/dfo/analyze/` contains rule-like detectors for VM efficiency issues: idle VMs, low CPU, stopped VMs, and compute mapping.
   - These analyzers operate on stored inventory/metrics rather than calling Azure directly, which keeps the decision layer deterministic and testable.
   - The analysis output becomes the source of truth for reports and execution plans.

6. **Rules subsystem**
   - `src/dfo/rules/` stores JSON-based rule definitions such as `vm_rules.json` and `storage_rules.json`.
   - The repo docs indicate a “rules-driven CLI” design, so rules are treated as configurable optimization policy separate from code.
   - This makes the architecture policy-driven: detectors identify candidates, rules decide how they are categorized or acted upon.

7. **Local data store**
   - `src/dfo/db/` is the persistence boundary, using DuckDB as the local embedded database.
   - `schema.sql` and `init_data.sql` define the canonical local schema and seed data, while `duck.py` encapsulates database access.
   - The database is the central integration point for discoveries, findings, execution plans, and audit logs.

8. **Reporting pipeline**
   - `src/dfo/report/` collects analysis results and renders them to console, JSON, or CSV.
   - Formatters are separated by output channel: console output for interactive use, structured JSON for automation, CSV for exports.
   - This layer is read-only: it transforms stored findings into human- or machine-consumable views without changing domain state.

9. **Execution workflows**
   - `src/dfo/execute/` contains the action layer for making changes in Azure.
   - It supports two distinct paths: direct execution (`direct.py`, `azure_executor.py`, `stop_vms.py`) and plan-based execution (`plan_manager.py`, `approvals.py`, `rollback.py`).
   - Validators and the `azure_validator.py`/`validators.py` modules enforce safety checks before live changes happen.

10. **Safety and auditability**
   - Live operations default to dry-run, and the system requires explicit confirmation flags for real changes.
   - `action_logger.py` records every action, including dry-runs, into the local database for auditability.
   - Protection tags such as `dfo-protected=true` are part of the safety boundary that prevents accidental changes.

11. **Core services and shared models**
   - `src/dfo/core/` contains shared configuration, auth, and data models used across the system.
   - `config.py` and `auth.py` bridge environment/config settings with Azure credentials, while `models.py` provides common domain structures.
   - `src/dfo/common/` holds cross-cutting utilities like terminal formatting and visualizations.

12. **Testing and delivery**
   - The repo is heavily test-driven, with extensive unit and integration coverage under `src/dfo/tests/` plus top-level rule tests.
   - CI is GitHub Actions-based, and the project is packaged as a Python CLI using `pyproject.toml`.
   - Example scripts in `examples/` and Azure helper scripts in `tools/azure/` support manual validation and onboarding.

13. **How data flows end-to-end**
   - Azure credentials and config are loaded first, then discovery pulls VM and metric data into DuckDB.
   - Analysis and rules evaluate the stored data to produce findings.
   - Reporting reads findings back out of DuckDB.
   - Execution uses those findings or direct user input to trigger Azure actions, then logs results and, if needed, rollback/approval state locally.

14. **Architectural boundary to note in a diagram**
   - The key boundary is between external Azure services and the local decision system.
   - Everything on the left side of that boundary is Azure API interaction through provider wrappers; everything on the right side is local compute, storage, policy, and audit logic.
   - DuckDB is the shared backbone connecting discovery, analysis, reporting, and execution.

Main entry points: [`src/dfo/cli.py`](https://github.com/vedanta/dfo/blob/main/src/dfo/cli.py) (CLI entry), [`src/dfo/discover/vms.py`](https://github.com/vedanta/dfo/blob/main/src/dfo/discover/vms.py) (VM discovery), [`src/dfo/execute/execution.py`](https://github.com/vedanta/dfo/blob/main/src/dfo/execute/execution.py) (Execution), [`src/dfo/db/duck.py`](https://github.com/vedanta/dfo/blob/main/src/dfo/db/duck.py) (DuckDB store)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI surface"]
  node_src_dfo_cli["CLI entry<br/>entrypoint<br/>[cli.py]"]
  node_src_dfo_cmd_azure["Azure cmd<br/>subcommands<br/>[azure.py]"]
  node_src_dfo_cmd_execute["Execute cmd<br/>subcommands<br/>[azure_execute.py]"]
  node_src_dfo_cmd_config["Config cmd<br/>subcommands<br/>[config.py]"]
  node_src_dfo_cmd_db["DB cmd<br/>subcommands<br/>[db.py]"]
  node_src_dfo_cmd_rules["Rules cmd<br/>subcommands<br/>[rules.py]"]
  node_src_dfo_cmd_status["Status cmd<br/>subcommands<br/>[status.py]"]
  node_src_dfo_cmd_version["Version cmd<br/>subcommands<br/>[version.py]"]
end

subgraph group_azure["Azure boundary"]
  node_src_dfo_providers_azure_client["Azure client<br/>provider client<br/>[client.py]"]
  node_src_dfo_providers_azure_compute["Compute API<br/>provider adapter<br/>[compute.py]"]
  node_src_dfo_providers_azure_monitor["Monitor API<br/>provider adapter<br/>[monitor.py]"]
  node_src_dfo_providers_azure_advisor["Advisor API<br/>provider adapter<br/>[advisor.py]"]
  node_src_dfo_providers_azure_pricing["Pricing API<br/>provider adapter<br/>[pricing.py]"]
  node_src_dfo_providers_azure_cost["Cost API<br/>provider adapter<br/>[cost.py]"]
  node_src_dfo_providers_azure_resource_graph["Resource graph<br/>provider adapter<br/>[resource_graph.py]"]
end

subgraph group_local["Local pipeline"]
  node_src_dfo_discover_vms["VM discovery<br/>pipeline stage<br/>[vms.py]"]
  node_src_dfo_db_duck[("DuckDB store<br/>persistence<br/>[duck.py]")]
  node_src_dfo_analyze["Analysis<br/>detectors<br/>[__init__.py]"]
  node_src_dfo_rules["Rules<br/>policy<br/>[vm_rules.json]"]
  node_src_dfo_inventory["Inventory<br/>browse<br/>[queries.py]"]
  node_src_dfo_report["Reporting<br/>rendering<br/>[json_report.py]"]
  node_src_dfo_execute["Execution<br/>actions<br/>[execution.py]"]
  node_src_dfo_core["Core models<br/>shared core<br/>[models.py]"]
  node_src_dfo_common["Common utils<br/>shared utils<br/>[visualizations.py]"]
end

node_src_dfo_cli -->|"dispatches"| node_src_dfo_cmd_azure
node_src_dfo_cli -->|"dispatches"| node_src_dfo_cmd_execute
node_src_dfo_cli -->|"dispatches"| node_src_dfo_cmd_config
node_src_dfo_cli -->|"dispatches"| node_src_dfo_cmd_db
node_src_dfo_cli -->|"dispatches"| node_src_dfo_cmd_rules
node_src_dfo_cli -->|"dispatches"| node_src_dfo_cmd_status
node_src_dfo_cli -->|"dispatches"| node_src_dfo_cmd_version
node_src_dfo_cmd_azure -->|"starts"| node_src_dfo_discover_vms
node_src_dfo_cmd_azure -->|"renders"| node_src_dfo_report
node_src_dfo_cmd_execute -->|"invokes"| node_src_dfo_execute
node_src_dfo_discover_vms -->|"uses"| node_src_dfo_providers_azure_client
node_src_dfo_discover_vms -->|"reads"| node_src_dfo_providers_azure_compute
node_src_dfo_discover_vms -->|"reads"| node_src_dfo_providers_azure_monitor
node_src_dfo_discover_vms -->|"writes"| node_src_dfo_db_duck
node_src_dfo_analyze -->|"reads"| node_src_dfo_db_duck
node_src_dfo_analyze -->|"applies"| node_src_dfo_rules
node_src_dfo_inventory -->|"queries"| node_src_dfo_db_duck
node_src_dfo_report -->|"reads"| node_src_dfo_db_duck
node_src_dfo_report -->|"formats"| node_src_dfo_common
node_src_dfo_execute -->|"logs"| node_src_dfo_db_duck
node_src_dfo_execute -->|"uses"| node_src_dfo_providers_azure_client
node_src_dfo_execute -->|"acts on"| node_src_dfo_providers_azure_compute
node_src_dfo_execute -->|"validates"| node_src_dfo_providers_azure_monitor
node_src_dfo_execute -->|"shares"| node_src_dfo_core
node_src_dfo_discover_vms -->|"shares"| node_src_dfo_core
node_src_dfo_report -->|"shares"| node_src_dfo_core

click node_src_dfo_cli "https://github.com/vedanta/dfo/blob/main/src/dfo/cli.py"
click node_src_dfo_cmd_azure "https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/azure.py"
click node_src_dfo_cmd_execute "https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/azure_execute.py"
click node_src_dfo_cmd_config "https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/config.py"
click node_src_dfo_cmd_db "https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/db.py"
click node_src_dfo_cmd_rules "https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/rules.py"
click node_src_dfo_cmd_status "https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/status.py"
click node_src_dfo_cmd_version "https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/version.py"
click node_src_dfo_providers_azure_client "https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/client.py"
click node_src_dfo_providers_azure_compute "https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/compute.py"
click node_src_dfo_providers_azure_monitor "https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/monitor.py"
click node_src_dfo_providers_azure_advisor "https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/advisor.py"
click node_src_dfo_providers_azure_pricing "https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/pricing.py"
click node_src_dfo_providers_azure_cost "https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/cost.py"
click node_src_dfo_providers_azure_resource_graph "https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/resource_graph.py"
click node_src_dfo_discover_vms "https://github.com/vedanta/dfo/blob/main/src/dfo/discover/vms.py"
click node_src_dfo_db_duck "https://github.com/vedanta/dfo/blob/main/src/dfo/db/duck.py"
click node_src_dfo_analyze "https://github.com/vedanta/dfo/blob/main/src/dfo/analyze/__init__.py"
click node_src_dfo_rules "https://github.com/vedanta/dfo/blob/main/src/dfo/rules/vm_rules.json"
click node_src_dfo_inventory "https://github.com/vedanta/dfo/blob/main/src/dfo/inventory/queries.py"
click node_src_dfo_report "https://github.com/vedanta/dfo/blob/main/src/dfo/report/json_report.py"
click node_src_dfo_execute "https://github.com/vedanta/dfo/blob/main/src/dfo/execute/execution.py"
click node_src_dfo_core "https://github.com/vedanta/dfo/blob/main/src/dfo/core/models.py"
click node_src_dfo_common "https://github.com/vedanta/dfo/blob/main/src/dfo/common/visualizations.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_src_dfo_cli,node_src_dfo_cmd_azure,node_src_dfo_cmd_execute,node_src_dfo_cmd_config,node_src_dfo_cmd_db,node_src_dfo_cmd_rules,node_src_dfo_cmd_status,node_src_dfo_cmd_version toneBlue
class node_src_dfo_providers_azure_client,node_src_dfo_providers_azure_compute,node_src_dfo_providers_azure_monitor,node_src_dfo_providers_azure_advisor,node_src_dfo_providers_azure_pricing,node_src_dfo_providers_azure_cost,node_src_dfo_providers_azure_resource_graph toneAmber
class node_src_dfo_discover_vms,node_src_dfo_db_duck,node_src_dfo_analyze,node_src_dfo_rules,node_src_dfo_inventory,node_src_dfo_report,node_src_dfo_execute,node_src_dfo_core,node_src_dfo_common toneMint
```

## Components

### CLI surface

User-facing command entrypoints and subcommands

- [CLI entry](https://github.com/vedanta/dfo/blob/main/src/dfo/cli.py): entrypoint · Main command dispatcher for the application · `src/dfo/cli.py`
- [Azure cmd](https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/azure.py): subcommands · Azure-facing command group and discovery/reporting entrypoints · `src/dfo/cmd/azure.py`
- [Execute cmd](https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/azure_execute.py): subcommands · Execution command surface for direct and planned actions · `src/dfo/cmd/azure_execute.py`
- [Config cmd](https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/config.py): subcommands · Configuration command surface · `src/dfo/cmd/config.py`
- [DB cmd](https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/db.py): subcommands · Local database command surface · `src/dfo/cmd/db.py`
- [Rules cmd](https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/rules.py): subcommands · Rule inspection and policy command surface · `src/dfo/cmd/rules.py`
- [Status cmd](https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/status.py): subcommands · Status and health command surface · `src/dfo/cmd/status.py`
- [Version cmd](https://github.com/vedanta/dfo/blob/main/src/dfo/cmd/version.py): subcommands · Version command surface · `src/dfo/cmd/version.py`

### Azure boundary

Provider wrappers around Azure APIs

- [Azure client](https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/client.py): provider client · Shared Azure SDK client wrapper · `src/dfo/providers/azure/client.py`
- [Compute API](https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/compute.py): provider adapter · Azure VM and compute inventory access · `src/dfo/providers/azure/compute.py`
- [Monitor API](https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/monitor.py): provider adapter · Azure metrics and telemetry access · `src/dfo/providers/azure/monitor.py`
- [Advisor API](https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/advisor.py): provider adapter · Azure advisor recommendations access · `src/dfo/providers/azure/advisor.py`
- [Pricing API](https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/pricing.py): provider adapter · Azure pricing and SKU lookup access · `src/dfo/providers/azure/pricing.py`
- [Cost API](https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/cost.py): provider adapter · Azure cost data access · `src/dfo/providers/azure/cost.py`
- [Resource graph](https://github.com/vedanta/dfo/blob/main/src/dfo/providers/azure/resource_graph.py): provider adapter · Azure resource graph query access · `src/dfo/providers/azure/resource_graph.py`

### Local pipeline

Discovery, analysis, storage, reporting, and execution inside the repo

- [VM discovery](https://github.com/vedanta/dfo/blob/main/src/dfo/discover/vms.py): pipeline stage · Pulls Azure VM inventory and related telemetry into local storage · `src/dfo/discover/vms.py`
- [DuckDB store](https://github.com/vedanta/dfo/blob/main/src/dfo/db/duck.py): persistence · Embedded database for discoveries, findings, plans, and audit state · `src/dfo/db/duck.py`
- [Analysis](https://github.com/vedanta/dfo/blob/main/src/dfo/analyze/__init__.py): detectors · Deterministic detection layer for efficiency issues · `src/dfo/analyze/__init__.py`
- [Rules](https://github.com/vedanta/dfo/blob/main/src/dfo/rules/vm_rules.json): policy · JSON-based optimization policy and categorization rules · `src/dfo/rules/vm_rules.json`
- [Inventory](https://github.com/vedanta/dfo/blob/main/src/dfo/inventory/queries.py): browse · Query and formatting layer for stored inventory results · `src/dfo/inventory/queries.py`
- [Reporting](https://github.com/vedanta/dfo/blob/main/src/dfo/report/json_report.py): rendering · Formats stored findings for console, JSON, and CSV · `src/dfo/report/json_report.py`
- [Execution](https://github.com/vedanta/dfo/blob/main/src/dfo/execute/execution.py): actions · Validates, approves, executes, and rolls back Azure actions · `src/dfo/execute/execution.py`
- [Core models](https://github.com/vedanta/dfo/blob/main/src/dfo/core/models.py): shared core · Shared config, auth, and domain models · `src/dfo/core/models.py`
- [Common utils](https://github.com/vedanta/dfo/blob/main/src/dfo/common/visualizations.py): shared utils · Cross-cutting terminal and visualization helpers · `src/dfo/common/visualizations.py`

## Connections

- CLI entry → Azure cmd: dispatches · Main CLI routes into Azure command group
- CLI entry → Execute cmd: dispatches · Main CLI routes into execution commands
- CLI entry → Config cmd: dispatches · Main CLI routes into config commands
- CLI entry → DB cmd: dispatches · Main CLI routes into database commands
- CLI entry → Rules cmd: dispatches · Main CLI routes into rule commands
- CLI entry → Status cmd: dispatches · Main CLI routes into status commands
- CLI entry → Version cmd: dispatches · Main CLI routes into version command
- Azure cmd → VM discovery: starts · Azure command triggers VM discovery
- Azure cmd → Reporting: renders · Azure command exposes report views
- Execute cmd → Execution: invokes · Execution command hands off to workflow engine
- VM discovery → Azure client: uses · Discovery relies on shared Azure client wrapper
- VM discovery → Compute API: reads · Discovery pulls compute inventory
- VM discovery → Monitor API: reads · Discovery pulls metrics and telemetry
- VM discovery → DuckDB store: writes · Normalized discovery data is stored locally
- Analysis → DuckDB store: reads · Analysis evaluates stored local data
- Analysis → Rules: applies · Detectors are shaped by policy rules
- Inventory → DuckDB store: queries · Inventory browsing reads from DuckDB
- Reporting → DuckDB store: reads · Reports are generated from persisted findings
- Reporting → Common utils: formats · Report output uses shared presentation helpers
- Execution → DuckDB store: logs · Execution writes actions and audit state
- Execution → Azure client: uses · Execution uses Azure client access
- Execution → Compute API: acts on · Execution targets compute resources
- Execution → Monitor API: validates · Execution may consult telemetry for safety checks
- Execution → Core models: shares · Execution uses shared config and models
- VM discovery → Core models: shares · Discovery uses shared config and auth
- Reporting → Core models: shares · Reporting uses common models
