121 lines
3.9 KiB
Python
121 lines
3.9 KiB
Python
"""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)
|
|
|
|
补卡 (supplementary punch) handling:
|
|
- A supplement record has ``supplementType`` set and ``signType == 4``. Its ``signDate``
|
|
is when the supplement was submitted, NOT the real punch time. The real punch time is
|
|
``supplementDate``, and it only counts when ``approveStatus == 1`` (approved).
|
|
- So each record is attributed to its *effective* day/time: normal punches use signDate;
|
|
approved supplements use supplementDate; unapproved supplements are ignored.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from datetime import datetime, date
|
|
|
|
MIN_WORK_HOURS = 8.0
|
|
_DT_FMT = "%Y-%m-%d %H:%M:%S"
|
|
|
|
STATUS_OK = "正常"
|
|
STATUS_FEW = "缺卡(不足2次)"
|
|
STATUS_SHORT = "打卡时间不足(<8h)"
|
|
|
|
APPROVE_PASSED = 1
|
|
|
|
|
|
def _parse_dt(s: str | None) -> datetime | None:
|
|
if not s:
|
|
return None
|
|
try:
|
|
return datetime.strptime(s[:19], _DT_FMT)
|
|
except ValueError:
|
|
return None
|
|
|
|
|
|
def effective_punch(r: dict) -> datetime | None:
|
|
"""Effective punch datetime for a record, or None if it should not count.
|
|
|
|
- 补卡 (supplementType not null): use supplementDate, only if approved; else ignore.
|
|
- 普通打卡: use signDate.
|
|
"""
|
|
if r.get("supplementType") is not None or r.get("signType") == 4:
|
|
if r.get("approveStatus") != APPROVE_PASSED:
|
|
return None
|
|
return _parse_dt(r.get("supplementDate"))
|
|
return _parse_dt(r.get("signDate"))
|
|
|
|
|
|
def is_supplement(r: dict) -> bool:
|
|
return r.get("supplementType") is not None or r.get("signType") == 4
|
|
|
|
|
|
@dataclass
|
|
class DaySummary:
|
|
day: date
|
|
count: int
|
|
first: datetime | None
|
|
last: datetime | None
|
|
hours: float
|
|
status: str
|
|
has_supplement: bool = False
|
|
records: list = field(default_factory=list)
|
|
|
|
@property
|
|
def is_problem(self) -> bool:
|
|
return self.status != STATUS_OK
|
|
|
|
|
|
def daily_summaries(rows: list[dict], start: date | None = None,
|
|
end: date | None = None) -> list[DaySummary]:
|
|
"""Group records by their effective day and classify each day.
|
|
|
|
Supplements are attributed to the day they supplement (supplementDate), so callers
|
|
should fetch a wide-enough window (supplements may be submitted days later). If
|
|
``start``/``end`` are given, only days within [start, end] are returned.
|
|
"""
|
|
by_day: dict[date, list[dict]] = {}
|
|
for r in rows:
|
|
dt = effective_punch(r)
|
|
if dt is None:
|
|
continue
|
|
rec = {**r, "_dt": dt}
|
|
by_day.setdefault(dt.date(), []).append(rec)
|
|
|
|
out: list[DaySummary] = []
|
|
for day in sorted(by_day):
|
|
if start and day < start:
|
|
continue
|
|
if end and day > end:
|
|
continue
|
|
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,
|
|
any(is_supplement(r) for r in recs), 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], start: date | None = None,
|
|
end: date | None = None) -> list[DaySummary]:
|
|
return [s for s in daily_summaries(rows, start, end) if s.is_problem]
|