WIP metrics reports

This commit is contained in:
Timothy Baldridge 2021-11-29 20:22:12 -07:00
parent cbb374af26
commit 371d23852b
5 changed files with 78 additions and 13 deletions

View File

@ -60,6 +60,7 @@ internal class Program
services.AddSingleton<IVerb, ValidateLists>();
services.AddSingleton<IVerb, DownloadCef>();
services.AddSingleton<IVerb, DownloadUrl>();
services.AddSingleton<IVerb, GenerateMetricsReports>();
}).Build();
var service = host.Services.GetService<CommandLineBuilder>();

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.DTOs.ServerResponses;
using Wabbajack.Paths;
namespace Wabbajack.CLI.Verbs;
public class GenerateMetricsReports : IVerb
{
private readonly HttpClient _client;
private readonly DTOSerializer _dtos;
public GenerateMetricsReports(HttpClient client, DTOSerializer dtos)
{
_client = client;
_dtos = dtos;
}
public Command MakeCommand()
{
var command = new Command("generate-metrics-report");
command.Add(new Option<AbsolutePath>(new[] {"-o", "-output"}, "Output folder"));
command.Description = "Generates usage metrics and outputs a html report about them";
command.Handler = CommandHandler.Create(Run);
return command;
}
private async Task<int> Run(AbsolutePath output)
{
var subjects = await GetMetrics("one day ago", "now", "finish_install")
.Select(async d => d.GroupingSubject)
.ToHashSet();
return 0;
}
private async IAsyncEnumerable<MetricResult> GetMetrics(string start, string end, string action)
{
await using var response = await _client.GetStreamAsync(new Uri($"https://build.wabbajack.org/metrics/report?action={action}&from={start}&end={end}"));
var sr = new StreamReader(response, leaveOpen: false);
while (true)
{
var line = await sr.ReadLineAsync();
if (line == null) break;
yield return _dtos.Deserialize<MetricResult>(line)!;
}
}
}

View File

@ -0,0 +1,14 @@
using System;
namespace Wabbajack.DTOs.ServerResponses;
public class MetricResult
{
public string Action { get; set; }
public string Subject { get; set; }
public string GroupingSubject { get; set; }
public long MetricKey { get; set; }
public string UserAgent { get; set; }
public DateTime Timestamp { get; set; }
}

View File

@ -64,20 +64,20 @@ public class MetricsController : ControllerBase
}
[HttpGet]
[Route("{subject}/{value}")]
public async Task<Result> LogMetricAsync(string subject, string value)
[Route("{action}/{subject}")]
public async Task<Result> LogMetricAsync(string action, string subject)
{
var date = DateTime.UtcNow;
var metricsKey = Request.Headers[_settings.MetricsKeyHeader].FirstOrDefault();
// Used in tests
if (value is "Default" or "untitled" || subject == "failed_download" || Guid.TryParse(value, out _))
if (subject is "Default" or "untitled" || subject == "failed_download" || Guid.TryParse(subject, out _))
return new Result {Timestamp = date};
await _metricsStore.Ingest(new Metric
{
Timestamp = DateTime.UtcNow,
Action = subject,
Action = action,
Subject = subject,
MetricsKey = metricsKey,
UserAgent = Request.Headers.UserAgent.FirstOrDefault() ?? "<unknown>",

View File

@ -9,6 +9,7 @@ using Microsoft.Toolkit.HighPerformance;
using Wabbajack.BuildServer;
using Wabbajack.Common;
using Wabbajack.DTOs.JsonConverters;
using Wabbajack.DTOs.ServerResponses;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
using Wabbajack.Server.DTOs;
@ -104,13 +105,4 @@ public class Metrics
}
}
public class MetricResult
{
public string Action { get; set; }
public string Subject { get; set; }
public string GroupingSubject { get; set; }
public long MetricKey { get; set; }
public string UserAgent { get; set; }
public DateTime Timestamp { get; set; }
}
}