# aces/data-query-tool architecture

> Architecture diagram of the GitHub repository aces/data-query-tool, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aces/data-query-tool
- Repository: https://github.com/aces/data-query-tool

## Overview

##### File structure
- Top-level project files:
- couchapp.json: metadata for Erica/Couchapp tooling
- rewrites.json: URL rewrite rules mapping friendly routes to design document functions
- filters/, views/, functions/: CouchDB design document components
- _attachments/: all static assets (HTML, JS, CSS, images, tests) that form the UI
- filters/: JavaScript functions for filtered replication or changes feeds
- views/: map/reduce functions for each data domain (categories, datadictionary, files, instruments, runlog, savedqueries, search, serverconfig, sessions)
- functions/: list.js, show.js, etc., which process view output or render documents
- _attachments/: contains:
– script/: UI JavaScript modules and unit tests (array_tools, query, user, UI modules)
– style/: CSS themes and layout styles
– index.html & help HTML pages
- No separate “backend” folder—CouchDB itself acts as the server, exposing all endpoints.

##### README for additional insights
- Confirms CouchDB ≥1.2 is required
- Installation is done via CouchDB replication (from central server) or Erica push
- Data is populated by external Loris import scripts (not part of this repo)
- Access point is the design document’s _rewrite endpoint

a. Main components to represent:
- Client Browser (UI)
- CouchDB Server (with the target database)
- CouchDB Design Document (“DQG-2.0”) containing:
– Static Assets (HTML/CSS/JS under _attachments)
– Views (map/reduce functions)
– Show/List/Update/Filter functions
– URL Rewrite Rules
- External CouchDB Instance (couchdb.loris.ca) for initial replication
- (Optionally) Loris Import Scripts / Admin tooling (Erica, PHP scripts)
b. Relationships & data flows:
- Browser issues HTTP GET/POST to CouchDB _rewrite endpoint → triggers show/list or serves static HTML/JS/CSS
- Browser fetches view results (JSON) from CouchDB via AJAX
- CouchDB serves map/reduce outputs or document attachments
- (Optional) Loris PHP scripts → push data into CouchDB via CouchDB HTTP API
c. Architectural patterns & design principles:
- Serverless Frontend: UI is purely static in CouchDB attachments
- RESTful HTTP API: CouchDB’s API serves as both data and static asset endpoints
- MapReduce Views: pre-aggregated queries via map/reduce functions
- Filtered replication: filters/ directory defines named filters for data sync
- /List functions: server-side rendering or JSON transformation of view rows
- URL Rewrites: friendly URLs mapped to CouchDB endpoints
d. Key technologies & frameworks:
- Apache CouchDB
- CouchDB design docs: views, shows, lists, filters, rewrites
- Erica (for dev push)
- JavaScript (client-side modules, Jasmine tests)
- jQuery UI themes for styling
- PHP import scripts (external Loris tools)

##### Detail
- Break out each view folder to show domain contexts (categories, search, sessions…)
- That list/show functions wrap view output for UI components
- Call out the rewrites.json mapping of friendly URLs
- Filters as logical nodes used by replication or changes feeds

## Diagram (Mermaid)

