using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tango.Telemetry { /// /// Represents the result of publishing a telemetry package to one or more destinations. /// Contains the status and metrics per destination and total publish time. /// public class TelemetryPublishResult { /// /// Defines the outcome of an attempt to publish to a specific destination. /// public enum DestinationStatus { None, Passed, Unavailable, Failed, Postponed } /// /// Contains information about the result of publishing telemetry to a specific destination. /// public class DestinationResult { /// /// The destination to which the telemetry was attempted to be published. /// public ITelemetryDestination Destination { get; set; } /// /// The result status of the publish attempt. /// public DestinationStatus Status { get; set; } /// /// Any error that occurred during the publish attempt. /// public Exception Error { get; set; } /// /// The amount of time it took to attempt publishing to this destination. /// public TimeSpan ElapsedTime { get; set; } /// /// Number of retry attempts for this destination. /// public int RetryCount { get; internal set; } /// /// Time until the next eligible retry attempt. /// public TimeSpan RetryDelay { get; internal set; } } /// /// Gets or sets the telemetry source that generated the package associated with this publish result. /// public ITelemetrySource Source { get; set; } /// /// Gets or sets the source type of the telemetry (e.g., Streaming, ExternalStorage, PendingStorage). /// public TelemetrySourceTypes SourceType { get; set; } /// /// List of results for each destination that was part of the publish process. /// public List DestinationsResults { get; set; } /// /// Total elapsed time taken to publish the telemetry package across all destinations. /// public TimeSpan TotalElapsedTime { get; set; } /// /// Time spent outside of destination publishing, typically system overhead or coordination. /// public TimeSpan OverheadTime { get { return TimeSpan.FromMilliseconds(TotalElapsedTime.TotalMilliseconds - DestinationsResults.Sum(x => x.ElapsedTime.TotalMilliseconds)); } } /// /// Initializes a new instance of the TelemetryPublishResult class. /// public TelemetryPublishResult() { DestinationsResults = new List(); } public override string ToString() { var sb = new StringBuilder(); sb.AppendLine($"Source: {Source?.Name ?? "Unknown"} ({SourceType})"); sb.AppendLine($"Total Elapsed Time: {TotalElapsedTime.TotalMilliseconds:F1} ms"); sb.AppendLine($"Overhead Time: {OverheadTime.TotalMilliseconds:F1} ms"); sb.AppendLine("Destination Results:"); foreach (var result in DestinationsResults ?? Enumerable.Empty()) { sb.Append($" - {result.Destination.Name}: {result.Status}, {result.ElapsedTime.TotalMilliseconds:F1} ms"); if (!string.IsNullOrWhiteSpace(result.Error.ToStringSafe())) sb.Append($" (Error: {result.Error})"); sb.AppendLine(); } return sb.ToString(); } } }