增加sqlalchemy后端,优化日志结构
This commit is contained in:
+56
-11
@@ -15,6 +15,8 @@ from urllib import error, request
|
||||
|
||||
import yaml
|
||||
|
||||
from sync_state_machine.common.persistence import PersistenceBackend
|
||||
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
@@ -312,26 +314,69 @@ def sqlite_rows(db_path: str | Path, sql: str, params: tuple[Any, ...] = ()) ->
|
||||
return [{key: row[key] for key in row.keys()} for row in rows]
|
||||
|
||||
|
||||
def persistence_summary(db_path: str | Path, node_type: str | None = None) -> dict[str, Any]:
|
||||
def persistence_rows(
|
||||
*,
|
||||
backend: str,
|
||||
sql: str,
|
||||
db_path: str | Path | None = None,
|
||||
url: str | None = None,
|
||||
limit: int = 200,
|
||||
) -> list[dict[str, Any]]:
|
||||
_, rows = PersistenceBackend.query_records(
|
||||
backend=backend,
|
||||
db_path=str(db_path) if db_path is not None else None,
|
||||
url=url,
|
||||
sql=sql,
|
||||
limit=limit,
|
||||
)
|
||||
return rows
|
||||
|
||||
|
||||
def persistence_summary(
|
||||
db_path: str | Path | None,
|
||||
node_type: str | None = None,
|
||||
*,
|
||||
backend: str = "sqlite",
|
||||
url: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
records = PersistenceBackend.records_summary(
|
||||
backend=backend,
|
||||
db_path=str(db_path) if db_path is not None else None,
|
||||
url=url,
|
||||
)
|
||||
summary: dict[str, Any] = {
|
||||
"backend": backend,
|
||||
"db_path": str(db_path),
|
||||
"db_exists": Path(db_path).exists(),
|
||||
"url": url,
|
||||
"db_exists": records.get("db_exists", False),
|
||||
"bindings": [],
|
||||
"node_status": [],
|
||||
}
|
||||
if not Path(db_path).exists():
|
||||
if not summary["db_exists"]:
|
||||
return summary
|
||||
|
||||
target_db_path = str(db_path) if db_path is not None else None
|
||||
if node_type:
|
||||
summary["bindings"] = sqlite_rows(
|
||||
db_path,
|
||||
"SELECT node_type, local_id, remote_id, last_updated FROM bindings WHERE node_type = ? ORDER BY last_updated DESC LIMIT 20",
|
||||
(node_type,),
|
||||
escaped_node_type = node_type.replace("'", "''")
|
||||
summary["bindings"] = persistence_rows(
|
||||
backend=backend,
|
||||
db_path=target_db_path,
|
||||
url=url,
|
||||
sql=(
|
||||
"SELECT node_type, local_id, remote_id, last_updated "
|
||||
f"FROM bindings WHERE node_type = '{escaped_node_type}' ORDER BY last_updated DESC LIMIT 20"
|
||||
),
|
||||
)
|
||||
summary["node_status"] = sqlite_rows(
|
||||
db_path,
|
||||
"SELECT collection_id, node_type, action, status, binding_status, COUNT(1) AS count FROM nodes WHERE node_type = ? GROUP BY collection_id, node_type, action, status, binding_status ORDER BY collection_id, action, status",
|
||||
(node_type,),
|
||||
summary["node_status"] = persistence_rows(
|
||||
backend=backend,
|
||||
db_path=target_db_path,
|
||||
url=url,
|
||||
sql=(
|
||||
"SELECT collection_id, node_type, action, status, binding_status, COUNT(1) AS count "
|
||||
f"FROM nodes WHERE node_type = '{escaped_node_type}' "
|
||||
"GROUP BY collection_id, node_type, action, status, binding_status "
|
||||
"ORDER BY collection_id, action, status"
|
||||
),
|
||||
)
|
||||
return summary
|
||||
|
||||
|
||||
Reference in New Issue
Block a user