# narenderrai/cyberthreatanalyzer architecture

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

- Interactive diagram: https://gitdiagram.com/narenderrai/cyberthreatanalyzer
- Repository: https://github.com/narenderrai/cyberthreatanalyzer

## Overview

##### Project Type and Purpose
- This is a **full‐stack, single‐service** Streamlit application for cybersecurity threat research, analysis, and visualization.
- It integrates with the OpenAI GPT API and persists threat data in a local SQLite database.

##### File Structure
- Top‐level entrypoint: **main.py** (Streamlit UI)
- UI/Presentation components: **components/ui.py** and **components/visualization.py**
- Business logic modules:
- **utils/gpt_helper.py** (GPTHandler)
- **utils/threat_analyzer.py** (ThreatAnalyzer)
- **utils/database.py** (DB connector/ORM)
- Templates/prompts: **templates/prompts.py**
- Sample data for offline/testing: **data/sample_threats.py**
- Database file: **threat_database.db**
- Deployment/config: **.streamlit/config.toml**, **rep.nix**, **Docker/ECS instructions** in README

##### Extract Architecture Insights from the README
- Clearly defined layers: UI → GPTHandler → ThreatAnalyzer → Database
- Optional NewsScraper (not in file tree but mentioned) can be slotted beside GPTHandler
- Supports Docker, AWS ECS/Fargate, Replit deployments

##### Main Components
a. Presentation Layer
- User’s Web Browser (HTTP)
- Streamlit UI (main.py + components/)
b. Application/Service Layer
- GPTHandler (utils/gpt_helper.py) → calls OpenAI API
- ThreatAnalyzer (utils/threat_analyzer.py) → orchestrates analysis, tagging, formatting
- Templates/Prompt Engine (templates/prompts.py)
- (Optional) NewsScraper module
c. Data Layer
- SQLite database (threat_database.db) via utils/database.py
- Sample Data source (data/sample_threats.py) for testing
d. External Services
- OpenAI GPT API
- (Optional) News feed endpoints

##### Relationships and Data Flows
- User submits query → Streamlit UI
- UI invokes ThreatAnalyzer
- ThreatAnalyzer builds prompt (templates/prompts.py)
- Calls GPTHandler → external GPT API
- Receives analysis + tags
- Persists results via Database module
- UI fetches history/search results from DB
- UI renders both raw and formatted data (components/visualization.py)

##### Architectural Patterns
- **Layered Architecture**: clear separation of Presentation, Application, Data layers
- **Modular Design**: distinct modules for GPT integration, analysis logic, UI components
- **Single‐Service Container**: runs as one Streamlit process, deployable via Docker/ECS

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        Browser["User Browser"]:::external
        UI["Streamlit UI"]:::presentation
        UICmp["UI Components"]:::presentation
        VizCmp["Visualization Components"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        TA["ThreatAnalyzer Module"]:::business
        GH["GPTHandler Module"]:::business
        PE["Prompt Engine"]:::business
        NS["NewsScraper (Optional)"]:::business
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        DBC["DB Connector/ORM"]:::data
        DBFile["SQLite DB File"]:::data
        Sample["Sample Data Source"]:::data
    end

    %% External Services
    subgraph "External Services"
        direction TB
        GPTAPI["OpenAI GPT API"]:::external
        NewsAPI["News Feed (Optional)"]:::external
    end

    %% Deployment/Config
    subgraph "Deployment/Config"
        direction TB
        Conf[".streamlit/config.toml"]:::deployment
        Nix["rep.nix"]:::deployment
        Readme["README.md"]:::deployment
    end

    %% Connections
    Browser -->|"HTTP Request"| UI
    UI -->|"invoke analysis"| TA
    UI --> UICmp
    UI --> VizCmp
    TA -->|"build prompt"| PE
    TA -->|"call GPT"| GH
    PE --> GH
    GH -->|"API call"| GPTAPI
    GPTAPI --> GH
    GH --> TA
    TA -->|"persist results"| DBC
    DBC --> DBFile
    UI -->|"fetch history"| DBC
    DBC --> UI
    Sample -->|"test data"| TA
    NS -->|"fetch news"| NewsAPI
    NewsAPI --> NS
    NS --> TA

    %% Click Events
    click UI "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/main.py"
    click UICmp "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/components/ui.py"
    click VizCmp "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/components/visualization.py"
    click GH "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/utils/gpt_helper.py"
    click TA "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/utils/threat_analyzer.py"
    click DBC "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/utils/database.py"
    click PE "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/templates/prompts.py"
    click Sample "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/data/sample_threats.py"
    click DBFile "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/threat_database.db"
    click Conf "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/.streamlit/config.toml"
    click Nix "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/rep.nix"
    click Readme "https://github.com/narenderrai/cyberthreatanalyzer/blob/main/README.md"

    %% Styles
    classDef presentation fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef business fill:#90EE90,stroke:#333,stroke-width:1px;
    classDef data fill:#FFD580,stroke:#333,stroke-width:1px;
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px;
    classDef deployment fill:#D8BFD8,stroke:#333,stroke-width:1px;
```
