< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

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
 7public record MatchdayOutcomeCollectionSummary(
 8    int Matchday,
 9    int FetchedMatches,
 10    int CompletedMatches,
 11    int PendingMatches,
 12    int CreatedCount,
 13    int UpdatedCount,
 14    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
 127    public MatchOutcomeCollectionService(
 128        IFirebaseServiceFactory firebaseServiceFactory,
 129        IKicktippClientFactory kicktippClientFactory,
 130        ILogger<MatchOutcomeCollectionService> logger)
 31    {
 132        _firebaseServiceFactory = firebaseServiceFactory;
 133        _kicktippClientFactory = kicktippClientFactory;
 134        _logger = logger;
 135    }
 36
 37    public async Task<MatchOutcomeCollectionResult> CollectAsync(
 38        string communityContext,
 39        bool dryRun,
 40        CancellationToken cancellationToken = default)
 41    {
 142        var kicktippClient = _kicktippClientFactory.CreateClient();
 143        var matchOutcomeRepository = _firebaseServiceFactory.CreateMatchOutcomeRepository();
 44
 145        var currentMatchday = await kicktippClient.GetCurrentTippuebersichtMatchdayAsync(communityContext);
 146        var incompleteMatchdays = await matchOutcomeRepository.GetIncompleteMatchdaysAsync(
 147            communityContext,
 148            currentMatchday,
 149            cancellationToken);
 50
 151        var summaries = new List<MatchdayOutcomeCollectionSummary>();
 52
 153        foreach (var matchday in incompleteMatchdays)
 54        {
 155            var outcomes = await kicktippClient.GetMatchdayOutcomesAsync(communityContext, matchday);
 156            var createdCount = 0;
 157            var updatedCount = 0;
 158            var unchangedCount = 0;
 59
 160            if (!dryRun)
 61            {
 162                foreach (var outcome in outcomes)
 63                {
 164                    var result = await matchOutcomeRepository.UpsertMatchOutcomeAsync(outcome, communityContext, cancell
 165                    switch (result.Disposition)
 66                    {
 67                        case MatchOutcomeWriteDisposition.Created:
 168                            createdCount++;
 169                            break;
 70                        case MatchOutcomeWriteDisposition.Updated:
 171                            updatedCount++;
 172                            break;
 73                        default:
 174                            unchangedCount++;
 75                            break;
 76                    }
 77                }
 78            }
 79
 180            summaries.Add(new MatchdayOutcomeCollectionSummary(
 181                matchday,
 182                outcomes.Count,
 183                outcomes.Count(outcome => outcome.HasOutcome),
 184                outcomes.Count(outcome => !outcome.HasOutcome),
 185                createdCount,
 186                updatedCount,
 187                unchangedCount));
 188        }
 89
 190        _logger.LogInformation(
 191            "Outcome collection evaluated current matchday {CurrentMatchday} and selected {IncompleteMatchdayCount} inco
 192            currentMatchday,
 193            incompleteMatchdays.Count,
 194            communityContext);
 95
 196        return new MatchOutcomeCollectionResult(
 197            currentMatchday,
 198            incompleteMatchdays,
 199            summaries.AsReadOnly());
 1100    }
 101}