using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Dapper; using Wabbajack.Server.DTOs; namespace Wabbajack.Server.DataLayer { public partial class SqlService { public async Task IngestMetric(Metric metric) { await using var conn = await Open(); await conn.ExecuteAsync(@"INSERT INTO dbo.Metrics (Timestamp, Action, Subject, MetricsKey) VALUES (@Timestamp, @Action, @Subject, @MetricsKey)", metric); } public async Task> MetricsReport(string action) { await using var conn = await Open(); return (await conn.QueryAsync(@" SELECT d.Date, d.GroupingSubject as Subject, Count(*) as Count FROM (select DISTINCT CONVERT(date, Timestamp) as Date, GroupingSubject, Action, MetricsKey from dbo.Metrics) m RIGHT OUTER JOIN (SELECT CONVERT(date, DATEADD(DAY, number + 1, dbo.MinMetricDate())) as Date, GroupingSubject, Action FROM master..spt_values CROSS JOIN ( SELECT DISTINCT GroupingSubject, Action FROM dbo.Metrics WHERE MetricsKey is not null AND Subject != 'Default' AND TRY_CONVERT(uniqueidentifier, Subject) is null) as keys WHERE type = 'P' AND DATEADD(DAY, number+1, dbo.MinMetricDate()) <= dbo.MaxMetricDate()) as d ON m.Date = d.Date AND m.GroupingSubject = d.GroupingSubject AND m.Action = d.Action WHERE d.Action = @action AND d.Date >= DATEADD(month, -1, GETUTCDATE()) group by d.Date, d.GroupingSubject, d.Action ORDER BY d.Date, d.GroupingSubject, d.Action", new {Action = action})) .ToList(); } } }