This example simulates health-care data streaming into a model and detects measurements that fall outside a specified range. Lua snippets are self-contained blocks of Lua code that exist at the project level, which can then be referenced by any other Lua entity in the project. A Lua snippet in the project subscribes to windows, stores values, and reads a JSON file. Lua connectors are also used to generate and inject data.
NOTE: Use this example with SAS Event Stream Processing 2023.04 and later.
Earlier releases of SAS Event Stream Processing do not support some of the Lua code in this example.
For more information about how to install and use example projects, see Using the Examples.
The patients.json file contains patient data. In addition, three of the four Source windows have Lua connectors that contain their own data, and the connectors inject that data into the model. When a model similar to the one used in this example is deployed in a live environment, real-time data sources would be used instead.
The following figure shows the diagram of the project:
- The "patients" window is a Source window. This window has a Lua connector that generates patient IDs from 1 to n, depending on the value of the
numpatients
variable in the connector's Lua code. - The "metrics" window is a Source window. This window has a Lua connector that publishes statistics related to patient health. The data contains the names of metrics along with the anatomical system to which each metric belongs, as well as upper and lower limits for acceptable levels. Example:
systolic_bp,Systolic Blood Pressure,cardiovascular,60,80
. - The "statistic" window is a Source window. This window has a Lua connector that generates random values for each patient for each metric. Twenty percent of the time, the code extends the boundaries for the generated value so that a value outside of the acceptable range might be generated.
- The "addMetricInfo" window is a Join window. This window joins the metrics from the "metrics" window with the values generated by the "statistic" window.
- The "isAlert" window is a Filter window. This window generates alerts only for the patients who are found in the data from the patients.json .
- The "alert" window is a Source window. This window receives data from a Lua snippet. For more information, see Understanding the Lua Snippet.
- The "analysis" window is a Lua window. This window generates summaries about values that are out of bounds and which doctors should be contacted. Example:
The systolic_bp is too low at 58 so you should contact Doctor 10 about the cardiovascular system.
This window also keeps track of the total number of alerts per patient.
View the Lua snippet used in this project:
- Click on the toolbar. Project-level properties are displayed in the right pane.
- In the right pane, expand Lua Snippets.
- In the table in this section, double-click the
snippet
row.
The Edit Lua Code Snippet window appears. - Observe that the name of the snippet is called
snippet
. - View the Lua code. It performs the following tasks:
- Subscribes to the "statistic" window and stores the latest values for each patient.
- Subscribes to the "metrics" window and stores the metric metadata.
- Subscribes to the "isAlert" window and receives alerts for patients, which it then republishes with full data into the "alert" Source window.
- Sets a property called
patient_data
that is used by the "isAlert" Filter window and the "analysis" Lua window. - Sets a property called
metrics
that is used by the "metrics" and "statistic" Source windows, and by the "analysis" Lua window. - Sets a property called
patient_alert_count
that is used by the "alert" Source window and the "analysis" Lua window. - References the
patients.json
file. The file reference in the Lua code looks like this:patients.json
.
- Observe that the Initialization function field specifies that the
init
function in the Lua code acts as an initialization function. Initialization is the process of locating and using defined values for variable data. - Observe that the Use repeating function with interval check box is selected. The other fields associated with this check box specify that the snippet reads the
patients.json
file every 5 seconds. - Observe that the Cleanup function field specifies that the
done
function in the Lua code acts as a cleanup function. The cleanup function runs when the project is stopped and performs any necessary Lua cleanup tasks. - Click Lua Snippets.
- When you use the code editor to edit Lua code in a project, this tab is present and you can use it to insert references to snippets in the same project. Double-clicking one of the items in the left pane would add code to the right pane.
- For example, the Set patient_alert_count item is present in the Lua Snippets tab because
snippet
includes the following code:esp_setProperty({name="patient_alert_count"..event.patient,value=patientAlertCount})
. Double-clicking Get patient_alert_count would add the following code to the right pane:esp_setProperty("patient_alert_count", VALUE)
. You would need to changeVALUE
to suit your purposes. In general, when a snippet sets a property, you can use the Lua Snippets tab to insert Lua code that gets that property. In this example, bothesp_setProperty
andesp_getProperty
appear in the snippet, but in some different use caseesp_getProperty
could appear in the Lua code in one of the windows. - For more information about this functionality in the code editor, see SAS Help Center: Edit Code and Expressions.
- Click Cancel.
If you do not use the Install example button in SAS Event Stream Processing Studio, you must edit the Lua snippet to point to the location where the patients.json
file is located on your system.
When you test the project, the results for each window appear on separate tabs. In the following text, the tabs are discussed in a different order than in which the tabs appear in the user interface:
- The metrics tab shows statistics related to patient health, including upper and lower limits for acceptable levels.
- The patients tab shows patient IDs.
- The statistic tab shows randomly generated values for statistics for each patient.
- The addMetricInfo tab shows the result of joining the metrics from the "metrics" window with the values generated by the "statistic" window. Observe that this tab shows information for ten patients.
- The isAlert tab shows alerts only for the patients who are found in the data from the
patients.json
file. That is, unlike the addMetricsInfo tab, the isAlert tab shows information for only two patients, because the patients.json file contains only two patients. - The alert tab shows data that the window receives data from the Lua snippet. For more information, see Understanding the Lua Snippet. By default, the tab shows only the first 15 fields. To see results for all fields, you can stop the test and run another test, with the
patient_alert_count
field selected in the left pane before clicking Run Test. - The analysis tab shows summaries such as
The systolic_bp is too low at 58 so you should contact Doctor 10 about the cardiovascular system.
This tab also shows the total number of alerts per patient.
The following figure shows the analysis tab:
You might see warnings in the Log pane about windows being throttled. You can ignore these warnings.