Session: Patch Time Series
-
To patch time series entries to a document, use
session.Advanced.Defer
.- You can pass
Defer
a script to Append, Get, and Remove time series entries. - You can handle a single document at a time.
- You can pass
Patching Using session.Advanced.Defer
- Defer is used for patching in general, not necessarily for time series data patching.
- To patch time series data, you need to customize the JavaScript
that
Defer
uses. - Learn more about time series JavaScript here.
Syntax
-
PatchCommandData
- Definition
Learn more aboutpublic PatchCommandData(string id, string changeVector, PatchRequest patch, PatchRequest patchIfMissing)
PatchCommandData
here.
- Definition
-
PatchRequest
- Definition
Learn more aboutprivate class PatchRequest { // Patching script public string Script { get; set; } // Values that can be used by the patching script public Dictionary<string, object> Values { get; set; } //... }
PatchRequest
here.
- Definition
Usage Flow
- Open a session
- Call
session.Advanced.Defer
and pass it aPatchCommandData
instance. -
Pass the
PatchCommandData
constructor method -- the document ID
- the change vector (or
null
) - a
PatchRequest
instance with a JavaScript that appends or removes time series entries.
- Call
session.SaveChanges()
to perform the patch.
Usage Samples
-
In this sample, we pass
Defer
a script that appends a document 100 time series entries with random heart rate values.
var baseline = DateTime.Today; // Create arrays of timestamps and random values to patch List<double> values = new List<double>(); List<DateTime> timeStamps = new List<DateTime>(); for (var cnt = 0; cnt < 100; cnt++) { values.Add(68 + Math.Round(19 * new Random().NextDouble())); timeStamps.Add(baseline.AddSeconds(cnt)); } session.Advanced.Defer(new PatchCommandData("users/1-A", null, new PatchRequest { Script = @" var i = 0; for(i = 0; i < $values.length; i++) { timeseries(id(this), $timeseries) .append ( new Date($timeStamps[i]), $values[i], $tag); }", Values = { { "timeseries", "HeartRates" }, { "timeStamps", timeStamps}, { "values", values }, { "tag", "watches/fitbit" } } }, null)); session.SaveChanges();
-
In this sample, we remove a range of 50 time series entries from a document.
// Delete time-series entries session.Advanced.Defer(new PatchCommandData("users/1-A", null, new PatchRequest { Script = @"timeseries(this, $timeseries) .delete( $from, $to );", Values = { { "timeseries", "HeartRates" }, { "from", baseline.AddSeconds(0) }, { "to", baseline.AddSeconds(49) } } }, null)); session.SaveChanges();