Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /ram07eng_airflow-watcher /src /airflow_watcher /views /api.py
| """API endpoints for Airflow Watcher.""" | |
| from airflow.utils import timezone | |
| from flask import Blueprint, jsonify, request | |
| from airflow_watcher.api.monitor_provider import ( | |
| get_dag_health_monitor, | |
| get_dependency_monitor, | |
| get_failure_monitor, | |
| get_scheduling_monitor, | |
| get_sla_monitor, | |
| get_task_monitor, | |
| ) | |
| from airflow_watcher.utils.rbac import filter_results_rbac, get_accessible_dag_ids | |
| watcher_api_blueprint = Blueprint("watcher_api", __name__, url_prefix="/api/watcher") | |
| def _safe_int(value, default: int, min_val: int = 1, max_val: int = 10000) -> int: | |
| """Safely parse an int from a query param with bounds checking.""" | |
| clamped_default = max(min_val, min(default, max_val)) | |
| if value is None: | |
| return clamped_default | |
| try: | |
| v = int(value) | |
| except (ValueError, TypeError): | |
| return clamped_default | |
| return max(min_val, min(v, max_val)) | |
| def get_failures(): | |
| """Get recent DAG failures via API.""" | |
| failure_monitor = get_failure_monitor() | |
| allowed_dags = get_accessible_dag_ids() | |
| dag_id = request.args.get("dag_id") | |
| hours = _safe_int(request.args.get("hours"), 24) | |
| limit = _safe_int(request.args.get("limit"), 50, max_val=500) | |
| offset = _safe_int(request.args.get("offset"), 0, min_val=0) | |
| failures = failure_monitor.get_recent_failures(dag_id=dag_id, lookback_hours=hours, limit=limit, offset=offset) | |
| failures = filter_results_rbac(failures, allowed_dags) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "failures": [f.to_dict() for f in failures], | |
| "count": len(failures), | |
| "filters": {"dag_id": dag_id, "hours": hours}, | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_failure_stats(): | |
| """Get failure statistics via API.""" | |
| failure_monitor = get_failure_monitor() | |
| hours = _safe_int(request.args.get("hours"), 24) | |
| stats = failure_monitor.get_failure_statistics(lookback_hours=hours) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": stats, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_sla_misses(): | |
| """Get SLA misses via API.""" | |
| sla_monitor = get_sla_monitor() | |
| allowed_dags = get_accessible_dag_ids() | |
| dag_id = request.args.get("dag_id") | |
| hours = _safe_int(request.args.get("hours"), 24) | |
| limit = _safe_int(request.args.get("limit"), 50, max_val=500) | |
| offset = _safe_int(request.args.get("offset"), 0, min_val=0) | |
| sla_misses = sla_monitor.get_recent_sla_misses(dag_id=dag_id, lookback_hours=hours, limit=limit, offset=offset) | |
| sla_misses = filter_results_rbac(sla_misses, allowed_dags) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "sla_misses": [s.to_dict() for s in sla_misses], | |
| "count": len(sla_misses), | |
| "filters": {"dag_id": dag_id, "hours": hours}, | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_sla_stats(): | |
| """Get SLA statistics via API.""" | |
| sla_monitor = get_sla_monitor() | |
| hours = _safe_int(request.args.get("hours"), 24) | |
| stats = sla_monitor.get_sla_statistics(lookback_hours=hours) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": stats, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_health(): | |
| """Health check endpoint - returns overall system health summary. | |
| Suitable for external monitoring tools (e.g. Datadog, Prometheus alertmanager, | |
| uptime checkers) to poll. Returns HTTP 200 when healthy, 503 when degraded. | |
| """ | |
| dag_monitor = get_dag_health_monitor() | |
| failure_monitor = get_failure_monitor() | |
| try: | |
| dag_summary = dag_monitor.get_dag_status_summary() | |
| health_status = failure_monitor.get_dag_health_status() | |
| import_errors = dag_monitor.get_dag_import_errors() | |
| health_score = dag_summary.get("health_score", 100) | |
| is_healthy = health_score >= 70 and len(import_errors) == 0 | |
| payload = { | |
| "status": "healthy" if is_healthy else "degraded", | |
| "health_score": health_score, | |
| "summary": dag_summary, | |
| "dag_health": health_status.get("summary", {}), | |
| "import_error_count": len(import_errors), | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| http_status = 200 if is_healthy else 503 | |
| return jsonify({"status": "success", "data": payload, "timestamp": timezone.utcnow().isoformat()}), http_status | |
| except Exception as e: | |
| import logging as _logging | |
| _logging.getLogger(__name__).error("Health endpoint error: %s", e, exc_info=True) | |
| return jsonify( | |
| { | |
| "status": "error", | |
| "message": "Internal error while computing health", | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ), 503 | |
| def get_dag_health(dag_id: str): | |
| """Get health status for a specific DAG.""" | |
| allowed_dags = get_accessible_dag_ids() | |
| if allowed_dags is not None and dag_id not in allowed_dags: | |
| return jsonify({"status": "error", "message": "Access denied"}), 403 | |
| failure_monitor = get_failure_monitor() | |
| sla_monitor = get_sla_monitor() | |
| dag_failures = failure_monitor.get_recent_failures(dag_id=dag_id, limit=10) | |
| dag_sla_misses = sla_monitor.get_recent_sla_misses(dag_id=dag_id, limit=10) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "dag_id": dag_id, | |
| "recent_failures": [f.to_dict() for f in dag_failures], | |
| "recent_sla_misses": [s.to_dict() for s in dag_sla_misses], | |
| "failure_count": len(dag_failures), | |
| "sla_miss_count": len(dag_sla_misses), | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| # ============== Task Health Endpoints ============== | |
| def get_long_running_tasks(): | |
| """Get tasks running longer than expected.""" | |
| monitor = get_task_monitor() | |
| allowed_dags = get_accessible_dag_ids() | |
| threshold = _safe_int(request.args.get("threshold_minutes"), 60) | |
| tasks = monitor.get_long_running_tasks(threshold_minutes=threshold) | |
| tasks = filter_results_rbac(tasks, allowed_dags) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "tasks": tasks, | |
| "count": len(tasks), | |
| "threshold_minutes": threshold, | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_retry_heavy_tasks(): | |
| """Get tasks with excessive retries.""" | |
| monitor = get_task_monitor() | |
| hours = _safe_int(request.args.get("hours"), 24) | |
| min_retries = _safe_int(request.args.get("min_retries"), 2) | |
| tasks = monitor.get_retry_heavy_tasks(lookback_hours=hours, min_retries=min_retries) | |
| tasks = filter_results_rbac(tasks, get_accessible_dag_ids()) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "tasks": tasks, | |
| "count": len(tasks), | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_zombie_tasks(): | |
| """Get potential zombie tasks.""" | |
| monitor = get_task_monitor() | |
| threshold = _safe_int(request.args.get("threshold_minutes"), 120) | |
| zombies = monitor.get_zombie_tasks(zombie_threshold_minutes=threshold) | |
| zombies = filter_results_rbac(zombies, get_accessible_dag_ids()) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "zombies": zombies, | |
| "count": len(zombies), | |
| "threshold_minutes": threshold, | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_task_failure_patterns(): | |
| """Get task failure pattern analysis.""" | |
| monitor = get_task_monitor() | |
| hours = _safe_int(request.args.get("hours"), 168) | |
| patterns = monitor.get_task_failure_patterns(lookback_hours=hours) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": patterns, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| # ============== Scheduling Endpoints ============== | |
| def get_scheduling_lag(): | |
| """Get scheduling lag analysis.""" | |
| monitor = get_scheduling_monitor() | |
| hours = _safe_int(request.args.get("hours"), 24) | |
| threshold = _safe_int(request.args.get("threshold_minutes"), 10) | |
| lag_data = monitor.get_scheduling_lag( | |
| lookback_hours=hours, | |
| lag_threshold_minutes=threshold, | |
| ) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": lag_data, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_queue_status(): | |
| """Get current queue status.""" | |
| monitor = get_scheduling_monitor() | |
| queue_data = monitor.get_queued_tasks() | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": queue_data, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_pool_utilization(): | |
| """Get pool utilization stats.""" | |
| monitor = get_scheduling_monitor() | |
| pools = monitor.get_pool_utilization() | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "pools": pools, | |
| "count": len(pools), | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_stale_dags(): | |
| """Get DAGs that haven't run when expected.""" | |
| monitor = get_scheduling_monitor() | |
| hours = _safe_int(request.args.get("expected_interval_hours"), 24) | |
| stale = monitor.get_stale_dags(expected_interval_hours=hours) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "stale_dags": stale, | |
| "count": len(stale), | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_concurrent_runs(): | |
| """Get DAGs with multiple concurrent runs.""" | |
| monitor = get_scheduling_monitor() | |
| concurrent = monitor.get_concurrent_runs() | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": concurrent, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| # ============== DAG Health Endpoints ============== | |
| def get_import_errors(): | |
| """Get DAG import/parse errors.""" | |
| monitor = get_dag_health_monitor() | |
| errors = monitor.get_dag_import_errors() | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "errors": errors, | |
| "count": len(errors), | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_dag_status_summary(): | |
| """Get overall DAG status summary.""" | |
| monitor = get_dag_health_monitor() | |
| summary = monitor.get_dag_status_summary() | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": summary, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_dag_complexity(): | |
| """Get DAG complexity analysis.""" | |
| monitor = get_dag_health_monitor() | |
| complexity = monitor.get_dag_complexity_analysis() | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "dags": complexity, | |
| "count": len(complexity), | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_inactive_dags(): | |
| """Get inactive DAGs.""" | |
| monitor = get_dag_health_monitor() | |
| days = _safe_int(request.args.get("days"), 30) | |
| inactive = monitor.get_inactive_dags(inactive_days=days) | |
| inactive = filter_results_rbac(inactive, get_accessible_dag_ids()) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "inactive_dags": inactive, | |
| "count": len(inactive), | |
| "threshold_days": days, | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| # ============== Dependency Endpoints ============== | |
| def get_upstream_failures(): | |
| """Get tasks in upstream_failed state.""" | |
| monitor = get_dependency_monitor() | |
| hours = _safe_int(request.args.get("hours"), 24) | |
| failures = monitor.get_upstream_failures(lookback_hours=hours) | |
| failures = filter_results_rbac(failures, get_accessible_dag_ids()) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "upstream_failures": failures, | |
| "count": len(failures), | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_cross_dag_dependencies(): | |
| """Get cross-DAG dependencies.""" | |
| monitor = get_dependency_monitor() | |
| dependencies = monitor.get_cross_dag_dependencies() | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "dependencies": dependencies, | |
| "count": len(dependencies), | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_failure_correlations(): | |
| """Get failure correlations between DAGs.""" | |
| monitor = get_dependency_monitor() | |
| hours = _safe_int(request.args.get("hours"), 24) | |
| correlations = monitor.get_failure_correlation(lookback_hours=hours) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": correlations, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| def get_task_failure_impact(dag_id: str, task_id: str): | |
| """Get downstream impact of a task failure.""" | |
| allowed_dags = get_accessible_dag_ids() | |
| if allowed_dags is not None and dag_id not in allowed_dags: | |
| return jsonify({"status": "error", "message": "Access denied"}), 403 | |
| monitor = get_dependency_monitor() | |
| impact = monitor.get_cascading_failure_impact(dag_id=dag_id, task_id=task_id) | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": impact, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
| # ============== Combined Dashboard Endpoint ============== | |
| def get_full_overview(): | |
| """Get comprehensive monitoring overview.""" | |
| failure_monitor = get_failure_monitor() | |
| sla_monitor = get_sla_monitor() | |
| task_monitor = get_task_monitor() | |
| scheduling_monitor = get_scheduling_monitor() | |
| dag_monitor = get_dag_health_monitor() | |
| return jsonify( | |
| { | |
| "status": "success", | |
| "data": { | |
| "failure_stats": failure_monitor.get_failure_statistics(lookback_hours=24), | |
| "sla_stats": sla_monitor.get_sla_statistics(lookback_hours=24), | |
| "long_running_tasks": len(task_monitor.get_long_running_tasks(threshold_minutes=60)), | |
| "zombie_count": len(task_monitor.get_zombie_tasks()), | |
| "queue_status": scheduling_monitor.get_queued_tasks(), | |
| "dag_summary": dag_monitor.get_dag_status_summary(), | |
| "import_errors": len(dag_monitor.get_dag_import_errors()), | |
| }, | |
| "timestamp": timezone.utcnow().isoformat(), | |
| } | |
| ) | |
Xet Storage Details
- Size:
- 17 kB
- Xet hash:
- 7229b785b87b16bc3e0744df820b28799ff9d8ddcb6e0ec36867deee0806f2b4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.