Include an anonymous key with metrics

This commit is contained in:
Timothy Baldridge 2020-01-03 19:49:08 -07:00
parent 412d854160
commit 8c34066a45
5 changed files with 23 additions and 4 deletions

View File

@ -12,5 +12,6 @@ namespace Wabbajack.CacheServer.DTOs
public DateTime Timestamp; public DateTime Timestamp;
public string Action; public string Action;
public string Subject; public string Subject;
public string MetricsKey;
} }
} }

View File

@ -4,6 +4,7 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem; using Alphaleonis.Win32.Filesystem;
using GraphQL;
using MongoDB.Driver; using MongoDB.Driver;
using MongoDB.Driver.Linq; using MongoDB.Driver.Linq;
using Nancy; using Nancy;
@ -19,12 +20,12 @@ namespace Wabbajack.CacheServer
{ {
private static SemaphoreSlim _lockObject = new SemaphoreSlim(1); private static SemaphoreSlim _lockObject = new SemaphoreSlim(1);
public static async Task Log(DateTime timestamp, string action, string subject) public static async Task Log(DateTime timestamp, string action, string subject, string metricsKey = null)
{ {
var msg = new[] {string.Join("\t", new[]{timestamp.ToString(), action, subject})}; var msg = new[] {string.Join("\t", new[]{timestamp.ToString(), metricsKey, action, subject})};
Utils.Log(msg.First()); Utils.Log(msg.First());
var db = Server.Config.Metrics.Connect(); var db = Server.Config.Metrics.Connect();
await db.InsertOneAsync(new Metric {Timestamp = timestamp, Action = action, Subject = subject}); await db.InsertOneAsync(new Metric {Timestamp = timestamp, Action = action, Subject = subject, MetricsKey = metricsKey});
} }
public static Task Log(string action, string subject) public static Task Log(string action, string subject)
@ -63,7 +64,7 @@ namespace Wabbajack.CacheServer
private async Task<string> HandleMetrics(dynamic arg) private async Task<string> HandleMetrics(dynamic arg)
{ {
var date = DateTime.UtcNow; var date = DateTime.UtcNow;
await Log(date, arg.Action, arg.Value); await Log(date, arg.Action, arg.Value, Request.Headers[Consts.MetricsKeyHeader].FirstOrDefault());
return date.ToString(); return date.ToString();
} }

View File

@ -90,6 +90,7 @@ namespace Wabbajack.Common
public static string HashFileExtension => ".xxHash"; public static string HashFileExtension => ".xxHash";
public static string LocalAppDataPath => Path.Combine(KnownFolders.LocalAppData.Path, "Wabbajack"); public static string LocalAppDataPath => Path.Combine(KnownFolders.LocalAppData.Path, "Wabbajack");
public static string MetricsKeyHeader => "x-metrics-key";
public static string WabbajackCacheLocation = "http://build.wabbajack.org/nexus_api_cache/"; public static string WabbajackCacheLocation = "http://build.wabbajack.org/nexus_api_cache/";

View File

@ -9,6 +9,13 @@ namespace Wabbajack.Common
{ {
public class Metrics public class Metrics
{ {
static Metrics()
{
if (!Utils.HaveEncryptedJson(Consts.MetricsKeyHeader))
{
Utils.ToEcryptedJson(Consts.MetricsKeyHeader, Utils.MakeRandomKey());
}
}
/// <summary> /// <summary>
/// This is all we track for metrics, action, and value. The action will be like /// This is all we track for metrics, action, and value. The action will be like
/// "downloaded", the value "Joe's list". /// "downloaded", the value "Joe's list".
@ -20,6 +27,7 @@ namespace Wabbajack.Common
var client = new HttpClient(); var client = new HttpClient();
try try
{ {
client.DefaultRequestHeaders.Add(Consts.MetricsKeyHeader, Utils.FromEncryptedJson<string>(Consts.MetricsKeyHeader));
await client.GetAsync($"http://build.wabbajack.org/metrics/{action}/{value}"); await client.GetAsync($"http://build.wabbajack.org/metrics/{action}/{value}");
} }
catch (Exception) { } catch (Exception) { }

View File

@ -1123,5 +1123,13 @@ namespace Wabbajack.Common
GlobalMemoryStatusEx(mstat); GlobalMemoryStatusEx(mstat);
return mstat; return mstat;
} }
public static string MakeRandomKey()
{
var random = new Random();
byte[] bytes = new byte[32];
random.NextBytes(bytes);
return bytes.ToHex();
}
} }
} }