# mohammedaltanib/dnscope architecture

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

- Interactive diagram: https://gitdiagram.com/mohammedaltanib/dnscope
- Repository: https://github.com/mohammedaltanib/dnscope

## Overview

DNScope is a local, batch-oriented Python analysis pipeline for DNS Internet Background Radiation in PCAP/PCAPNG data. The architecture is file-driven: raw capture files are transformed step-by-step into a merged analytical dataset, then into tables and figures for three research questions.

1. **Primary boundary: raw packet data vs. analytical data**
   - Input starts as compressed archives or PCAP/PCAPNG files from the Cloud Telescope dataset.
   - Output ends as Parquet datasets plus CSV tables and PNG charts.
   - The repo is centered on moving data across these storage formats rather than serving an API or web UI.

2. **Stage 1: packet extraction**
   - `fast_extract.py` is the ingest boundary.
   - It uses `tshark` externally to pull DNS-relevant fields from PCAPs into temporary CSV files.
   - This stage is where low-level packet captures are reduced to structured event rows.

3. **Stage 2: column enrichment and format normalization**
   - `csv_to_parquet_addcols.py` converts the extracted CSVs into Parquet.
   - It also adds derived fields used later in analysis:
     - `src_24` for /24 source network aggregation
     - `eTLD+1` for domain-level grouping
   - This is the first semantic normalization layer in the pipeline.

4. **Stage 3: dataset consolidation**
   - `merge.py` combines all Parquet fragments into one unified merged dataset.
   - This merged Parquet file is the main analytical substrate for the rest of the repo.
   - The architecture assumes many per-file intermediates feeding one consolidated corpus.

5. **Stage 4: analysis engine**
   - `Results.py` is the central analysis module and the main reusable entry point.
   - It exposes a `dnscope_mini(...)` function for notebook or script use, and generates the outputs for all three research questions.
   - This module reads the merged Parquet and writes summary tables and figures.

6. **Research question modules are logical slices, not separate services**
   - `RQ1.py`, `RQ2.py`, and `RQ3.py` appear to isolate analysis logic per question.
   - Their responsibilities map to the README:
     - RQ1: DNS share across regions and time
     - RQ2: query types and response codes
     - RQ3: source networks and geographic distribution
   - `Results.py` likely orchestrates or aggregates these analyses into one run.

7. **Visualization boundary**
   - `dnscope_mini_figs.py` produces figures from the merged Parquet or downstream tables.
   - This is a presentation layer separate from the extraction and transformation stages.
   - The repo outputs static PNGs, not interactive dashboards.

8. **Output structure is deterministic and file-based**
   - The pipeline writes into a run directory such as `run_out/`.
   - Two main output families exist:
     - `tables/` for CSV summaries
     - `figs/` for charts
   - This makes the diagram easy: one input dataset fan-outs into multiple analytical artifacts.

9. **Core technologies**
   - Python is the runtime for all repo scripts.
   - Key libraries named in the README shape the architecture:
     - `pandas` for tabular manipulation
     - `pyarrow` for Parquet I/O
     - `duckdb` for efficient local analytical queries
     - `matplotlib` for static figures
     - `tldextract` for domain decomposition
     - `geoip2` for geographic enrichment
   - `tshark` is an important external dependency for packet decoding.

10. **Operational model**
    - This is not a distributed system or long-running service.
    - It is a reproducible offline workflow meant to be run sequentially from the command line or inside Jupyter/Anaconda.
    - The main architectural dependency chain is:
      raw captures → CSV extraction → enriched Parquet → merged Parquet → tables/figures.

11. **Data semantics**
    - The analysis is DNS-centric rather than general packet analysis.
    - The pipeline preserves enough metadata to support time-series counts, query/rcode cross-tabs, source-prefix aggregation, and region-based comparisons.
    - Geographic and network grouping are derived analysis dimensions, not raw input properties.

12. **What to draw in an architecture diagram**
    - Show a left-to-right flow with four layers:
      - raw PCAP/ZIP sources
      - extraction/transformation scripts
      - merged Parquet analytical store
      - analysis/visualization outputs
    - Place `Results.py` as the main consumer of the merged dataset.
    - Treat `tshark`, `geoip2`, and `tldextract` as supporting dependencies on the extraction/analysis boundary, not as standalone subsystems.

