# Analysis queries

Run a read-only SQL query over your own order, traffic and Trace records, written by you or by a model, in a sandbox that holds only your rows.

An analysis query is one SQL `SELECT` over your own records: order decisions, traffic decisions and Trace
assessments. You can write it yourself, or a model can write it for you, as Suggestions does to test a draft rule.
Either way it runs in a sandbox that holds only your account's rows, so it can't read anyone else's.

## When to use it

- Count how your rules decided over a period, by outcome, rule, country or day.
- Check a draft rule against your labelled orders before you test it properly.
- Answer a one-off question about your traffic or your agents' actions.

## How it works

1. **You send the query and a window.** `POST /v1/account/analysis/queries` with `sql` and `window` (`from` and `to`,
   at most 13 months).
2. **We load your rows.** For each view the query names, we read your account's records in the window into a fresh
   sandbox database. Nothing else is in it: no other account's records and none of our own tables.
3. **The query runs, read-only.** It runs as a role that can only read those views, in a read-only transaction, as
   one statement. Anything that writes, a second statement and the system catalogs are refused.
4. **You get the rows.** At most 1,000 rows and 1 MB come back; past either the result says `truncated`. A query
   still running after 5 seconds is stopped.

## The views

`GET /v1/account/analysis/views` lists every column with its type. Every view has `id` and `created_at`.

| View | Rows | A few columns |
| --- | --- | --- |
| `orders` | Order decisions from checkout assessments | `outcome`, `rule_id`, `order_amount`, `order_currency`, `billing_country`, `label`, `stopped`, `bad`, `clean` |
| `traffic` | Traffic decisions, until they expire | `decision`, `class`, `rule_id`, `agent_verified`, `bot_score` |
| `trace_evals` | Trace assessments | `outcome`, `rule_id`, `agent_id`, `action_tool`, `action_amount` |

On `orders`, `label` is the latest outcome you reported for the order. `stopped` means the order was challenged or
rejected, `bad` that you reported a chargeback, fraud or refund abuse, and `clean` that you reported it legitimate.
Together they give the labelled results:

```sql
SELECT rule_id,
       count(*) FILTER (WHERE stopped AND bad)         AS caught,
       count(*) FILTER (WHERE stopped AND clean)       AS false_alarms,
       count(*) FILTER (WHERE NOT stopped AND bad)     AS missed
FROM orders
GROUP BY rule_id
ORDER BY caught DESC
```

Joins between the views are allowed. Personal columns, such as a billing postal prefix or a request path, can be read
once your account has accepted the risk score data terms; until then they aren't in the sandbox.

## Who can run it

A signed-in member with the Manage policies permission, or an account API key with the scope of each view it reads:
`authorize` for `orders`, `identify` for `traffic`, `trace` for `trace_evals`. Two queries run for an account at once.
Analysis queries aren't billed.

## Errors

| Code | When |
| --- | --- |
| `analysis_query_invalid` (400) | The query isn't one `SELECT`, names a system catalog or no view, or Postgres refused it; or the window is over 13 months or holds more than 50,000 records of one view. The message says which. |
| `analysis_unavailable` (503) | The query passed 5 seconds, or your records couldn't be read. No partial result is returned. |
| `rate_limit_exceeded` (429) | Two queries are already running for your account. |

## Related

- [AI assists](https://provenanceauth.com/docs/account/ai-assists.md)
- [Errors](https://provenanceauth.com/docs/platform/errors.md)
