# miztiik/azure-fraud-detection-with-stream-analytics architecture

> Architecture diagram of the GitHub repository miztiik/azure-fraud-detection-with-stream-analytics, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/miztiik/azure-fraud-detection-with-stream-analytics
- Repository: https://github.com/miztiik/azure-fraud-detection-with-stream-analytics

## Overview

##### Project Type & Purpose
- This is a cloud-native, event-driven streaming pipeline built on Azure. Its purpose is to ingest high-volume “sale_event” and “inventory_event” messages, persist them for audit, filter for potential fraud (high discount + priority shipping), and route suspect events for further processing.

##### File Structure Highlights
- **app/**: Python Azure Functions (HTTP-triggered producers & Service-Bus-triggered consumers).
- **app/function_code/**: Local function projects with their function.json, host.json, requirements.txt.
- **deployment_scripts/**: Bash scripts to deploy/destroy the ARM/Bicep stack and generate test data.
- **main.bicep + modules/**: Bicep ARM templates that define Resource Group, VNet, Storage, Event Hub, Stream Analytics, Service Bus, Cosmos DB, Functions, Key Vault, Monitor, etc.
- **README.MD**: Describes end-to-end flow, sample events, Stream Analytics query, and Azure service interactions.

##### Core Components & Flow (to be represented
a. **Azure Functions – Event Producer**
- HTTP Trigger receives “count” param → generates simulated store events → publishes to Azure Event Hub
- Runs under a User-Assigned Managed Identity
b. **Azure Event Hub**
- Ingests events, partitions by event_type (even=inventory, odd=sale)
- **Capture** enabled: writes raw streams to Blob Storage (`all-events`) and Cosmos DB (audit)
c. **Azure Stream Analytics Job**
- **Input**: Event Hub Stream
- **Query**: T-SQL tumbling window filter where discount>50 AND priority_shipping=true AND event_type=‘sale_event’
- **Outputs**:
- Service Bus Queue (`fraud-detective-output-008`)
- Blob Storage container (`suspicious-events`)
d. **Azure Functions – Fraud Analyzer**
- Service Bus Queue Trigger picks up batched suspicious IDs → writes full events to Blob Storage (`fraud-events`)
e. **Data Stores**
- **Blob Storage**: raw capture (`all-events`), suspicious events, fraud-events
- **Cosmos DB**: raw audit store for queries and reconciliation
f. **Supporting Infrastructure**
- **Managed Identity** grants least-privilege access to Event Hub, Storage, Service Bus
- **Key Vault** holds secrets/config
- **Monitor (Log Analytics, Alerts)** watches metrics
- **Optional VM/VNet** modules (unused in this demo)

##### Relationships & Data Flow
1. HTTP Trigger → Event Hub
2. Event Hub → Capture → Blob & Cosmos
3. Event Hub → Stream Analytics → Service Bus Queue & suspicious-events blob
4. Service Bus Queue → Fraud Analyzer Function → fraud-events blob
- Managed Identity being used by Functions and Stream Analytics

##### Architectural Patterns & Principles
- **Event-Driven Architecture**: decoupled producers/consumers via Event Hub and Service Bus
- **Serverless & PaaS**: Functions + Stream Analytics minimize infrastructure management
- **Publish-Subscribe**: multiple consumers (capture, analytics) on the same event stream
- **Data Lake + Schema-On-Read**: raw events in Blob Storage & Cosmos DB for audit

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Serverless Functions"
        Producer["Event Producer Function (HTTP Trigger)"]:::func
        FraudAnalyzer["Fraud Analyzer Function (Service Bus Trigger)"]:::func
    end

    subgraph "Messaging & Analytics"
        EventHub["Azure Event Hub"]:::messaging
        StreamAnalytics["Stream Analytics Job: Fraud Filter"]:::analytics
        ServiceBus["Service Bus Queue: fraud-detective-output-008"]:::messaging
    end

    subgraph "Data Stores"
        BlobRaw["Blob Storage (all-events)"]:::storage
        Cosmos["Cosmos DB (audit store)"]:::db
        BlobSuspicious["Blob Storage (suspicious-events / fraud-events)"]:::storage
    end

    subgraph "Security & Monitoring"
        UAMI["Managed Identity (User-Assigned)"]:::infra
        KeyVault["Key Vault"]:::infra
        Monitor["Monitor (Log Analytics, Alerts)"]:::infra
    end

    subgraph "Infrastructure as Code"
        InfraCode["Bicep Templates (main & modules)"]:::docs
    end

    subgraph "Deployment Scripts"
        Scripts["deploy.sh / destroy.sh / generate_data.sh"]:::docs
    end

    Producer -->|"1.ingest events"| EventHub
    EventHub -->|"2a.capture raw"| BlobRaw
    EventHub -->|"2a.capture raw"| Cosmos
    EventHub -->|"3.stream input"| StreamAnalytics
    StreamAnalytics -->|"3a.output to queue"| ServiceBus
    StreamAnalytics -->|"3b.output to blob"| BlobSuspicious
    ServiceBus -->|"4.batch trigger"| FraudAnalyzer
    FraudAnalyzer -->|"5.write fraud events"| BlobSuspicious

    UAMI --> Producer
    UAMI --> FraudAnalyzer
    UAMI --> StreamAnalytics

    InfraCode --> EventHub
    InfraCode --> StreamAnalytics
    InfraCode --> ServiceBus
    InfraCode --> BlobRaw
    InfraCode --> Cosmos
    InfraCode --> BlobSuspicious
    InfraCode --> UAMI
    InfraCode --> KeyVault
    InfraCode --> Monitor

    Scripts --> InfraCode

    click Producer "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/tree/producer-refactoring/app/function_code/store-backend-ops/store-events-producer-fn"
    click FraudAnalyzer "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/tree/producer-refactoring/app/function_code/store-backend-ops/store-events-consumer-fn"
    click EventHub "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/modules/integration/create_event_hub.bicep"
    click BlobRaw "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/modules/storage/create_blob.bicep"
    click Cosmos "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/modules/database/cosmos.bicep"
    click StreamAnalytics "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/modules/integration/create_stream_analytics.bicep"
    click ServiceBus "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/modules/integration/create_svc_bus.bicep"
    click BlobSuspicious "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/modules/storage/create_blob.bicep"
    click UAMI "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/modules/identity/create_uami.bicep"
    click KeyVault "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/modules/keyvault/create_kv.bicep"
    click Monitor "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/modules/monitor/log_analytics_workspace.bicep"
    click Scripts "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/deployment_scripts/deploy.sh"
    click InfraCode "https://github.com/miztiik/azure-fraud-detection-with-stream-analytics/blob/producer-refactoring/main.bicep"

    classDef func fill:#dae8fc,stroke:#6c8ebf
    classDef messaging fill:#e1d5e7,stroke:#9673a6
    classDef analytics fill:#d5e8d4,stroke:#82b366
    classDef storage fill:#f8cecc,stroke:#b85450
    classDef db fill:#f8cecc,stroke:#b85450
    classDef infra fill:#eeeeee,stroke:#666666,stroke-dasharray:2 2
    classDef docs fill:#fff2cc,stroke:#d6b656
```
