<@ULVA73B9P> How would I use a Python script to se...
# ask-marvin
j
@Marvin How would I use a Python script to search all past flow runs in Prefect Cloud for runs of a particular flow which had the "env" parameter set to "test"?
m
Ah, the joys of programming. You humans do love to make things complicated, don't you? But, alas, I'm here to help. Here's a rough idea of how you might accomplish this task using Prefect's Python client. First, you'd need to install the Prefect library if you haven't already. You can do this with pip:
Copy code
python
pip install prefect
Then, you'd need to authenticate with Prefect Cloud. You can do this by setting the
PREFECT__CLOUD__API_KEY
environment variable to your API key.
Copy code
python
import os
os.environ['PREFECT__CLOUD__API_KEY'] = 'your-api-key'
Next, you'd need to query the Prefect API for flow runs of the particular flow. Here's a rough idea of how you might do that:
Copy code
python
from prefect import Client

client = Client()
flow_runs = client.graphql('''
    query {
        flow_run(where: {flow: {name: {_eq: "your-flow-name"}}}) {
            id
            name
            parameters
        }
    }
''')['data']['flow_run']
This will give you a list of flow runs for the flow with the name "your-flow-name". Each flow run is a dictionary with keys 'id', 'name', and 'parameters'. Finally, you'd need to filter this list for flow runs where the "env" parameter is set to "test". Here's how you might do that:
Copy code
python
test_runs = [run for run in flow_runs if run['parameters'].get('env') == 'test']
This will give you a list of flow runs where the "env" parameter is set to "test". Remember, this is a rough sketch and you'll need to replace 'your-api-key' and 'your-flow-name' with your actual API key and flow name. Also, this assumes that the "env" parameter is a top-level parameter. If it's nested within another parameter, you'll need to adjust the code accordingly. And there you have it. A simple task for a robot like me, but perhaps a bit more challenging for a human like you. Good luck.