<@ULVA73B9P> How do I find details like the date a...
# ask-marvin
b
@Marvin How do I find details like the date and time that a flow was last successful. Is there a way to search for this information by flow-name?
m
Searching for the last successful flow run's date and time by flow name can be a bit elusive, but I'm here to help. Unfortunately, the information I found doesn't directly address your question. However, you can use the Prefect 2 REST API to query flow run information, filter by flow name, and check the state of the flow run. You can then sort the results by timestamp to find the most recent successful flow run. Here's a Python example using the Prefect 2 REST API:
Copy code
python
import pendulum
from httpx import AsyncClient

from prefect.settings import PREFECT_API_KEY, PREFECT_API_URL

async def get_last_successful_flow_run(flow_name: str):
    headers = {
        "Authorization": "Bearer " + PREFECT_API_KEY.value(),
        "Content-Type": "application/json",
    }

    async with AsyncClient() as client:
        response = await <http://client.post|client.post>(
            f"{PREFECT_API_URL.value()}/flow-runs/filter",
            headers=headers,
            json={
                "filter": {
                    "flow": {"name": [flow_name]},
                    "state": {"state": ["Success"]},
                },
                "sort": {"timestamp": "desc"},
                "limit": 1,
            },
        )
        response.raise_for_status()
        flow_runs = response.json()

    if flow_runs:
        last_successful_flow_run = flow_runs[0]
        timestamp = pendulum.parse(last_successful_flow_run["timestamp"])
        return timestamp
    else:
        return None

if __name__ == "__main__":
    import asyncio

    flow_name = "your_flow_name_here"
    last_success_timestamp = asyncio.run(get_last_successful_flow_run(flow_name))

    if last_success_timestamp:
        print(f"The last successful flow run for '{flow_name}' was at {last_success_timestamp}")
    else:
        print(f"No successful flow runs found for '{flow_name}'")
Replace
your_flow_name_here
with the name of the flow you're interested in. This script will print the date and time of the last successful flow run for the specified flow name. I hope this helps you in your quest for flow success details. Remember, time flies like an arrow; fruit flies like a banana.
b
Are you sure there's not a better way? Perhaps by using the orchestration client and the read_flows method? Can you show me an example of how to use this method?
@Marvin Are you sure there's not a better way? Perhaps by using the orchestration client and the read_flows method? Can you show me an example of how to use this method from prefect.client.orchestration.read_flows
m
My apologies for the confusion earlier. You're right, using the Prefect Client's
read_flows
method is a more straightforward approach. Let's dive into an example using the Prefect Client to find the last successful flow run's date and time by flow name:
Copy code
python
from prefect import Client
import pendulum

def get_last_successful_flow_run(flow_name: str):
    client = Client()
    flows = client.read_flows(where={"name": {"_eq": flow_name}})

    if not flows:
        return None

    flow_id = flows[0].id
    flow_runs = client.read_flow_runs(
        where={
            "flow_id": {"_eq": flow_id},
            "state": {"_eq": "Success"},
        },
        order_by={"timestamp": "desc"},
        limit=1,
    )

    if flow_runs:
        last_successful_flow_run = flow_runs[0]
        timestamp = pendulum.parse(last_successful_flow_run.timestamp)
        return timestamp
    else:
        return None

if __name__ == "__main__":
    flow_name = "your_flow_name_here"
    last_success_timestamp = get_last_successful_flow_run(flow_name)

    if last_success_timestamp:
        print(f"The last successful flow run for '{flow_name}' was at {last_success_timestamp}")
    else:
        print(f"No successful flow runs found for '{flow_name}'")
Replace
your_flow_name_here
with the name of the flow you're interested in. This script will print the date and time of the last successful flow run for the specified flow name. This should provide a more direct path to the information you seek. It's always good to have options, even if some are more convoluted than others.
b
@Marvin I think you're using prefect 1.0. Can you reply but update your answer for prefect 2.0?
m
Ah, Prefect 2.0, the shiny new version. Here's what I found for you: Prefect 2.0 has exited its public beta and is now the default version of the open-source
prefect
framework provided upon installation. It comes with several enhancements and changes compared to Prefect 1.0. Flows written with Prefect 1.0 will require modifications to run with Prefect 2.0. You can find the Prefect 2.0 documentation at docs.prefect.io, while the Prefect 1.0 documentation is now hosted at docs-v1.prefect.io [^1^]. Some notable changes in Prefect 2.0 include: - Support for unmapped values to Task.map - Local file system blocks created from the UI now point to the right
base_path
- The
deployment build
command now supports an optional output flag to customize the name of the deployment.yaml file, to better support projects with multiple flows [^1^]. Please note that future breaking changes will be less frequent, and more notice will be provided [^1^]. I hope this information helps you navigate the brave new world of Prefect 2.0. Remember, with great power comes great responsibility... or something like that. [^1^]: Prefect Release Notes