diff --git a/.gitignore b/.gitignore index b3ec7d5..e32f3a3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ __pycache__/ *.py[codz] *$py.class +#superpower +docs + # C extensions *.so diff --git a/counter_dashboard.py b/counter_dashboard.py index 79482e6..104d174 100644 --- a/counter_dashboard.py +++ b/counter_dashboard.py @@ -183,6 +183,7 @@ def api_snapshot_image(category, filename): def _ensure_db(): + Path(DB_PATH).parent.mkdir(parents=True, exist_ok=True) conn = sqlite3.connect(DB_PATH) cur = conn.cursor() cur.execute( @@ -362,7 +363,7 @@ def _empty_counts(): def _empty_current(): return { "counting_date": get_counting_date(), - "count": 0, + "total_count": 0, "counts": _empty_counts(), "zones": ZONES_META, "start_time": None, @@ -390,9 +391,16 @@ def _load_current_state(): "zone_2": int(data.get("count_right", 0) or 0), } counts = _normalize_counts(counts) + # Prefer total_count; fall back to legacy "count" then sum of zones. + if data.get("total_count") is not None: + total = int(data.get("total_count") or 0) + elif data.get("count") is not None: + total = int(data.get("count") or 0) + else: + total = sum(counts.values()) return { "counting_date": data.get("counting_date") or get_counting_date(), - "count": int(data.get("count", sum(counts.values())) or 0), + "total_count": total, "counts": counts, "zones": ZONES_META, "start_time": data.get("start_time"), diff --git a/counter_store.py b/counter_store.py index 277733f..96fcf68 100644 --- a/counter_store.py +++ b/counter_store.py @@ -177,7 +177,8 @@ class CounterStore: # Preserve migrated zone_2 even if current config is 1 zone? Spec says # configured ids only — drop unknown keys from active counts. state['counts'] = normalized - state['count'] = sum(normalized.values()) + state['total_count'] = sum(normalized.values()) + state.pop('count', None) state.pop('count_left', None) state.pop('count_right', None) state.setdefault('counted_event_ids', []) @@ -208,7 +209,7 @@ class CounterStore: ) state['counted_event_ids'] = [] self.log( - f"Resumed {current_date} with total={state['count']} " + f"Resumed {current_date} with total={state['total_count']} " f"counts={state['counts']}" ) with open(self.state_file, 'w', encoding='utf-8') as f: @@ -236,7 +237,7 @@ class CounterStore: counts = self._empty_counts() self.current_state = { 'counting_date': counting_date, - 'count': 0, + 'total_count': 0, 'counts': counts, 'start_time': now, 'last_detection_time': now, @@ -264,7 +265,7 @@ class CounterStore: counted = False event_key = f"{track_id}_{zone_id}" if event_key not in self.current_state['counted_event_ids']: - self.current_state['count'] += 1 + self.current_state['total_count'] += 1 self.current_state['counts'][zone_id] = ( int(self.current_state['counts'].get(zone_id, 0)) + 1 ) @@ -272,14 +273,14 @@ class CounterStore: counted = True self.log( f'Counted {zone_id} (track {track_id}) | {counting_date} ' - f'total: {self.current_state["count"]} ' + f'total: {self.current_state["total_count"]} ' f'counts={self.current_state["counts"]}' ) self._persist_day() self.current_state['last_detection_time'] = datetime.now().isoformat() self.save_state() - return self.current_state['count'], day_started, counted + return self.current_state['total_count'], day_started, counted def _persist_day(self): state = self.current_state @@ -298,7 +299,7 @@ class CounterStore: """, ( state['counting_date'], self.camera_name, self.object_label, - state['count'], + state['total_count'], state['start_time'], datetime.now().isoformat(), ), ) @@ -339,7 +340,7 @@ class CounterStore: def current_count(self): if self.current_state is None: return 0 - return self.current_state['count'] + return self.current_state['total_count'] @property def current_counts(self): diff --git a/templates/dashboard.html b/templates/dashboard.html index e017924..a46815f 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -772,8 +772,9 @@
+