Skip to main content

Turn readings into events

Raw datapoints answer "what is the value?"; events answer "what happened?". A common pipeline reads recent readings, checks them against a rule, and records a discrete, queryable event when the rule fires — a threshold breach, a state change, an alarm. Events carry a type, a time, and metadata, and can reference the resources they concern.

Detect a threshold breach and record it

Read the latest hour, and if any reading exceeds a limit, create an event.

import java.time.ZonedDateTime;

var filter = new RetrieveFilter();
filter.setExternalId("engine_temperature");
filter.setStart(ZonedDateTime.now().minusHours(1));
filter.setEnd(ZonedDateTime.now());

var request = new DataRetriever<RetrieveFilter>();
request.setItems(List.of(filter));

var series = client.timeseries().retrieve(request).getItems().get(0);
boolean tooHot = series.getDatapoints().stream()
.anyMatch(p -> Double.parseDouble(p.getValue()) > 110.0);

if (tooHot) {
EventModel event = new EventModel();
event.setExternalId("overheat_press_07_" + System.currentTimeMillis());
event.setType("overheat");
event.setStatus("open");
event.setMetadata(Map.of("series", "engine_temperature", "limit", "110"));
event.setEventTime(ZonedDateTime.now());
client.events().create(List.of(event));
}

Query the events later

Events are first-class records — filter them by type, time or metadata for an audit trail or an incident timeline.

EventRetreiver retriever = new EventRetreiver();
retriever.setLimit(100);
retriever.getFilter().setType("overheat");
DataWrapper<EventModel> overheats = client.events().filter(retriever);
Run it on the live stream

Polling the last hour is fine for a cron job. To react the moment a reading crosses the line, run this rule inside a live subscription loop instead of on a timer.