Lesson 3: Event Processing Agent Introduction
Last updated
Last updated
Objective: Build an event-processing agent that receives the incoming temperature events and monitors temperature data for anomalies.
Description:
Add a weather-alert native-object.
Create an event-processing flow to monitor temperature data, either from manual submissions (like in Project 1) or through simulated temperature data (from an inject node). The goal is to detect temperature readings that fall outside of a set range (e.g., below 0°C or above 35°C) and create an alert object
Create an event-processing agent using the new flow
Add a trigger to the existing weather-report native object to send the message (as an event) to the new agent POST-INSERT.
Terminal Objective: Gain a basic understanding of how to create an event-to-flow agent that uses function nodes and switch nodes to process events, with the results being stored in additional native objects.
Enabling Objectives:
Set up an inject node to simulate a weather-report event.
Use a function node to detect low-temperature or high-temperature events.
Add logic (switch node) to detect low/high temperature anomalies and create an alert native-object.
Testing using an inject node.
Skills: FLOW-2, TRIGGER-1, CORE-1, CORE-3, CORE-5
This lessson begins where Lesson 2 left off. Ensure that you have compled lesson 2 and have the agent, flow, and object-type from that lesson in your tenant before continuing.
In this lesson we are going to send the weather reports received by the HTTP agent to an Event processing agent that will monitor for anomalous temperature readings, and when found, generate a "Weather Alert" record.
Create a new object-type called "weather-alert" as shown below:
Create a new flow called "weather-monitor". Open the flow in the flow editor.
Add an Inject node, a Prepare Event node, and an Event Start node to the canvas as shown below.
Modify the Inject node, changing the msg.payload type to "JSON" and setting the JSON value to:
Give the Inject node a name as well: "TEST". Save the changes to the Inject node.
Add a function node to the canvas and wire it in immediately after the Event Start node.
Double-click the function node to open the editor.
Add the following JavaScript code to the function node.
Give the function node a name: "Check Temperature"
Click "Done" to save the changes to the function node.
Your flow should now look like this:
Drag a Switch node onto the canvas and wire it right after the function node.
Double-click the Switch node to open the configuration editor.
Give the switch node a name: "Alert?"
Modify the "Property" field to check for msg.alert
Modify the condition below the property to "is true"
Add another condition and set it to "otherwise"
At the bottom of the window, change the dropdown from "checking all rules" to "stopping after first match".
Your Switch node should now look like this:
Drag a new Function node onto the canvas and wire it to the "is true" output of the Switch node, like this:
Open the function node and add the following JS. Give it a name: "Prep New Alert".
Drag a new Create Object node onto the canvas and wire it after the "Prep New Alert" Function node.
Configure it as shown below:
Drag an Event End node onto the canvas and wire it after the Create Object node.
Connect the "Otherwise" output of the Switch node to the Event End node also.
Your flow should now appear as shown below.
Drag a Catch Node onto the canvas.
Add log-tap node and wire it right after the Catch node. Name it "Error" and configure it for "Complete Msg Object" and log-level = Error.
Add an Event Error node after the log-tap node.
Your error handling should now look like this:
Click Save to save your flow.
Create a new agent as shown below:
NOTE: The agent type for this agent is "Event to Flow" and NOT "HTTP to Flow".
Open the agent and wait for it to show "Running" in the Operations tab.
Navigate to Components/Object Types and open the weather-report object type.
Click on the "Triggers" tab and add a "Post-Insert" trigger using the "+" button.
Select "SendToAgent"
Give the trigger a name: "Send Weather Report"
If needed, select "Weather Monitor" as the agent to send the event to. The trigger configuration should look like this:
Save the trigger by clicking on "Create Trigger".
Go back to the browser tab where you have the Submit Weather Report form. If you don't have it open any longer, go back to the "Submit Weather Report" agent and copy the agent URL and remember to append "/form" to the URL.
Submit a weather report with a temperature within the range 0 < temperature < 38. For example: 25.
Now submit a weather report with a temperature above the high temperature threshold. For example: 40.
Now submit a weather report with a temperature below the log temperature threshold. For example, -5.
Now navigate to Records/Data/Weather Reports in the console and verify that you see all of these new reports, as shown below.
Now navigate to Records/Data/Weather Alerts in the console and verify that you see two weather alerts, for the HIGH_TEMP alert and the LOW_TEMP alert.
Congratulations! You have now completed Lesson 3 of the Basic Developer Training Course. You now have an understanding of how to create Event Processing agents as well as new function nodes such as Function and Switch.