Archived
1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FoxTube/FoxTube.Core/Utils/Metrics.cs
T
Michael Gordeev 787a6e9f48 Refactored core
UI navigation framework

Related Work Items: #408, #414, #416
2020-06-15 15:46:38 +03:00

75 lines
2.0 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 FoxTube.Services;
using System.Globalization;
namespace FoxTube.Utils
{
public static class Metrics
{
static readonly Stopwatch sw = new Stopwatch();
public static TimeSpan Uptime
{
get => StorageService.Uptime;
set => StorageService.Uptime = value;
}
public static string CurrentVersion
{
get
{
// TODO: Remove on release
/*PackageVersion v = Package.Current.Id.Version;
return $"{v.Major}.{v.Minor}.{v.Revision}.{v.Build}";*/
return "2.0 Preview 1";
}
}
static Metrics()
{
sw.Start();
if (!SettingsService.AllowAnalytics)
return;
AppCenter.Start(SecretConstants.MetricsId, typeof(Analytics), typeof(Crashes));
AppCenter.SetCountryCode(RegionInfo.CurrentRegion.TwoLetterISORegionName);
AppCenter.LogLevel = LogLevel.Verbose;
}
public static void EndSession()
{
sw.Stop();
Uptime += sw.Elapsed;
if (SettingsService.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 (SettingsService.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 (SettingsService.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);
}
}
}
}