astrbot_plugin_imagestudio/backend/services/dashboard_service.py

110 行
3.8 KiB
Python

"""Dashboard summary service."""
from __future__ import annotations
from pathlib import Path
from typing import Any
from core.config_service import ConfigService, ImageStudioConfig
from database.db_helper import Database, REPO_ROOT
from database.repositories.job_repository import JobRepository
class DashboardService:
def __init__(
self,
database: Database,
config_service: ConfigService | None = None,
):
self.database = database
self.config_service = config_service or ConfigService()
def get_summary(self) -> dict[str, Any]:
config = self.config_service.load()
queue_repository = JobRepository(self.database)
return {
"plugin_status": "running",
"today_generation_count": 0,
"active_profiles": self._count_active_profiles(),
"queue": self._queue_summary(queue_repository),
"storage": self._storage_summary(config),
"recent_jobs": [task.to_dict() for task in queue_repository.recent(limit=10)],
"recent_images": self._recent_images(),
}
def _count_active_profiles(self) -> int:
placeholder = self.database._placeholder
conn = self.database.connect()
try:
row = conn.execute(
f"SELECT COUNT(*) AS total FROM character_profiles WHERE status = {placeholder}",
("active",),
).fetchone()
finally:
conn.close()
return int(row["total"] if row else 0)
def _queue_summary(self, queue_repository: JobRepository) -> dict[str, int]:
counts = queue_repository.count_by_status()
return {
"pending": counts.get("created", 0) + counts.get("queued", 0),
"running": counts.get("running", 0),
"failed": counts.get("failed", 0),
"dead_letter": counts.get("dead_letter", 0),
}
def _recent_images(self) -> list[dict[str, Any]]:
conn = self.database.connect()
try:
rows = conn.execute(
"""
SELECT image_id, node_id, workflow_id, profile_id, thumbnail_uri, image_uri, created_at
FROM generation_images
WHERE is_archived = 0
ORDER BY created_at DESC
LIMIT 10
"""
).fetchall()
finally:
conn.close()
return [
{
"image_id": row["image_id"],
"node_id": row["node_id"],
"workflow_id": row["workflow_id"],
"profile_id": row["profile_id"],
"thumbnail_uri": row["thumbnail_uri"],
"image_uri": row["image_uri"],
"created_at": str(row["created_at"]),
}
for row in rows
]
def _storage_summary(self, config: ImageStudioConfig) -> dict[str, int]:
images_bytes = _directory_size(_resolve_data_path(config.storage.images_path))
thumbnails_bytes = _directory_size(_resolve_data_path(config.storage.thumbnails_path))
materials_bytes = _directory_size(_resolve_data_path(config.storage.materials_path))
temp_bytes = _directory_size(_resolve_data_path(config.storage.temp_path))
return {
"total_bytes": images_bytes + thumbnails_bytes + materials_bytes + temp_bytes,
"images_bytes": images_bytes + thumbnails_bytes,
"materials_bytes": materials_bytes,
"temp_bytes": temp_bytes,
}
def _resolve_data_path(path: str) -> Path:
resolved = Path(path)
if not resolved.is_absolute():
resolved = REPO_ROOT / resolved
return resolved
def _directory_size(path: Path) -> int:
if not path.exists():
return 0
total = 0
for child in path.rglob("*"):
if child.is_file():
total += child.stat().st_size
return total