Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactoring and implement primary constructor #4747

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions src/Aspire.Dashboard/DashboardWebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,14 @@ public sealed class DashboardWebApplication : IAsyncDisposable
private Func<EndpointInfo>? _otlpServiceGrpcEndPointAccessor;
private Func<EndpointInfo>? _otlpServiceHttpEndPointAccessor;

public Func<EndpointInfo> FrontendEndPointAccessor
{
get => _frontendEndPointAccessor ?? throw new InvalidOperationException("WebApplication not started yet.");
}
public Func<EndpointInfo> FrontendEndPointAccessor =>
_frontendEndPointAccessor ?? throw new InvalidOperationException("WebApplication not started yet.");

public Func<EndpointInfo> OtlpServiceGrpcEndPointAccessor
{
get => _otlpServiceGrpcEndPointAccessor ?? throw new InvalidOperationException("WebApplication not started yet.");
}
public Func<EndpointInfo> OtlpServiceGrpcEndPointAccessor =>
_otlpServiceGrpcEndPointAccessor ?? throw new InvalidOperationException("WebApplication not started yet.");

public Func<EndpointInfo> OtlpServiceHttpEndPointAccessor
{
get => _otlpServiceHttpEndPointAccessor ?? throw new InvalidOperationException("WebApplication not started yet.");
}
public Func<EndpointInfo> OtlpServiceHttpEndPointAccessor =>
_otlpServiceHttpEndPointAccessor ?? throw new InvalidOperationException("WebApplication not started yet.");

public IOptionsMonitor<DashboardOptions> DashboardOptionsMonitor => _dashboardOptionsMonitor;

Expand Down Expand Up @@ -317,10 +311,7 @@ await Microsoft.AspNetCore.Authentication.AuthenticationHttpContextExtensions.Si
}
}

private ILogger<DashboardWebApplication> GetLogger()
{
return _app.Services.GetRequiredService<ILoggerFactory>().CreateLogger<DashboardWebApplication>();
}
private ILogger<DashboardWebApplication> GetLogger() => _app.Services.GetRequiredService<ILoggerFactory>().CreateLogger<DashboardWebApplication>();

private static void WriteValidationFailures(ILogger<DashboardWebApplication> logger, IReadOnlyList<string> validationFailures)
{
Expand Down Expand Up @@ -688,10 +679,7 @@ public Task StopAsync(CancellationToken cancellationToken = default)
return _app.StopAsync(cancellationToken);
}

public ValueTask DisposeAsync()
{
return _app.DisposeAsync();
}
public ValueTask DisposeAsync() => _app.DisposeAsync();

private static bool IsHttpsOrNull(Uri? uri) => uri == null || string.Equals(uri.Scheme, "https", StringComparison.Ordinal);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ namespace Aspire.Dashboard.Model;

