fix reset state
This commit is contained in:
@@ -1224,6 +1224,27 @@ def serve_dashboard(host: str, port: int, status_file: str, *, image_dir: str |
|
||||
except (ValueError, json.JSONDecodeError) as exc:
|
||||
self._send(400, "application/json", json.dumps({"error": str(exc)}).encode("utf-8"))
|
||||
return
|
||||
if self.path == "/api/feedback/clear" and self._is_loopback_client():
|
||||
try:
|
||||
payload = json.loads(self.rfile.read(min(int(self.headers.get("Content-Length", "0")), 16_384)).decode("utf-8"))
|
||||
status = str(payload.get("status", "") or "")
|
||||
self._send(200, "application/json", json.dumps(FeedbackStore().clear(status=status or None)).encode("utf-8"))
|
||||
except (ValueError, json.JSONDecodeError) as exc:
|
||||
self._send(400, "application/json", json.dumps({"error": str(exc)}).encode("utf-8"))
|
||||
return
|
||||
if self.path == "/api/review-state/clear" and self._is_loopback_client():
|
||||
try:
|
||||
payload = json.loads(self.rfile.read(min(int(self.headers.get("Content-Length", "0")), 16_384)).decode("utf-8") or "{}")
|
||||
clear_incidents = bool(payload.get("incidents", True))
|
||||
clear_feedback = bool(payload.get("feedback", True))
|
||||
result = {
|
||||
"incidents": IncidentStore().clear() if clear_incidents else {"removed": 0, "remaining": len(IncidentStore().entries()), "status": "skipped"},
|
||||
"feedback": FeedbackStore().clear() if clear_feedback else {"removed": 0, "remaining": len(FeedbackStore().entries()), "status": "skipped"},
|
||||
}
|
||||
self._send(200, "application/json", json.dumps(result).encode("utf-8"))
|
||||
except (ValueError, json.JSONDecodeError) as exc:
|
||||
self._send(400, "application/json", json.dumps({"error": str(exc)}).encode("utf-8"))
|
||||
return
|
||||
if self.path == "/api/incidents" and self._is_loopback_client():
|
||||
try:
|
||||
payload = json.loads(self.rfile.read(min(int(self.headers.get("Content-Length", "0")), 16_384)).decode("utf-8"))
|
||||
|
||||
@@ -25,3 +25,19 @@ class FeedbackStore:
|
||||
entries = [value for value in self.entries() if (value.get("stream_id"), value.get("entity"), value.get("field"), value.get("value", "")) != (entry["stream_id"], entry["entity"], entry["field"], entry["value"])]
|
||||
entries.append(entry); self.path.parent.mkdir(parents=True, exist_ok=True); self.path.write_text(json.dumps(entries, indent=2), encoding="utf-8")
|
||||
return entry
|
||||
|
||||
def clear(self, *, status: str | None = None) -> dict[str, object]:
|
||||
entries = self.entries()
|
||||
if status is None:
|
||||
removed = len(entries)
|
||||
entries = []
|
||||
else:
|
||||
status = status.lower()
|
||||
if status not in {"false_positive", "expected", "confirmed"}:
|
||||
raise ValueError("invalid feedback status")
|
||||
before = len(entries)
|
||||
entries = [item for item in entries if str(item.get("status", "")).lower() != status]
|
||||
removed = before - len(entries)
|
||||
self.path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self.path.write_text(json.dumps(entries, indent=2), encoding="utf-8")
|
||||
return {"removed": removed, "remaining": len(entries), "status": status or "all"}
|
||||
|
||||
Reference in New Issue
Block a user