Skip to content
Merged
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
17 changes: 11 additions & 6 deletions src/Aspire.Dashboard/Components/Pages/Metrics.razor
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@
<ChildContent>
@if (PageViewModel.Dashpages.Length is not 0)
{
<FluentTreeItem Text="@Loc[Aspire.Dashboard.Resources.Metrics.DashpagesSectionName]" title="@Loc[Aspire.Dashboard.Resources.Metrics.DashpagesSectionName]" InitiallyExpanded="true">
@foreach (DashpageDefinition dashpage in PageViewModel.Dashpages)
{
<FluentTreeItem @key="dashpage" Class="nested" Text="@dashpage.Name" Data="@dashpage" title="@dashpage.Name" InitiallySelected="@(dashpage.Name == PageViewModel.SelectedDashpage?.Name)"/>
}
</FluentTreeItem>
var filteredDashpages = PageViewModel.Dashpages.Where(PageViewModel.IsDashpageAvailable).ToList();

if (filteredDashpages.Count is not 0)
{
<FluentTreeItem Text="@Loc[Aspire.Dashboard.Resources.Metrics.DashpagesSectionName]" title="@Loc[Aspire.Dashboard.Resources.Metrics.DashpagesSectionName]" InitiallyExpanded="true">
@foreach (DashpageDefinition dashpage in filteredDashpages)
{
<FluentTreeItem @key="dashpage" Class="nested" Text="@dashpage.Name" Data="@dashpage" title="@dashpage.Name" InitiallySelected="@(dashpage.Name == PageViewModel.SelectedDashpage?.Name)"/>
}
</FluentTreeItem>
}
}
@foreach ((var meter, var instruments) in PageViewModel.Instruments.GroupBy(i => i.Parent).OrderBy(g => g.Key.MeterName))
{
Expand Down
67 changes: 66 additions & 1 deletion src/Aspire.Dashboard/Components/Pages/Metrics.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ private void UpdateApplications()
_applications = TelemetryRepository.GetApplications();
_applicationViewModels = ApplicationsSelectHelpers.CreateApplications(_applications);
_applicationViewModels.Insert(0, _selectApplication);

PageViewModel.ApplicationNames = _applications.Select(a => a.ApplicationName).ToImmutableHashSet(StringComparers.OtlpApplicationName);

UpdateSubscription();
}

Expand Down Expand Up @@ -226,6 +229,69 @@ public sealed class MetricsViewModel
public required MetricViewKind? SelectedViewKind { get; set; }

public required ImmutableArray<DashpageDefinition> Dashpages { get; init; }

public ImmutableHashSet<string> ApplicationNames { get; set; } = [];

internal bool IsDashpageAvailable(DashpageDefinition dashpage)
{
if (dashpage.Charts.Count == 0)
{
return false;
}

bool foundOne = false;
bool allExplicitResources = true;

foreach (var chart in dashpage.Charts)
{
bool isInstrumentAvailable = IsInstrumentAvailable(chart.InstrumentName);

if (isInstrumentAvailable)
{
foundOne = true;
}
else if (chart.IsRequired)
{
// A required chart's instrument isn't available.
return false;
}

if (chart.ResourceName is not null)
{
if (IsResourceAvailable(chart.ResourceName))
{
foundOne = true;
}
else
{
// A chart's explicit resource isn't available.
return false;
}
}
else
{
allExplicitResources = false;
}
}

if (!allExplicitResources && SelectedApplication.Id is null)
{
// No resource is selected and at least one chart doesn't specify an explicit resource.
return false;
}

return foundOne;

bool IsInstrumentAvailable(string instrumentName)
{
return Instruments?.Any(i => string.Equals(i.Name, instrumentName, StringComparisons.OtlpInstrumentName)) ?? false;
}

bool IsResourceAvailable(string resourceName)
{
return ApplicationNames.Contains(resourceName);
}
}
}

[JsonConverter(typeof(JsonStringEnumConverter))]
Expand Down Expand Up @@ -318,5 +384,4 @@ public void Dispose()
_applicationsSubscription?.Dispose();
_metricsSubscription?.Dispose();
}

}
5 changes: 5 additions & 0 deletions src/Shared/StringComparers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ internal static class StringComparers
public static StringComparer UrlPath => StringComparer.OrdinalIgnoreCase;
public static StringComparer UrlHost => StringComparer.OrdinalIgnoreCase;
public static StringComparer Attribute => StringComparer.Ordinal;
public static StringComparer OtlpInstrumentName => StringComparer.OrdinalIgnoreCase;
public static StringComparer OtlpApplicationName => StringComparer.OrdinalIgnoreCase;
}

internal static class StringComparisons
Expand All @@ -31,4 +33,7 @@ internal static class StringComparisons
public static StringComparison EnvironmentVariableName => StringComparison.InvariantCultureIgnoreCase;
public static StringComparison UrlPath => StringComparison.OrdinalIgnoreCase;
public static StringComparison UrlHost => StringComparison.OrdinalIgnoreCase;
public static StringComparison Attribute => StringComparison.Ordinal;
public static StringComparison OtlpInstrumentName => StringComparison.OrdinalIgnoreCase;
public static StringComparison OtlpApplicationName => StringComparison.OrdinalIgnoreCase;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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.Model;
using Aspire.Dashboard.Otlp.Model;
using Xunit;
using static Aspire.Dashboard.Components.Pages.Metrics;

namespace Aspire.Dashboard.Components.Tests.Pages;

public sealed class MetricsViewModelTests
{
[Fact]
public void IsDashpageAvailable()
{
OtlpInstrument instrument = new()
{
Name = "present-instrument",
Description = "",
Options = null!,
Parent = null!,
Type = OtlpInstrumentType.Histogram,
Unit = null!
};

MetricsViewModel vm = new()
{
Dashpages = [],
SelectedDuration = null!,
SelectedViewKind = MetricViewKind.Graph,
SelectedApplication = new() { Id = ResourceTypeDetails.CreateSingleton("my-instance", "my-replica-set"), Name = "" },
Instruments = [instrument],
ApplicationNames = ["present-application"],
};

Assert.True(vm.IsDashpageAvailable(new() { Name = "", Charts = [new() { InstrumentName = "present-instrument", Title = "" }] }));
Assert.True(vm.IsDashpageAvailable(new() { Name = "", Charts = [new() { InstrumentName = "present-instrument", Title = "", ResourceName = "present-application" }] }));
Assert.False(vm.IsDashpageAvailable(new() { Name = "", Charts = [new() { InstrumentName = "present-instrument", Title = "", ResourceName = "absent-application" }] }));
Assert.True(vm.IsDashpageAvailable(new() { Name = "", Charts = [new() { InstrumentName = "present-instrument", Title = "" }, new() { InstrumentName = "absent-instrument", Title = "" }] }));
Assert.False(vm.IsDashpageAvailable(new() { Name = "", Charts = [new() { InstrumentName = "absent-instrument", Title = "" }] }));
Assert.False(vm.IsDashpageAvailable(new() { Name = "", Charts = [new() { InstrumentName = "absent-instrument", Title = "" }, new() { InstrumentName = "absent-instrument", Title = "" }] }));
Assert.False(vm.IsDashpageAvailable(new() { Name = "", Charts = [new() { InstrumentName = "present-instrument", Title = "" }, new() { InstrumentName = "absent-instrument", Title = "", IsRequired = true }] }));
}
}