> For the complete documentation index, see [llms.txt](https://docs.erathos.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.erathos.com/api/concepts/rules.md).

# Rules

## Overview

If you prefer to send a webhook only when predefined criteria are met, you can set up rules on it. The rules consist of three components: `variable_name`, `operation`, and `value`. Every rule must pass for the webhook to be sent; a webhook with no rules is sent after every run, successful or failed.

The available operations, and how the value must be written, vary depending on the data type of the variable:

| Type                | Operations                                                        | Value format                  |
| ------------------- | ----------------------------------------------------------------- | ----------------------------- |
| `uuid` and `string` | `EQUAL`, `NOT_EQUAL`, `IN`, `NOT_IN`                              | Any text                      |
| `integer`           | `EQUAL`, `NOT_EQUAL`, `IN`, `NOT_IN`, `LESS_THAN`, `GREATER_THAN` | A number, e.g. `"0"`          |
| `date` and `time`   | `LESS_THAN`, `GREATER_THAN`                                       | `YYYY-MM-DD` / `HH:MM` in UTC |
| `boolean`           | `EQUAL`, `NOT_EQUAL`                                              | `"true"` or `"false"`         |

`IN` and `NOT_IN` take a list of values, e.g. `["Erathos Schedule", "ana@acme.com"]`; values can contain spaces and commas. Every other operation takes a single value.

Rules are provided via the `rules` attribute, when creating a webhook or updating it (an update replaces all of its rules). Below is an example of a body with multiple rules for reference:

```json
{   
    "description": "airflow_trigger_example",
    "is_active": true,
    "jobs": [
        "uuid_1", 
        "uuid_2", 
        ..., 
        "uuid_n"
    ],
    "method": "POST",
    "url": "https://${{variables.AIRFLOW_HOST}}/api/v1/dags/${{erathos.job_id}}/dagRuns",
    "header": {
        "Content-Type": "application/json",
        "Authorization": "Basic ${{secrets.AIRFLOW_KEY}}"
    },
    "body": {
        "conf": {
            "name": "${{erathos.table_name}}",
            "status": "${{erathos.status}}"
        }
    },
    "rules": [
        {
            "variable_name": "STATUS",
            "operation": "EQUAL",
            "value": "FINISHED"
        },
        {
            "variable_name": "ROWS",
            "operation": "GREATER_THAN",
            "value": "0"
        },
        {
            "variable_name": "FINISHED_AT_TIME",
            "operation": "GREATER_THAN",
            "value": "00:00"
        },
        {
            "variable_name": "FINISHED_AT_TIME",
            "operation": "LESS_THAN",
            "value": "08:00"
        },
        {
            "variable_name": "FINISHED_AT_WEEKDAY",
            "operation": "NOT_IN",
            "value": ["0", "6"]
        }
    ]
}
```

To discover the variables available for creating rules, refer to [Metadata](https://docs.erathos.com/api/concepts/metadata.md).

## Examples

Below are groups of frequently used rules that can be included in your custom webhooks and combined with the webhook templates presented next.

### Execution status

#### Success

Every successful execution that has encountered any data.

```json
{
  ...
  "rules": [
    {
      "variable_name": "STATUS",
      "operation": "EQUAL",
      "value": "FINISHED"
    },
    {
      "variable_name": "ROWS",
      "operation": "GREATER_THAN",
      "value": "0"
    }
  ],
  ...
}
```

#### Failed

Every execution that results in a failure.

```json
{
  ...
  "rules": [
    {
      "variable_name": "STATUS",
      "operation": "EQUAL",
      "value": "FAILED"
    }
  ],
  ...
}
```

### Dependent jobs

Filter only the main tables (endpoints with nested tables generate a separate webhook for each table when this rule is not used).

```json
{
  ...
  "rules": [
    {
      "variable_name": "NESTED_TABLE",
      "operation": "EQUAL",
      "value": "false"
    }
  ],
  ...
}
```

### Time

Executions during weekdays between 00:00 BRT and 08:00 BRT (03:00 to 11:00 UTC: times are compared in UTC).

```json
{
  ...
  "rules": [
    {
      "variable_name": "FINISHED_AT_WEEKDAY",
      "operation": "IN",
      "value": ["1", "2", "3", "4", "5"]
    },
    {
      "variable_name": "FINISHED_AT_TIME",
      "operation": "GREATER_THAN",
      "value": "02:59"
    },
    {
      "variable_name": "FINISHED_AT_TIME",
      "operation": "LESS_THAN",
      "value": "11:00"
    }
  ],
  ...
}
```

### Execution type

Filter only Full Refresh executions.

```json
{
  ...
  "rules": [
    {
      "variable_name": "FORCE_HISTORY",
      "operation": "EQUAL",
      "value": "true"
    }
  ],
  ...
}
```

### Schedules

Filter only scheduled executions.

```json
{
  ...
  "rules": [
    {
      "variable_name": "TRIGGERED_BY",
      "operation": "EQUAL",
      "value": "Erathos Schedule"
    }
  ],
  ...
}
```

### Orchestration

Filter only executions triggered programmatically.

```json
{
  ...
  "rules": [
    {
      "variable_name": "IS_ORCHESTRATION",
      "operation": "EQUAL",
      "value": "true"
    }
  ],
  ...
}
```
