# jkabutaro/usstock-csvdownloader architecture

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

- Interactive diagram: https://gitdiagram.com/jkabutaro/usstock-csvdownloader
- Repository: https://github.com/jkabutaro/usstock-csvdownloader

## Overview

This project is a command-line tool written in .NET (C#) designed for batch downloading US stock price data in CSV format from Yahoo Finance and other sources (e.g., SBI Securities). It includes features such as parallel downloads, data validation, error handling with retry mechanisms using Polly, caching via SQLite, and multiple symbol source integrations (S&P 500, NY Dow, Buffett, etc.).

##### Main Components
- Command Line Interface & Entry Point: The project’s main entry (Program.cs) handles command-line arguments and initializes processing.
- Options & Configuration: The Options directory contains classes (e.g., DownloadOptions, RetryOptions) for user-specified settings and configuration parameters.
- Services Layer: This directory is central to the application’s functionality. It includes:
- StockDataService, StockDownloadManager, various cache services (e.g., BuffettCacheService, IndexCacheService, NYDCacheService, SP500CacheService).
- Specific stock data fetchers (like SBIStockFetcher and the YahooFinance related services).
- Other service helpers such as ProgressManager and the SymbolListExportService.
- Models: Contains data transfer objects (StockData, StockSymbol, etc.) and serializer contexts that are used for both data handling and CSV generation.
- Exception Handling: A dedicated directory for custom exceptions (DataParsingException, StockDataExceptions) representing error states.
- Utilities: The Utils directory provides helper functions like CacheManager, PathUtils, logger extensions, and runtime/environment checkers (e.g., DotNetRuntimeChecker, WindowsVersionChecker).
- Tests: There is a full suite of tests under USStockDownloader.Tests, covering all major services and components.

##### Relationships and Interactions
- The CLI receives user input and parses command-line options, which are then mapped into Options classes.
- The Services layer handles business logic—for example, the StockDownloadManager coordinates data fetching by invoking specific services (YahooFinance, SBIStockFetcher, etc.) and utilizes the caching services to store retrieved data.
- Exception handling is deeply integrated. Custom exceptions get thrown and caught across service boundaries, while error logging is managed via utilities.
- The Utils and helpers are used throughout to support operational logic, such as checking system requirements at startup, managing logs, and handling file paths.
- Output components (CSV creation and SQLite caching) interact with both the models (data structure) and the file system.

##### Architectural Patterns and Design Principles
- Modular Design: Each functionality (data fetching, caching, options configuration, and logging) resides in its own module.
- Separation of Concerns: Services, Models, Options, and Utils are clearly separated, making unit testing and maintenance easier.
- Error Handling & Resilience: Use of Polly (for retries with exponential backoff) is a notable pattern used for robustness.
- Test-Driven: The presence of an extensive test suite implies adherence to testing best practices.

##### Technologies and Frameworks
- .NET 9.0 Runtime, Windows-specific features (e.g., WindowsVersionChecker).
- Polly library for retry policies.
- SQLite for caching data.
- Standard C# and .NET libraries for HTTP requests (likely HttpClient) wrapped by HttpClientFactoryWrapper.
- GitHub Actions workflows (dotnet.yml) for continuous integration/deployment.

##### Key Elements
- Use clear labels for each component (e.g., "Entry Point (Program.cs)", "Command Line Options", "StockDownloadManager", "YahooFinance Service", "Cache Manager / SQLite").
- Annotations such as “Retry (Polly)” near the HTTP and data fetching services to emphasize the built-in error handling mechanism.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Level: Entry Point
    CLI["Entry Point (Program.cs)"]:::cli

    %% Options & Configuration Subgraph
    subgraph "Options & Configuration"
        OptDownload["Download Options (DownloadOptions.cs)"]:::options
        OptRetry["Retry Options (RetryOptions.cs)"]:::options
    end

    %% Services Layer Subgraph
    subgraph "Services Layer"
        StockDataService["StockDataService"]:::services
        StockDownloadManager["StockDownloadManager"]:::services
        YahooFinanceService["YahooFinance Service"]:::services
        SBIStockFetcher["SBI Stock Fetcher"]:::services
        BuffettCacheService["Buffett Cache Service"]:::services
        IndexCacheService["Index Cache Service"]:::services
        NYDCacheService["NYD Cache Service"]:::services
        SP500CacheService["S&P 500 Cache Service"]:::services
        ProgressManager["Progress Manager"]:::services
        SymbolListExportService["Symbol List Export Service"]:::services
        HttpClientFactoryWrapper["HttpClient Factory Wrapper"]:::services
    end

    %% Other Core Components
    Models["Models (StockData, StockSymbol, etc.)"]:::models
    Exceptions["Exceptions (DataParsing, StockDataExceptions)"]:::exceptions
    Utils["Utils (CacheManager, PathUtils, LoggerExtensions, DotNetRuntimeChecker, WindowsVersionChecker)"]:::utils
    CSV["CSV Generation"]:::services
    SQLiteCache["SQLite Cache"]:::database
    Tests["Tests (USStockDownloader.Tests)"]:::tests

    %% External Systems
    ExternalYahoo["Yahoo Finance API"]:::external
    ExternalSBI["SBI Securities"]:::external

    %% Connections - Input Flow
    CLI --> OptDownload
    CLI --> OptRetry
    OptDownload --> StockDownloadManager
    OptRetry --> StockDownloadManager

    %% Connections - Services Interactions
    StockDownloadManager -->|"coordinates"| StockDataService
    StockDownloadManager -->|"fetches_YahooFinance"| YahooFinanceService
    StockDownloadManager -->|"fetches_SBI_Data"| SBIStockFetcher
    StockDownloadManager -->|"updates_progress"| ProgressManager
    StockDownloadManager -->|"exports_symbols"| SymbolListExportService
    StockDownloadManager -->|"caches_data"| BuffettCacheService
    StockDownloadManager -->|"caches_data"| IndexCacheService
    StockDownloadManager -->|"caches_data"| NYDCacheService
    StockDownloadManager -->|"caches_data"| SP500CacheService

    YahooFinanceService -->|"Polly_retry"| HttpClientFactoryWrapper
    YahooFinanceService -->|"fetch_data"| ExternalYahoo
    SBIStockFetcher -->|"fetch_data"| ExternalSBI

    StockDataService -->|"produces_models"| Models
    SymbolListExportService -->|"generates_csv"| CSV

    BuffettCacheService -->|"store"| SQLiteCache
    IndexCacheService -->|"store"| SQLiteCache
    NYDCacheService -->|"store"| SQLiteCache
    SP500CacheService -->|"store"| SQLiteCache

    StockDataService -->|"error_handling"| Exceptions
    StockDownloadManager -->|"utilizes"| Utils

    %% Tests connecting to major modules
    Tests --- CLI
    Tests --- StockDownloadManager
    Tests --- Models
    Tests --- Utils

    %% Class Definitions for Styling
    classDef cli fill:#F4CCCC,stroke:#000,stroke-width:1px;
    classDef options fill:#C9DAF8,stroke:#000,stroke-width:1px;
    classDef services fill:#D9EAD3,stroke:#000,stroke-width:1px;
    classDef models fill:#FFF2CC,stroke:#000,stroke-width:1px;
    classDef exceptions fill:#FCE5CD,stroke:#000,stroke-width:1px;
    classDef utils fill:#D0E0E3,stroke:#000,stroke-width:1px;
    classDef external fill:#EAD1DC,stroke:#000,stroke-width:1px;
    classDef database fill:#B6D7A8,stroke:#000,stroke-width:1px;
    classDef tests fill:#FFD966,stroke:#000,stroke-width:1px;

    %% Click Events for Component Mapping
    click CLI "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Program.cs"
    click OptDownload "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Options/DownloadOptions.cs"
    click OptRetry "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Options/RetryOptions.cs"
    click StockDataService "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/StockDataService.cs"
    click StockDownloadManager "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/StockDownloadManager.cs"
    click YahooFinanceService "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/YahooFinance/YahooFinanceLatestTradingDateService.cs"
    click SBIStockFetcher "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/SBIStockFetcher.cs"
    click BuffettCacheService "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/BuffettCacheService.cs"
    click IndexCacheService "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/IndexCacheService.cs"
    click NYDCacheService "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/NYDCacheService.cs"
    click SP500CacheService "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/SP500CacheService.cs"
    click ProgressManager "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/ProgressManager.cs"
    click SymbolListExportService "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/SymbolListExportService.cs"
    click HttpClientFactoryWrapper "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader/Services/HttpClientFactoryWrapper.cs"
    click Models "https://github.com/jkabutaro/usstock-csvdownloader/tree/main/USStockDownloader/Models"
    click Exceptions "https://github.com/jkabutaro/usstock-csvdownloader/tree/main/USStockDownloader/Exceptions"
    click Utils "https://github.com/jkabutaro/usstock-csvdownloader/tree/main/USStockDownloader/Utils"
    click Tests "https://github.com/jkabutaro/usstock-csvdownloader/blob/main/USStockDownloader.Tests"
```
