# yeldarby/nycerebro architecture

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

- Interactive diagram: https://gitdiagram.com/yeldarby/nycerebro
- Repository: https://github.com/yeldarby/nycerebro

## Overview

##### Project type and purpose
- This is a full‑stack, search‑driven monitoring app (“NYCerebro”) built with a Next.js frontend, serverless API routes, a Python/Node.js back‑end image‐processing pipeline, and external services (Roboflow, Supabase, Mapbox).
- Its goal is to let users enter a text query (e.g. “busy”), convert it to a CLIP embedding via Roboflow, compare it to live NYC camera embeddings in Supabase, and show the best live feed plus a heatmap.

##### Main components
a. Frontend (Next.js “v0-site”)
- Pages: home (`app/page.tsx`), search result (`app/search/page.tsx` + loading).
- UI components under `components/ui` and specialized components (HeatMap, LiveWebcam).
- Uses Mapbox JS for heatmap, tailwind for styling.
b. Serverless API (Next.js App Router)
- `/api/search`: accepts `q`, calls Roboflow, queries Supabase for top 25 embeddings.
- `/api/getResults`: returns best camera info and dynamic image URL.
- `/api/proxy-image`: streams/refetches raw camera frames to avoid CORS.
c. Image‐processing pipeline (resources folder)
- A Python script (`example_supabase_update_workflow_block.py`) scheduled or run in Roboflow custom block to upsert new camera embeddings.
- A Node.js helper or SQL scripts (`convert-to-sql.js`, `update_clip_sql.py`) to prepare or update Supabase schema/data.
d. External services
- Roboflow Workflow: accepts text (with dummy PNG), outputs CLIP embedding.
- Supabase: Postgres headless DB storing camera metadata, CLIP vectors, timestamps.
- Mapbox: client‐side heatmap visualization.
- NYC public camera streams: roughly 900 HLS/HTTP endpoints.

##### Relationships & data flows
- User → Frontend UI → Serverless `/api/search?q=`
→ (1) POST text to Roboflow Workflow → receives CLIP vector
→ (2) Query Supabase for nearest cameras by vector similarity
→ Returns metadata + camera IDs to frontend
→ Frontend renders LiveWebcam component by polling `/api/proxy-image?cameraId=` every 2 s.
→ Frontend renders Mapbox heatmap overlay using the geo‐coordinates from Supabase results.
- Separately: Python/Node.js pipeline (cron or Roboflow custom block)
→ Fetch latest frames from all NYC camera URLs
→ Send each to Roboflow for embedding (via InferenceHTTPClient)
→ Upsert camera ID, embedding vector, timestamp into Supabase (via Supabase client).

##### Architectural patterns & design principles
- Serverless microservices: Next.js API routes handle search logic.
- Event‐driven ingestion: scheduled or dataflow pipeline feeds Supabase.
- Separation of concerns: UI, API logic, and embedding‐generation pipeline are distinct.
- Vector search in Postgres (Supabase) using embeddings for nearest‐neighbor queries.
- CDN/edge deploy (Vercel) for Next.js, serverless functions, and static assets.

