File: Model\ExportHelpers.cs
Web Access
Project: src\src\Aspire.Dashboard\Aspire.Dashboard.csproj (Aspire.Dashboard)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using Aspire.Dashboard.Otlp.Model;
using Aspire.Dashboard.Otlp.Storage;
 
namespace Aspire.Dashboard.Model;
 
/// <summary>
/// Represents the result of exporting data.
/// </summary>
/// <param name="Content">The content to export.</param>
/// <param name="FileName">The suggested file name for downloading the content.</param>
internal sealed record ExportResult(string Content, string FileName);
 
/// <summary>
/// Helper methods for exporting data.
/// </summary>
internal static class ExportHelpers
{
    /// <summary>
    /// Gets a span as a JSON export result, including associated log entries.
    /// </summary>
    /// <param name="span">The span to convert.</param>
    /// <param name="telemetryRepository">The telemetry repository to fetch logs from.</param>
    /// <returns>A result containing the JSON representation and suggested file name.</returns>
    public static ExportResult GetSpanAsJson(OtlpSpan span, TelemetryRepository telemetryRepository)
    {
        var logs = telemetryRepository.GetLogsForSpan(span.TraceId, span.SpanId);
        var json = TelemetryExportService.ConvertSpanToJson(span, logs);
        var fileName = $"span-{OtlpHelpers.ToShortenedId(span.SpanId)}.json";
        return new ExportResult(json, fileName);
    }
 
    /// <summary>
    /// Gets a log entry as a JSON export result.
    /// </summary>
    /// <param name="logEntry">The log entry to convert.</param>
    /// <returns>A result containing the JSON representation and suggested file name.</returns>
    public static ExportResult GetLogEntryAsJson(OtlpLogEntry logEntry)
    {
        var json = TelemetryExportService.ConvertLogEntryToJson(logEntry);
        var fileName = $"log-{logEntry.InternalId}.json";
        return new ExportResult(json, fileName);
    }
 
    /// <summary>
    /// Gets all spans in a trace as a JSON export result, including associated log entries.
    /// </summary>
    /// <param name="trace">The trace to convert.</param>
    /// <param name="telemetryRepository">The telemetry repository to fetch logs from.</param>
    /// <returns>A result containing the JSON representation and suggested file name.</returns>
    public static ExportResult GetTraceAsJson(OtlpTrace trace, TelemetryRepository telemetryRepository)
    {
        var logs = telemetryRepository.GetLogsForTrace(trace.TraceId);
        var json = TelemetryExportService.ConvertTraceToJson(trace, logs);
        var fileName = $"trace-{OtlpHelpers.ToShortenedId(trace.TraceId)}.json";
        return new ExportResult(json, fileName);
    }
 
    /// <summary>
    /// Gets a resource as a JSON export result.
    /// </summary>
    /// <param name="resource">The resource to convert.</param>
    /// <param name="getResourceName">A function to resolve the resource name for the file name.</param>
    /// <returns>A result containing the JSON representation and suggested file name.</returns>
    public static ExportResult GetResourceAsJson(ResourceViewModel resource, Func<ResourceViewModel, string> getResourceName)
    {
        var json = TelemetryExportService.ConvertResourceToJson(resource);
        var fileName = $"{getResourceName(resource)}.json";
        return new ExportResult(json, fileName);
    }
 
    /// <summary>
    /// Gets environment variables as a .env file export result.
    /// </summary>
    /// <param name="resource">The resource containing environment variables.</param>
    /// <param name="getResourceName">A function to resolve the resource name for the file name.</param>
    /// <returns>A result containing the .env file content and suggested file name.</returns>
    public static ExportResult GetEnvironmentVariablesAsEnvFile(ResourceViewModel resource, Func<ResourceViewModel, string> getResourceName)
    {
        var envContent = EnvHelpers.ConvertToEnvFormat(resource.Environment.Select(e => new KeyValuePair<string, string?>(e.Name, e.Value)));
        var fileName = $"{getResourceName(resource)}.env";
        return new ExportResult(envContent, fileName);
    }
}