Kamal
11/01/2024, 1:15 PMMarvin
11/01/2024, 1:15 PMKamal
11/01/2024, 1:15 PMKamal
11/01/2024, 1:16 PMMarvin
11/01/2024, 1:16 PMpython
from prefect import Client
from prefect.orion.schemas.states import StateType
from datetime import datetime, timedelta
# Initialize the client
client = Client()
# Define the time period
end_time = datetime.utcnow()
start_time = end_time - timedelta(days=7) # for example, the past week
# Retrieve flow runs
flow_runs = client.graphql(
{'query': """
query {
flow_run(where: {state: {_eq: "Failed"}, created: {_gte: $start_time, _lte: $end_time}}) {
id
name
created
state
}
}
""", "variables": {"start_time": start_time.isoformat(), "end_time": end_time.isoformat()}}
)
for run in flow_runs.data.flow_run:
print(f"Flow Run ID: {run.id}, Name: {run.name}, Created: {run.created}")
# Remember to handle pagination if you have many runs!
Make sure to replace the query and variable fields according to your needs, especially if your API client or structure differs slightly. Also, ensure that your client connection settings match your Prefect setup.
You can find more details and examples in the Prefect documentation related to client usage and state management. If you encounter issues or need further customization, you might want to check relevant GitHub issues as well.Kamal
11/01/2024, 1:23 PMMarvin
11/01/2024, 1:24 PMpython
from prefect import Client
from datetime import datetime, timedelta
# Initialize the Prefect Client
client = Client()
# Define the time period
end_time = datetime.utcnow()
start_time = end_time - timedelta(days=7) # Adjust the period as needed
# Query for flow runs
flow_runs = client.graphql("""
query {
flow_run(where: {
state: {_eq: "Failed"},
start_time: {_gte: $start_time, _lte: $end_time}
}) {
id
name
start_time
state
}
}
""", variables={"start_time": start_time.isoformat(), "end_time": end_time.isoformat()})
# Print the failed flow runs
for flow_run in flow_runs.data.flow_run:
print(f"Flow Run ID: {flow_run['id']}, Name: {flow_run['name']}, Start Time: {flow_run['start_time']}")
In Prefect 0.x, you'll need:
- To authenticate the client properly if you're using Prefect Cloud.
- To adjust your GraphQL queries according to the API schema.
Make sure you adapt the query and field names to match those used in your specific Prefect deployment, as these might differ slightly based on your setup or version.
For more details, you may want to refer to the Prefect 0.x documentation and GitHub issues related to querying flow runs and handling states. If you have any trouble or specific needs, these resources can be very helpful.Kamal
11/01/2024, 1:26 PMMarvin
11/01/2024, 1:26 PMpython
from prefect import Client
from prefect.orion.schemas.states import StateType
from datetime import datetime, timedelta
# Connect to the Prefect Client
client = Client()
# Define the time period
end_time = datetime.utcnow()
start_time = end_time - timedelta(days=7) # Example: last 7 days
# Querying flow runs that are not completed
incomplete_states = [StateType.PENDING, StateType.RUNNING, StateType.FAILED, StateType.CANCELLED]
flow_runs = client.graphql(
{'query': """
query {
flow_run(where: {
state: {_in: $states},
created: {_gte: $start_time, _lte: $end_time}
}) {
id
name
created
state
}
}
""", "variables": {"states": incomplete_states, "start_time": start_time.isoformat(), "end_time": end_time.isoformat()}}
)
for run in flow_runs.data.flow_run:
print(f"Flow Run ID: {run.id}, Name: {run.name}, Created: {run.created}, State: {run.state}")
### Prefect 3.x
For Prefect 3.x, the process is similar, but there might be slight differences since the client and API may have additional features or changes. Ensure you adjust your client connection and queries according to any updates or changes in API methods. Check the latest Prefect documentation for 3.x for any specific methods or changes.
Common Steps for Prefect 3.x:
1. Use the Prefect Client: Initialize and connect to the Prefect instance.
2. Define State Filters and Time Range: Use filtering options to select flow runs that are not in the "Completed" state within your specific time frame.
For more detailed instructions or any changes, please refer to the Prefect documentation and the Prefect GitHub issues for specific queries and troubleshooting tips.