```mermaid
flowchart TD
    Browser["Browser UI"]:::frontend
    RemoteCouch["Remote Loris CouchDB"]:::external
    Import["Loris Import Scripts"]:::external
    Erica["Erica Dev Tool"]:::external

    subgraph "CouchDB Server"
        DB["Local CouchDB Database"]:::database
        subgraph "Design Document: DQG-2.0"
            Rewrites["URL Rewrite Rules"]:::code
            subgraph Attachments
                Index["index.html"]:::frontend
                HelpPages["help/*.html"]:::frontend
                ScriptsDir["script/"]:::frontend
                StyleDir["style/"]:::frontend
                VendorDir["vendor/"]:::frontend
            end
            subgraph Views
                Categories["views/categories/"]:::code
                DataDict["views/datadictionary/"]:::code
                FilesVs["views/files/"]:::code
                Instruments["views/instruments/"]:::code
                RunLogV["views/runlog/"]:::code
                SavedQueriesV["views/savedqueries/"]:::code
                SearchV["views/search/"]:::code
                ServerConfigV["views/serverconfig/"]:::code
                SessionsV["views/sessions/"]:::code
            end
            subgraph Filters
                IBIS["filters/IBISSites.js"]:::code
                RunLogsF["filters/RunLogs.js"]:::code
                SavedQF["filters/SavedQueries.js"]:::code
            end
            subgraph Functions
                ShowF["functions/show.js"]:::code
                ListF["functions/list.js"]:::code
                FilterF["functions/filter.js"]:::code
                UpdateF["functions/update.js"]:::code
                SpatialF["functions/spatial.js"]:::code
                ValidateF["functions/validate_doc_update.js"]:::code
                GenericMap["functions/map.js"]:::code
                GenericReduce["functions/reduce.js"]:::code
            end
        end
    end

    Browser -->|"_rewrite endpoint"| Rewrites
    Rewrites -->|serves| Index
    Rewrites -->|serves| HelpPages
    Rewrites -->|serves| ScriptsDir
    Rewrites -->|serves| StyleDir
    Rewrites -->|serves| VendorDir
    Browser -->|AJAX view queries| Categories
    Browser -->|AJAX view queries| DataDict
    Browser -->|AJAX view queries| FilesVs
    Browser -->|AJAX view queries| Instruments
    Browser -->|AJAX view queries| RunLogV
    Browser -->|AJAX view queries| SavedQueriesV
    Browser -->|AJAX view queries| SearchV
    Browser -->|AJAX view queries| ServerConfigV
    Browser -->|AJAX view queries| SessionsV
    Categories -->|map/reduce| DB
    DataDict -->|map/reduce| DB
    FilesVs -->|map/reduce| DB
    Instruments -->|map/reduce| DB
    RunLogV -->|map/reduce| DB
    SavedQueriesV -->|map/reduce| DB
    SearchV -->|map/reduce| DB
    ServerConfigV -->|map/reduce| DB
    SessionsV -->|map/reduce| DB
    DB -->|changes feed| IBIS
    DB -->|changes feed| RunLogsF
    DB -->|changes feed| SavedQF
    Browser -->|show/list functions| ShowF
    Browser -->|show/list functions| ListF
    ShowF -->|transforms| DB
    ListF -->|transforms| DB
    OtherFuncs[Other Functions]:::code
    FilterF -->|replication filter| DB
    UpdateF -->|update handlers| DB
    SpatialF -->|spatial indexing| DB
    ValidateF -->|validation| DB
    GenericMap -->|generic map| DB
    GenericReduce -->|generic reduce| DB

    RemoteCouch -->|replication| DB
    Import -->|HTTP API writes| DB
    Erica -->|push design doc| DB

    click Index "https://github.com/aces/data-query-tool/blob/master/_attachments/index.html"
    click HelpPages "https://github.com/aces/data-query-tool/tree/master/_attachments/help/"
    click ScriptsDir "https://github.com/aces/data-query-tool/tree/master/_attachments/script/"
    click StyleDir "https://github.com/aces/data-query-tool/tree/master/_attachments/style/"
    click VendorDir "https://github.com/aces/data-query-tool/tree/master/_attachments/vendor/"
    click Categories "https://github.com/aces/data-query-tool/tree/master/views/categories/"
    click DataDict "https://github.com/aces/data-query-tool/tree/master/views/datadictionary/"
    click FilesVs "https://github.com/aces/data-query-tool/tree/master/views/files/"
    click Instruments "https://github.com/aces/data-query-tool/tree/master/views/instruments/"
    click RunLogV "https://github.com/aces/data-query-tool/tree/master/views/runlog/"
    click SavedQueriesV "https://github.com/aces/data-query-tool/tree/master/views/savedqueries/"
    click SearchV "https://github.com/aces/data-query-tool/tree/master/views/search/"
    click ServerConfigV "https://github.com/aces/data-query-tool/tree/master/views/serverconfig/"
    click SessionsV "https://github.com/aces/data-query-tool/tree/master/views/sessions/"
    click IBIS "https://github.com/aces/data-query-tool/blob/master/filters/IBISSites.js"
    click RunLogsF "https://github.com/aces/data-query-tool/blob/master/filters/RunLogs.js"
    click SavedQF "https://github.com/aces/data-query-tool/blob/master/filters/SavedQueries.js"
    click ShowF "https://github.com/aces/data-query-tool/blob/master/functions/show.js"
    click ListF "https://github.com/aces/data-query-tool/blob/master/functions/list.js"
    click FilterF "https://github.com/aces/data-query-tool/blob/master/functions/filter.js"
    click UpdateF "https://github.com/aces/data-query-tool/blob/master/functions/update.js"
    click SpatialF "https://github.com/aces/data-query-tool/blob/master/functions/spatial.js"
    click ValidateF "https://github.com/aces/data-query-tool/blob/master/functions/validate_doc_update.js"
    click GenericMap "https://github.com/aces/data-query-tool/blob/master/functions/map.js"
    click GenericReduce "https://github.com/aces/data-query-tool/blob/master/functions/reduce.js"

    classDef frontend fill:#f9f,stroke:#333,stroke-width:1px
    classDef code fill:#bbf,stroke:#333,stroke-width:1px
    classDef database fill:#bfb,stroke:#333,stroke-width:1px
    classDef external fill:#fbb,stroke:#333,stroke-width:1px
```
