79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json.Linq;
|
|
using RudderSdk.Core.Models;
|
|
|
|
namespace RudderSdk.Core;
|
|
|
|
/// <summary>Context of the scenario node a session was created for.</summary>
|
|
public class ScenarioNodeContext
|
|
{
|
|
private readonly ScenarioService _service;
|
|
|
|
internal ScenarioNodeContext(ScenarioService service, PlanRun run, ExecutionPlanNode node)
|
|
{
|
|
_service = service;
|
|
Run = run;
|
|
Node = node;
|
|
Data = node?.Data as JObject ?? new JObject();
|
|
}
|
|
|
|
/// <summary>The run this node belongs to.</summary>
|
|
public PlanRun Run { get; }
|
|
|
|
/// <summary>The plan node.</summary>
|
|
public ExecutionPlanNode Node { get; }
|
|
|
|
/// <summary>Run id.</summary>
|
|
public string RunId => Run.RunId;
|
|
|
|
/// <summary>Plan id.</summary>
|
|
public string PlanId => Run.PlanId;
|
|
|
|
/// <summary>Scenario id.</summary>
|
|
public string ScenarioId => Run.ScenarioId;
|
|
|
|
/// <summary>Node id.</summary>
|
|
public string NodeId => Node.Id;
|
|
|
|
/// <summary>Node type.</summary>
|
|
public string Type => Node.Type;
|
|
|
|
/// <summary>Node data payload.</summary>
|
|
public JObject Data { get; }
|
|
|
|
/// <summary>Reads a typed value from the node data.</summary>
|
|
public T Get<T>(string key, T defaultValue = default!)
|
|
{
|
|
if (Data == null || !Data.TryGetValue(key, out var value))
|
|
return defaultValue;
|
|
try { return value.ToObject<T>() ?? defaultValue; }
|
|
catch { return defaultValue; }
|
|
}
|
|
|
|
/// <summary>Deserializes the whole node data payload.</summary>
|
|
public T Get<T>()
|
|
{
|
|
try { return Data == null ? default! : Data.ToObject<T>() ?? default!; }
|
|
catch { return default!; }
|
|
}
|
|
|
|
/// <summary>Returns the node data as a plain dictionary.</summary>
|
|
public Dictionary<string, object> AsObjectDictionary()
|
|
{
|
|
return Data?.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
|
|
}
|
|
|
|
internal Task CompleteAsync(string handle, CancellationToken cancellationToken = default)
|
|
=> _service.CompleteNodeAsync(RunId, NodeId, handle, cancellationToken);
|
|
|
|
internal void Complete(string handle)
|
|
{
|
|
_ = CompleteAsync(handle);
|
|
}
|
|
|
|
internal Task AddProgressAsync(string counterKey, long amount, CancellationToken cancellationToken = default)
|
|
=> _service.UpdateProgressAsync(RunId, NodeId, counterKey, amount, cancellationToken);
|
|
}
|