public sealed class BrowserLinkOutgoingPeerResolver : IOutgoingPeerResolver
{
public IDisposable OnPeerChanges(Func<Task> callback)
{
return new NullSubscription();
}
public IDisposable OnPeerChanges(Func<Task> callback)=> new NullSubscription();

private sealed class NullSubscription : IDisposable
{
Expand Down
16 changes: 4 additions & 12 deletions src/Aspire.Dashboard/Model/BrowserSecurityHeadersMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@ namespace Aspire.Dashboard.Model;
/// - Referrer-Policy
/// - X-Content-Type-Options
/// </summary>
internal sealed class BrowserSecurityHeadersMiddleware
internal sealed class BrowserSecurityHeadersMiddleware(RequestDelegate next, IHostEnvironment environment)
{
private readonly RequestDelegate _next;
private readonly string _cspContentHttp;
private readonly string _cspContentHttps;

public BrowserSecurityHeadersMiddleware(RequestDelegate next, IHostEnvironment environment)
{
_next = next;

_cspContentHttp = GenerateCspContent(environment, isHttps: false);
_cspContentHttps = GenerateCspContent(environment, isHttps: true);
}
private readonly RequestDelegate _next = next;
private readonly string _cspContentHttp = GenerateCspContent(environment, isHttps: false);
private readonly string _cspContentHttps = GenerateCspContent(environment, isHttps: true);

private static string GenerateCspContent(IHostEnvironment environment, bool isHttps)
{
Expand Down
14 changes: 3 additions & 11 deletions src/Aspire.Dashboard/Model/BrowserTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,12 @@ namespace Aspire.Dashboard.Model;
/// - BrowserTimeProvider must be scoped to the user's session.
/// - The built-in TimeProvider registration must be singleton for the system time (used by auth).
/// </summary>
public class BrowserTimeProvider : TimeProvider
public class BrowserTimeProvider(ILoggerFactory loggerFactory) : TimeProvider
{
private readonly ILogger _logger;
private readonly ILogger _logger = loggerFactory.CreateLogger(typeof(BrowserTimeProvider));
private TimeZoneInfo? _browserLocalTimeZone;

public BrowserTimeProvider(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger(typeof(BrowserTimeProvider));
}

public override TimeZoneInfo LocalTimeZone
{
get => _browserLocalTimeZone ?? base.LocalTimeZone;
}
public override TimeZoneInfo LocalTimeZone => _browserLocalTimeZone ?? base.LocalTimeZone;

public void SetBrowserTimeZone(string timeZone)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Aspire.Dashboard/Model/ResourceOutgoingPeerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ public ResourceOutgoingPeerResolver(IDashboardClient resourceService)
});
}

public bool TryResolvePeerName(KeyValuePair<string, string>[] attributes, [NotNullWhen(true)] out string? name)
{
return TryResolvePeerNameCore(_resourceByName, attributes, out name);
}
public bool TryResolvePeerName(KeyValuePair<string, string>[] attributes, [NotNullWhen(true)] out string? name) =>
TryResolvePeerNameCore(_resourceByName, attributes, out name);

internal static bool TryResolvePeerNameCore(IDictionary<string, ResourceViewModel> resources, KeyValuePair<string, string>[] attributes, out string? name)
{
Expand Down
24 changes: 8 additions & 16 deletions src/Aspire.Dashboard/Model/ResourceTypeDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,15 @@ private ResourceTypeDetails(OtlpApplicationType type, string? instanceId, string
public string? InstanceId { get; }
public string? ReplicaSetName { get; }

public static ResourceTypeDetails CreateReplicaSet(string replicaSetName)
{
return new ResourceTypeDetails(OtlpApplicationType.ReplicaSet, instanceId: null, replicaSetName);
}
public static ResourceTypeDetails CreateReplicaSet(string replicaSetName) =>
new ResourceTypeDetails(OtlpApplicationType.ReplicaSet, instanceId: null, replicaSetName);

public static ResourceTypeDetails CreateSingleton(string instanceId)
{
return new ResourceTypeDetails(OtlpApplicationType.Singleton, instanceId, replicaSetName: null);
}
public static ResourceTypeDetails CreateSingleton(string instanceId) =>
new ResourceTypeDetails(OtlpApplicationType.Singleton, instanceId, replicaSetName: null);

public static ResourceTypeDetails CreateReplicaInstance(string instanceId, string replicaSetName)
{
return new ResourceTypeDetails(OtlpApplicationType.ReplicaInstance, instanceId, replicaSetName);
}
public static ResourceTypeDetails CreateReplicaInstance(string instanceId, string replicaSetName) =>
new ResourceTypeDetails(OtlpApplicationType.ReplicaInstance, instanceId, replicaSetName);

public override string ToString()
{
return $"Type = {Type}, InstanceId = {InstanceId}, ReplicaSetName = {ReplicaSetName}";
}
public override string ToString() =>
$"Type = {Type}, InstanceId = {InstanceId}, ReplicaSetName = {ReplicaSetName}";
}
6 changes: 2 additions & 4 deletions src/Aspire.Dashboard/Model/ResourceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ public sealed class ResourceViewModel
public required ImmutableArray<CommandViewModel> Commands { get; init; }
public KnownResourceState? KnownState { get; init; }

internal bool MatchesFilter(string filter)
{
internal bool MatchesFilter(string filter) =>
// TODO let ResourceType define the additional data values we include in searches
return Name.Contains(filter, StringComparisons.UserTextSearch);
}
Name.Contains(filter, StringComparisons.UserTextSearch);

public static string GetResourceName(ResourceViewModel resource, ConcurrentDictionary<string, ResourceViewModel> allResources)
{
Expand Down
42 changes: 14 additions & 28 deletions src/Aspire.Dashboard/Model/ResourceViewModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ namespace Aspire.Dashboard.Model;

internal static class ResourceViewModelExtensions
{
public static bool IsContainer(this ResourceViewModel resource)
{
return StringComparers.ResourceType.Equals(resource.ResourceType, KnownResourceTypes.Container);
}
public static bool IsContainer(this ResourceViewModel resource) =>
StringComparers.ResourceType.Equals(resource.ResourceType, KnownResourceTypes.Container);

public static bool IsProject(this ResourceViewModel resource)
{
return StringComparers.ResourceType.Equals(resource.ResourceType, KnownResourceTypes.Project);
}
public static bool IsProject(this ResourceViewModel resource) =>
StringComparers.ResourceType.Equals(resource.ResourceType, KnownResourceTypes.Project);

public static bool IsExecutable(this ResourceViewModel resource, bool allowSubtypes)
{
Expand All @@ -34,30 +30,20 @@ public static bool IsExecutable(this ResourceViewModel resource, bool allowSubty
return false;
}

public static bool TryGetExitCode(this ResourceViewModel resource, out int exitCode)
{
return resource.TryGetCustomDataInt(KnownProperties.Resource.ExitCode, out exitCode);
}
public static bool TryGetExitCode(this ResourceViewModel resource, out int exitCode) =>
resource.TryGetCustomDataInt(KnownProperties.Resource.ExitCode, out exitCode);

public static bool TryGetContainerImage(this ResourceViewModel resource, [NotNullWhen(returnValue: true)] out string? containerImage)
{
return resource.TryGetCustomDataString(KnownProperties.Container.Image, out containerImage);
}
public static bool TryGetContainerImage(this ResourceViewModel resource, [NotNullWhen(returnValue: true)] out string? containerImage) =>
resource.TryGetCustomDataString(KnownProperties.Container.Image, out containerImage);

public static bool TryGetProjectPath(this ResourceViewModel resource, [NotNullWhen(returnValue: true)] out string? projectPath)
{
return resource.TryGetCustomDataString(KnownProperties.Project.Path, out projectPath);
}
public static bool TryGetProjectPath(this ResourceViewModel resource, [NotNullWhen(returnValue: true)] out string? projectPath) =>
resource.TryGetCustomDataString(KnownProperties.Project.Path, out projectPath);

public static bool TryGetExecutablePath(this ResourceViewModel resource, [NotNullWhen(returnValue: true)] out string? executablePath)
{
return resource.TryGetCustomDataString(KnownProperties.Executable.Path, out executablePath);
}
public static bool TryGetExecutablePath(this ResourceViewModel resource, [NotNullWhen(returnValue: true)] out string? executablePath) =>
resource.TryGetCustomDataString(KnownProperties.Executable.Path, out executablePath);

public static bool TryGetExecutableArguments(this ResourceViewModel resource, out ImmutableArray<string> arguments)
{
return resource.TryGetCustomDataStringArray(KnownProperties.Executable.Args, out arguments);
}
public static bool TryGetExecutableArguments(this ResourceViewModel resource, out ImmutableArray<string> arguments) =>
resource.TryGetCustomDataStringArray(KnownProperties.Executable.Args, out arguments);

private static bool TryGetCustomDataString(this ResourceViewModel resource, string key, [NotNullWhen(returnValue: true)] out string? s)
{
Expand Down
14 changes: 3 additions & 11 deletions src/Aspire.Dashboard/Model/StructuredLogsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace Aspire.Dashboard.Model;

public class StructuredLogsViewModel
public class StructuredLogsViewModel(TelemetryRepository telemetryRepository)
{
private readonly TelemetryRepository _telemetryRepository;
private readonly TelemetryRepository _telemetryRepository = telemetryRepository;
private readonly List<LogFilter> _filters = new();

private PagedResult<OtlpLogEntry>? _logs;
Expand All @@ -19,11 +19,6 @@ public class StructuredLogsViewModel
private int? _logsCount;
private LogLevel? _logLevel;

public StructuredLogsViewModel(TelemetryRepository telemetryRepository)
{
_telemetryRepository = telemetryRepository;
}

public string? ApplicationServiceId { get => _applicationServiceId; set => SetValue(ref _applicationServiceId, value); }
public string FilterText { get => _filterText; set => SetValue(ref _filterText, value); }
public IReadOnlyList<LogFilter> Filters => _filters;
Expand Down Expand Up @@ -97,8 +92,5 @@ public PagedResult<OtlpLogEntry> GetLogs()
return logs;
}

public void ClearData()
{
_logs = null;
}
public void ClearData() => _logs = null;
}
14 changes: 3 additions & 11 deletions src/Aspire.Dashboard/Model/TracesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@

namespace Aspire.Dashboard.Model;

public class TracesViewModel
public class TracesViewModel(TelemetryRepository telemetryRepository)
{
private readonly TelemetryRepository _telemetryRepository;
private readonly TelemetryRepository _telemetryRepository = telemetryRepository;

private PagedResult<OtlpTrace>? _traces;
private string? _applicationServiceId;
private string _filterText = string.Empty;
private int _startIndex;
private int? _count;

public TracesViewModel(TelemetryRepository telemetryRepository)
{
_telemetryRepository = telemetryRepository;
}

public string? ApplicationServiceId { get => _applicationServiceId; set => SetValue(ref _applicationServiceId, value); }
public string FilterText { get => _filterText; set => SetValue(ref _filterText, value); }
public int StartIndex { get => _startIndex; set => SetValue(ref _startIndex, value); }
Expand Down Expand Up @@ -58,9 +53,6 @@ public PagedResult<OtlpTrace> GetTraces()
return traces;
}

public void ClearData()
{
_traces = null;
}
public void ClearData() => _traces = null;
}

15 changes: 4 additions & 11 deletions src/Aspire.Dashboard/Model/ValidateTokenMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@

namespace Aspire.Dashboard.Model;

internal sealed class ValidateTokenMiddleware
internal sealed class ValidateTokenMiddleware(RequestDelegate next, IOptionsMonitor<DashboardOptions> options, ILogger<ValidateTokenMiddleware> logger)
{
private readonly RequestDelegate _next;
private readonly IOptionsMonitor<DashboardOptions> _options;
private readonly ILogger<ValidateTokenMiddleware> _logger;

public ValidateTokenMiddleware(RequestDelegate next, IOptionsMonitor<DashboardOptions> options, ILogger<ValidateTokenMiddleware> logger)
{
_next = next;
_options = options;
_logger = logger;
}
private readonly RequestDelegate _next = next;
private readonly IOptionsMonitor<DashboardOptions> _options = options;
private readonly ILogger<ValidateTokenMiddleware> _logger = logger;

public async Task InvokeAsync(HttpContext context)
{
Expand Down
15 changes: 4 additions & 11 deletions src/Aspire.Dashboard/Otlp/Grpc/OtlpGrpcLogsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ namespace Aspire.Dashboard.Otlp.Grpc;

[Authorize(Policy = OtlpAuthorization.PolicyName)]
[SkipStatusCodePages]
public class OtlpGrpcLogsService : LogsService.LogsServiceBase
public class OtlpGrpcLogsService(OtlpLogsService logsService) : LogsService.LogsServiceBase
{
private readonly OtlpLogsService _logsService;
private readonly OtlpLogsService _logsService = logsService;

public OtlpGrpcLogsService(OtlpLogsService logsService)
{
_logsService = logsService;
}

public override Task<ExportLogsServiceResponse> Export(ExportLogsServiceRequest request, ServerCallContext context)
{
return Task.FromResult(_logsService.Export(request));
}
public override Task<ExportLogsServiceResponse> Export(ExportLogsServiceRequest request, ServerCallContext context) =>
Task.FromResult(_logsService.Export(request));
}
15 changes: 4 additions & 11 deletions src/Aspire.Dashboard/Otlp/Grpc/OtlpGrpcMetricsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ namespace Aspire.Dashboard.Otlp.Grpc;

[Authorize(Policy = OtlpAuthorization.PolicyName)]
[SkipStatusCodePages]
public class OtlpGrpcMetricsService : MetricsService.MetricsServiceBase
public class OtlpGrpcMetricsService(OtlpMetricsService metricsService) : MetricsService.MetricsServiceBase
{
private readonly OtlpMetricsService _metricsService;
private readonly OtlpMetricsService _metricsService = metricsService;

public OtlpGrpcMetricsService(OtlpMetricsService metricsService)
{
_metricsService = metricsService;
}

public override Task<ExportMetricsServiceResponse> Export(ExportMetricsServiceRequest request, ServerCallContext context)
{
return Task.FromResult(_metricsService.Export(request));
}
public override Task<ExportMetricsServiceResponse> Export(ExportMetricsServiceRequest request, ServerCallContext context) =>
Task.FromResult(_metricsService.Export(request));
}
11 changes: 3 additions & 8 deletions src/Aspire.Dashboard/Otlp/Grpc/OtlpGrpcTraceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ public class OtlpGrpcTraceService : TraceService.TraceServiceBase
{
private readonly OtlpTraceService _traceService;

public OtlpGrpcTraceService(OtlpTraceService traceService)
{
_traceService = traceService;
}
public OtlpGrpcTraceService(OtlpTraceService traceService) => _traceService = traceService;

public override Task<ExportTraceServiceResponse> Export(ExportTraceServiceRequest request, ServerCallContext context)
{
return Task.FromResult(_traceService.Export(request));
}
public override Task<ExportTraceServiceResponse> Export(ExportTraceServiceRequest request, ServerCallContext context) =>
Task.FromResult(_traceService.Export(request));
}
Loading