Append & Update Time Series
-
Use
time_series_for.append_single
ortime_series_for.append
for the following actions:- Creating a new time series
Appending an entry to a time series that doesn't exist yet
will create the time series and add it the new entry. - Creating a new time series entry
Appending a new entry to an existing time series
will add the entry to the series at the specified timestamp. - Modifying an existing time series entry
Useappend_single
orappend
to update the data of an existing entry with the specified timestamp.
- Creating a new time series
-
Each call to
append_single
handles a single time series value at a single time series entry.
Each call toappend
can handle multiple time series values at a single time series entry. -
To append multiple entries in a single transaction you can:
- Call
append_single
orappend
as many times as needed before callingsession.save_changes
, as shown in the examples below. - Use patching to update the time series. Learn more in Patch time series entries.
- Append entries directly on the Store via Operations. Learn more in Append time series operations.
- Call
-
In this page:
Usage
Flow:
- Open a session.
-
Create an instance of
time_series_for
and pass it the following:- Provide an explicit document ID, -or-
pass an entity tracked by the session, e.g. a document object returned from session.query or from session.load. - Specify the time series name.
- Provide an explicit document ID, -or-
- Call
time_series_for.append_single
ortime_series_for.append
and pass it the time series entry details. - Call
session.save_changes
for the action to take effect on the server.
Note:
- A
DocumentDoesNotExistException
exception is thrown if the specified document does not exist.
Examples
Append entries with a single value:
- In this example, entries are appended with a single value.
- Although a loop is used to append multiple entries,
all entries are appended in a single transaction whensave_changes
is executed.
base_line = datetime.utcnow()
# Append 10 HeartRate values
with store.open_session() as session:
session.store(User(name="John"), "users/john")
tsf = session.time_series_for("users/john", "HeartRates")
for i in range(10):
tsf.append_single(base_line + timedelta(seconds=i), 67.0, "watches/fitbit")
session.save_changes()
Append entries with multiple values:
- In this example, we append multi-value StockPrice entries.
- Notice the clarity gained by naming the values.
with store.open_session() as session:
session.store(User(name="John"), "users/john")
session.time_series_for("users/john", "StockPrices").append(
base_line + timedelta(days=1), [52, 54, 63.5, 51.4, 9824], "companies/kitchenAppliances"
)
session.time_series_for("users/john", "StockPrices").append(
base_line + timedelta(days=2), [54, 55, 61.5, 49.4, 8400], "companies/kitchenAppliances"
)
session.time_series_for("users/john", "StockPrices").append(
base_line + timedelta(days=3), [55, 57, 65.5, 50, 9020], "companies/kitchenAppliances"
)
session.save_changes()
with store.open_session() as session:
session.store(User(name="John"), "users/john")
session.typed_time_series_for(StockPrice, "users/john").append_single(
base_line + timedelta(days=1), StockPrice(52, 54, 63.5, 51.4, 9824), "companies/kitchenAppliances"
)
session.typed_time_series_for(StockPrice, "users/john").append_single(
base_line + timedelta(days=2), StockPrice(54, 55, 61.5, 49.4, 8400), "companies/kitchenAppliances"
)
session.typed_time_series_for(StockPrice, "users/john").append_single(
base_line + timedelta(days=3), StockPrice(55, 57, 65.5, 50, 9020), "companies/kitchenAppliances"
)
session.save_changes()
Syntax
def append_single(self, timestamp: datetime, value: float, tag: Optional[str] = None) -> None: ...
def append(self, timestamp: datetime, values: List[float], tag: Optional[str] = None) -> None: ...
Parameter | Type | Description |
---|---|---|
timestamp | datetime |
Time series entry's timestamp |
value | float |
Entry's value |
values | List[float] |
Entry's values |
tag (Optional) | str |
An optional tag for the entry |