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
public PatchCommandData(string id, string changeVector,
PatchRequest patch, PatchRequest patchIfMissing)
Learn more about PatchCommandData
here.
PatchRequest
- Definition
public class PatchRequest
{
// The patching script
public string Script { get; set; }
// Values for the parameters used by the patching script
public Dictionary<string, object> Values { get; set; }
}
Learn more about PatchRequest
here.
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 i = 0; i < 100; i++)
{
values.Add(68 + Math.Round(19 * new Random().NextDouble()));
timeStamps.Add(baseline.AddSeconds(i));
}
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();