File size: 3,124 Bytes
a42b16d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Real-time deployment status monitoring."""

from __future__ import annotations

import time
from typing import Any, Dict, List, Optional


class DeploymentMonitor:
    """Monitors deployment status in real-time."""

    def __init__(self):
        self.deployments: Dict[str, Dict[str, Any]] = {}

    def track_deployment(
        self,
        deployment_id: str,
        platform: str,
        repo: str,
        status: str = "initiated"
    ) -> Dict[str, Any]:
        """Track a new deployment."""
        deployment = {
            "id": deployment_id,
            "platform": platform,
            "repo": repo,
            "status": status,
            "started_at": time.time(),
            "updated_at": time.time(),
            "stages": [],
            "logs": []
        }
        self.deployments[deployment_id] = deployment
        return deployment

    def update_deployment_status(
        self,
        deployment_id: str,
        status: str,
        stage: Optional[str] = None,
        log_message: Optional[str] = None
    ) -> Dict[str, Any]:
        """Update deployment status."""
        if deployment_id not in self.deployments:
            return {"error": "Deployment not found"}

        deployment = self.deployments[deployment_id]
        deployment["status"] = status
        deployment["updated_at"] = time.time()

        if stage:
            deployment["stages"].append({
                "name": stage,
                "status": status,
                "timestamp": time.time()
            })

        if log_message:
            deployment["logs"].append({
                "message": log_message,
                "timestamp": time.time(),
                "level": "info" if status == "success" else "warning" if status == "pending" else "error"
            })

        return deployment

    def get_deployment_status(self, deployment_id: str) -> Dict[str, Any]:
        """Get current deployment status."""
        if deployment_id not in self.deployments:
            return {"error": "Deployment not found"}

        deployment = self.deployments[deployment_id]
        elapsed = time.time() - deployment["started_at"]

        return {
            **deployment,
            "elapsed_time": f"{elapsed:.1f}s",
            "is_complete": deployment["status"] in ["success", "failed", "cancelled"],
            "is_running": deployment["status"] in ["initiated", "building", "deploying"]
        }

    def get_status_summary(self) -> Dict[str, Any]:
        """Get summary of all deployments."""
        total = len(self.deployments)
        running = len([d for d in self.deployments.values() if d["status"] in ["initiated", "building", "deploying"]])
        successful = len([d for d in self.deployments.values() if d["status"] == "success"])
        failed = len([d for d in self.deployments.values() if d["status"] == "failed"])

        return {
            "total_deployments": total,
            "running": running,
            "successful": successful,
            "failed": failed,
            "success_rate": (successful / total * 100) if total > 0 else 0
        }