using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using RudderSdk.Core.Models; namespace RudderSdk.Core; /// Context of the scenario node a session was created for. 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(); } /// The run this node belongs to. public PlanRun Run { get; } /// The plan node. public ExecutionPlanNode Node { get; } /// Run id. public string RunId => Run.RunId; /// Plan id. public string PlanId => Run.PlanId; /// Scenario id. public string ScenarioId => Run.ScenarioId; /// Node id. public string NodeId => Node.Id; /// Node type. public string Type => Node.Type; /// Node data payload. public JObject Data { get; } /// Reads a typed value from the node data. public T Get(string key, T defaultValue = default!) { if (Data == null || !Data.TryGetValue(key, out var value)) return defaultValue; try { return value.ToObject() ?? defaultValue; } catch { return defaultValue; } } /// Deserializes the whole node data payload. public T Get() { try { return Data == null ? default! : Data.ToObject() ?? default!; } catch { return default!; } } /// Returns the node data as a plain dictionary. public Dictionary AsObjectDictionary() { return Data?.ToObject>() ?? new Dictionary(); } 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); }