# Transformations

Manufacturing Data Engine (MDE) provides a straightforward way to do post-processing
of messages during ingestion and generate new derived tags and records from the
input messages. These post-processors are called *transformations* . They can be
associated to a given type and generate new messages that are then re-ingested
in MDE. *transformations* are built-in Dataflow
pipelines and can be configured using certain specific *transformation parameters*.

MDE 1.3 and later versions provide two default transformations:

- **Group Event Change**
- **Windowing**

*transformations* are activated at the *type* level. This implies that all
*tags* that belong to that type will be transformed. A type can be associated
to none, one or more than one *transformations* . The association between the
type and a specific transformation can be set up in the web interface and using the
configuration API. A type can be associated to a *transformation* at any given
time and that association can also be removed whenever is no longer needed.

A transformation might need *transformation parameters* to define its specific
behavior. The *transformation parameters* are specific to each *transformation*
type and can be different from one *transformation* to another.

The specific *transformation parameter values* for a given *transformation*
are set at type level too. Different types can use different parameter values
and use the same *transformation* differently.

*Transformations* receive all messages mapped to the type as input where the
transformation is active. Each message is processed by the *transformation*
processor in Dataflow.

The output of any *transformation* is a raw JSON message that is ingested in
the initial landing topic. MDE provides a unique
*configuration package* with each *transformation* . This
*configuration package* needs to be applied to MDE
if you are planning to use the *transformation* . The configuration package
creates a set of a *message class* , *parser* , *type* and
*metadata bucket* collection. These configuration entities allow
MDE to identify and process the *transformation*
output messages into a specific *type* . transformation-specific types store the
information emitted by the *transformation* . They are also associated to
specific transformation *Metadata Buckets* storing reference information
about the original transformed tag or Record.

The two *types* provided to process the raw messages generated by each
*transformation* are the following:

- **`group-event-change-records`** : Destination type for the output messages of *group event change transformation*.
- **`window-transformation-records`** : Destination type for the output messages of *windowing transformation*.

## Group event change transformation

This *transformation* monitors any field in a tag or a group of tags and
generates an event every time the monitored values changes in any of those tags.
This transformation can be associated to a type of any *archetype* . The
*transformation* appears in the web interface as **`eventChange`**.

This *transformation* emits a message that is parsed to a specific
**`CONTINUOUS_DATA_SERIES`** type named **`group-event-change-records`**
provided in its associated *configuration package* . The type contains the
**start time** and **end time** of the period between value changes as well as
the previous value, the current value and the name of the tag that triggered the
change.

You can define a group of tags of the same type to be monitored
together. You can chose a *key criteria* to define how to group tags.
All tags where the key has the same value are monitored together. The value of
the *key* can be any element of the tag *proto*, including any data field
and any element of the metadata.

To make the *transformation* work for an individual tag the *key* is set to
the `TagName`.

### Parameters

The two *transformation parameters* required to configure this *transformation*
are the following:

1. **Key Expression** : It is displayed in the web interface as **`keyExpression`** .
   Defines the expression that will be used to group tags. All tags where
   *key expression* has the same value are monitored together. The *transformation*
   emits a value change event every time a change is detected in any of those
   tags. The values are compared to the previous values of the same
   tag, but an event is triggered when any of the tags changes. The
   *key expression* is defined as an *SpEL expression* and can use any of the
   available fields in the tag proto. For example, to only monitor a single tag
   based on the tag name, the SpEL expression would be defined as:

       #message['tagName']

   Alternatively, to group tags based on a common metadata field, like machine
   name (assuming an `asset-hierarchy` *bucket* exists with an attribute called
   `machine-name` in it), the expression can be set to:

       #message['metadata']['asset-hierarchy']['machine-name']

2. **Value Expression** : It is displayed in the web interface as **`valueExpression`** .
   Defines the value field that the *transformation* monitors to emit a new
   change event. It is also defined as an *SpEL expression* . The expression
   can use any field of the tag proto structure. The output of the expression
   must be of type **`String`** to function correctly.

   For example, to monitor a `value` field in the `Data` section of the
   tag proto, the *Value Expression* can be set to:

       #message['data']['numeric'].toString()

### Output Message

The content of the *Output Message* generated by the
*group event change transformation* provides the details of the detected event.
The specific attributes available in the *output message* are:

