using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace Wabbajack.Common { public class Metrics { public const string Downloading = "downloading"; public const string BeginInstall = "begin_install"; public const string FinishInstall = "finish_install"; private static AsyncLock _creationLock = new AsyncLock(); public static async ValueTask GetMetricsKey() { using var _ = await _creationLock.WaitAsync(); if (!Utils.HaveEncryptedJson(Consts.MetricsKeyHeader)) { var key = Utils.MakeRandomKey(); await key.ToEcryptedJson(Consts.MetricsKeyHeader); return key; } try { return await Utils.FromEncryptedJson(Consts.MetricsKeyHeader); } catch (Exception) { var key = Utils.MakeRandomKey(); await key.ToEcryptedJson(Consts.MetricsKeyHeader); return key; } } /// /// This is all we track for metrics, action, and value. The action will be like /// "downloaded", the value "Joe's list". /// /// /// public static async Task Send(string action, string value) { var client = new HttpClient(); try { client.DefaultRequestHeaders.Add(Consts.MetricsKeyHeader, await GetMetricsKey()); await client.GetAsync($"{Consts.WabbajackBuildServerUri}metrics/{action}/{value}"); } catch (Exception) { // ignored } } } }