# Aggregation Alerts

Canonical page: https://www.adaptabletools.com/docs/handbook-alerting-aggregation

- Aggregation Alerts can use Aggregation Boolean Expressions
- These will evaluate using multiple rows and fire if the return value is true

Alerts in AdapTable can fire as the result of an [Aggregation Expressions](https://www.adaptabletools.com/docs/adaptable-ql-expression-aggregation/index.md).

This is a special type of Expression which runs against **aggregated data** and returns a true / false value.

For instance to define an Alert which fires when the total of a 'PnL' column is > 50M:

```ts [[1, 2, "SUM"],[2, 2, "PnL"],[3, 2, ">"],[4, 2, "50M"]]
 // Is the sum of all PnL values over 5M
 SUM{[PnL]} > '50M' 
```

## Aggregation Functions

Currently 5 Aggregation Functions are supported:

- `SUM`
- `MIN`
- `MAX`
- `AVG`
- `COUNT`

Aggregation Expressions require the Alert to have a [Scope](https://www.adaptabletools.com/docs/dev-guide-columns-scope/index.md) of `All` (i.e. the whole row)

With the [Server-Side Row Model](https://www.adaptabletools.com/docs/dev-guide-row-models-server-overview/index.md), the client may not have every row required for the aggregation — see [Displaying Alerts](https://www.adaptabletools.com/docs/handbook-alerting/index.md#server-side-row-model) and [Server Evaluation](https://www.adaptabletools.com/docs/adaptable-server-evaluation/index.md).

The [AdapTableQL Guide](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md) provides full details on writing [Aggregation Expressions](https://www.adaptabletools.com/docs/adaptable-ql-expression-aggregation/index.md)

## WHERE Clause

Aggregation Alerts often have a `WHERE` clause which limits which rows are included in the evaluation.

For instance to define the Alert above, but only in rows with a Dollar Currency we would write:

```ts [[1, 2, "SUM"],[2, 2, "PnL"],[3, 2, ">"],[4, 2, "50M"],[5, 2, "WHERE [Currency] = 'USD'"]]
 // Are all PnL values in rows with Dollar Currency over 5M
 SUM{[PnL]} > '50M' WHERE [Currency] = 'USD'
```

**Example: Alerts: Aggregated Expressions**

Alerts fired due to Aggregated Boolean Expressions

- This demo contains 4 Alerts that fire when an Aggregated Boolean Expression is triggered
  - `Info` Alert if the lowest `Github Watchers` value is under 50
  - `Success` Alert when the total of all the `Github Stars` rows is over 1.1 million
  - `Warning` Alert when the total of all `Open Issues` - **only** for rows where Language is JavaScript - is over 6,000
  - `Error` Alert when the count of all `Language` is 3 or greater where value is 'HTML' 

- In the first row change the value of:
  - `Github Watchers` column to 45 and see the `Info` Alert be fired
  - `Github Stars` column to 279429 and see the `Success` Alert be fired
  - `Open Issues` column to 1220 and see the `Warning` Alert be fired (because `Language` in first row is "JavaScript")
  - `Language` column to *HTML* and see the `Error` Alert be fired (because we now have 3 values with HTML)
  

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Aggregated Boolean Expression Alert',
  initialState: {
    Dashboard: {
      ModuleButtons: ['Alert'],
      Tabs: [
        {
          Name: 'Toolbars',
          Toolbars: ['Alert', 'CellSummary'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Alert'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Alert: {
      AlertDefinitions: [
        {
          Name: 'alert-github-watchers',
          MessageType: 'Info',
          Scope: {
            All: true,
          },
          Rule: 'MIN{[github_watchers]} < 50',
          AlertProperties: {
            DisplayNotification: true,
          },
        },
        {
          Name: 'alert-github-stars',
          MessageType: 'Success',
          Scope: {
            All: true,
          },
          Rule: "SUM{[github_stars]} > '1.1M'",
          AlertProperties: {
            DisplayNotification: true,
          },
        },
        {
          Name: 'alert-open-issues-count',
          MessageType: 'Warning',
          Scope: {
            All: true,
          },
          Rule: "SUM{[open_issues_count]} > '6k' WHERE [language] = 'JavaScript'",
          AlertProperties: {
            DisplayNotification: true,
          },
        },
        {
          Name: 'alert-language',
          MessageType: 'Error',
          Scope: {
            All: true,
          },
          Rule: "COUNT{[language]} >= 3 WHERE [language] = 'HTML'",
          AlertProperties: {
            DisplayNotification: true,
          },
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_watchers',
            'license',
            'github_stars',
            'open_issues_count',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

### Providing Aggregation Alerts in Initial State

Aggregation rules evaluate across the grid (or a `WHERE` subset). Use `Scope: { All: true }`.

Use `SUM`, `MIN`, `MAX`, `AVG`, or `COUNT` with curly-brace syntax; optional `WHERE` limits rows in the aggregation.

```ts [[1, 2, "Alert"], [1, 3, "AlertDefinitions"], [1, 7, "Scope"], [1, 8, "All"], [2, 10, "Rule"], [2, 10, "MIN"], [1, 19, "Scope"], [1, 20, "All"], [2, 22, "Rule"], [2, 22, "SUM"], [2, 22, "WHERE"]]
const initialState: InitialState = {
  Alert: {
    AlertDefinitions: [
      {
        Name: 'alert-min-watchers',
        MessageType: 'Info',
        Scope: {
          All: true,
        },
        Rule: 'MIN{[github_watchers]} < 50',
        AlertProperties: {
          DisplayNotification: true,
          LogToConsole: true,
        },
      },
      {
        Name: 'alert-js-open-issues',
        MessageType: 'Warning',
        Scope: {
          All: true,
        },
        Rule: "SUM{[open_issues_count]} > '6k' WHERE [language] = 'JavaScript'",
        AlertProperties: {
          DisplayNotification: true,
          HighlightCell: true,
        },
      },
    ],
  },
};
```
