Get successfull task runs via GraphQL API

Hi everyone,

I would like to use the interactive API to get the number of successful task runs for a given time period e.g. one month. I want to build a flow that keeps track of my task balance and alerts me if I get close to zero on my billing usage.

As far as I know a task is counted towards billing if its state is “Success” and the run-time is greater than or equal to 1sec.

Based on this information I built the following query:

{
  task_run_aggregate(
    where: {
      created: {_lt: "2022-08-01 00:00:00", _gte: "2022-07-01 00:00:00"}, 
      state: {_eq: "Success"}, 
      details: {run_time: {_gte: "00:00:01"}}, 
      tenant_id: {_in: ["list", "of", "tenants"]}}
  ) {
    aggregate {
      count
    }
  }
}

Which works fine and produce an output like:

{
  "data": {
    "task_run_aggregate": {
      "aggregate": {
        "count": 10
      }
    }
  }
}

However, this number is different from the number I see under “Usage” on cloud.prefect.io/admin/account. The number displayed there is higher.

What would be the correct query to get the number of successful tasks that are billed?

Thanks!

1 Like

You should be able to do that with billing_usage query:

{
  billing_usage(order_by: {timestamp: desc}) {
    runs
    timestamp
  }
}

the query gives the number of task runs per day in the current usage cycle – when you then take the sum of it, you should see the same number as in the UI

Thank you! This works.

How could I determine which task (and eventually flow) used how many of these billed runs?
I would like to get the billing stats for my flows to keep track of them.

you would need to make multiple queries and join them; not sure it’s worth it though - seems like the query does what you need to get you an alert, once you get it, you can check the rest in the UI right?

Indeed, this will be sufficient to get an alert. But I would like to also get some accounting going on a flow level. To report which flows use the lion’s share of our resources.

I tried to dig deeper with the snippet you sent and extended it to also get an id:

{
  billing_usage(order_by: {timestamp: desc},
  where: {timestamp: {_gte:"2022-08-13 00:00:00"}}) {
    runs
    timestamp
    description
    kind
    id
  }
}

However, I am having trouble figuring out to which this id corresponds. I tried to match it to other id fields I could query for, but so far I was not successful in figuring out the correct field. Is there maybe some documentation on the different fields somewhere or could you point me in the right direction?

Thanks!

Afaik, only the interactive API docs you can view here:
https://cloud.prefect.io/your_team_slug/api

Thank you, this is what I found:

Which queries where you refereeing to here? I am not afraid of joining and digging, but so far I was not able to either reproduce the reported billing_usage.run numbers or link the billing_usage.id to any other id.

1 Like

You know more about this already than I do :slight_smile:

Again, I wouldn’t dive deeper into this, given that Cloud 2 is LTS product version and it no longer has limits on the number of task runs, but up to you