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 string Action;
public string Subject;
public string MetricsKey;
}
}

View File

@ -4,6 +4,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
using GraphQL;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Nancy;
@ -19,12 +20,12 @@ namespace Wabbajack.CacheServer
{
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());
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)
@ -63,7 +64,7 @@ namespace Wabbajack.CacheServer
private async Task<string> HandleMetrics(dynamic arg)
{
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();
}

View File

@ -90,6 +90,7 @@ namespace Wabbajack.Common
public static string HashFileExtension => ".xxHash";
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/";

View File

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

View File

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