> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Evaluate limits

> How Atoti Limits orchestrates limit evaluation using IEvaluationTaskManager and IEvaluationService, including Intraday scheduled evaluations, TaskScheduler configuration, and on-demand REST endpoints at limit, structure, and application levels

## The `IEvaluationTaskManager` and `IEvaluationService`

The `IEvaluationTaskManager` orchestrates the evaluation of limits. The default implementation of this interface can be found in `DefaultEvaluationTaskManager`.
The `IEvaluationService` contains the logic for evaluating limits. The default implementation of this interface can be found in `DefaultEvaluationService`. For
both of these interfaces, you can define your own implementation - see [Adding Custom Evaluation Logic](../extend/custom-evaluation) for more information.

When a breach or warning is detected, an incident is recorded, the incident workflow is updated, and an `incident*.csv` file is generated. For details on the `incident*.csv` file, see [Incidents file](https://docs.activeviam.com/atoti-intelligence/workflows/limits/6.2/reference/input-files/incidents.md).

Optionally, you can also store passes, which are limit evaluations that do not result in a breach or warning. See the
[Limit Status screen](https://docs.activeviam.com/atoti-intelligence/workflows/limits/6.2/user-guide/breaches-and-warnings/status-screen.md#include-all-limit-evaluations) for more information.

Limit evaluations through the `IEvaluationService` can either be scheduled or executed on-demand.

## Scheduled evaluations

The `IEvaluationTaskManager` automatically schedules future evaluations for limit structures with an Intraday polling frequency.
You can configure the Intraday polling frequency using the `limits.alert-task.sample-rate-cron` property values.

### Properties

<table><thead><tr><th>Property</th><th>Value</th><th>Description</th></tr></thead><tbody><tr><td>limits.evaluation.scheduler.thread-pool-size</td><td>4</td><td>Thread pool size for the task scheduler.</td></tr><tr><td>limits.alert-task.sample-rate-cron</td><td>0 \*/5 \* \* \* \*</td><td>Cron expression for the Intraday Alert task’s cron job.</td></tr></tbody></table>

### Scheduler threads

Scheduled evaluations run on a dedicated thread pool. To change its size, set `limits.evaluation.scheduler.thread-pool-size` (default: 4).

To stop a scheduled evaluation, use the [unschedule endpoint](#unschedule-tasks-from-the-taskscheduler).

## On-demand limit evaluation

The `IEvaluationTaskManager` is also responsible for evaluating limits on-demand. In general this is done through the UI by [evaluating limits](https://docs.activeviam.com/atoti-intelligence/workflows/limits/6.2/user-guide/breaches-and-warnings/evaluate-limits.md),
but could also be done programmatically or via REST requests.

On-demand evaluations run in parallel. To set the number of threads they use, configure the following [Configuration Property](https://docs.activeviam.com/atoti-intelligence/workflows/limits/6.2/reference/properties/config-properties/limits-activeviam.md):

<table><thead><tr><th>Property</th><th>Value</th><th>Description</th></tr></thead><tbody><tr><td>limits.incident.evaluation.parallel-tasks</td><td>-1</td><td>Thread pool size for the evaluation task executors. A negative value indicates that half the available processors will be used.</td></tr></tbody></table>

You can also configure the behavior when errors occur. For example, you can include a stack trace in the response by using the following property from the [application.yml](https://docs.activeviam.com/atoti-intelligence/workflows/limits/6.2/reference/properties/property-files/application-yml.md#section-incidents) file:

<table><thead><tr><th>Property</th><th>Value</th><th>Description</th></tr></thead><tbody><tr><td>limits.incident.evaluation.error.include-stacktrace</td><td>false</td><td>`true` if the stack trace should be available in the evaluation response to the UI.</td></tr></tbody></table>

### REST

Evaluation REST endpoints are available for:

* [On-demand limit evaluation](#on-demand-limit-evaluation)
* [Unscheduling tasks from the TaskScheduler](#unschedule-tasks-from-the-taskscheduler)

For any of the following endpoints that use an `asOfDate` query parameter, note that the `asOfDate` is optional. It is included in the examples to demonstrate the syntax, but if not provided, Atoti Limits defaults to the date
provided in the [as\_of\_date.csv file](https://docs.activeviam.com/atoti-intelligence/workflows/limits/6.2/reference/input-files/as_of_date.md). The result of each response is a DTO containing the number of breaches, warnings and passes (if enabled) as well as any errors encountered during evaluation.

> [!NOTE]
>
> Triggering an evaluation records the resulting incidents and creates or updates their workflow.

#### At the limit level

For evaluation on a per-limit basis, the following two REST endpoints can be used:

##### Evaluate a single limit

* Endpoint: `/limits/rest/v2/limitEvaluation/limit/evaluate/{limitId}`
* Method: Post
* Example Endpoint: `http://localhost:3090/limits/rest/v2/limitEvaluation/limit/evaluate/12345678?asOfDate=2023-01-31`

##### Evaluate multiple limits

> [!NOTE]
>
> The limit keys provided do not need to belong to the same limit structure.

* Endpoint: `/limits/rest/v2/limitEvaluation/limit/evaluate`
* Method: Post
* Example Endpoint: `http://localhost:3090/limits/rest/v2/limitEvaluation/limit/evaluate?asOfDate=2023-01-31`
* Example Request Body (the limit keys):

```json theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
[
  12345678,
  -87654321
]
```

#### At the limit structure level

To evaluate on a per-limit structure basis, the following two REST endpoints can be used:

##### Evaluate a single limit structure

* Endpoint: `/limits/rest/v2/limitEvaluation/structure/evaluate/{limitStructureId}`
* Method: Post
* Example Endpoint: `http://localhost:3090/limits/rest/v2/limitEvaluation/structure/evaluate/12345678?asOfDate=2023-01-31`

##### Evaluate multiple limit structures

* Endpoint: `/limits/rest/v2/limitEvaluation/structure/evaluate`
* Method: Post
* Example Endpoint: `http://localhost:3090/limits/rest/v2/limitEvaluation/structure/evaluate?asOfDate=2023-01-31`
* Example Request Body (the limit structure keys):

```json theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
[
  12345678,
  -87654321
]
```

##### Evaluate by KPI

This is equivalent to [evaluation per limit structure](#at-the-limit-structure-level). To evaluate a KPI against the business cubes, use the following REST endpoint:

* Endpoint: `/limits/rest/v2/limitEvaluation`
* Method: Post
* Example Endpoint: `http://localhost:3090/limits/rest/v2/limitEvaluation/evaluate`
* Example Request Body:

```json theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
{
    "kpiName":"SA Book Limit",
    "cubeName":"StandardisedApproachCube",
    "serverName":"FRTB"
}
```

#### At the application level

This endpoint evaluates all approved limits:

* Endpoint: `http://localhost:3090/limits/rest/v2/limitEvaluation/evaluate/all`
* Method: Post
* Example Endpoint: `http://localhost:3090/limits/rest/v2/limitEvaluation/evaluate/all`

> [!NOTE]
>
> This endpoint is not optimized and may not be performant.

### Unschedule tasks from the TaskScheduler

To unschedule a task from the `IEvaluationTaskManager`, use the following REST endpoint:

* Endpoint: `/limits/rest/v2/limitEvaluation/unschedule`
* Method: POST
* Example Endpoint: `http://localhost:3090/limits/v2/rest/limitEvaluation/unschedule`

Example Body:

```JSON theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
{
   "serverName":"FRTB",
   "cubeName":"StandardisedApproachCube",
   "kpiName":"SA Book Limit",
   "pollingFrequency":"INTRADAY"
}
```

Example Response (`true` if the task is unscheduled successfully, `false` otherwise):

```JSON theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
{
  "true"
}
```
