Heinrich's-law play: the signals we already compute but used to throw away
become a spatial risk map of the workplace, drawn over the camera's own frame.
Edge:
- near_fall (INFO): a track that reached FALLING (sustained prone) but recovered
before the FALLEN confirm. Treated as a full event per product decision -
same ingest, snapshots, dedup, ack flow as incidents. Fix en route: prone_s
used a truthiness check that turned prone_since=0.0 into "no timer".
- Every per-track event (fall/no_motion/near_fall/immobility/zone hits) now
carries the foot point in the zones' normalized 0..1 space (reusing the zone
detector's own coordinate math); ghost's pixel-space loss point is normalized
at emit. to_payload omits the key when absent, so an old server ignores it.
Server:
- move_events.spatial_x/spatial_y (nullable DOUBLE) + validator: optional
[x, y] finite-number pair; range deliberately NOT enforced (an off-by-a-hair
foot must never reject a FALL alert - renderers clamp).
- MIGRATION FIX: chk_events_type would have REJECTED the previous commit's
lone_worker/overcapacity/overstay inserts on MySQL 8.0.16+ - the constraint
is rebuilt with all 13 types (incl. near_fall).
- GET /api/heatmap/{camera}?days=N (1..90, default 30, 5000-point cap):
spatial points with the same fail-closed site scoping as the event list.
UI (zone editor): "위험 히트맵" toggle + day window. Additive radial blots in
an offscreen layer under the zone polygons; severity weighs the alpha so a
cluster of near-misses reads lighter than one confirmed fall at the same spot.
Events console/detail label near_fall in Korean.
Verified: pytest 222 pass / PHPUnit 130 pass + Chrome real-render (blue
near-miss cluster + red incident hotspot inside the restricted zone, amber
cluster in the warning zone, zones drawn on top; no console errors).
85 lines
3.2 KiB
Python
85 lines
3.2 KiB
Python
import uuid as _uuid
|
|
from dataclasses import dataclass, field
|
|
from enum import Enum
|
|
|
|
|
|
class EventType(str, Enum):
|
|
FALL = "fall"
|
|
NO_MOTION = "no_motion"
|
|
IMMOBILITY = "immobility"
|
|
GHOST = "ghost"
|
|
ZONE_WARNING = "zone_warning"
|
|
ZONE_RESTRICTED = "zone_restricted"
|
|
PINCH = "pinch"
|
|
MONITORING_DEGRADED = "monitoring_degraded"
|
|
DEVICE_FAULT = "device_fault"
|
|
LONE_WORKER = "lone_worker" # 2인 1조 존에 단독 작업 지속
|
|
OVERCAPACITY = "overcapacity" # 존 정원 초과 지속
|
|
OVERSTAY = "overstay" # 존 체류 상한 초과 (트랙별)
|
|
NEAR_FALL = "near_fall" # 니어미스: FALLING까지 갔다가 확정 전 회복
|
|
|
|
|
|
class Severity(str, Enum):
|
|
CRITICAL = "critical"
|
|
HIGH = "high"
|
|
WARNING = "warning"
|
|
INFO = "info"
|
|
|
|
|
|
_SEVERITY = {
|
|
EventType.NO_MOTION: Severity.CRITICAL,
|
|
EventType.GHOST: Severity.CRITICAL,
|
|
EventType.FALL: Severity.HIGH,
|
|
EventType.ZONE_RESTRICTED: Severity.HIGH,
|
|
EventType.PINCH: Severity.HIGH,
|
|
EventType.IMMOBILITY: Severity.WARNING,
|
|
EventType.ZONE_WARNING: Severity.WARNING,
|
|
EventType.MONITORING_DEGRADED: Severity.WARNING,
|
|
EventType.DEVICE_FAULT: Severity.WARNING,
|
|
# 단독 작업은 규정 위반이자 사고 시 구조 지연 요인 — 정원/체류보다 한 단계 높게.
|
|
EventType.LONE_WORKER: Severity.HIGH,
|
|
EventType.OVERCAPACITY: Severity.WARNING,
|
|
EventType.OVERSTAY: Severity.WARNING,
|
|
# 니어미스는 알람이 아니라 히트맵/통계 재료 — 소음이 되면 안 되므로 INFO.
|
|
EventType.NEAR_FALL: Severity.INFO,
|
|
}
|
|
|
|
|
|
def severity_for(event_type: EventType) -> Severity:
|
|
return _SEVERITY.get(event_type, Severity.INFO)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Event:
|
|
event_type: EventType
|
|
severity: Severity
|
|
camera_id: str
|
|
track_id: int | None
|
|
zone_id: str | None
|
|
occurred_at: float
|
|
duration_s: float
|
|
metrics: dict
|
|
spatial: tuple | None # (x, y) 유령사고 등 공간 기준
|
|
# Stable idempotency key: generated ONCE at event creation and carried unchanged through the
|
|
# offline queue and every retry, so a POST that succeeded server-side but timed out
|
|
# client-side is deduplicated by the server (see server IngestController) instead of stored twice.
|
|
event_uuid: str = field(default_factory=lambda: _uuid.uuid4().hex)
|
|
|
|
def to_payload(self, device_id: str) -> dict:
|
|
payload = {
|
|
"device_id": device_id,
|
|
"event_uuid": self.event_uuid,
|
|
"camera_id": self.camera_id,
|
|
"event_type": self.event_type.value,
|
|
"severity": self.severity.value,
|
|
"track_id": self.track_id,
|
|
"zone_id": self.zone_id,
|
|
"occurred_at": self.occurred_at,
|
|
"duration_s": round(self.duration_s, 2),
|
|
"metrics": self.metrics,
|
|
}
|
|
# 정규화 발/기준 좌표(0..1) — 서버 히트맵의 원료. None이면 키 자체를 생략해
|
|
# 구서버(스키마에 spatial 없음)와의 하위호환을 지킨다.
|
|
if self.spatial is not None:
|
|
payload["spatial"] = [round(float(self.spatial[0]), 4), round(float(self.spatial[1]), 4)]
|
|
return payload
|