c58d846057
Related Work Items: #416, #422, #423, #424
72 lines
2.1 KiB
C#
72 lines
2.1 KiB
C#
using Microsoft.AppCenter;
|
|
using Microsoft.AppCenter.Analytics;
|
|
using Microsoft.AppCenter.Crashes;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using Windows.ApplicationModel;
|
|
using FoxTube.Services;
|
|
|
|
namespace FoxTube.Utils
|
|
{
|
|
public static class Metrics
|
|
{
|
|
static readonly Stopwatch sw = new Stopwatch();
|
|
public static TimeSpan Uptime
|
|
{
|
|
get => Storage.GetValue<TimeSpan?>(Storage.Metrics.Uptime) ?? TimeSpan.FromSeconds(0);
|
|
set => Storage.SetValue(Storage.Metrics.Uptime, value);
|
|
}
|
|
public static string CurrentVersion
|
|
{
|
|
get
|
|
{
|
|
PackageVersion v = Package.Current.Id.Version;
|
|
return $"{v.Major}.{v.Minor}.{v.Revision}.{v.Build}";
|
|
}
|
|
}
|
|
|
|
static Metrics()
|
|
{
|
|
sw.Start();
|
|
if (!Storage.GetValue<bool>(Storage.Settings.AllowAnalytics))
|
|
return;
|
|
|
|
AppCenter.Start(SecretConstants.MetricsId, typeof(Analytics), typeof(Crashes));
|
|
AppCenter.SetCountryCode(Storage.GetValue<string>(Storage.Settings.Region));
|
|
AppCenter.LogLevel = LogLevel.Verbose;
|
|
}
|
|
|
|
public static void EndSession()
|
|
{
|
|
sw.Stop();
|
|
Uptime += sw.Elapsed;
|
|
|
|
if (Storage.GetValue<bool>(Storage.Settings.AllowAnalytics))
|
|
AddEvent("Session closed",
|
|
("Duration", sw.Elapsed.ToString()),
|
|
("Spend time total", Uptime.ToString()));
|
|
}
|
|
|
|
public static void AddEvent(string eventName, params (string key, string value)[] details)
|
|
{
|
|
if (Storage.GetValue<bool>(Storage.Settings.AllowAnalytics))
|
|
Analytics.TrackEvent(eventName,
|
|
details.Length < 1 ? null :
|
|
details.Select(i => new KeyValuePair<string, string>(i.key, i.value)) as Dictionary<string, string>);
|
|
}
|
|
|
|
public static void SendReport(Exception exception, ErrorAttachmentLog[] logs = null, params (string key, string value)[] details)
|
|
{
|
|
if (Storage.GetValue<bool>(Storage.Settings.AllowAnalytics))
|
|
{
|
|
logs ??= new ErrorAttachmentLog[0];
|
|
Crashes.TrackError(exception ?? new Exception("Unknown exception"),
|
|
details.Length < 1 ? null :
|
|
details.Select(i => new KeyValuePair<string, string>(i.key, i.value)) as Dictionary<string, string>,
|
|
logs);
|
|
}
|
|
}
|
|
}
|
|
} |