update migration to .env from sources.env

This commit is contained in:
Alberto-Audrix committed 2026-10-14 11:46:36 +07:00
1 parent b90338034d
commit e1ffb57391
8 files changed
+83 -120

No files matched your search

+30 -9
View File
@@ -3,18 +3,31 @@
# Copy once per machine, then edit values for that site:
# cp .env.example .env
#
# Note: Source URLs (KTC/KPC) are configured in sources.env (see sources.env.example)
#
# Restart services after changes:
# sudo systemctl restart karung-web-admin.service
# sudo systemctl restart karung-web-admin.service karung-web-admin-pull-sources.service
# -----------------------------------------------------------------------------
# Data file paths (optional — defaults are in the app directory)
# Source URLs (KTC/Tuang and KPC/Masuk)
# -----------------------------------------------------------------------------
# KARUNG_TUANG_JSON=karung_tuang.json
# KARUNG_MASUK_JSON=karung_masuk.json
# KARUNG_TUANG_DB=karung_tuang.db
# KARUNG_MASUK_DB=karung_masuk.db
# Grouping syntax:
# `|` separates cameras (K1, K2, ...)
# `,` separates hosts inside one camera (summed into that Kn)
# No `|` keeps legacy behavior: each comma-separated host is its own Kn
# One camera from many hosts: add a trailing `|` (e.g. http://a,http://b|)
#
# Example: K1 = three hosts summed, K2 = one host
# KTC_BASE_URL=http://REPLACE_HOST_A:9000,http://REPLACE_HOST_B:9000,http://REPLACE_HOST_C:9000|http://REPLACE_HOST_D:9000
KTC_BASE_URL=http://REPLACE_HOST_A:9000|http://REPLACE_HOST_B:9000
# Example: K1 = three hosts summed, K2 = two hosts summed
# KPC_BASE_URL=http://REPLACE_HOST_A:7000,http://REPLACE_HOST_B:7000,http://REPLACE_HOST_C:7000|http://REPLACE_HOST_D:7000,http://REPLACE_HOST_E:7000
KPC_BASE_URL=http://REPLACE_HOST_A:7000|http://REPLACE_HOST_B:7000
# History window (days) to pull and default query history window for API
SOURCE_HISTORY_DAYS=30
# Background pull interval (seconds)
SOURCE_PULL_INTERVAL_SECONDS=30
# -----------------------------------------------------------------------------
# Daily data business-day cutoffs (optional — overrides dashboard DB settings)
@@ -32,10 +45,18 @@ TUANG_CUTOFF_TIME=17:00:00
# When Karung Masuk (In/Out) rolls to the next business day
MASUK_CUTOFF_TIME=24:00:00
# -----------------------------------------------------------------------------
# Cycle API (maps K1 -> Kandang 1, K2 -> Kandang 2, etc.)
# -----------------------------------------------------------------------------
# Base URL without trailing slash; http:// is added automatically if omitted.
CYCLE_API_BASE_URL=http://192.168.192.106:5002
CYCLE_API_KEY=test-api-key
# Optional path for exported counter settings JSON (used by other services)
# -----------------------------------------------------------------------------
# Data file paths (optional — defaults are in the app directory)
# -----------------------------------------------------------------------------
# KARUNG_TUANG_JSON=karung_tuang.json
# KARUNG_MASUK_JSON=karung_masuk.json
# KARUNG_TUANG_DB=karung_tuang.db
# KARUNG_MASUK_DB=karung_masuk.db
# COUNTER_SETTINGS_FILE=counter_settings.json
-1
View File
@@ -130,7 +130,6 @@ celerybeat.pid
# Environments
.env
sources.env
.venv
env/
venv/
+21 -55
View File
@@ -1,16 +1,12 @@
{
"ktc": {
"status": "ok",
"pulled_at": "2026-09-02T01:00:03Z",
"base_url": null,
"pulled_at": "2026-10-14T04:25:06Z",
"base_url": "http://192.168.192.15:9000",
"base_urls": [
"http://192.168.192.17:9000",
"http://192.168.192.15:9000"
],
"source_groups": [
[
"http://192.168.192.17:9000"
],
[
"http://192.168.192.15:9000"
]
@@ -19,50 +15,34 @@
"hosts": [
{
"status": "ok",
"pulled_at": "2026-09-02T01:00:02Z",
"base_url": "http://192.168.192.17:9000",
"pulled_at": "2026-10-14T04:25:06Z",
"base_url": "http://192.168.192.15:9000",
"source_index": 1,
"error": null,
"live_sources": [
"K1 - Use"
],
"history_rows": 2
},
{
"status": "ok",
"pulled_at": "2026-09-02T01:00:02Z",
"base_url": "http://192.168.192.15:9000",
"source_index": 2,
"error": null,
"live_sources": [
"K2 - Use"
],
"history_rows": 2
"history_rows": 61
}
],
"error": null,
"live_sources": [
"K1 - Use",
"K2 - Use"
"K1 - Use"
],
"history_rows": 4,
"hosts_ok": 2,
"hosts_total": 2,
"groups_total": 2,
"history_rows": 61,
"hosts_ok": 1,
"hosts_total": 1,
"groups_total": 1,
"source_key": "ktc"
},
"kpc": {
"status": "ok",
"pulled_at": "2026-09-02T01:00:03Z",
"base_url": null,
"pulled_at": "2026-10-14T04:25:06Z",
"base_url": "http://192.168.192.15:7000",
"base_urls": [
"http://192.168.192.17:7000",
"http://192.168.192.15:7000"
],
"source_groups": [
[
"http://192.168.192.17:7000"
],
[
"http://192.168.192.15:7000"
]
@@ -71,41 +51,27 @@
"hosts": [
{
"status": "ok",
"pulled_at": "2026-09-02T01:00:03Z",
"base_url": "http://192.168.192.17:7000",
"pulled_at": "2026-10-14T04:25:06Z",
"base_url": "http://192.168.192.15:7000",
"source_index": 1,
"error": null,
"live_sources": [
"K1 - In",
"K1 - Out"
],
"history_rows": 6
},
{
"status": "ok",
"pulled_at": "2026-09-02T01:00:03Z",
"base_url": "http://192.168.192.15:7000",
"source_index": 2,
"error": null,
"live_sources": [
"K2 - In",
"K2 - Out"
],
"history_rows": 12
"history_rows": 122
}
],
"error": null,
"live_sources": [
"K1 - In",
"K1 - Out",
"K2 - In",
"K2 - Out"
"K1 - Out"
],
"history_rows": 18,
"hosts_ok": 2,
"hosts_total": 2,
"groups_total": 2,
"history_rows": 122,
"hosts_ok": 1,
"hosts_total": 1,
"groups_total": 1,
"source_key": "kpc"
},
"updated_at": "2026-09-02T01:00:03Z"
"updated_at": "2026-10-14T04:25:06Z"
}
+24 -12
View File
@@ -9,7 +9,7 @@ Admin dashboard that aggregates **KTC** (tuang) and **KPC** (masuk) counter data
| Tuang | `zenai-ktc-python` | `9000` | `K1 - Use`, `K2 - Use`, ... |
| Masuk | `zenai-kpc-python` | `7000` | `K1 - In`, `K1 - Out`, `K2 - In`, `K2 - Out`, ... |
Host IPs are **not** hardcoded. Configure them per machine in `sources.env`.
Host IPs are **not** hardcoded. Configure them per machine in `.env`.
Source URL grouping:
@@ -24,15 +24,15 @@ Tuang uses each host's `total_count` as `K{n} - Use`; masuk keeps `K{n} - In` an
Deploy code to `/opt/karung-web-admin` (matches the systemd units below).
## Configure source hosts (required)
## Configure environment (required)
```bash
cd /opt/karung-web-admin
cp sources.env.example sources.env
nano sources.env # set real KTC/KPC URLs for this site
cp .env.example .env
nano .env # set real KTC/KPC URLs and other settings for this site
```
Example `sources.env`:
Example `.env`:
```bash
# K1 = three hosts summed; K2 = one host
@@ -41,6 +41,13 @@ KTC_BASE_URL=http://10.0.0.11:9000,http://10.0.0.12:9000,http://10.0.0.13:9000|h
KPC_BASE_URL=http://10.0.0.11:7000,http://10.0.0.12:7000,http://10.0.0.13:7000|http://10.0.0.14:7000
SOURCE_HISTORY_DAYS=30
SOURCE_PULL_INTERVAL_SECONDS=30
LOCAL_TIMEZONE=Asia/Jakarta
TUANG_CUTOFF_TIME=17:00:00
MASUK_CUTOFF_TIME=24:00:00
CYCLE_API_BASE_URL=http://192.168.192.106:5002
CYCLE_API_KEY=test-api-key
```
Legacy (unchanged): `http://a:9000,http://b:9000` still means `K1` and `K2` (same for KPC).
@@ -48,10 +55,10 @@ Legacy (unchanged): `http://a:9000,http://b:9000` still means `K1` and `K2` (sam
After editing:
```bash
sudo systemctl restart karung-web-admin-pull-sources.service
sudo systemctl restart karung-web-admin.service karung-web-admin-pull-sources.service
```
`sources.env` is gitignored so each production host keeps its own IPs.
`.env` is gitignored so each production host keeps its own IPs and secrets.
## Puller (systemd service)
@@ -61,7 +68,7 @@ sudo systemctl daemon-reload
sudo systemctl enable --now karung-web-admin-pull-sources.service
```
It loads `/opt/karung-web-admin/sources.env`, runs `pull_sources.py --loop`, and refreshes every `SOURCE_PULL_INTERVAL_SECONDS` (default 30).
It loads `/opt/karung-web-admin/.env`, runs `pull_sources.py --loop`, and refreshes every `SOURCE_PULL_INTERVAL_SECONDS` (default 30).
Writes:
@@ -100,16 +107,21 @@ sudo systemctl daemon-reload
Dashboard: `http://0.0.0.0:8090/`
Combined API: `GET /api/combined?history_days=30`
## Environment
## Environment Variables
Configured in `sources.env` (see `sources.env.example`):
Configured in `.env` (see `.env.example`):
| Variable | Description |
|----------|-------------|
| `KTC_BASE_URL` | Tuang/KTC source groups (`\|` = cameras, `,` = hosts in a camera) |
| `KPC_BASE_URL` | Masuk/KPC source groups (same grouping syntax) |
| `SOURCE_HISTORY_DAYS` | History window to pull (default `30`) |
| `SOURCE_PULL_INTERVAL_SECONDS` | Pull loop interval (default `30`) |
| `SOURCE_HISTORY_DAYS` | History window to pull & default query window for combined API (default `30`) |
| `SOURCE_PULL_INTERVAL_SECONDS` | Pull loop interval in seconds (default `30`) |
| `LOCAL_TIMEZONE` | Timezone for business cutoffs (e.g. `Asia/Jakarta`) |
| `TUANG_CUTOFF_TIME` | Cutoff time for Karung Tuang (e.g. `17:00:00`) |
| `MASUK_CUTOFF_TIME` | Cutoff time for Karung Masuk (e.g. `24:00:00`) |
| `CYCLE_API_BASE_URL` | Kandang cycle mapping API base URL |
| `CYCLE_API_KEY` | Kandang cycle mapping API key |
| `KARUNG_TUANG_JSON` | Live tuang JSON path (default `karung_tuang.json`) |
| `KARUNG_MASUK_JSON` | Live masuk JSON path (default `karung_masuk.json`) |
| `KARUNG_TUANG_DB` | Tuang history DB path (default `karung_tuang.db`) |
+2 -2
View File
@@ -5,8 +5,8 @@ After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/karung-web-admin
# Edit IPs in /opt/karung-web-admin/sources.env (copy from sources.env.example)
EnvironmentFile=/opt/karung-web-admin/sources.env
# Configuration in /opt/karung-web-admin/.env (copy from .env.example)
EnvironmentFile=-/opt/karung-web-admin/.env
ExecStart=/usr/bin/python3 -u /opt/karung-web-admin/pull_sources.py --loop
Restart=always
RestartSec=5s
+1
View File
@@ -5,6 +5,7 @@ After=network.target
[Service]
Type=simple
WorkingDirectory=/opt/karung-web-admin
EnvironmentFile=-/opt/karung-web-admin/.env
ExecStart=/usr/bin/python3 -u /opt/karung-web-admin/app.py
Restart=always
RestartSec=5s
+5 -16
View File
@@ -30,7 +30,7 @@ BASE_DIR = Path(__file__).resolve().parent
DEFAULT_HISTORY_DAYS = 30
DEFAULT_PULL_INTERVAL_SECONDS = 30
PULL_STATUS_FILE = BASE_DIR / ".pull_status.json"
SOURCES_ENV_FILE = BASE_DIR / "sources.env"
ENV_FILE = BASE_DIR / ".env"
# Field suffix is combined with source index into names like "K1 - Use".
# KTC uses total_count so hosts without left/right still report correctly.
@@ -121,10 +121,10 @@ def get_required_source_groups(env_name):
if groups:
return groups
example_path = BASE_DIR / "sources.env.example"
example_path = BASE_DIR / ".env.example"
raise SystemExit(
f"Missing or empty {env_name}. "
f"Copy {example_path.name} to {SOURCES_ENV_FILE.name}, set the host URLs for this site, "
f"Copy {example_path.name} to {ENV_FILE.name}, set the host URLs for this site, "
"then restart the puller service."
)
@@ -139,17 +139,7 @@ def build_source_camera_name(source_index, camera_suffix):
def extract_history_calendar_date(row):
"""Use the actual activity date from KPC history, not the forward-looking counting_date."""
start_time = row.get("start_time")
if start_time:
try:
parsed = datetime.fromisoformat(str(start_time).replace("Z", "+00:00"))
if parsed.tzinfo is not None:
parsed = parsed.astimezone()
return parsed.date().isoformat()
except ValueError:
pass
"""Use the counting/business date directly from KTC/KPC history payload."""
counting_date = row.get("date") or row.get("counting_date")
return str(counting_date) if counting_date else None
@@ -522,8 +512,7 @@ def run_pull_loop(interval_seconds=None):
def main(argv=None):
load_env_file(SOURCES_ENV_FILE)
load_env_file(BASE_DIR / ".env")
load_env_file(ENV_FILE)
args = list(argv if argv is not None else sys.argv[1:])
if "--loop" in args:
-25
View File
@@ -1,25 +0,0 @@
# Site source URLs for karung-web-admin
# Copy this file once per machine, then edit the IPs for that site:
# cp sources.env.example sources.env
#
# Grouping:
# `|` separates cameras (K1, K2, ...)
# `,` separates hosts inside one camera (summed into that Kn)
# No `|` keeps legacy behavior: each host is its own Kn
# One camera from many hosts: add a trailing `|`
#
# Restart after changes:
# sudo systemctl restart karung-web-admin-pull-sources.service
# Tuang (KTC)
# Example: K1 = three hosts summed, K2 = one host
# KTC_BASE_URL=http://REPLACE_HOST_A:9000,http://REPLACE_HOST_B:9000,http://REPLACE_HOST_C:9000|http://REPLACE_HOST_D:9000
KTC_BASE_URL=http://REPLACE_HOST_A:9000|http://REPLACE_HOST_B:9000
# Masuk (KPC) — same grouping; hosts in a group sum into Kn - In / Kn - Out
# Example: K1 = three hosts summed, K2 = two hosts summed
# KPC_BASE_URL=http://REPLACE_HOST_A:7000,http://REPLACE_HOST_B:7000,http://REPLACE_HOST_C:7000|http://REPLACE_HOST_D:7000,http://REPLACE_HOST_E:7000
KPC_BASE_URL=http://REPLACE_HOST_A:7000|http://REPLACE_HOST_B:7000
SOURCE_HISTORY_DAYS=30
SOURCE_PULL_INTERVAL_SECONDS=30