2026-07-05 00:11:05 +08:00
|
|
|
"""Dashboard summary service."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
2026-07-05 20:38:47 +08:00
|
|
|
from backend.dto.common import ApiError
|
2026-07-05 00:11:05 +08:00
|
|
|
from core.config_service import ConfigService, ImageStudioConfig
|
2026-07-11 21:50:34 +08:00
|
|
|
from core.runtime_paths import resolve_data_path
|
|
|
|
|
from database.db_helper import Database
|
2026-07-05 09:07:34 +08:00
|
|
|
from database.repositories.job_repository import JobRepository
|
2026-07-05 20:38:47 +08:00
|
|
|
from backend.services.media_service import MediaService
|
2026-07-05 00:11:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DashboardService:
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
database: Database,
|
|
|
|
|
config_service: ConfigService | None = None,
|
|
|
|
|
):
|
|
|
|
|
self.database = database
|
|
|
|
|
self.config_service = config_service or ConfigService()
|
2026-07-05 20:38:47 +08:00
|
|
|
self.media_service = MediaService(self.config_service)
|
2026-07-05 00:11:05 +08:00
|
|
|
|
|
|
|
|
def get_summary(self) -> dict[str, Any]:
|
|
|
|
|
config = self.config_service.load()
|
2026-07-05 09:07:34 +08:00
|
|
|
queue_repository = JobRepository(self.database)
|
2026-07-05 00:11:05 +08:00
|
|
|
return {
|
|
|
|
|
"plugin_status": "running",
|
|
|
|
|
"today_generation_count": 0,
|
|
|
|
|
"active_profiles": self._count_active_profiles(),
|
2026-07-05 09:07:34 +08:00
|
|
|
"queue": self._queue_summary(queue_repository),
|
2026-07-05 00:11:05 +08:00
|
|
|
"storage": self._storage_summary(config),
|
2026-07-05 09:07:34 +08:00
|
|
|
"recent_jobs": [task.to_dict() for task in queue_repository.recent(limit=10)],
|
2026-07-05 00:11:05 +08:00
|
|
|
"recent_images": self._recent_images(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def _count_active_profiles(self) -> int:
|
2026-07-05 09:07:34 +08:00
|
|
|
placeholder = self.database._placeholder
|
2026-07-05 00:11:05 +08:00
|
|
|
conn = self.database.connect()
|
|
|
|
|
try:
|
|
|
|
|
row = conn.execute(
|
2026-07-05 09:07:34 +08:00
|
|
|
f"SELECT COUNT(*) AS total FROM character_profiles WHERE status = {placeholder}",
|
2026-07-05 00:11:05 +08:00
|
|
|
("active",),
|
|
|
|
|
).fetchone()
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
return int(row["total"] if row else 0)
|
|
|
|
|
|
2026-07-05 09:07:34 +08:00
|
|
|
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),
|
2026-07-11 21:50:34 +08:00
|
|
|
"running": counts.get("running", 0) + counts.get("retrying", 0),
|
2026-07-05 09:07:34 +08:00
|
|
|
"failed": counts.get("failed", 0),
|
|
|
|
|
"dead_letter": counts.get("dead_letter", 0),
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 00:11:05 +08:00
|
|
|
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"],
|
2026-07-05 20:38:47 +08:00
|
|
|
"thumbnail_url": self._signed_url(row["thumbnail_uri"]),
|
|
|
|
|
"image_url": self._signed_url(row["image_uri"]),
|
2026-07-05 00:11:05 +08:00
|
|
|
"created_at": str(row["created_at"]),
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
2026-07-05 20:38:47 +08:00
|
|
|
def _signed_url(self, uri: str | None) -> str | None:
|
|
|
|
|
if not uri:
|
|
|
|
|
return None
|
|
|
|
|
try:
|
|
|
|
|
return self.media_service.sign_media(uri)["url"]
|
|
|
|
|
except ApiError:
|
|
|
|
|
return None
|
|
|
|
|
|
2026-07-05 00:11:05 +08:00
|
|
|
def _storage_summary(self, config: ImageStudioConfig) -> dict[str, int]:
|
2026-07-11 21:50:34 +08:00
|
|
|
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))
|
2026-07-05 00:11:05 +08:00
|
|
|
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 _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
|