Absence of Change

Responding in the absence of changes

Scenario

In this tutorial, we will explore the use case where the requirement is to monitor a fleet of freezers and trigger an alert if one of them remains above 32 degrees continuously for more than 15 minutes, an alert must not fire if the freezer rises above 32 but drops back below that threshold in less than 15 minutes. There is no explicit event or mechanism that confirms the fact that the temperature has been above the threshold continuously for 15 minutes. For demonstration purposes, we will use a duration of 10 seconds instead of 15 minutes. The source data is a PostgreSQL table that stores the latest reported temperature of each freezer.

Tutorial Modes

You can follow along the steps below in a Github codespace, a VSCode Dev Container or your own Kubernetes environment.

The easiest way to follow along with this tutorial is to launch a Github Codespace using the link below. This will allow you to run the example application within your browser without setting up anything on your own machines.

Open in Github Codespaces

This will open a page with some configuration options. Make sure that the Branch selected is main and set the Dev Container configuration to Absence of Change with Drasi.

To follow along with a Dev Container, you will need to install:

Next, clone the learning repo from Github, and open the repo in VS Code. Make sure that Docker daemon (or Docker Desktop) is running.

Once the solution is open in VS Code, follow these steps:

  • Press Cmd + Shift + P (on MacOS) or Ctrl + Shift + P (Windows or Linux) to launch the command palette.
  • Select Dev Containers: Rebuild and Reopen in Container.
  • Select the Absence of Change with Drasi option to launch this tutorial.

You need to have your own Kubernetes cluster setup. You can use any Kubernetes setup. For a local testing setup, you can choose one of alternatives like Kind, Minikube or k3d.

Make sure that kubectl on your system points to your Kubernetes cluster.

You will need VS Code

You will need the Drasi VS Code extension

You will need the PostgreSQL CLI tool

You will need to deploy PostgreSQL to your cluster. The following command can be used, it will also create the table and data required for this tutorial.

kubectl apply -f ./postgres.yaml 
kubectl wait --for=condition=ready pod -l app=postgres --timeout=60s

If you are not using the GitHub codespace or VS Code dev container, you will need to open a port forward to access the PostgreSQL instance.

kubectl port-forward services/postgres 5432:5432

PostgreSQL Table

A PostgreSQL table named Freezer has been pre-loaded with that following data:

id temp
1 20
2 28
3 35
4 20
5 40

You can view this data by connecting to the PostgreSQL instance.

psql

Then run the following SQL script:

select * from "Freezer";

Deploy the source

Next we need to connect Drasi to the PostgreSQL database, use the following command to deploy the PostgreSQL source.

drasi apply -f source.yaml

The following command will wait for the source to be ready.

drasi wait -f source.yaml

Continuous Query

To express that a condition must be continuously true for a specified duration, we will use the trueFor function. The trueFor function takes an expression that must evaluate to true for the duration specified, if this expression holds true for the entire length of the duration specified, only then will a notification be emitted that a new item has been added to the result set.

MATCH 
  (f:Freezer)
WHERE drasi.trueFor(f.temp > 32, duration( { seconds: 10 } ))
RETURN
  f.id AS id,
  f.temp AS temp

We will use the Visual Studio extension debug feature to check our query. Open freezer-query.yaml in the editor, and click the Debug action.

This will open a window that will render a table with the live result set of the query.

Simulate freezer updates

Next, we will simulate some temperature changes by updating the PostgreSQL rows.

Connect to the PostgreSQL instance.

psql

Then run the following SQL script:

UPDATE "Freezer" SET temp = 38 WHERE id = 1;

If you watch the debug window, you will notice that freezer 1 only appears in the result set after 10 seconds have elapsed.

Now update freezer 3 from 35 degrees to 42 degrees, and you will see the update reflect immediately because the condition has already been true for more than 10 seconds.

UPDATE "Freezer" SET temp = 42 WHERE id = 3;

Now update freezer 5 from 40 degrees to 20 degrees, and you will see it removed from the result set immediately.

UPDATE "Freezer" SET temp = 20 WHERE id = 5;

If we update freezer 4 from 20 degrees to 35 degrees, and back down to 20 degrees before 10 seconds elapse, then it will never enter the result set.

UPDATE "Freezer" SET temp = 35 WHERE id = 4;
UPDATE "Freezer" SET temp = 20 WHERE id = 4;

Further reading

There are several more functions with similar functionality for different use cases that you can explore in the documentation