-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve selected instrument when switching resources (#5083)
- Loading branch information
Showing
20 changed files
with
274 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
tests/Aspire.Dashboard.Components.Tests/Pages/MetricsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Dashboard.Components.Controls; | ||
using Aspire.Dashboard.Components.Pages; | ||
using Aspire.Dashboard.Components.Resize; | ||
using Aspire.Dashboard.Components.Tests.Shared; | ||
using Aspire.Dashboard.Otlp.Model; | ||
using Aspire.Dashboard.Otlp.Storage; | ||
using Aspire.Dashboard.Utils; | ||
using Bunit; | ||
using Google.Protobuf.Collections; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry.Proto.Metrics.V1; | ||
using Xunit; | ||
using static Aspire.Tests.Shared.Telemetry.TelemetryTestHelpers; | ||
|
||
namespace Aspire.Dashboard.Components.Tests.Pages; | ||
|
||
[UseCulture("en-US")] | ||
public partial class MetricsTests : TestContext | ||
{ | ||
private static readonly DateTime s_testTime = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
|
||
[Fact] | ||
public void ChangeResource_MeterAndInstrumentOnNewResource_InstrumentSet() | ||
{ | ||
ChangeResourceAndAssertInstrument( | ||
app1InstrumentName: "test1", | ||
app2InstrumentName: "test1", | ||
expectedInstrumentNameAfterChange: "test1"); | ||
} | ||
|
||
[Fact] | ||
public void ChangeResource_MeterAndInstrumentNotOnNewResources_InstrumentCleared() | ||
{ | ||
ChangeResourceAndAssertInstrument( | ||
app1InstrumentName: "test1", | ||
app2InstrumentName: "test2", | ||
expectedInstrumentNameAfterChange: null); | ||
} | ||
|
||
private void ChangeResourceAndAssertInstrument(string app1InstrumentName, string app2InstrumentName, string? expectedInstrumentNameAfterChange) | ||
{ | ||
// Arrange | ||
MetricsSetupHelpers.SetupMetricsPage(this); | ||
|
||
var navigationManager = Services.GetRequiredService<NavigationManager>(); | ||
navigationManager.NavigateTo(DashboardUrls.MetricsUrl(resource: "TestApp", meter: "test-meter", instrument: app1InstrumentName)); | ||
|
||
var telemetryRepository = Services.GetRequiredService<TelemetryRepository>(); | ||
telemetryRepository.AddMetrics(new AddContext(), new RepeatedField<ResourceMetrics> | ||
{ | ||
new ResourceMetrics | ||
{ | ||
Resource = CreateResource(name: "TestApp"), | ||
ScopeMetrics = | ||
{ | ||
new ScopeMetrics | ||
{ | ||
Scope = CreateScope(name: "test-meter"), | ||
Metrics = | ||
{ | ||
CreateSumMetric(metricName: app1InstrumentName, startTime: s_testTime.AddMinutes(1)) | ||
} | ||
} | ||
} | ||
}, | ||
new ResourceMetrics | ||
{ | ||
Resource = CreateResource(name: "TestApp2"), | ||
ScopeMetrics = | ||
{ | ||
new ScopeMetrics | ||
{ | ||
Scope = CreateScope(name: "test-meter"), | ||
Metrics = | ||
{ | ||
CreateSumMetric(metricName: app2InstrumentName, startTime: s_testTime.AddMinutes(1)) | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
// Act 1 | ||
var cut = RenderComponent<Metrics>(builder => | ||
{ | ||
builder.Add(m => m.ApplicationName, "TestApp"); | ||
builder.AddCascadingValue(new ViewportInformation(IsDesktop: true, IsUltraLowHeight: false)); | ||
}); | ||
|
||
var viewModel = cut.Instance.PageViewModel; | ||
|
||
// Assert 1 | ||
Assert.Equal("test-meter", viewModel.SelectedMeter!.MeterName); | ||
Assert.Equal(app1InstrumentName, viewModel.SelectedInstrument!.Name); | ||
|
||
// Act 2 | ||
var resourceSelect = cut.FindComponent<ResourceSelect>(); | ||
var innerSelect = resourceSelect.Find("fluent-select"); | ||
innerSelect.Change("TestApp2"); | ||
|
||
cut.WaitForAssertion(() => Assert.Equal("TestApp2", viewModel.SelectedApplication.Name), TestConstants.WaitTimeout); | ||
|
||
Assert.Equal(expectedInstrumentNameAfterChange, viewModel.SelectedInstrument?.Name); | ||
if (expectedInstrumentNameAfterChange != null) | ||
{ | ||
// Meter is cleared if instrument is cleared. | ||
Assert.Equal("test-meter", viewModel.SelectedMeter!.MeterName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
tests/Aspire.Dashboard.Components.Tests/Shared/MetricsSetupHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Aspire.Dashboard.Components.Resize; | ||
using Aspire.Dashboard.Components.Tests.Controls; | ||
using Aspire.Dashboard.Configuration; | ||
using Aspire.Dashboard.Model; | ||
using Aspire.Dashboard.Model.BrowserStorage; | ||
using Aspire.Dashboard.Otlp.Storage; | ||
using Bunit; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.FluentUI.AspNetCore.Components; | ||
|
||
namespace Aspire.Dashboard.Components.Tests.Shared; | ||
|
||
internal static class MetricsSetupHelpers | ||
{ | ||
public static void SetupChartContainer(TestContext context) | ||
{ | ||
_ = context.JSInterop.SetupModule("/Components/Controls/Chart/MetricTable.razor.js"); | ||
|
||
var tabModule = context.JSInterop.SetupModule("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Tabs/FluentTab.razor.js?v=4.9.3.24205"); | ||
tabModule.SetupVoid("TabEditable_Changed", _ => true); | ||
|
||
var overflowModule = context.JSInterop.SetupModule("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Overflow/FluentOverflow.razor.js?v=4.9.3.24205"); | ||
overflowModule.SetupVoid("fluentOverflowInitialize", _ => true); | ||
|
||
SetupPlotlyChart(context); | ||
} | ||
|
||
internal static void SetupPlotlyChart(TestContext context) | ||
{ | ||
var module = context.JSInterop.SetupModule("/js/app-metrics.js"); | ||
module.SetupVoid("initializeChart", _ => true); | ||
module.SetupVoid("updateChart", _ => true); | ||
|
||
context.Services.AddLocalization(); | ||
context.Services.AddSingleton<IInstrumentUnitResolver, TestInstrumentUnitResolver>(); | ||
context.Services.AddSingleton<BrowserTimeProvider, TestTimeProvider>(); | ||
context.Services.AddSingleton<TelemetryRepository>(); | ||
context.Services.AddSingleton<IDialogService, DialogService>(); | ||
} | ||
|
||
internal static void SetupMetricsPage(TestContext context) | ||
{ | ||
var version = typeof(FluentMain).Assembly.GetName().Version!; | ||
|
||
var dividerModule = context.JSInterop.SetupModule(GetFluentFile("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Divider/FluentDivider.razor.js", version)); | ||
dividerModule.SetupVoid("setDividerAriaOrientation"); | ||
|
||
var inputLabelModule = context.JSInterop.SetupModule(GetFluentFile("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Label/FluentInputLabel.razor.js", version)); | ||
inputLabelModule.SetupVoid("setInputAriaLabel", _ => true); | ||
|
||
var dataGridModule = context.JSInterop.SetupModule(GetFluentFile("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/DataGrid/FluentDataGrid.razor.js", version)); | ||
var dataGridRef = dataGridModule.SetupModule("init", _ => true); | ||
dataGridRef.SetupVoid("stop"); | ||
|
||
var listModule = context.JSInterop.SetupModule(GetFluentFile("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/List/ListComponentBase.razor.js", version)); | ||
|
||
var searchModule = context.JSInterop.SetupModule(GetFluentFile("./_content/Microsoft.FluentUI.AspNetCore.Components/Components/Search/FluentSearch.razor.js", version)); | ||
searchModule.SetupVoid("addAriaHidden", _ => true); | ||
|
||
MetricsSetupHelpers.SetupChartContainer(context); | ||
|
||
context.Services.AddLocalization(); | ||
context.Services.AddSingleton<TelemetryRepository>(); | ||
context.Services.AddSingleton<IMessageService, MessageService>(); | ||
context.Services.AddSingleton<IOptions<DashboardOptions>>(Options.Create(new DashboardOptions())); | ||
context.Services.AddSingleton<DimensionManager>(); | ||
context.Services.AddSingleton<IDialogService, DialogService>(); | ||
context.Services.AddSingleton<BrowserTimeProvider, TestTimeProvider>(); | ||
context.Services.AddSingleton<ISessionStorage, TestSessionStorage>(); | ||
context.Services.AddSingleton<ILocalStorage, TestLocalStorage>(); | ||
context.Services.AddSingleton<ShortcutManager>(); | ||
context.Services.AddSingleton<LibraryConfiguration>(); | ||
context.Services.AddSingleton<IKeyCodeService, KeyCodeService>(); | ||
context.Services.AddSingleton<ThemeManager>(); | ||
} | ||
|
||
private static string GetFluentFile(string filePath, Version version) | ||
{ | ||
return $"{filePath}?v={version}"; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/Aspire.Dashboard.Components.Tests/Shared/TestConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Aspire.Dashboard.Components.Tests.Shared; | ||
|
||
internal static class TestConstants | ||
{ | ||
public static readonly TimeSpan WaitTimeout = TimeSpan.FromSeconds(5); | ||
} |
4 changes: 2 additions & 2 deletions
4
tests/Aspire.Dashboard.Components.Tests/Shared/TestInstrumentUnitResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
tests/Aspire.Dashboard.Components.Tests/Shared/TestLocalStorage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.