added restapi
This commit is contained in:
+22
-1
@@ -8,7 +8,7 @@ import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from config import UI_LOG_LIMIT, cmd_path, status_path, ui_log_path
|
||||
from config import UI_LOG_LIMIT, cmd_path, history_path, status_path, ui_log_path
|
||||
|
||||
log = logging.getLogger("collector.ui")
|
||||
|
||||
@@ -23,6 +23,27 @@ def write_status(status: dict) -> None:
|
||||
log.error("status write failed: %s", e)
|
||||
|
||||
|
||||
def write_history(data: dict) -> None:
|
||||
path = history_path()
|
||||
tmp = Path(str(path) + ".tmp")
|
||||
try:
|
||||
tmp.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
|
||||
tmp.replace(path)
|
||||
except OSError as e:
|
||||
log.error("history write failed: %s", e)
|
||||
|
||||
|
||||
def read_history() -> dict:
|
||||
path = history_path()
|
||||
if not path.exists():
|
||||
return {}
|
||||
try:
|
||||
raw = json.loads(path.read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError):
|
||||
return {}
|
||||
return raw if isinstance(raw, dict) else {}
|
||||
|
||||
|
||||
def append_ui_log(level: str, message: str, **extra: Any) -> None:
|
||||
path = ui_log_path()
|
||||
entry = {
|
||||
|
||||
Reference in New Issue
Block a user