feat: 打卡记录拉取/缺卡统计工具(iEPMS token直连, 无需签名)

This commit is contained in:
2026-11-06 17:46:58 +08:00
parent 11c9b0b06f
commit 87af63a9c0
17 changed files with 988 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
"""Analyze punch records: daily summary, missing/insufficient detection, today reminder.
Rule (user-defined): for each day, take the earliest and the latest punch of that day.
- If the day has fewer than 2 punches -> 缺卡 (insufficient count)
- Else if (latest - earliest) < 8 hours -> 打卡时间不足 (insufficient duration)
- Else -> 正常 (OK)
"""
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import datetime, date, timedelta
MIN_WORK_HOURS = 8.0
_DT_FMT = "%Y-%m-%d %H:%M:%S"
STATUS_OK = "正常"
STATUS_FEW = "缺卡(不足2次)"
STATUS_SHORT = "打卡时间不足(<8h)"
def _parse_dt(s: str) -> datetime:
return datetime.strptime(s[:19], _DT_FMT)
@dataclass
class DaySummary:
day: date
count: int
first: datetime | None
last: datetime | None
hours: float
status: str
records: list = field(default_factory=list)
@property
def is_problem(self) -> bool:
return self.status != STATUS_OK
def daily_summaries(rows: list[dict]) -> list[DaySummary]:
by_day: dict[date, list[dict]] = {}
for r in rows:
sd = r.get("signDate")
if not sd:
continue
try:
dt = _parse_dt(sd)
except ValueError:
continue
r = {**r, "_dt": dt}
by_day.setdefault(dt.date(), []).append(r)
out: list[DaySummary] = []
for day in sorted(by_day):
recs = sorted(by_day[day], key=lambda x: x["_dt"])
first = recs[0]["_dt"]
last = recs[-1]["_dt"]
hours = (last - first).total_seconds() / 3600.0
if len(recs) < 2:
status = STATUS_FEW
elif hours < MIN_WORK_HOURS:
status = STATUS_SHORT
else:
status = STATUS_OK
out.append(DaySummary(day, len(recs), first, last, hours, status, recs))
return out
def today_status(rows: list[dict], today: date | None = None) -> DaySummary | None:
today = today or datetime.now().date()
for s in daily_summaries(rows):
if s.day == today:
return s
return None
def problems(rows: list[dict]) -> list[DaySummary]:
return [s for s in daily_summaries(rows) if s.is_problem]