1. **`TagName`** : The *transformation* creates an event under a Tagname which corresponds to the value of the *Key Expression* and the suffix `"_event_change"`. If the *Key Expression* is defined as an individual TagName the derived tag will be `"TagName_event_change"`.
2. **Event Change Continuous Timestamp Object** : contains the initial (`event_timestamp_start` attribute) and final (`event_timestamp_end` attribute) time stamps of the event. It also contains the duration (`duration` attribute) of the event as a `double`.
3. **Event Change Message Data Object** : Contains the name of the tags that changed value during the event (`changed_tags` attribute) and the list of tags that match the grouping criteria at the time the event is emitted (`contributing_tags` attribute). Both can contain more than one tag per message.
4. **Tag Value Change Object** : The name (`tag_name` attribute), old value (`old_expression_value` attribute), new value (`new_expression_attribute` attribute) and message (`message_id` attribute) that triggered the state change event.
5. **Changed Value Message Object** (on v1.3.6 and later): Contains the message that triggered the event change.

See the specific schema in the
[reference](https://docs.cloud.google.com/manufacturing-data-engine/docs/reference/transformation-schemas.md.txt)
section.

## Windowing transformation

This *transformation* applies a *Windowing* function to all tags of a
type. *Windowing* refers to the summarization of multiple records
over a period of time in a single record. The *windowing transformation* can
be associated to any type of any archetype. It can summarize the records of tags
from 1 minute up to 60 minutes. The result of the *transformation* is a raw message
containing the summary values for the value expression during the selected
period of time.

This *transformation* can be applied multiple times to the same type as far
as the *Id* of each *windowing transformation* (as defined in its
parameters) is unique within the type. You can add the *Windowing
transformation* using the web interface but you must use the API to add multiple
*windowing transformations* to a given type.

The *transformation* generates a derived tag of a provided type (named
`window-transformation-records`) containing a summary of all the records
received during that time window for that Tag. This transformation operates at
the tag level: it will generate a summarized tag for each tag of the type to
which the transformation has been associated. The name of the tag is composed
from the original `Tagname` and the suffix `-window-stats` and the `id` of
the *windowing transformation*.

### Parameters

This transformation requires three parameters to operate:

1. **Duration of the Window** : available in the web interface as `durationMinutes`. It refers to the duration of the Window to apply in minutes. For example, a value of 1, will generate a derived time series of 1 minute resolution.
2. **ID of the Window** : available in the web interface as `id`. Assigns a unique identifier to each *windowing transformation* of the type in order to enable more than one to be applied in parallel. The ID can be any String.
3. **Value Expression** : available in the web interface as `valueExpression`. It is
   the *SpEL* expression that is applied to the tag proto to generate a
   Numeric value that will be summarized by the *windowing transformation* .
   For example, if you require to use the value field of the a generic numeric
   tag, the expression of `valueExpression` can be defined as:

       #message['data']['numeric']

### Output message

The *transformation* emits one message for each time period defined by the
*transformation Parameter* `durationMinutes`. The output message contains
the following attributes:

1. **TagName** : Name of the tag of the derived time series. It is generated automatically by the *windowing transformation* . It is formed using the original TagName and the suffix **`-window-stats`** and the `id` of the *windowing transformation* .. For example, for a TagName called `Temperature` and an ID `1`, the derived tag will be named `Temperature-window-stats-1`.
2. **TimeStamp** : Contains the start and end time stamps of the Window (available as `event_timestamp_start` and `event_timestamp_end` attributes).
3. **Window Summary Stats** : Contains all the summarized data for the period which includes:
   1. **Mean**: The mean value of all values received during the time window.
   2. **Min**: The minimal value of all values received during the time window.
   3. **Max**: The maximum value of all values received during the time window.
   4. **Standard Deviation**: The standard deviation of all value received during the time window.
   5. **p50**: The 50% confidence level of not being exceeded for the values received during the time window.
   6. **p90**: The 90% confidence level of not being exceeded for the values received during the time window.
   7. **Sum**: The summary of all values received during the time window.
   8. **Count**: The number of values received during the time window.
   9. **Delta**: The difference between the last and the first value received during the time window.

For more information, see the specific schema in the
[Transformation Schemas](https://docs.cloud.google.com/manufacturing-data-engine/docs/reference/transformation-schemas.md.txt)
section.