< Summary

Information
Class: Orchestrator.Services.MatchdayOutcomeCollectionSummary
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Services/MatchOutcomeCollectionService.cs
Line coverage
100%
Covered lines: 8
Uncovered lines: 0
Coverable lines: 8
Total lines: 102
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

/home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Services/MatchOutcomeCollectionService.cs

#LineLine coverage
 1using EHonda.KicktippAi.Core;
 2using Microsoft.Extensions.Logging;
 3using Orchestrator.Infrastructure.Factories;
 4
 5namespace Orchestrator.Services;
 6
 17public record MatchdayOutcomeCollectionSummary(
 18    int Matchday,
 19    int FetchedMatches,
 110    int CompletedMatches,
 111    int PendingMatches,
 112    int CreatedCount,
 113    int UpdatedCount,
 114    int UnchangedCount);
 15
 16public record MatchOutcomeCollectionResult(
 17    int CurrentMatchday,
 18    IReadOnlyList<int> IncompleteMatchdays,
 19    IReadOnlyList<MatchdayOutcomeCollectionSummary> MatchdaySummaries);
 20
 21public 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        string? competition = null,
 41        CancellationToken cancellationToken = default)
 42    {
 43        var kicktippClient = _kicktippClientFactory.CreateClient();
 44        var matchOutcomeRepository = _firebaseServiceFactory.CreateMatchOutcomeRepository(competition);
 45
 46        var currentMatchday = await kicktippClient.GetCurrentTippuebersichtMatchdayAsync(communityContext);
 47        var incompleteMatchdays = await matchOutcomeRepository.GetIncompleteMatchdaysAsync(
 48            communityContext,
 49            currentMatchday,
 50            cancellationToken);
 51
 52        var summaries = new List<MatchdayOutcomeCollectionSummary>();
 53
 54        foreach (var matchday in incompleteMatchdays)
 55        {
 56            var outcomes = await kicktippClient.GetMatchdayOutcomesAsync(communityContext, matchday);
 57            var createdCount = 0;
 58            var updatedCount = 0;
 59            var unchangedCount = 0;
 60
 61            if (!dryRun)
 62            {
 63                foreach (var outcome in outcomes)
 64                {
 65                    var result = await matchOutcomeRepository.UpsertMatchOutcomeAsync(outcome, communityContext, cancell
 66                    switch (result.Disposition)
 67                    {
 68                        case MatchOutcomeWriteDisposition.Created:
 69                            createdCount++;
 70                            break;
 71                        case MatchOutcomeWriteDisposition.Updated:
 72                            updatedCount++;
 73                            break;
 74                        default:
 75                            unchangedCount++;
 76                            break;
 77                    }
 78                }
 79            }
 80
 81            summaries.Add(new MatchdayOutcomeCollectionSummary(
 82                matchday,
 83                outcomes.Count,
 84                outcomes.Count(outcome => outcome.HasOutcome),
 85                outcomes.Count(outcome => !outcome.HasOutcome),
 86                createdCount,
 87                updatedCount,
 88                unchangedCount));
 89        }
 90
 91        _logger.LogInformation(
 92            "Outcome collection evaluated current matchday {CurrentMatchday} and selected {IncompleteMatchdayCount} inco
 93            currentMatchday,
 94            incompleteMatchdays.Count,
 95            communityContext);
 96
 97        return new MatchOutcomeCollectionResult(
 98            currentMatchday,
 99            incompleteMatchdays,
 100            summaries.AsReadOnly());
 101    }
 102}