Patch Time Series Entries
-
Patching multiple time series entries (append or delete entries) can be performed via the Session
usingsession.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. -
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
null
) - 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.saveChanges
.
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.
$baseTime = DateUtils::today();
/ Create arrays of timestamps and random values to patch
values = [];
timeStamps = [];
or ($i = 0; $i < 100; $i++)
$values[] = 68 + rand(0, 19);
$timeStamps[] = (clone $baseTime)->add(new DateInterval("PT" . $i . "S"));
patchRequest = new PatchRequest();
patchRequest->setScript("
var i = 0;
for(i = 0; i < \$values.length; i++)
{
timeseries(id(this), \$timeseries)
.append (
new Date(\$timeStamps[i]),
\$values[i],
\$tag);
}");
patchRequest->setValues([
"timeseries" => "HeartRates",
"timeStamps" => $timeStamps,
"values" => $values,
"tag" => "watches/fitbit"
);
session->advanced()->defer(new PatchCommandData("users/1-A", null, $patchRequest, null));
session->saveChanges();
Delete multiple entries:
In this example, we remove a range of 50 time series entries from a document.
// Delete time-series entries
$patchRequest = new PatchRequest();
$patchRequest->setScript("timeseries(this, \$timeseries)
.delete(
\$from,
\$to
);");
$patchRequest->setValues([
"timeseries" => "HeartRates",
"from" => $baseTime,
"to" => (clone $baseTime)->add(new DateInterval("PT49S"))
]);
$session->advanced()->defer(new PatchCommandData("users/1-A", null, $patchRequest, null));
$session->saveChanges();
Syntax
PatchCommandData
new PatchCommandData(?string $id, ?string $changeVector, ?PatchRequest $patch, ?PatchRequest $patchIfMissing = null);
PatchRequest
class PatchRequest
{
// The patching script
private ?string $script = null;
// Values for the parameters used by the patching script
private ?ObjectMap $values = null;
// ... getters and setters ...
}