From c8d597018a296e211f19c0c5346aa7b340dce638 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 5 Jan 2022 20:49:38 +0100 Subject: [PATCH 1/4] Add analytics service base setup to UWP sample app --- .gitignore | 3 ++ build/CreateBlankAppCenterToken.ps1 | 14 +++++ ...mputeSharp.SwapChain.Core.Shared.projitems | 2 + .../Services/DebugAnalyticsService.cs | 30 +++++++++++ .../Services/IAnalyticsService.cs | 16 ++++++ .../ViewModels/MainViewModel.cs | 32 ++++++++---- .../ComputeSharp.SwapChain.Uwp/App.xaml.cs | 51 ++++++++++++++++++- .../ComputeSharp.SwapChain.Uwp.csproj | 14 +++++ .../Services/AppCenterService.cs | 41 +++++++++++++++ .../Views/MainView.xaml | 4 -- .../Views/MainView.xaml.cs | 8 +++ 11 files changed, 198 insertions(+), 17 deletions(-) create mode 100644 build/CreateBlankAppCenterToken.ps1 create mode 100644 samples/ComputeSharp.SwapChain.Core.Shared/Services/DebugAnalyticsService.cs create mode 100644 samples/ComputeSharp.SwapChain.Core.Shared/Services/IAnalyticsService.cs create mode 100644 samples/ComputeSharp.SwapChain.Uwp/Services/AppCenterService.cs diff --git a/.gitignore b/.gitignore index b83d378f..b444c010 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +# App tokens +**/ServiceTokens/ + # User-specific files *.suo *.user diff --git a/build/CreateBlankAppCenterToken.ps1 b/build/CreateBlankAppCenterToken.ps1 new file mode 100644 index 00000000..9c1bfe99 --- /dev/null +++ b/build/CreateBlankAppCenterToken.ps1 @@ -0,0 +1,14 @@ +# Create the tokens folder +$TOKENS_FOLDER_RELATIVE_PATH = Join-Path -Path $PSScriptRoot -ChildPath "..\samples\ComputeSharp.SwapChain.Uwp\Assets\ServiceTokens" +if(!(Test-Path -Path $TOKENS_FOLDER_RELATIVE_PATH)) { + New-Item -ItemType directory -Path $TOKENS_FOLDER_RELATIVE_PATH +} +$TOKENS_FOLDER_PATH = Resolve-Path -Path $TOKENS_FOLDER_RELATIVE_PATH + +# AppCenter tokens +$APPCENTER_TOKENS_NAME = "AppCenter.txt" +$APPCENTER_TOKENS_PATH = Join-Path -Path $TOKENS_FOLDER_PATH -ChildPath $APPCENTER_TOKENS_NAME +if (!(Test-Path -Path $APPCENTER_TOKENS_PATH)) +{ + echo "" > $APPCENTER_TOKENS_PATH +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems index 2a5e911c..aa773398 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems @@ -12,6 +12,8 @@ + + diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Services/DebugAnalyticsService.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Services/DebugAnalyticsService.cs new file mode 100644 index 00000000..fbe016e7 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Services/DebugAnalyticsService.cs @@ -0,0 +1,30 @@ +using System.Diagnostics; +using System.Text; + +#nullable enable + +namespace ComputeSharp.SwapChain.Core.Services; + +/// +/// A that manages the analytics service in a test environment. +/// +public sealed class DebugAnalyticsService : IAnalyticsService +{ + /// + public void Log(string title, params (string Property, string Value)[]? data) + { + StringBuilder builder = new(); + + builder.AppendLine($"[EVENT]: \"{title}\""); + + if (data is not null) + { + foreach (var info in data) + { + builder.AppendLine($">> {info.Property}: \"{info.Value}\""); + } + } + + Trace.Write(builder); + } +} diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Services/IAnalyticsService.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Services/IAnalyticsService.cs new file mode 100644 index 00000000..28c6aa7d --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Services/IAnalyticsService.cs @@ -0,0 +1,16 @@ +#nullable enable + +namespace ComputeSharp.SwapChain.Core.Services; + +/// +/// The default for an analytics service. +/// +public interface IAnalyticsService +{ + /// + /// Logs an event with a specified title and optional properties. + /// + /// The title of the event to track. + /// The optional event properties. + void Log(string title, params (string Property, string Value)[]? data); +} diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs index db1a5868..233d4a51 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ViewModels/MainViewModel.cs @@ -2,6 +2,7 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using ComputeSharp.SwapChain.Core.Models; +using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.Shaders.Runners; using ComputeSharp.SwapChain.Shaders; #if WINDOWS_UWP @@ -9,6 +10,7 @@ using ComputeSharp.Uwp; #else using ComputeSharp.WinUI; #endif +using Microsoft.Toolkit.Diagnostics; #nullable enable @@ -19,11 +21,19 @@ namespace ComputeSharp.SwapChain.Core.ViewModels; /// public sealed class MainViewModel : ObservableObject { + /// + /// The instance currently in use. + /// + private readonly IAnalyticsService analyticsService; + /// /// Creates a new instance. /// - public MainViewModel() + public MainViewModel(IAnalyticsService analyticsService) { + Guard.IsNotNull(analyticsService, nameof(analyticsService)); + + this.analyticsService = analyticsService; this.isDynamicResolutionEnabled = true; this.selectedResolutionScale = 100; this.selectedComputeShader = ComputeShaderOptions[0]; @@ -78,16 +88,16 @@ public sealed class MainViewModel : ObservableObject /// public IReadOnlyList ComputeShaderOptions { get; } = new ComputeShader[] { - new("Colorful infinity", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Extruded truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Fractal tiling", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Menger Journey", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Octagrams", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Protean clouds", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Two tiled truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Pyramid pattern", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Triangle grid contouring", new ShaderRunner(static time => new((float)time.TotalSeconds))), - new("Contoured layers", new ContouredLayersRunner()) + new("Colorful infinity", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Extruded truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Fractal tiling", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Menger Journey", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Octagrams", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Protean clouds", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Two tiled truchet", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Pyramid pattern", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Triangle grid contouring", new ShaderRunner(static time => new((float)time.TotalSeconds))), + new("Contoured layers", new ContouredLayersRunner()) }; private ComputeShader selectedComputeShader; diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index 720413f7..1dd4a5d7 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -1,4 +1,13 @@ -using ComputeSharp.SwapChain.Uwp.Views; +using System; +using System.Diagnostics; +using System.IO; +using System.Reflection; +using CommunityToolkit.Mvvm.DependencyInjection; +using ComputeSharp.SwapChain.Core.Services; +using ComputeSharp.SwapChain.Core.ViewModels; +using ComputeSharp.SwapChain.Uwp.Services; +using ComputeSharp.SwapChain.Uwp.Views; +using Microsoft.Extensions.DependencyInjection; using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.Core; using Windows.UI; @@ -13,7 +22,7 @@ namespace ComputeSharp.SwapChain.Uwp; sealed partial class App : Application { /// - /// Initializes the singleton application object. This is the first line of authored code + /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// public App() @@ -26,6 +35,7 @@ sealed partial class App : Application { if (Window.Current.Content is not MainView) { + ConfigureServices(); StyleTitleBar(); ExpandViewIntoTitleBar(); @@ -38,6 +48,43 @@ sealed partial class App : Application } } + /// + /// Configures the services for the application. + /// + private static void ConfigureServices() + { + ServiceCollection services = new(); + +#if RELEASE + if (Debugger.IsAttached) + { + services.AddSingleton(); + } + else + { + using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ComputeSharp.SwapChain.Uwp.Assets.ServiceTokens.AppCenter.txt"); + using StreamReader reader = new(stream); + + string appCenterSecret = reader.ReadToEnd().Trim(); + + if (Guid.TryParse(appCenterSecret, out _)) + { + services.AddSingleton(new AppCenterService(appCenterSecret)); + } + else + { + services.AddSingleton(); + } + } +#else + services.AddSingleton(); +#endif + + services.AddTransient(); + + Ioc.Default.ConfigureServices(services.BuildServiceProvider()); + } + /// /// Styles the title bar buttons according to the theme in use /// diff --git a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj index 477be62b..24613ecc 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj +++ b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj @@ -124,6 +124,7 @@ + MainView.xaml @@ -146,6 +147,7 @@ Assets\Textures\RustyMetal.png PreserveNewest + @@ -204,6 +206,15 @@ 7.1.2 + + 4.4.0 + + + 4.4.0 + + + 6.0.0 + 6.2.13 @@ -232,6 +243,9 @@ 14.0 + + powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankAppCenterToken.ps1 + + + + \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/Services/AppCenterService.cs b/samples/ComputeSharp.SwapChain.Uwp/Services/AppCenterService.cs index 7846efcb..ba7f26b2 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Services/AppCenterService.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/Services/AppCenterService.cs @@ -1,6 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; -using System.Threading; using ComputeSharp.SwapChain.Core.Services; using Microsoft.AppCenter; using Microsoft.AppCenter.Analytics; @@ -28,14 +28,34 @@ public sealed class AppCenterService : IAnalyticsService } /// - public void Log(string title, params (string Property, string Value)[]? data) + public void Log(string title, params (string Property, object? Value)[]? data) { - IDictionary? properties = data?.ToDictionary( - pair => pair.Property, - pair => pair.Value.Length <= PropertyStringMaxLength - ? pair.Value - : $"|{pair.Value.Substring(pair.Value.Length - PropertyStringMaxLength)}"); + Analytics.TrackEvent(title, GetProperties(data)); + } - Analytics.TrackEvent(title, properties); + /// + public void Log(string title, Exception exception, params (string Property, object? Value)[]? data) + { + Crashes.TrackError(exception, GetProperties(data)); + } + + /// + /// Gets the additional logging properties from the input data. + /// + /// The optional event properties. + /// The additional logging properties to track. + private static IDictionary? GetProperties((string Property, object? Value)[]? data) + { + return + data?.ToDictionary( + pair => pair.Property, + pair => + { + string text = (pair.Value ?? "").ToString(); + + return text.Length <= PropertyStringMaxLength + ? text + : $"|{text.Substring(text.Length - PropertyStringMaxLength)}"; + }); } } diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml index 9705fdcc..f453cc71 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml @@ -121,7 +121,7 @@ - +