# Writing Expressions for KloudMate Alarms

Alarm expressions allow users to manipulate query results using math and other operations. KloudMate alarms can only be targeted on numeric data. So, if a query returns time series data then it should be reduced using expressions before it can be used as an alarm target.

Each individual expression or query is represented by a unique alphabetical notation such as A, B, and so on. While writing an expression, one or more queries or other expressions can be passed as parameters using their notations.

KloudMate supports the following operations in Alarm Expressions:

- Math expression
- Reduce
- Condition expression

Math expression takes time series or number data returned by a query or an expression and turns them into different time series and numbers using various mathematical operations or functions.

**The following are the acceptable inputs for a math expression:**

Input: Any mathematical operation that is to be applied on the data returned from the other queries or expressions.

For math expressions, the queries or the expressions are as passed as parameters using their alphabetical notation prefixed with a dollar sign, for example $A.

KloudMate supports various mathematical operations and functions for math expressions.

- Arithmetic operations:
- ++ (Increment)
- -- (Decrement)
- + (Addition)
- - (Subtraction)
- * (Multiplication)
- / (Division)
- % (Modulo)
- ^ (Exponentiation)
- Example: ++$A, $A++, $A+1, $A-2, $A*2, $A/6, $A%$B, $A^2

- Boolean operations:
- == (Equal to)
- != (Not equal to)
- < (Less than)
- > (Greater than)
- <= (Less than or equal to)
- >= (Greater than or equal to)
- Example: $A ==0, !$A !=0, $A<$B, $A>10, $A<=$D, $A>=$D

- Logical operations:
- && (AND)
- || (OR)
- Example: $A && $D, $A and $D, $A or $D, $A || $D

Note that for the above mentioned operations:

- When both $A and $B paramteres are numbers, then the operation is performed between the two numbers
- If one paramter is a number, and the other is a time series, then the operation performed between the number and each value in the time series individually
- If both $A and $B paramters are time series data, then the operation performed between each value in the two series having the same time stamps
- The boolean and logical operations return 0 for false 1 for true

- abs: Returns the absolute value of its parameter which can be a number or a series. E.g., abs(-1) or abs($A)
- log: Returns the natural logarithm of its parameter which can be a number or a series. If the value is less than 0, NaN is returned. E.g., log(-1) or log($A)
- round: Returns a rounded integer value . E.g., round(3.123) or round($A)
- ceil: Rounds the number up to the nearest integer value. E.g., ceil(3.123) will return 4
- floor: Rounds the number down to the nearest integer value. E.g., floor(3.123) will return 3

Reduce takes one or more time series or number data returned from a query or an expression and turns them into a single number.

**The following are the acceptable inputs for a reduce expression:**

- Reduction function: Functions supported by reduce expression such as mean(), max(), min(), sum(), last(), count()
- Query or expression: Data returned by a query or an expression is passed as a parameter using their respective alphabetical notation

The selected reduction function is used to aggregate the values of a query or an expression into a single value. KloudMate supports various reduction functions for reduce expressions.

**mean() :**Get the average value**max() :**Get the maximum value**min() :**Get the minimum value**sum() :**Get the sum of all value**last() :**Get the last value**count() :**Get the total number of value

Condition expression takes one or more time series or number data returned from queries or expressions and gives a boolean value as a result. 0 (False) if the condition is not met or 1 (True) if the condition is met.

**The following are the acceptable inputs for a condition expression:**

- Reduction function: Functions supported by reduce expression such as mean(), max(), min(), sum(), last(), count()
- Query or expression: Data returned by a query or an expression is passed as a parameter using their respective alphabetical notation
- Condition string: IS ABOVE, IS SAME OR ABOVE, IS BELOW, IS SAME OR BELOW
- Value: A number which will be used as the condition target

The selected reduction function will be applied to the selected query/expression to generate a single boolean value. The generated value will be compared to the provided value according to the selected condition string. When the input is a collection of time series or number data, the reduction function is applied to each element in the collection. Each of these reduced outputs are evaluated against the condition individually. The outputs of the condition evaluation are then combined using the **AND **operation.

You can add multiple conditions in a condition expression and choose how they work together using logical operators such as **OR & AND.**