Patch Time Series Entries
-
Patching multiple time series entries (append or delete entries) can be performed via the Session
using session.advanced.defer, as described below.- You can handle a single document at a time.
- The patching action is defined by the provided JavaScript.
-
Patching time series entries can also be done directly on the Store via Operations,
where multiple documents can be handled at a time. Learn more in Patching time series operations. -
In this page:
Usage
- Open a session
-
Construct a
PatchCommandData
instance and pass it the following:- The document ID that contains the time series
- The document change vector (or
None
) - A
PatchRequest
instance with a JavaScript that appends or removes time series entries
- Call
session.advanced.defer
and pass it thePatchCommandData
command.
Note that you can call Defer multiple times prior to calling SaveChanges. - Call
session.save_changes
.
All patch requests added via Defer will be sent to the server for execution when SaveChanges is called.
Patching examples
Append multiple entries:
In this example, we append 100 time series entries with random heart rate values to a document.
base_line = datetime.utcnow()
# Create arrays of timestamps and random values to patch
values = []
time_stamps = []
for i in range(100):
values.append(68 + round(19 * random.uniform(0.0, 1.0)))
time_stamps.append(base_line + timedelta(seconds=i))
session.advanced.defer(
PatchCommandData(
"users/1-A",
None,
PatchRequest(
script=(
"var i = 0;"
"for(i = 0; i < $values.length; i++)"
"{"
" timeseries(id(this), $timeseries)"
" .append ("
" new Date($time_stamps[i]),"
" $values[i],"
" $tag);"
"}"
),
values={
"timeseries": "HeartRates",
"time_stamps": time_stamps,
"values": values,
"tag": "watches/fitbit",
},
),
None,
)
)
session.save_changes()
Delete multiple entries:
In this example, we remove a range of 50 time series entries from a document.
# Delete time-series entries
session.advanced.defer(
PatchCommandData(
"users/1-A",
None,
PatchRequest(
script=("timeseries(this, $timeseries)" ".delete(" " $from," " $to" ");"),
values={
"timeseries": "HeartRates",
"from": base_line,
"to": base_line + timedelta(seconds=49),
},
),
None,
)
)
Syntax
PatchCommandData
class PatchCommandData(CommandData):
def __init__(
self,
key: str,
change_vector: Union[None, str],
patch: PatchRequest,
patch_if_missing: Optional[PatchRequest] = None,
): ...
Learn more about PatchCommandData
here.
PatchRequest
class PatchRequest:
def __init__(self, script: Optional[str] = "", values: Optional[Dict[str, object]] = None): ...
Learn more about PatchRequest
here.