33 lines
No EOL
1.4 KiB
Python
33 lines
No EOL
1.4 KiB
Python
import redis
|
|
import json
|
|
from typing import List, Dict, Any
|
|
from datetime import datetime
|
|
from ..models.container import ContainerLog
|
|
|
|
class RedisService:
|
|
def __init__(self):
|
|
self.redis = redis.Redis(host='localhost', port=6379, db=0)
|
|
self.logs_key_prefix = "container_logs:"
|
|
|
|
async def get_logs(self, container_id: str, limit: int = 100) -> List[ContainerLog]:
|
|
"""Récupère les logs d'un conteneur depuis Redis."""
|
|
key = f"{self.logs_key_prefix}{container_id}"
|
|
logs = self.redis.lrange(key, 0, limit - 1)
|
|
return [ContainerLog(**json.loads(log)) for log in logs]
|
|
|
|
async def add_log(self, container_id: str, log: ContainerLog) -> None:
|
|
"""Ajoute un log pour un conteneur dans Redis."""
|
|
key = f"{self.logs_key_prefix}{container_id}"
|
|
self.redis.lpush(key, log.model_dump_json())
|
|
# Garder seulement les 1000 derniers logs
|
|
self.redis.ltrim(key, 0, 999)
|
|
|
|
async def clear_logs(self, container_id: str) -> None:
|
|
"""Supprime tous les logs d'un conteneur."""
|
|
key = f"{self.logs_key_prefix}{container_id}"
|
|
self.redis.delete(key)
|
|
|
|
async def get_container_ids(self) -> List[str]:
|
|
"""Récupère la liste des IDs de conteneurs ayant des logs."""
|
|
keys = self.redis.keys(f"{self.logs_key_prefix}*")
|
|
return [key.replace(self.logs_key_prefix, "") for key in keys] |