| | | 1 | | using EHonda.KicktippAi.Core; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using Orchestrator.Infrastructure.Factories; |
| | | 4 | | |
| | | 5 | | namespace Orchestrator.Services; |
| | | 6 | | |
| | | 7 | | public record MatchdayOutcomeCollectionSummary( |
| | | 8 | | int Matchday, |
| | | 9 | | int FetchedMatches, |
| | | 10 | | int CompletedMatches, |
| | | 11 | | int PendingMatches, |
| | | 12 | | int CreatedCount, |
| | | 13 | | int UpdatedCount, |
| | | 14 | | int UnchangedCount); |
| | | 15 | | |
| | 1 | 16 | | public record MatchOutcomeCollectionResult( |
| | 1 | 17 | | int CurrentMatchday, |
| | 1 | 18 | | IReadOnlyList<int> IncompleteMatchdays, |
| | 1 | 19 | | IReadOnlyList<MatchdayOutcomeCollectionSummary> MatchdaySummaries); |
| | | 20 | | |
| | | 21 | | public class MatchOutcomeCollectionService |
| | | 22 | | { |
| | | 23 | | private readonly IFirebaseServiceFactory _firebaseServiceFactory; |
| | | 24 | | private readonly IKicktippClientFactory _kicktippClientFactory; |
| | | 25 | | private readonly ILogger<MatchOutcomeCollectionService> _logger; |
| | | 26 | | |
| | | 27 | | public MatchOutcomeCollectionService( |
| | | 28 | | IFirebaseServiceFactory firebaseServiceFactory, |
| | | 29 | | IKicktippClientFactory kicktippClientFactory, |
| | | 30 | | ILogger<MatchOutcomeCollectionService> logger) |
| | | 31 | | { |
| | | 32 | | _firebaseServiceFactory = firebaseServiceFactory; |
| | | 33 | | _kicktippClientFactory = kicktippClientFactory; |
| | | 34 | | _logger = logger; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<MatchOutcomeCollectionResult> CollectAsync( |
| | | 38 | | string communityContext, |
| | | 39 | | bool dryRun, |
| | | 40 | | CancellationToken cancellationToken = default) |
| | | 41 | | { |
| | | 42 | | var kicktippClient = _kicktippClientFactory.CreateClient(); |
| | | 43 | | var matchOutcomeRepository = _firebaseServiceFactory.CreateMatchOutcomeRepository(); |
| | | 44 | | |
| | | 45 | | var currentMatchday = await kicktippClient.GetCurrentTippuebersichtMatchdayAsync(communityContext); |
| | | 46 | | var incompleteMatchdays = await matchOutcomeRepository.GetIncompleteMatchdaysAsync( |
| | | 47 | | communityContext, |
| | | 48 | | currentMatchday, |
| | | 49 | | cancellationToken); |
| | | 50 | | |
| | | 51 | | var summaries = new List<MatchdayOutcomeCollectionSummary>(); |
| | | 52 | | |
| | | 53 | | foreach (var matchday in incompleteMatchdays) |
| | | 54 | | { |
| | | 55 | | var outcomes = await kicktippClient.GetMatchdayOutcomesAsync(communityContext, matchday); |
| | | 56 | | var createdCount = 0; |
| | | 57 | | var updatedCount = 0; |
| | | 58 | | var unchangedCount = 0; |
| | | 59 | | |
| | | 60 | | if (!dryRun) |
| | | 61 | | { |
| | | 62 | | foreach (var outcome in outcomes) |
| | | 63 | | { |
| | | 64 | | var result = await matchOutcomeRepository.UpsertMatchOutcomeAsync(outcome, communityContext, cancell |
| | | 65 | | switch (result.Disposition) |
| | | 66 | | { |
| | | 67 | | case MatchOutcomeWriteDisposition.Created: |
| | | 68 | | createdCount++; |
| | | 69 | | break; |
| | | 70 | | case MatchOutcomeWriteDisposition.Updated: |
| | | 71 | | updatedCount++; |
| | | 72 | | break; |
| | | 73 | | default: |
| | | 74 | | unchangedCount++; |
| | | 75 | | break; |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | summaries.Add(new MatchdayOutcomeCollectionSummary( |
| | | 81 | | matchday, |
| | | 82 | | outcomes.Count, |
| | | 83 | | outcomes.Count(outcome => outcome.HasOutcome), |
| | | 84 | | outcomes.Count(outcome => !outcome.HasOutcome), |
| | | 85 | | createdCount, |
| | | 86 | | updatedCount, |
| | | 87 | | unchangedCount)); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | _logger.LogInformation( |
| | | 91 | | "Outcome collection evaluated current matchday {CurrentMatchday} and selected {IncompleteMatchdayCount} inco |
| | | 92 | | currentMatchday, |
| | | 93 | | incompleteMatchdays.Count, |
| | | 94 | | communityContext); |
| | | 95 | | |
| | | 96 | | return new MatchOutcomeCollectionResult( |
| | | 97 | | currentMatchday, |
| | | 98 | | incompleteMatchdays, |
| | | 99 | | summaries.AsReadOnly()); |
| | | 100 | | } |
| | | 101 | | } |