2021-12-01 05:14:14 +00:00
|
|
|
|
using System.Reflection;
|
2021-11-29 23:35:23 +00:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Chronic.Core;
|
2022-06-22 01:38:42 +00:00
|
|
|
|
using CouchDB.Driver;
|
2022-06-25 20:22:06 +00:00
|
|
|
|
using CouchDB.Driver.Views;
|
2020-05-09 03:56:06 +00:00
|
|
|
|
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-12-01 05:14:14 +00:00
|
|
|
|
using Wabbajack.DTOs.ServerResponses;
|
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;
|
2022-06-22 01:38:42 +00:00
|
|
|
|
private readonly ICouchDatabase<Metric> _db;
|
2020-06-15 04:05:00 +00:00
|
|
|
|
|
2021-11-27 18:31:35 +00:00
|
|
|
|
public MetricsController(ILogger<MetricsController> logger, Metrics metricsStore,
|
2022-06-22 01:38:42 +00:00
|
|
|
|
AppSettings settings, ICouchDatabase<Metric> db)
|
2021-10-23 16:51:17 +00:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_settings = settings;
|
2021-11-27 18:31:35 +00:00
|
|
|
|
_metricsStore = metricsStore;
|
2022-06-22 01:38:42 +00:00
|
|
|
|
_db = db;
|
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]
|
2021-11-30 03:30:24 +00:00
|
|
|
|
[Route("{subject}/{value}")]
|
|
|
|
|
public async Task<Result> LogMetricAsync(string subject, string value)
|
2021-10-23 16:51:17 +00:00
|
|
|
|
{
|
|
|
|
|
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-30 03:30:24 +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
|
|
|
|
|
2022-06-22 21:11:26 +00:00
|
|
|
|
await _db.AddAsync(new Metric
|
2021-02-17 05:46:05 +00:00
|
|
|
|
{
|
2022-06-22 21:11:26 +00:00
|
|
|
|
Timestamp = date,
|
|
|
|
|
Action = subject,
|
|
|
|
|
Subject = value,
|
2021-11-27 18:31:35 +00:00
|
|
|
|
MetricsKey = metricsKey,
|
|
|
|
|
UserAgent = Request.Headers.UserAgent.FirstOrDefault() ?? "<unknown>",
|
2022-06-22 21:11:26 +00:00
|
|
|
|
Ip = Request.Headers["cf-connecting-ip"].FirstOrDefault() ??
|
|
|
|
|
(Request.HttpContext.Connection.RemoteIpAddress?.ToString() ?? "")
|
2021-10-23 16:51:17 +00:00
|
|
|
|
});
|
2022-06-22 21:11:26 +00:00
|
|
|
|
|
2021-11-27 18:31:35 +00:00
|
|
|
|
return new Result {Timestamp = date};
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 23:35:23 +00:00
|
|
|
|
private static byte[] EOL = {(byte)'\n'};
|
2021-12-01 22:53:32 +00:00
|
|
|
|
private static byte[] LBRACKET = {(byte)'['};
|
|
|
|
|
private static byte[] RBRACKET = {(byte)']'};
|
|
|
|
|
private static byte[] COMMA = {(byte) ','};
|
2022-06-22 01:38:42 +00:00
|
|
|
|
|
2021-11-29 23:35:23 +00:00
|
|
|
|
[HttpGet]
|
2021-12-01 05:14:14 +00:00
|
|
|
|
[Route("dump")]
|
2022-02-09 18:24:31 +00:00
|
|
|
|
public async Task GetMetrics([FromQuery] string action, [FromQuery] string from, [FromQuery] string? to, [FromQuery] string? subject)
|
2021-11-29 23:35:23 +00:00
|
|
|
|
{
|
2022-06-25 20:22:06 +00:00
|
|
|
|
throw new NotImplementedException();
|
2021-11-29 23:35:23 +00:00
|
|
|
|
var parser = new Parser();
|
|
|
|
|
|
|
|
|
|
to ??= "now";
|
|
|
|
|
|
|
|
|
|
var toDate = parser.Parse(to).Start;
|
|
|
|
|
var fromDate = parser.Parse(from).Start;
|
|
|
|
|
|
|
|
|
|
var records = _metricsStore.GetRecords(fromDate!.Value, toDate!.Value, action);
|
2022-02-09 18:24:31 +00:00
|
|
|
|
|
2021-11-29 23:35:23 +00:00
|
|
|
|
Response.Headers.ContentType = "application/json";
|
|
|
|
|
await foreach (var record in records)
|
|
|
|
|
{
|
2022-02-09 18:24:31 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(subject) && !record.Subject.Contains(subject))
|
|
|
|
|
continue;
|
2021-11-29 23:35:23 +00:00
|
|
|
|
|
|
|
|
|
await JsonSerializer.SerializeAsync(Response.Body, record);
|
|
|
|
|
await Response.Body.WriteAsync(EOL);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-09 18:24:31 +00:00
|
|
|
|
|
2021-12-01 05:14:14 +00:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("report")]
|
2022-06-25 20:23:08 +00:00
|
|
|
|
[ResponseCache(Duration = 60 * 10, VaryByQueryKeys = new [] {"action", "from", "to"})]
|
2022-06-25 20:22:06 +00:00
|
|
|
|
public async Task GetReport([FromQuery] string action, [FromQuery] string from, [FromQuery] string? to)
|
2021-12-01 05:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
var parser = new Parser();
|
|
|
|
|
|
|
|
|
|
to ??= "now";
|
|
|
|
|
|
|
|
|
|
var toDate = parser.Parse(to).Start!.Value.TruncateToDate();
|
|
|
|
|
|
|
|
|
|
var groupFilterStart = parser.Parse("three days ago").Start!.Value.TruncateToDate();
|
|
|
|
|
toDate = new DateTime(toDate.Year, toDate.Month, toDate.Day);
|
|
|
|
|
|
2022-06-25 20:22:06 +00:00
|
|
|
|
var prefetch = (await GetByAction(action, groupFilterStart, toDate))
|
|
|
|
|
.Select(d => d.Subject)
|
|
|
|
|
.ToHashSet();
|
|
|
|
|
|
2021-12-01 05:14:14 +00:00
|
|
|
|
var fromDate = parser.Parse(from).Start!.Value.TruncateToDate();
|
|
|
|
|
|
2022-06-25 20:22:06 +00:00
|
|
|
|
var counts = (await GetByAction(action, fromDate, toDate))
|
|
|
|
|
.Where(r => prefetch.Contains(r.Subject))
|
|
|
|
|
.ToDictionary(kv => (kv.Date, kv.Subject), kv => kv.Count);
|
2021-12-02 00:12:21 +00:00
|
|
|
|
|
2021-12-01 05:14:14 +00:00
|
|
|
|
Response.Headers.ContentType = "application/json";
|
|
|
|
|
var row = new Dictionary<string, object>();
|
2021-12-01 22:53:32 +00:00
|
|
|
|
|
2021-12-02 00:12:21 +00:00
|
|
|
|
Response.Body.Write(LBRACKET);
|
2021-12-01 05:14:14 +00:00
|
|
|
|
for (var d = fromDate; d <= toDate; d = d.AddDays(1))
|
|
|
|
|
{
|
|
|
|
|
row["_Timestamp"] = d;
|
|
|
|
|
foreach (var group in prefetch)
|
|
|
|
|
{
|
|
|
|
|
if (counts.TryGetValue((d, group), out var found))
|
|
|
|
|
row[group] = found;
|
|
|
|
|
else
|
|
|
|
|
row[group] = 0;
|
|
|
|
|
}
|
2022-06-25 20:22:06 +00:00
|
|
|
|
await JsonSerializer.SerializeAsync(Response.Body, row);
|
2021-12-02 00:12:21 +00:00
|
|
|
|
Response.Body.Write(EOL);
|
2021-12-01 22:53:32 +00:00
|
|
|
|
if (d != toDate)
|
2021-12-02 00:12:21 +00:00
|
|
|
|
Response.Body.Write(COMMA);
|
2021-12-01 05:14:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-02 00:12:21 +00:00
|
|
|
|
Response.Body.Write(RBRACKET);
|
2021-12-01 22:53:32 +00:00
|
|
|
|
|
2021-12-01 05:14:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-06-25 20:22:06 +00:00
|
|
|
|
private async Task<IReadOnlyList<(DateTime Date, string Subject, long Count)>> GetByAction(string action, DateTime from, DateTime to)
|
|
|
|
|
{
|
|
|
|
|
var records = await _db.GetViewAsync<object?[], long>("Indexes", "ActionDaySubject",
|
|
|
|
|
new CouchViewOptions<object?[]>
|
|
|
|
|
{
|
|
|
|
|
StartKey = new object?[]{action, from.Year, from.Month, from.Day, null},
|
|
|
|
|
EndKey = new object?[]{action, to.Year, to.Month, to.Day, new()},
|
|
|
|
|
Reduce = true,
|
|
|
|
|
GroupLevel = 10,
|
|
|
|
|
Group = true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var results = records
|
|
|
|
|
.Where(r => r.Key.Length >= 4 && r.Key[4] != null)
|
|
|
|
|
.Select(r =>
|
|
|
|
|
(new DateTime((int)(long)r.Key[1]!, (int)(long)r.Key[2]!, (int)(long)r.Key[3]!), (string)r.Key[4]!, r.Value));
|
|
|
|
|
return results.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 23:35:23 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public class Result
|
|
|
|
|
{
|
|
|
|
|
public DateTime Timestamp { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|