Add analytics service base setup to UWP sample app
このコミットが含まれているのは:
1 親
1477e50c01
コミット
c8d597018a
11ファイル変更
+198
-17
検索条件に一致するファイルはありません
@@ -3,6 +3,9 @@
|
||||
##
|
||||
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
|
||||
|
||||
# App tokens
|
||||
**/ServiceTokens/
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
|
||||
@@ -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 "<APP_CENTER_SECRET>" > $APPCENTER_TOKENS_PATH
|
||||
}
|
||||
@@ -12,6 +12,8 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Converters\RenderingPauseConverter.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Converters\ResolutionScaleConverter.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\ComputeShader.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Services\DebugAnalyticsService.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Services\IAnalyticsService.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Shaders\Runners\ContouredLayersRunner.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\MainViewModel.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that manages the analytics service in a test environment.
|
||||
/// </summary>
|
||||
public sealed class DebugAnalyticsService : IAnalyticsService
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.SwapChain.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// The default <see langword="interface"/> for an analytics service.
|
||||
/// </summary>
|
||||
public interface IAnalyticsService
|
||||
{
|
||||
/// <summary>
|
||||
/// Logs an event with a specified title and optional properties.
|
||||
/// </summary>
|
||||
/// <param name="title">The title of the event to track.</param>
|
||||
/// <param name="data">The optional event properties.</param>
|
||||
void Log(string title, params (string Property, string Value)[]? data);
|
||||
}
|
||||
@@ -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;
|
||||
/// </summary>
|
||||
public sealed class MainViewModel : ObservableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="IAnalyticsService"/> instance currently in use.
|
||||
/// </summary>
|
||||
private readonly IAnalyticsService analyticsService;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="MainViewModel"/> instance.
|
||||
/// </summary>
|
||||
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
|
||||
/// </summary>
|
||||
public IReadOnlyList<ComputeShader> ComputeShaderOptions { get; } = new ComputeShader[]
|
||||
{
|
||||
new("Colorful infinity", new ShaderRunner<ColorfulInfinity>(static time => new((float)time.TotalSeconds))),
|
||||
new("Extruded truchet", new ShaderRunner<ExtrudedTruchetPattern>(static time => new((float)time.TotalSeconds))),
|
||||
new("Fractal tiling", new ShaderRunner<FractalTiling>(static time => new((float)time.TotalSeconds))),
|
||||
new("Menger Journey", new ShaderRunner<MengerJourney>(static time => new((float)time.TotalSeconds))),
|
||||
new("Octagrams", new ShaderRunner<Octagrams>(static time => new((float)time.TotalSeconds))),
|
||||
new("Protean clouds", new ShaderRunner<ProteanClouds>(static time => new((float)time.TotalSeconds))),
|
||||
new("Two tiled truchet", new ShaderRunner<TwoTiledTruchet>(static time => new((float)time.TotalSeconds))),
|
||||
new("Pyramid pattern", new ShaderRunner<PyramidPattern>(static time => new((float)time.TotalSeconds))),
|
||||
new("Triangle grid contouring", new ShaderRunner<TriangleGridContouring>(static time => new((float)time.TotalSeconds))),
|
||||
new("Contoured layers", new ContouredLayersRunner())
|
||||
new("Colorful infinity", new ShaderRunner<ColorfulInfinity>(static time => new((float)time.TotalSeconds))),
|
||||
new("Extruded truchet", new ShaderRunner<ExtrudedTruchetPattern>(static time => new((float)time.TotalSeconds))),
|
||||
new("Fractal tiling", new ShaderRunner<FractalTiling>(static time => new((float)time.TotalSeconds))),
|
||||
new("Menger Journey", new ShaderRunner<MengerJourney>(static time => new((float)time.TotalSeconds))),
|
||||
new("Octagrams", new ShaderRunner<Octagrams>(static time => new((float)time.TotalSeconds))),
|
||||
new("Protean clouds", new ShaderRunner<ProteanClouds>(static time => new((float)time.TotalSeconds))),
|
||||
new("Two tiled truchet", new ShaderRunner<TwoTiledTruchet>(static time => new((float)time.TotalSeconds))),
|
||||
new("Pyramid pattern", new ShaderRunner<PyramidPattern>(static time => new((float)time.TotalSeconds))),
|
||||
new("Triangle grid contouring", new ShaderRunner<TriangleGridContouring>(static time => new((float)time.TotalSeconds))),
|
||||
new("Contoured layers", new ContouredLayersRunner())
|
||||
};
|
||||
|
||||
private ComputeShader selectedComputeShader;
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 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().
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configures the services for the application.
|
||||
/// </summary>
|
||||
private static void ConfigureServices()
|
||||
{
|
||||
ServiceCollection services = new();
|
||||
|
||||
#if RELEASE
|
||||
if (Debugger.IsAttached)
|
||||
{
|
||||
services.AddSingleton<IAnalyticsService, DebugAnalyticsService>();
|
||||
}
|
||||
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<IAnalyticsService>(new AppCenterService(appCenterSecret));
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddSingleton<IAnalyticsService, DebugAnalyticsService>();
|
||||
}
|
||||
}
|
||||
#else
|
||||
services.AddSingleton<IAnalyticsService, DebugAnalyticsService>();
|
||||
#endif
|
||||
|
||||
services.AddTransient<MainViewModel>();
|
||||
|
||||
Ioc.Default.ConfigureServices(services.BuildServiceProvider());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Styles the title bar buttons according to the theme in use
|
||||
/// </summary>
|
||||
|
||||
@@ -124,6 +124,7 @@
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Properties\SkipLocalsInitAttribute.cs" />
|
||||
<Compile Include="Services\AppCenterService.cs" />
|
||||
<Compile Include="Views\MainView.xaml.cs">
|
||||
<DependentUpon>MainView.xaml</DependentUpon>
|
||||
</Compile>
|
||||
@@ -146,6 +147,7 @@
|
||||
<Link>Assets\Textures\RustyMetal.png</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<EmbeddedResource Include="Assets\ServiceTokens\AppCenter.txt" />
|
||||
<Content Include="Assets\SmallTile.scale-100.png" />
|
||||
<Content Include="Assets\SmallTile.scale-125.png" />
|
||||
<Content Include="Assets\SmallTile.scale-150.png" />
|
||||
@@ -204,6 +206,15 @@
|
||||
<PackageReference Include="CommunityToolkit.Mvvm">
|
||||
<Version>7.1.2</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.AppCenter.Analytics">
|
||||
<Version>4.4.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.AppCenter.Crashes">
|
||||
<Version>4.4.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
|
||||
<Version>6.0.0</Version>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
|
||||
<Version>6.2.13</Version>
|
||||
</PackageReference>
|
||||
@@ -232,6 +243,9 @@
|
||||
<VisualStudioVersion>14.0</VisualStudioVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
<PreBuildEvent>powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankAppCenterToken.ps1</PreBuildEvent>
|
||||
</PropertyGroup>
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using ComputeSharp.SwapChain.Core.Services;
|
||||
using Microsoft.AppCenter;
|
||||
using Microsoft.AppCenter.Analytics;
|
||||
using Microsoft.AppCenter.Crashes;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace ComputeSharp.SwapChain.Uwp.Services;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IAnalyticsService"/> implementation using AppCenter.
|
||||
/// </summary>
|
||||
public sealed class AppCenterService : IAnalyticsService
|
||||
{
|
||||
/// <summary>
|
||||
/// The maximum length for any property name and value
|
||||
/// </summary>
|
||||
/// <remarks>It's 125, but one character is reserved for the leading '|' to indicate trimming.</remarks>
|
||||
private const int PropertyStringMaxLength = 124;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public AppCenterService(string secret)
|
||||
{
|
||||
AppCenter.Start(secret, typeof(Crashes), typeof(Analytics));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Log(string title, params (string Property, string Value)[]? data)
|
||||
{
|
||||
IDictionary<string, string>? properties = data?.ToDictionary(
|
||||
pair => pair.Property,
|
||||
pair => pair.Value.Length <= PropertyStringMaxLength
|
||||
? pair.Value
|
||||
: $"|{pair.Value.Substring(pair.Value.Length - PropertyStringMaxLength)}");
|
||||
|
||||
Analytics.TrackEvent(title, properties);
|
||||
}
|
||||
}
|
||||
@@ -11,16 +11,12 @@
|
||||
xmlns:media="using:Microsoft.Toolkit.Uwp.UI.Media"
|
||||
xmlns:models="using:ComputeSharp.SwapChain.Core.Models"
|
||||
xmlns:ui="using:Microsoft.Toolkit.Uwp.UI"
|
||||
xmlns:viewmodels="using:ComputeSharp.SwapChain.Core.ViewModels"
|
||||
d:DesignHeight="300"
|
||||
d:DesignWidth="400"
|
||||
SizeChanged="UserControl_SizeChanged"
|
||||
mc:Ignorable="d">
|
||||
|
||||
<Grid x:Name="Root">
|
||||
<Grid.DataContext>
|
||||
<viewmodels:MainViewModel x:Name="ViewModel" />
|
||||
</Grid.DataContext>
|
||||
<Grid.Resources>
|
||||
|
||||
<!--
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using CommunityToolkit.Mvvm.DependencyInjection;
|
||||
using ComputeSharp.SwapChain.Core.ViewModels;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Input;
|
||||
@@ -13,8 +15,14 @@ public sealed partial class MainView : UserControl
|
||||
public MainView()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
this.DataContext = Ioc.Default.GetRequiredService<MainViewModel>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="MainViewModel"/> instance for the current view.
|
||||
/// </summary>
|
||||
public MainViewModel ViewModel => (MainViewModel)DataContext;
|
||||
|
||||
// Opens the shader selection panel
|
||||
private void OpenShaderSelectionPanelButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする