| | | 1 | | using GistBackend.Handlers; |
| | | 2 | | using GistBackend.Handlers.AIHandler; |
| | | 3 | | using GistBackend.Handlers.MariaDbHandler; |
| | | 4 | | using GistBackend.Utils; |
| | | 5 | | using Microsoft.Extensions.Hosting; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using Prometheus; |
| | | 8 | | using static GistBackend.Utils.LogEvents; |
| | | 9 | | |
| | | 10 | | namespace GistBackend.Services; |
| | | 11 | | |
| | 7 | 12 | | public class RecapService( |
| | 7 | 13 | | IMariaDbHandler mariaDbHandler, |
| | 7 | 14 | | IAIHandler aiHandler, |
| | 7 | 15 | | IDateTimeHandler dateTimeHandler, |
| | 7 | 16 | | ILogger<RecapService>? logger = null) : BackgroundService |
| | | 17 | | { |
| | 1 | 18 | | private static readonly Gauge DailyRecapGauge = |
| | 1 | 19 | | Metrics.CreateGauge("daily_recap_seconds", "Time spent to create daily recap"); |
| | 1 | 20 | | private static readonly Gauge WeeklyRecapGauge = |
| | 1 | 21 | | Metrics.CreateGauge("weekly_recap_seconds", "Time spent to create weekly recap"); |
| | | 22 | | private const int UtcHourToCreateRecapAt = 5; |
| | | 23 | | |
| | | 24 | | protected override async Task ExecuteAsync(CancellationToken ct) |
| | | 25 | | { |
| | 7 | 26 | | while (!ct.IsCancellationRequested) |
| | | 27 | | { |
| | 7 | 28 | | var startTime = dateTimeHandler.GetUtcNow(); |
| | 7 | 29 | | await CreateDailyRecapIfNecessaryAsync(startTime, ct); |
| | 7 | 30 | | await CreateWeeklyRecapIfNecessaryAsync(startTime, ct); |
| | 7 | 31 | | await ServiceUtils.DelayUntilNextExecutionAsync(startTime, 5, logger, ct, dateTimeHandler); |
| | | 32 | | } |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | private async Task CreateDailyRecapIfNecessaryAsync(DateTime startTime, CancellationToken ct) |
| | | 36 | | { |
| | 7 | 37 | | if (await DailyRecapIsNecessaryAsync(startTime, ct)) |
| | | 38 | | { |
| | 4 | 39 | | using (new SelfReportingStopwatch(elapsed => DailyRecapGauge.Set(elapsed))) |
| | | 40 | | { |
| | 2 | 41 | | await CreateDailyRecapAsync(ct); |
| | 2 | 42 | | } |
| | | 43 | | } |
| | 7 | 44 | | } |
| | | 45 | | |
| | | 46 | | private async Task CreateWeeklyRecapIfNecessaryAsync(DateTime startTime, CancellationToken ct) |
| | | 47 | | { |
| | 7 | 48 | | if (await WeeklyRecapIsNecessaryAsync(startTime, ct)) |
| | | 49 | | { |
| | 4 | 50 | | using (new SelfReportingStopwatch(elapsed => WeeklyRecapGauge.Set(elapsed))) |
| | | 51 | | { |
| | 2 | 52 | | await CreateWeeklyRecapAsync(ct); |
| | 2 | 53 | | } |
| | | 54 | | } |
| | 7 | 55 | | } |
| | | 56 | | |
| | | 57 | | private async Task<bool> DailyRecapIsNecessaryAsync(DateTimeOffset now, CancellationToken ct) => |
| | 7 | 58 | | now.Hour >= UtcHourToCreateRecapAt && !await mariaDbHandler.DailyRecapExistsAsync(ct); |
| | | 59 | | |
| | | 60 | | private async Task<bool> WeeklyRecapIsNecessaryAsync(DateTimeOffset now, CancellationToken ct) => |
| | 7 | 61 | | now.Hour >= UtcHourToCreateRecapAt && !await mariaDbHandler.WeeklyRecapExistsAsync(ct); |
| | | 62 | | |
| | | 63 | | private async Task CreateDailyRecapAsync(CancellationToken ct) |
| | | 64 | | { |
| | 2 | 65 | | var gists = await mariaDbHandler.GetConstructedGistsOfLastDayAsync(ct); |
| | 2 | 66 | | if (gists.Count == 0) |
| | | 67 | | { |
| | 1 | 68 | | logger?.LogInformation(NoGistsForDailyRecap, "No gists to create daily recap"); |
| | 1 | 69 | | return; |
| | | 70 | | } |
| | 1 | 71 | | var recapAIResponse = await aiHandler.GenerateDailyRecapAsync(gists, ct); |
| | 1 | 72 | | await mariaDbHandler.InsertDailyRecapAsync(recapAIResponse, ct); |
| | 1 | 73 | | logger?.LogInformation(DailyRecapCreated, "Daily recap created"); |
| | 2 | 74 | | } |
| | | 75 | | |
| | | 76 | | private async Task CreateWeeklyRecapAsync(CancellationToken ct) |
| | | 77 | | { |
| | 2 | 78 | | var gists = await mariaDbHandler.GetConstructedGistsOfLastWeekAsync(ct); |
| | 2 | 79 | | if (gists.Count == 0) |
| | | 80 | | { |
| | 1 | 81 | | logger?.LogInformation(NoGistsForWeeklyRecap, "No gists to create weekly recap"); |
| | 1 | 82 | | return; |
| | | 83 | | } |
| | 1 | 84 | | var recap = await aiHandler.GenerateWeeklyRecapAsync(gists, ct); |
| | 1 | 85 | | await mariaDbHandler.InsertWeeklyRecapAsync(recap, ct); |
| | 1 | 86 | | logger?.LogInformation(WeeklyRecapCreated, "Weekly recap created"); |
| | 2 | 87 | | } |
| | | 88 | | } |