File size: 7,660 Bytes
7dd0d2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""
Background Worker Management API
Provides endpoints to manage and monitor the background data collection worker
"""

from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse
from typing import Dict, Any
import logging

from backend.workers.background_collector_worker import get_worker_instance

logger = logging.getLogger(__name__)

router = APIRouter(prefix="/api/worker", tags=["Background Worker"])


@router.get("/status")
async def get_worker_status():
    """
    Get background worker status and statistics
    
    Returns:
        Worker status including collection stats, schedules, and recent errors
    """
    try:
        worker = await get_worker_instance()
        stats = worker.get_stats()
        
        return JSONResponse(content={
            "success": True,
            "worker_status": stats,
            "message_fa": "وضعیت Worker به موفقیت دریافت شد",
            "message_en": "Worker status retrieved successfully"
        })
        
    except Exception as e:
        logger.error(f"Error getting worker status: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/start")
async def start_worker():
    """
    Start the background worker
    
    Returns:
        Success status
    """
    try:
        worker = await get_worker_instance()
        
        if worker.is_running:
            return JSONResponse(content={
                "success": False,
                "message_fa": "Worker در حال اجرا است",
                "message_en": "Worker is already running"
            })
        
        worker.start()
        
        return JSONResponse(content={
            "success": True,
            "message_fa": "Worker با موفقیت راه‌اندازی شد",
            "message_en": "Worker started successfully",
            "schedules": {
                "ui_data": "Every 5 minutes",
                "historical_data": "Every 15 minutes"
            }
        })
        
    except Exception as e:
        logger.error(f"Error starting worker: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/stop")
async def stop_worker():
    """
    Stop the background worker
    
    Returns:
        Success status
    """
    try:
        worker = await get_worker_instance()
        
        if not worker.is_running:
            return JSONResponse(content={
                "success": False,
                "message_fa": "Worker در حال اجرا نیست",
                "message_en": "Worker is not running"
            })
        
        worker.stop()
        
        return JSONResponse(content={
            "success": True,
            "message_fa": "Worker با موفقیت متوقف شد",
            "message_en": "Worker stopped successfully"
        })
        
    except Exception as e:
        logger.error(f"Error stopping worker: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/force-collection")
async def force_collection(collection_type: str = "both"):
    """
    Force immediate data collection
    
    Args:
        collection_type: Type of collection ('ui', 'historical', or 'both')
    
    Returns:
        Success status
    """
    try:
        if collection_type not in ['ui', 'historical', 'both']:
            raise HTTPException(
                status_code=400,
                detail="Invalid collection_type. Must be 'ui', 'historical', or 'both'"
            )
        
        worker = await get_worker_instance()
        
        if not worker.is_running:
            raise HTTPException(
                status_code=400,
                detail="Worker is not running. Start the worker first."
            )
        
        worker.force_collection(collection_type)
        
        return JSONResponse(content={
            "success": True,
            "message_fa": f"جمع‌آوری {collection_type} با موفقیت آغاز شد",
            "message_en": f"Manual {collection_type} collection started successfully",
            "collection_type": collection_type
        })
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error forcing collection: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/stats")
async def get_collection_stats():
    """
    Get detailed collection statistics
    
    Returns:
        Detailed statistics about data collections
    """
    try:
        worker = await get_worker_instance()
        stats = worker.get_stats()
        
        return JSONResponse(content={
            "success": True,
            "statistics": {
                "total_ui_collections": stats['ui_collections'],
                "total_historical_collections": stats['historical_collections'],
                "total_records_saved": stats['total_records_saved'],
                "last_ui_collection": stats['last_ui_collection'],
                "last_historical_collection": stats['last_historical_collection'],
                "average_records_per_ui_collection": (
                    stats['total_records_saved'] / stats['ui_collections']
                    if stats['ui_collections'] > 0 else 0
                ),
                "average_records_per_historical_collection": (
                    stats['total_records_saved'] / stats['historical_collections']
                    if stats['historical_collections'] > 0 else 0
                )
            },
            "recent_errors": stats['recent_errors'],
            "message_fa": "آمار جمع‌آوری داده‌ها",
            "message_en": "Data collection statistics"
        })
        
    except Exception as e:
        logger.error(f"Error getting collection stats: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/schedules")
async def get_schedules():
    """
    Get current collection schedules
    
    Returns:
        Schedule information for all collection jobs
    """
    try:
        worker = await get_worker_instance()
        stats = worker.get_stats()
        
        return JSONResponse(content={
            "success": True,
            "schedules": stats['scheduler_jobs'],
            "message_fa": "زمان‌بندی جمع‌آوری داده‌ها",
            "message_en": "Data collection schedules"
        })
        
    except Exception as e:
        logger.error(f"Error getting schedules: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/health")
async def worker_health_check():
    """
    Health check for background worker
    
    Returns:
        Health status
    """
    try:
        worker = await get_worker_instance()
        
        is_healthy = worker.is_running
        
        return JSONResponse(content={
            "success": True,
            "healthy": is_healthy,
            "status": "running" if is_healthy else "stopped",
            "message_fa": "Worker سالم است" if is_healthy else "Worker متوقف است",
            "message_en": "Worker is healthy" if is_healthy else "Worker is stopped"
        })
        
    except Exception as e:
        logger.error(f"Error in health check: {e}")
        return JSONResponse(
            status_code=503,
            content={
                "success": False,
                "healthy": False,
                "status": "error",
                "error": str(e),
                "message_fa": "خطا در بررسی سلامت Worker",
                "message_en": "Error checking worker health"
            }
        )