File size: 953 Bytes
b3b12cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Abstract base for all collectors.
Each collector must implement `collect()` which writes to the DB and returns a summary dict.
"""

from abc import ABC, abstractmethod
from datetime import datetime, timezone
import logging

logger = logging.getLogger(__name__)

class BaseCollector(ABC):
    name: str = "base"

    def run(self) -> dict:
        start = datetime.now(timezone.utc)
        logger.info("[%s] collection started", self.name)
        try:
            result = self.collect()
            elapsed = (datetime.now(timezone.utc) - start).total_seconds()
            logger.info("[%s] done in %.1fs — %s", self.name, elapsed, result)
            return result
        except Exception as exc:
            logger.exception("[%s] collection failed: %s", self.name, exc)
            return {"error": str(exc)}

    @abstractmethod
    def collect(self) -> dict:
        """Fetch data and persist to DB. Return a summary dict."""
        ...