Main entry points: [`Results.py`](https://github.com/mohammedaltanib/dnscope/blob/main/Results.py) (Results), [`fast_extract.py`](https://github.com/mohammedaltanib/dnscope/blob/main/fast_extract.py) (Fast extract), [`csv_to_parquet_addcols.py`](https://github.com/mohammedaltanib/dnscope/blob/main/csv_to_parquet_addcols.py) (CSV enrich), [`RQ1.py`](https://github.com/mohammedaltanib/dnscope/blob/main/RQ1.py) (RQ1)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_inputs["Raw inputs"]
  node_raw_captures[("PCAP data<br/>capture files")]
  node_tshark["tshark<br/>external decoder"]
end

subgraph group_prep["Preparation pipeline"]
  node_fast_extract["Fast extract<br/>ingest script<br/>[fast_extract.py]"]
  node_csv_enrich["CSV enrich<br/>normalize script"]
  node_merge_stage["Merge<br/>consolidation script<br/>[merge.py]"]
  node_merged_parquet[("Merged Parquet<br/>analytical store")]
end

subgraph group_analysis["Analysis layer"]
  node_python_libs["Data libs<br/>python dependencies"]
  node_results_core["Results<br/>analysis entrypoint<br/>[Results.py]"]
  node_rq1["RQ1<br/>analysis slice<br/>[RQ1.py]"]
  node_rq2["RQ2<br/>analysis slice"]
  node_rq3["RQ3<br/>analysis slice"]
  node_figs_module["Figures<br/>plotting module"]
end

subgraph group_outputs["Outputs"]
  node_tables_out[("Tables<br/>csv outputs")]
  node_figs_out[("Figures out<br/>png outputs")]
  node_run_dir[("Run output<br/>output directory")]
end

node_raw_captures -->|"input"| node_fast_extract
node_tshark -.->|"decode"| node_fast_extract
node_fast_extract -->|"CSV rows"| node_csv_enrich
node_csv_enrich -->|"Parquet fragments"| node_merge_stage
node_merge_stage -->|"write"| node_merged_parquet
node_merged_parquet -->|"read"| node_results_core
node_python_libs -.->|"supports"| node_results_core
node_python_libs -.->|"supports"| node_csv_enrich
node_merged_parquet -->|"analyze"| node_rq1
node_merged_parquet -->|"analyze"| node_rq2
node_merged_parquet -->|"analyze"| node_rq3
node_results_core -->|"orchestrates"| node_rq1
node_results_core -->|"orchestrates"| node_rq2
node_results_core -->|"orchestrates"| node_rq3
node_rq1 -->|"write tables"| node_tables_out
node_rq2 -->|"write tables"| node_tables_out
node_rq3 -->|"write tables"| node_tables_out
node_merged_parquet -->|"read"| node_figs_module
node_results_core -->|"triggers"| node_figs_module
node_figs_module -->|"write figures"| node_figs_out
node_tables_out -->|"contained in"| node_run_dir
node_figs_out -->|"contained in"| node_run_dir

click node_fast_extract "https://github.com/mohammedaltanib/dnscope/blob/main/fast_extract.py"
click node_csv_enrich "https://github.com/mohammedaltanib/dnscope/blob/main/csv_to_parquet_addcols.py"
click node_merge_stage "https://github.com/mohammedaltanib/dnscope/blob/main/merge.py"
click node_results_core "https://github.com/mohammedaltanib/dnscope/blob/main/Results.py"
click node_rq1 "https://github.com/mohammedaltanib/dnscope/blob/main/RQ1.py"
click node_figs_module "https://github.com/mohammedaltanib/dnscope/blob/main/dnscope_mini_figs.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_raw_captures,node_tshark toneBlue
class node_fast_extract,node_csv_enrich,node_merge_stage,node_merged_parquet toneAmber
class node_python_libs,node_results_core,node_rq1,node_rq2,node_rq3,node_figs_module toneMint
class node_tables_out,node_figs_out,node_run_dir toneRose
```

## Components

### Raw inputs

Source captures and external decoding dependency.

- **PCAP data**: capture files · Compressed archives and PCAP/PCAPNG inputs from the Cloud Telescope dataset.
- **tshark**: external decoder · External packet decoder used to extract DNS-relevant fields from captures.

### Preparation pipeline

Extraction, normalization, and dataset consolidation steps.

- [Fast extract](https://github.com/mohammedaltanib/dnscope/blob/main/fast_extract.py): ingest script · Reads raw captures and produces temporary CSV event rows. · `fast_extract.py`
- [CSV enrich](https://github.com/mohammedaltanib/dnscope/blob/main/csv_to_parquet_addcols.py): normalize script · Converts extracted CSVs to Parquet and adds derived columns such as src_24 and eTLD+1. · `csv_to_parquet_addcols.py`
- [Merge](https://github.com/mohammedaltanib/dnscope/blob/main/merge.py): consolidation script · Combines per-file Parquet fragments into one unified analytical dataset. · `merge.py`
- **Merged Parquet**: analytical store · Main corpus used by downstream analysis and visualization.

### Analysis layer

Main analytical entrypoint and question-specific logic.

- **Data libs**: python dependencies · Core libraries shaping the offline workflow: pandas, pyarrow, duckdb, tldextract, geoip2, matplotlib.
- [Results](https://github.com/mohammedaltanib/dnscope/blob/main/Results.py): analysis entrypoint · Central reusable analysis module; orchestrates the run and exposes dnscope_mini(...). · `Results.py`
- [RQ1](https://github.com/mohammedaltanib/dnscope/blob/main/RQ1.py): analysis slice · DNS share across regions and time. · `RQ1.py`
- **RQ2**: analysis slice · Query types and response codes.
- **RQ3**: analysis slice · Source networks and geographic distribution.
- [Figures](https://github.com/mohammedaltanib/dnscope/blob/main/dnscope_mini_figs.py): plotting module · Produces static PNG charts from merged data or downstream tables. · `dnscope_mini_figs.py`

### Outputs

Materialized tables and static figures.

- **Tables**: csv outputs · Run-directory CSV summaries written for the research questions.
- **Figures out**: png outputs · Run-directory static figures produced by the pipeline.
- **Run output**: output directory · Deterministic output tree such as run_out/ containing tables/ and figs/.

## Connections

- PCAP data → Fast extract: input · Raw captures are read by the extraction stage.
- tshark → Fast extract: decode · tshark performs DNS field extraction for fast_extract.
- Fast extract → CSV enrich: CSV rows · Temporary CSV event rows feed enrichment and Parquet conversion.
- CSV enrich → Merge: Parquet fragments · Enriched per-file Parquet outputs are consolidated.
- Merge → Merged Parquet: write · The merge stage materializes the unified analytical dataset.
- Merged Parquet → Results: read · Results consumes the merged Parquet corpus.
- Data libs → Results: supports · Analysis depends on tabular, query, geo, and plotting libraries.
- Data libs → CSV enrich: supports · Normalization and Parquet conversion use data libraries.
- Merged Parquet → RQ1: analyze · RQ1 computes DNS share by region and time.
- Merged Parquet → RQ2: analyze · RQ2 computes query-type and rcode distributions.
- Merged Parquet → RQ3: analyze · RQ3 computes source-network and geography summaries.
- Results → RQ1: orchestrates · Results coordinates the question-specific analysis modules.
- Results → RQ2: orchestrates · Results coordinates the question-specific analysis modules.
- Results → RQ3: orchestrates · Results coordinates the question-specific analysis modules.
- RQ1 → Tables: write tables · RQ1 contributes CSV summaries.
- RQ2 → Tables: write tables · RQ2 contributes CSV summaries.
- RQ3 → Tables: write tables · RQ3 contributes CSV summaries.
- Merged Parquet → Figures: read · Figure generation consumes the merged dataset or derived tables.
- Results → Figures: triggers · Results coordinates figure generation.
- Figures → Figures out: write figures · Static PNG charts are written to the figures output area.
- Tables → Run output: contained in · CSV summaries live under the run directory.
- Figures out → Run output: contained in · PNG figures live under the run directory.
