Append & Delete Time Series Operations



Usage

Flow:

  • Prepare the Append and Delete operations:
    • Create an instance of TimeSeriesOperation.AppendOperation to define an Append action.
    • Create an instance of TimeSeriesOperation.DeleteOperation fo define a Delete action.
  • Create an instance of TimeSeriesOperation and pass it the the time series name.
    • Call TimeSeriesOperation.Append to add the Append operation.
    • Call TimeSeriesOperation.Delete to add the Delete operation.
  • Create a TimeSeriesBatchOperation instance and pass it:
    • The document ID
    • The TimeSeriesOperation object
  • Execute the TimeSeriesBatchOperation operation by calling store.Operations.Send

Note:

  • All the added Append and Delete operations will be executed in a single-node transaction.
  • Delete actions are executed before Append actions. As seen in this example.
  • Appending entries to a time series that doesn't yet exist yet will create the time series.
  • An exception will be thrown if the specified document does Not exist.

Examples

Append multiple entries

In this example, we append four entries to a time series.

var baseTime = DateTime.Today;

// Define the Append operations:
// =============================
var appendOp1 = new TimeSeriesOperation.AppendOperation
{
    Timestamp = baseTime.AddMinutes(1), Values = new[] {79d}, Tag = "watches/fitbit"
};

var appendOp2 = new TimeSeriesOperation.AppendOperation
{
    Timestamp = baseTime.AddMinutes(2), Values = new[] {82d}, Tag = "watches/fitbit"
};

var appendOp3 = new TimeSeriesOperation.AppendOperation
{
    Timestamp = baseTime.AddMinutes(3), Values = new[] {80d}, Tag = "watches/fitbit" 
};

var appendOp4 = new TimeSeriesOperation.AppendOperation
{
    Timestamp = baseTime.AddMinutes(4), Values = new[] {78d}, Tag = "watches/fitbit" 
};

// Define 'TimeSeriesOperation' and add the Append operations:
// ===========================================================
var timeSeriesOp = new TimeSeriesOperation
{
    Name = "HeartRates"
};

timeSeriesOp.Append(appendOp1);
timeSeriesOp.Append(appendOp2);
timeSeriesOp.Append(appendOp3);
timeSeriesOp.Append(appendOp4);


// Define 'TimeSeriesBatchOperation' and execute:
// ==============================================
var timeSeriesBatchOp = new TimeSeriesBatchOperation("users/john", timeSeriesOp);
store.Operations.Send(timeSeriesBatchOp);

Delete multiple entries

In this example, we delete a range of two entries from a time series.

var baseTime = DateTime.Today;

var deleteOp = new TimeSeriesOperation.DeleteOperation
{
    From = baseTime.AddMinutes(2), To = baseTime.AddMinutes(3)
};

var timeSeriesOp = new TimeSeriesOperation
{
    Name = "HeartRates"
};

timeSeriesOp.Delete(deleteOp);

var timeSeriesBatchOp = new TimeSeriesBatchOperation("users/john", timeSeriesOp);

store.Operations.Send(timeSeriesBatchOp);

Append & Delete entries in the same batch

  • In this example, we append and delete entries in the same batch operation.

  • Note: the Delete actions are executed before all Append actions.

var baseTime = DateTime.Today;

// Define some Append operations:
var appendOp1 = new TimeSeriesOperation.AppendOperation
{
    Timestamp = baseTime.AddMinutes(1), Values = new[] {79d}, Tag = "watches/fitbit"
};

var appendOp2 = new TimeSeriesOperation.AppendOperation
{
    Timestamp = baseTime.AddMinutes(2), Values = new[] {82d}, Tag = "watches/fitbit"
};

var appendOp3 = new TimeSeriesOperation.AppendOperation
{
    Timestamp = baseTime.AddMinutes(3), Values = new[] {80d}, Tag = "watches/fitbit" 
};

// Define a Delete operation:
var deleteOp = new TimeSeriesOperation.DeleteOperation
{
    From = baseTime.AddMinutes(2), To = baseTime.AddMinutes(3)
};

var timeSeriesOp = new TimeSeriesOperation
{
    Name = "HeartRates"
};

// Add the Append & Delete operations to the list of actions
// Note: the Delete action will be executed BEFORE all the Append actions
//       even though it is added last  
timeSeriesOp.Append(appendOp1);
timeSeriesOp.Append(appendOp2);
timeSeriesOp.Append(appendOp3);
timeSeriesOp.Delete(deleteOp);

var timeSeriesBatchOp = new TimeSeriesBatchOperation("users/john", timeSeriesOp);

store.Operations.Send(timeSeriesBatchOp);

// Results:
// All 3 entries that were appended will exist and are not deleted.
// This is because the Delete action occurs first, before all Append actions.

Syntax

TimeSeriesBatchOperation

public TimeSeriesBatchOperation(string documentId, TimeSeriesOperation operation)
Parameter Type Description
documentId string The ID of the document to which you want to Append/Delete time series data.
operation TimeSeriesOperation This class defines which Append/Delete actions to perform within the batch operation.

TimeSeriesOperation

public class TimeSeriesOperation
{
      public string Name;
      public void Append(AppendOperation appendOperation)
      public void Delete(DeleteOperation deleteOperation)
}
Property Type Description
Name string The time series name
Append method Add an AppendOperation to the list
Delete method Add a DeleteOperation to the list

AppendOperation

public class AppendOperation
{
      public DateTime Timestamp;
      public double[] Values;
      public string Tag;
}
Property Type Description
Timestamp DateTime The time series entry will be appended at this timestamp
Values double[] Entry values
Tag string Entry tag (optional)

DeleteOperation

public class DeleteOperation
{
     public DateTime? From, To;
}
Property Type Description
From DateTime? Entries will be deleted starting at this timestamp (inclusive)
Default: DateTime.MinValue
To DateTime? Entries will be deleted up to this timestamp (inclusive)
Default: DateTime.MaxValue