2020-05-09 03:56:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2021-02-17 05:46:05 +00:00
|
|
|
|
using System.Reflection;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-06-15 04:05:00 +00:00
|
|
|
|
using Nettle;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using Wabbajack.Common;
|
2021-11-27 18:31:35 +00:00
|
|
|
|
using Wabbajack.Server.DataModels;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
using Wabbajack.Server.DTOs;
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
namespace Wabbajack.BuildServer.Controllers;
|
2020-05-09 13:04:38 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("/metrics")]
|
|
|
|
|
public class MetricsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private static readonly Func<object, string> ReportTemplate = NettleEngine.GetCompiler().Compile(@"
|
2020-06-15 04:05:00 +00:00
|
|
|
|
<html><body>
|
|
|
|
|
<h2>Tar Report for {{$.key}}</h2>
|
|
|
|
|
<h3>Ban Status: {{$.status}}</h3>
|
|
|
|
|
<table>
|
|
|
|
|
{{each $.log }}
|
|
|
|
|
<tr>
|
|
|
|
|
<td>{{$.Timestamp}}</td>
|
|
|
|
|
<td>{{$.Path}}</td>
|
|
|
|
|
<td>{{$.Key}}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
{{/each}}
|
|
|
|
|
</table>
|
|
|
|
|
</body></html>
|
|
|
|
|
");
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private static Func<object, string> _totalListTemplate;
|
|
|
|
|
private readonly AppSettings _settings;
|
|
|
|
|
private ILogger<MetricsController> _logger;
|
2021-11-27 18:31:35 +00:00
|
|
|
|
private readonly Metrics _metricsStore;
|
2020-06-15 04:05:00 +00:00
|
|
|
|
|
2021-11-27 18:31:35 +00:00
|
|
|
|
public MetricsController(ILogger<MetricsController> logger, Metrics metricsStore,
|
2021-10-23 16:51:17 +00:00
|
|
|
|
AppSettings settings)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_settings = settings;
|
2021-11-27 18:31:35 +00:00
|
|
|
|
_metricsStore = metricsStore;
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2020-05-09 03:56:06 +00:00
|
|
|
|
|
2021-02-17 05:46:05 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
private static Func<object, string> TotalListTemplate
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_totalListTemplate == null)
|
2021-02-17 05:46:05 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
var resource = Assembly.GetExecutingAssembly()
|
|
|
|
|
.GetManifestResourceStream("Wabbajack.Server.Controllers.Templates.TotalListTemplate.html")!
|
|
|
|
|
.ReadAllText();
|
|
|
|
|
_totalListTemplate = NettleEngine.GetCompiler().Compile(resource);
|
2021-02-17 05:46:05 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
|
|
|
|
return _totalListTemplate;
|
2021-02-17 05:46:05 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2021-02-17 05:46:05 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{subject}/{value}")]
|
|
|
|
|
public async Task<Result> LogMetricAsync(string subject, string value)
|
|
|
|
|
{
|
|
|
|
|
var date = DateTime.UtcNow;
|
|
|
|
|
var metricsKey = Request.Headers[_settings.MetricsKeyHeader].FirstOrDefault();
|
2021-02-17 05:46:05 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
// Used in tests
|
2021-11-27 18:31:35 +00:00
|
|
|
|
if (value is "Default" or "untitled" || subject == "failed_download" || Guid.TryParse(value, out _))
|
2021-10-23 16:51:17 +00:00
|
|
|
|
return new Result {Timestamp = date};
|
2021-02-18 05:44:54 +00:00
|
|
|
|
|
2021-11-27 18:31:35 +00:00
|
|
|
|
await _metricsStore.Ingest(new Metric
|
2021-02-17 05:46:05 +00:00
|
|
|
|
{
|
2021-11-27 18:31:35 +00:00
|
|
|
|
Timestamp = DateTime.UtcNow,
|
|
|
|
|
Action = subject,
|
|
|
|
|
Subject = subject,
|
|
|
|
|
MetricsKey = metricsKey,
|
|
|
|
|
UserAgent = Request.Headers.UserAgent.FirstOrDefault() ?? "<unknown>",
|
2021-10-23 16:51:17 +00:00
|
|
|
|
});
|
2021-11-27 18:31:35 +00:00
|
|
|
|
return new Result {Timestamp = date};
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Result
|
|
|
|
|
{
|
|
|
|
|
public DateTime Timestamp { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|