This project provides a Python backend for the Chicago Crimes dataset task.
It covers:
- dataset structure understanding via metadata API
- relational storage workflow (raw table to clean typed table)
- at least 10 independent SQL analysis angles
- frontend-ready API contract for charts and text conclusions
crime_analytics_backend/
app/
core/
config.py
routers/
analysis.py
metadata.py
system.py
services/
analysis_service.py
static/
app.js
dashboard_snapshot.json
index.html
db.py
main.py
schemas.py
docs/
API.md
scripts/
build_dashboard_snapshot.py
opengauss_import.py
opengauss_prepare_clean.py
sql/
prepare_clean_table.sql
analysis_queries.sql
requirements.txt
start_server.py
.env.example
- Python 3.10+
- Huawei Cloud openGauss
- Existing raw import table:
crimes_raw
After cloning, you can run directly:
cd d:/database/crime_analytics_backend
python start_server.py --host 127.0.0.1 --port 8016The project will auto-install dependencies and use built-in openGauss defaults.
cd d:/database/crime_analytics_backend
C:/Users/73110/AppData/Local/Microsoft/WindowsApps/python3.11.exe -m pip install -r requirements.txtThe project now provides an auto-bootstrap launcher. If dependencies are missing or version-mismatched, it will install them automatically on startup. So manual install can be skipped.
Optional: copy .env.example to .env if you want to override default openGauss connection settings.
cd d:/database/crime_analytics_backend
python scripts/opengauss_prepare_clean.pycd d:/database/crime_analytics_backend
python start_server.py --host 127.0.0.1 --port 8016After startup, browser auto-opens /dashboard by default.
If you want to disable auto-open:
python start_server.py --host 127.0.0.1 --port 8016 --no-open-browserService URLs:
- API docs:
http://127.0.0.1:8016/docs - Dashboard:
http://127.0.0.1:8016/dashboard
Stop service with Ctrl + C in the same terminal.
When data volume is very large, default dashboard queries can be slow. You can pre-build a static snapshot so the first screen loads from local JSON instead of querying all analysis endpoints.
cd d:/database/crime_analytics_backend
python scripts/build_dashboard_snapshot.pyThis generates:
app/static/dashboard_snapshot.json
Dashboard behavior after this change:
- default view (no filters + topN=10): uses static snapshot for fast loading
- filtered view (year/type changed): still uses live API queries
If database is unavailable, you can generate a sample snapshot:
python scripts/build_dashboard_snapshot.py --sample- Fill
OG_PASSWORDin.env. - Run importer script:
cd d:/database/crime_analytics_backend
python scripts/opengauss_import.pyThen build analysis table (crimes_clean) used by API:
python scripts/opengauss_prepare_clean.pyIf the user does not have permission on public schema, use:
python scripts/opengauss_import.py --schema testuserQuick verification with partial data:
python scripts/opengauss_import.py --schema testuser --max-rows 10000The script will:
- connect to openGauss server
- create schema/table if not exists
- truncate target table by default
- bulk import CSV using
COPY
Optional arguments:
--no-truncate: keep existing rows and append--table crimes_raw_new: import to another table name--max-rows 10000: import only first N rows for connectivity check--password your_password: pass password from CLI (less secure than.env)
- Swagger UI:
http://127.0.0.1:8016/docs - ReDoc:
http://127.0.0.1:8016/redoc - Frontend integration contract:
docs/API.md
- Dashboard bundle (recommended for frontend dynamic refresh)
- Annual trend
- Weekly distribution
- Hourly distribution
- Crime type share
- District comparison
- Day-hour heatmap
- YoY trend of top crime types
- Text conclusions bundle (10+ statements)
sql/analysis_queries.sqlcontains standalone SQL examples for report writing.- Most analysis and metadata APIs support
sample=truefor frontend demo without database dependency. start_server.pychecks dependency versions againstrequirements.txtand auto-installs missing items before server startup.- For production deployment, add authentication, tighter CORS, and caching.