This section provides instructions for setting up alerts in Time Series Analytics Microservice.
The following MQTT alerts are configured for both Wind Turbine Anomaly Detection
and Weld Anomaly Detection sample apps
wind-turbine-anomaly-detection/time-series-analytics-config/config.json
"alerts": {
"mqtt": {
"mqtt_broker_host": "ia-mqtt-broker",
"mqtt_broker_port": 1883,
"name": "my_mqtt_broker"
}
}weld-anomaly-detection/time-series-analytics-config/config.json
"alerts": {
"mqtt": {
"mqtt_broker_host": "ia-mqtt-broker",
"mqtt_broker_port": 1883,
"name": "my_mqtt_broker"
}
}The following code snippets show how to add the MQTT, if not
already added, to Wind Turbine Anomaly Detection and Weld Anomaly Detection
sample apps. The TICK script has the following configuration done by default.
data0
|alert()
.crit(lambda: "anomaly_status" > 0)
.message('Anomaly detected for wind speed: {{ index .Fields "wind_speed" }} Grid Active Power: {{ index .Fields "grid_active_power" }} Anomaly Status: {{ index .Fields "anomaly_status" }} ')
.noRecoveries()
.mqtt('my_mqtt_broker')
.topic('alerts/wind_turbine')
.qos(1)weld-anomaly-detection/time-series-analytics-config/tick_scripts/weld_anomaly_detector.tick
data0
|alert()
.crit(lambda: "anomaly_status" > 0)
.message('{"time": "{{ index .Time }}", "Pressure": {{ index .Fields "Pressure" }}, "CO2 Weld Flow": {{ index .Fields "CO2 Weld Flow" }}, "anomaly_status": {{ index .Fields "anomaly_status" }} } ')
.noRecoveries()
.mqtt('my_mqtt_broker')
.topic('alerts/weld_defects')
.qos(1)Note: Setting QoS to
1ensures messages are delivered at least once. Alerts are preserved and re-sent if the MQTT broker reconnects after downtime.
Follow the steps to subscribe to the published MQTT alerts.
- To subscribe to all MQTT topics, execute the following command:
docker exec -ti ia-mqtt-broker mosquitto_sub -h localhost -v -t '#' -p 1883-
To subscribe to a specific MQTT topic, such as
**Wind Turbine Anomaly Detection**alerts/wind_turbine, use the following command. Note that the topic information can be found in the TICK Script.**Weld Anomaly Detection**docker exec -ti ia-mqtt-broker mosquitto_sub -h localhost -v -t alerts/wind_turbine -p 1883weld-anomaly-detection/time-series-analytics-config/tick_scripts/weld_anomaly_detector.tick
docker exec -ti ia-mqtt-broker mosquitto_sub -h localhost -v -t alerts/weld_defects -p 1883
Note: This section is applicable to Wind Turbine Anomaly Dectection sample app only. In other words, OPC UA alerts are not supported for Weld Anomaly Detection sample app.
Ensure that make up_opcua_ingestion has been executed by following the steps
in the getting started guide for the docker compose deployment
To enable OPC-UA alerts in Time Series Analytics Microservice, use the following steps.
The following code snippets show how to add the OPC-UA alert, if not already added, replace this in place of MQTT alert section in the TICK script.
**Wind Turbine Anomaly Detection**data0
|alert()
.crit(lambda: "anomaly_status" > 0)
.message('Anomaly detected: Wind Speed: {{ index .Fields "wind_speed" }}, Grid Active Power: {{ index .Fields "grid_active_power" }}, Anomaly Status: {{ index .Fields "anomaly_status" }}')
.noRecoveries()
.post('http://localhost:5000/opcua_alerts')
.timeout(30s)weld-anomaly-detection/time-series-analytics-config/tick_scripts/weld_anomaly_detector.tick
data0
|alert()
.crit(lambda: "anomaly_status" > 0)
.message('{"time": "{{ index .Time }}", "Pressure": {{ index .Fields "Pressure" }}, "CO2 Weld Flow": {{ index .Fields "CO2 Weld Flow" }}, "anomaly_status": {{ index .Fields "anomaly_status" }} } ')
.noRecoveries()
.mqtt('my_mqtt_broker')
.topic('alerts/weld_defects')
.qos(1)Note:
- The
noRecoveries()method suppresses recovery alerts, ensuring only critical alerts are sent.- If doing a Helm-based deployment on a Kubernetes cluster, after making changes to the TICKscript, copy the UDF deployment package using step 4.
Make the following REST API call to the Time Series Analytics microservice. Note that the
mqtt alerts key is replaced with the opcua key and its specific details:
wind-turbine-anomaly-detection/time-series-analytics-config/config.json
curl -k -X 'POST' \
'https://<HOST_IP>:3000/ts-api/config' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"udfs": {
"name": "windturbine_anomaly_detector",
"models": "windturbine_anomaly_detector.pkl",
"device": "cpu"
},
"alerts": {
"opcua": {
"opcua_server": "opc.tcp://ia-opcua-server:4840/freeopcua/server/",
"namespace": 1,
"node_id": 2004
}
}
}'weld-anomaly-detection/time-series-analytics-config/config.json
curl -k -X 'POST' \
'https://<HOST_IP>:3000/ts-api/config' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"udfs": {
"name": "weld_anomaly_detector",
"models": "weld_anomaly_detector.cb"
},
"alerts": {
"mqtt": {
"mqtt_broker_host": "ia-mqtt-broker",
"mqtt_broker_port": 1883,
"name": "my_mqtt_broker"
}
}
}'-
Install Python packages
asyncioandasyncuato run the sample OPC UA subscriberpip install asyncio asyncua
-
Run the following sample OPC UA subscriber by updating the
<IP-Address of OPCUA Server>to read the alerts published to server on tagns=1;i=2004from Time Series Analytics Microservice.import asyncio from asyncua import Client, Node class SubscriptionHandler: def datachange_notification(self, node: Node, val, data): print(val) async def main(): client = Client(url="opc.tcp://<IP-Address of OPCUA Server>:30003/freeopcua/server/") async with client: handler = SubscriptionHandler() subscription = await client.create_subscription(50, handler) myvarnode = client.get_node("ns=1;i=2004") await subscription.subscribe_data_change(myvarnode) await asyncio.sleep(100) await subscription.delete() await asyncio.sleep(1) if __name__ == "__main__": asyncio.run(main())
For detailed instructions on configuring and publishing MQTT alerts, refer to the Publish MQTT Alerts section.
Follow the steps to subscribe to the published MQTT alerts.
To subscribe to MQTT topics in a Helm deployment, execute the following command:
-
Identify the MQTT broker pod name by running:
kubectl get pods -n ts-sample-app | grep mqtt-broker -
Use the pod name from the output of the above command to subscribe to all topics:
kubectl exec -it -n ts-sample-app <mqtt_broker_pod_name> -- mosquitto_sub -h localhost -v -t '#' -p 1883
-
To subscribe to MQTT topic such as
**Wind Turbine Anomaly Detection**alerts/wind_turbine, use the following command:**Weld Anomaly Detection**kubectl exec -it -n ts-sample-app <mqtt_broker_pod_name> -- mosquitto_sub -h localhost -v -t alerts/wind_turbine -p 1883
kubectl exec -it -n ts-sample-app <mqtt_broker_pod_name> -- mosquitto_sub -h localhost -v -t alerts/weld_defects -p 1883
Note:
Ensure a sample app is deployed by following the installation step for OPC-UA ingestion.
To enable OPC-UA alerts in Time Series Analytics Microservice, please follow below steps.
-
Configuring OPC-UA Alert in TICK Script
Configure the TICK script by following these instructions.
-
Copying the TICK Script
Copy the TICK script using the following commands:
**Wind Turbine Anomaly Detection****Weld Anomaly Detection**cd edge-ai-suites/manufacturing-ai-suite/industrial-edge-insights-time-series/apps/wind-turbine-anomaly-detection # path relative to git clone folder cd time-series-analytics-config export SAMPLE_APP="wind-turbine-anomaly-detection" mkdir -p $SAMPLE_APP cp -r models tick_scripts udfs $SAMPLE_APP/. POD_NAME=$(kubectl get pods -n ts-sample-app -o jsonpath='{.items[*].metadata.name}' | tr ' ' '\n' | grep deployment-time-series-analytics-microservice | head -n 1) kubectl cp $SAMPLE_APP $POD_NAME:/tmp/ -n ts-sample-app
cd edge-ai-suites/manufacturing-ai-suite/industrial-edge-insights-time-series/apps/weld-anomaly-detection # path relative to git clone folder cd time-series-analytics-config export SAMPLE_APP="weld-anomaly-detection" mkdir -p $SAMPLE_APP cp -r models tick_scripts udfs $SAMPLE_APP/. POD_NAME=$(kubectl get pods -n ts-sample-app -o jsonpath='{.items[*].metadata.name}' | tr ' ' '\n' | grep deployment-time-series-analytics-microservice | head -n 1) kubectl cp $SAMPLE_APP $POD_NAME:/tmp/ -n ts-sample-app
-
Configuring OPC-UA Alert in
config.jsonMake the following REST API call to the Time Series Analytics microservice. Note that the
**Wind Turbine Anomaly Detection**mqttalerts key is replaced with theopcuakey and its specific details:wind-turbine-anomaly-detection/time-series-analytics-config/config.json
**Weld Anomaly Detection**curl -k -X 'POST' \ 'https://<HOST_IP>:30001/ts-api/config' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "udfs": { "name": "windturbine_anomaly_detector", "models": "windturbine_anomaly_detector.pkl", "device": "cpu" }, "alerts": { "opcua": { "opcua_server": "opc.tcp://ia-opcua-server:4840/freeopcua/server/", "namespace": 1, "node_id": 2004 } } }'
weld-anomaly-detection/time-series-analytics-config/config.json
curl -k -X 'POST' \ 'https://<HOST_IP>:30001/ts-api/config' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "udfs": { "name": "weld_anomaly_detector", "models": "weld_anomaly_detector.pkl", "device": "cpu" }, "alerts": { "opcua": { "opcua_server": "opc.tcp://ia-opcua-server:4840/freeopcua/server/", "namespace": 1, "node_id": 2004 } } }'
To subscribe to OPC-UA alerts, follow these steps.