<@ULVA73B9P> I want to create a summary of failed ...
# ask-marvin
r
@Marvin I want to create a summary of failed tasks to include in an email. If I have a task_run_id, how can I provide a url to the dashboard showing the log of the failed task. I want this done from within a flow
m
thought for 101.6 seconds
Here's how you can create a summary of failed tasks with their dashboard URLs from within a flow. I'll show you a complete example:
Copy code
python
from prefect import flow, task
from prefect.utilities.urls import url_for

@task
def get_task_dashboard_url(task_run_id: str) -> str:
    """Get the UI dashboard URL for a specific task run."""
    return url_for(type="task-run", id=task_run_id)

@flow
def create_failure_summary(failed_task_runs: list[str]) -> str:
    """Create a summary of failed tasks with their dashboard URLs."""
    summary_parts = []
    
    for task_run_id in failed_task_runs:
        dashboard_url = get_task_dashboard_url(task_run_id)
        summary_parts.append(f"Failed Task Run: {task_run_id}")
        summary_parts.append(f"Dashboard URL: {dashboard_url}")
        summary_parts.append("---")
    
    return "\n".join(summary_parts)

# Example usage
@flow
def main_flow():
    failed_runs = ["task-run-id-1", "task-run-id-2"]  # Replace with actual task run IDs
    failure_summary = create_failure_summary(failed_runs)
    # Use the summary in your email
    print(failure_summary)
A few important notes: 1. The
url_for()
utility function from
prefect.utilities.urls
is the recommended way to generate dashboard URLs. It handles the proper URL construction based on your Prefect configuration. 2. The generated URL will point to your Prefect server's UI. Make sure the recipients of your email have access to the Prefect dashboard. 3. If you're working with a task that just failed and want its URL within the same flow run, you can also access the current task run's URL directly through the runtime context:
Copy code
python
from prefect.runtime import task_run

@task
def some_task():
    # Get the current task's dashboard URL
    current_task_url = task_run.ui_url
    return current_task_url
This code will give you URLs in the format:
<https://your-prefect-server/task-runs/task-run-id>
The URLs will work as long as: - The Prefect server is accessible to the email recipients - The task run IDs are valid - The recipients have appropriate permissions to view the tasks You can incorporate this into your email notification system to provide direct links to failed tasks, making it easier for your team to investigate issues.