Skip to content

Time Series

diffusion.features.timeseries

TimeSeries

Bases: Component

This feature allows a session to update and query time series topics.

Time series topics

A time series is a sequence of events. Each event contains a value and has server-assigned metadata comprised of a sequence number, timestamp, and author. Events in a time series are ordered by increasing sequence number. Sequence numbers have values between 0 and Number.MAX_INTEGER and are contiguous: an event with sequence number n will be followed by one with sequence number n + 1 . Two events with the same sequence number will be equal – having the same timestamp, author, and value.

A time series topic allows sessions to access a time series that is maintained by the server. A time series topic has an associated DataType event data type, such as Binary, String, or JSON, that determines the type of value associated with each event.

This feature provides a historic query API for time series topics, allowing a session to query arbitrary sub-sequences of a time series.

To create a Time Series type, use the TimeSeries.of method.

The Session.topics and Topics.add_value_stream features complete the API, providing ways to create and subscribe to a time series topic using this type.

The API presents a time series as an append-only data structure of immutable events that is only changed by adding new events.

Edit events

Although a time series is append-only, an event can be overridden by appending an edit event. An edit event is a special type of event that overrides an earlier event in the time series (referred to as the original event) with a new value. When an edit event is added to a time series, the server retains both the original event and the edit event, allowing subscription and query results to reflect the edit.

For example, suppose a time series has two events with the values A and B , and the first event has been overridden by a later edit event that provides a new value of X . The server has the following information about the time series.

Sequence value tp
0 A original event
1 B original event
2 X edit of sequence 0

The current value of the event with sequence number 0 is X .

If an original event has several edit events, the latest edit event (the one with the highest sequence number) determines its current value. Each edit event refers to an original event, never to another edit event.

Extending the example by appending a further edit event to the time series:

Sequence value tp
3 Y second edit of sequence 0

The current value of the event with sequence number 0 is now Y .

Retained range

A time series topic retains a range of the most recent events. When a new event is added to the time series, older events that fall outside of the range are discarded. By default, this range includes the ten most recent events. A different range can be configured by setting the TIME_SERIES_RETAINED_RANGE property.

Subscribing to a time series topic

A session can select a time series topic and add a value stream to receive updates about events appended to the time series. Events are represented by Event instances. Each event has a value and metadata. An edit event has two sets of metadata – its own metadata and that of the original event that it replaces.

Subscription range

New subscribers are sent a range of events from the end of the time series. This is known as the subscription range. Configuring a subscription range is a convenient way to provide new subscribers with an appropriate subset of the latest events.

The default subscription range depends on whether the topic is configured to publish delta streams. If delta streams are enabled, new subscribers are sent the latest event if one exists. If delta streams are disabled, new subscribers are sent no events. Delta streams are enabled by default and can be disabled by setting the PUBLISH_VALUES_ONLY property to true.

A larger subscription range can be configured by setting the TIME_SERIES_SUBSCRIPTION_RANGE property. Regardless of this property, if delta streams are enabled, new subscribers will be sent at least the latest event if one exists.

If the range of events is insufficient, the subscribing session can use a range query to retrieve older events.

When configuring a non-default subscription range for a time series topic, register value streams before subscribing to the topic. The session only maintains a local cache if the latest value received for a topic, not the full subscription range. If a value stream is added after a session has subscribed to a matching time series topic, the new stream will only be notified of the latest value.

Updating a time series topic

A session can use append to submit a value to be added to a time series. The server will add an event to the end of the time series based on the supplied value, with a new sequence number, timestamp, and the author set to the authenticated principal of the session.

Providing a number as fourth argument to append allows a session to submit a value and supplied time. This provides control over the timestamp of the event. The supplied instant must be synchronous to or more recent than the latest event stored by the time series topic. There are no other restrictions.

A session can use edit to submit an edit to an original time series event, identified by its sequence number. The server will add an edit event to the end of the time series based on the supplied value, with a new sequence number, timestamp, and the author set to the authenticated principal of the session.

Time series topics can also be updated using the functionality provided by the Topic Update feature. This includes set_topic and Update Streams. This usage performs an append operation with the added benefits of Update Constraints, topic creation when updating (upsert), and delta streams. When using methods from Topic Update the sequence number, timestamp and author metadata will be generated using the same rules as TimeSeries.append but the associated EventMetadata will not be returned to the caller.

Changes to a time series made outside the API

The API presents a time series as an append-only data structure of immutable events that is only changed by adding new events. The API does not allow events to be deleted or edited.

There are circumstances in which events can be removed from a time series by server operations outside the API. For example, a time series topic can be configured to discard or archive older events to save storage space; or the time series may be held in memory and lost if the server restarts. Subscribed sessions are not notified when events are removed in this way, but a session can infer the removal of events that are no longer included in query results. Similarly, an event's value can be changed on the server. For example, if an administrator changes its value to redact sensitive data. Again, subscribed sessions are not notified when events are modified, but a session can infer this has happened from query results.

Whether such changes can happen for a particular time series topic depends on the topic specification, and the administrative actions that are allowed. To write a robust application, do not rely on two Event instances with the same sequence number but obtained though different API calls, being equal; nor that there are no sequence number gaps between events in query results.

Access control

The session must have the READ_TOPIC topic permission for a topic to query a time series topic. The QUERY_OBSOLETE_TIME_SERIES_EVENTS path permission is additionally required to evaluate an edit range query, or a value range query with an edit range.

The session must have the UPDATE_TOPIC path permission for a topic to append a new event to a time series topic. The EDIT_TIME_SERIES_EVENTS path permission is additionally required to submit an edit to any time series event. The more restrictive EDIT_OWN_TIME_SERIES_EVENTS path permission allows a session to submit edits to time series topic events that are authored by the principal of the calling session.

Since 6.8.3