##### What
- Clear textual labels on each component
- Notes on timing: e.g., “frontend polls every 2 s”

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Browser
    subgraph "User (Browser)" 
        direction TB
        Browser["User Browser"]:::client
    end

    %% Frontend
    subgraph "Frontend (Next.js v0-site)"
        direction TB
        Home["Home Page"]:::client
        SearchPage["Search Results Page"]:::client
        Loading["Search Loading State"]:::client
        HeatMap["Heatmap Visualization"]:::client
        LiveCam["Live Webcam Feed"]:::client
        UILib["UI Library Components"]:::client
        MobileHook["Hook: use-mobile"]:::client
        ToastHook["Hook: use-toast"]:::client
        GlobalCSS1["globals.css (app)"]:::client
        GlobalCSS2["globals.css (styles)"]:::client
        TailwindCfg["tailwind.config.ts"]:::client

        Browser -->|Load pages| Home
        Home -->|Navigate| SearchPage
        SearchPage -->|renders| Loading
        SearchPage -->|renders| HeatMap
        SearchPage -->|renders| LiveCam
        SearchPage -->|uses| UILib
        SearchPage -->|uses| MobileHook
        SearchPage -->|uses| ToastHook
        SearchPage -->|styled by| GlobalCSS1
        SearchPage -->|styled by| GlobalCSS2
        SearchPage -->|styled by| TailwindCfg
    end

    Browser -->|search?q=…| APIsearch
    Browser -->|poll every 2s| APIproxy
    Browser -->|render heatmap| Mapbox

    %% Serverless API
    subgraph "Serverless API (Next.js)" 
        direction TB
        APIsearch["/api/search"]:::api
        APIget["/api/getResults"]:::api
        APIproxy["/api/proxy-image"]:::api
    end

    APIsearch -->|HTTP POST text+dummyPNG → embedding| Roboflow
    APIsearch -->|SQL SELECT top25 by vector similarity| SupaDB
    APIget -->|GET metadata & URL| SupaDB
    APIproxy -->|Fetch HLS frame| CameraStreams

    %% External SaaS
    subgraph "External SaaS"
        direction TB
        Roboflow["Roboflow Workflow"]:::saas
        SupaDB["Supabase DB (Postgres + vector index)"]:::saas
        Mapbox["Mapbox (Heatmap)"]:::saas
    end

    %% Passive Data Source
    subgraph "Passive Data Source"
        CameraStreams["NYC Camera Streams (~900 HLS endpoints)"]:::data
    end

    %% Image-Processing Pipeline
    subgraph "Image-Processing Pipeline (resources)"
        direction TB
        CamList["cameras.json"]:::pipeline
        PythonWorkflow["example_supabase_update_workflow_block.py"]:::pipeline
        ConvertSQL["convert-to-sql.js"]:::pipeline
        SQLSetup["create-table.sql"]:::pipeline
        UpdateSQL["update_clip_sql.py"]:::pipeline

        CameraStreams -->|fetch frames| PythonWorkflow
        PythonWorkflow -->|send frames → embedding| Roboflow
        PythonWorkflow -->|upsert embedding| SupaDB
    end

    %% Click Events
    click Home "https://github.com/yeldarby/nycerebro/blob/main/v0-site/app/page.tsx"
    click SearchPage "https://github.com/yeldarby/nycerebro/blob/main/v0-site/app/search/page.tsx"
    click Loading "https://github.com/yeldarby/nycerebro/blob/main/v0-site/app/search/loading.tsx"
    click HeatMap "https://github.com/yeldarby/nycerebro/blob/main/v0-site/components/HeatMap.tsx"
    click LiveCam "https://github.com/yeldarby/nycerebro/blob/main/v0-site/components/LiveWebcam.tsx"
    click UILib "https://github.com/yeldarby/nycerebro/tree/main/v0-site/components/ui/"
    click MobileHook "https://github.com/yeldarby/nycerebro/blob/main/v0-site/hooks/use-mobile.tsx"
    click ToastHook "https://github.com/yeldarby/nycerebro/blob/main/v0-site/hooks/use-toast.ts"
    click GlobalCSS1 "https://github.com/yeldarby/nycerebro/blob/main/v0-site/app/globals.css"
    click GlobalCSS2 "https://github.com/yeldarby/nycerebro/blob/main/v0-site/styles/globals.css"
    click TailwindCfg "https://github.com/yeldarby/nycerebro/blob/main/v0-site/tailwind.config.ts"
    click APIsearch "https://github.com/yeldarby/nycerebro/blob/main/v0-site/app/api/search/route.ts"
    click APIget "https://github.com/yeldarby/nycerebro/blob/main/v0-site/app/api/getResults/route.ts"
    click APIproxy "https://github.com/yeldarby/nycerebro/blob/main/v0-site/app/api/proxy-image/route.ts"
    click PythonWorkflow "https://github.com/yeldarby/nycerebro/blob/main/resources/example_supabase_update_workflow_block.py"
    click ConvertSQL "https://github.com/yeldarby/nycerebro/blob/main/resources/convert-to-sql.js"
    click SQLSetup "https://github.com/yeldarby/nycerebro/blob/main/resources/create-table.sql"
    click UpdateSQL "https://github.com/yeldarby/nycerebro/blob/main/resources/update_clip_sql.py"
    click CamList "https://github.com/yeldarby/nycerebro/blob/main/resources/cameras.json"

    %% Styles
    classDef client fill:#a8f0c6,stroke:#333,stroke-width:1px
    classDef api fill:#aedcff,stroke:#333,stroke-width:1px
    classDef saas fill:#ffd59e,stroke:#333,stroke-width:1px
    classDef data fill:#e0e0e0,stroke:#333,stroke-width:1px
    classDef pipeline fill:#f0d9ff,stroke:#333,stroke-width:1px
```
