< 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: 102
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        string? competition = null,
 41        CancellationToken cancellationToken = default)
 42    {
 143        var kicktippClient = _kicktippClientFactory.CreateClient();
 144        var matchOutcomeRepository = _firebaseServiceFactory.CreateMatchOutcomeRepository(competition);
 45
 146        var currentMatchday = await kicktippClient.GetCurrentTippuebersichtMatchdayAsync(communityContext);
 147        var incompleteMatchdays = await matchOutcomeRepository.GetIncompleteMatchdaysAsync(
 148            communityContext,
 149            currentMatchday,
 150            cancellationToken);
 51
 152        var summaries = new List<MatchdayOutcomeCollectionSummary>();
 53
 154        foreach (var matchday in incompleteMatchdays)
 55        {
 156            var outcomes = await kicktippClient.GetMatchdayOutcomesAsync(communityContext, matchday);
 157            var createdCount = 0;
 158            var updatedCount = 0;
 159            var unchangedCount = 0;
 60
 161            if (!dryRun)
 62            {
 163                foreach (var outcome in outcomes)
 64                {
 165                    var result = await matchOutcomeRepository.UpsertMatchOutcomeAsync(outcome, communityContext, cancell
 166                    switch (result.Disposition)
 67                    {
 68                        case MatchOutcomeWriteDisposition.Created:
 169                            createdCount++;
 170                            break;
 71                        case MatchOutcomeWriteDisposition.Updated:
 172                            updatedCount++;
 173                            break;
 74                        default:
 175                            unchangedCount++;
 76                            break;
 77                    }
 78                }
 79            }
 80
 181            summaries.Add(new MatchdayOutcomeCollectionSummary(
 182                matchday,
 183                outcomes.Count,
 184                outcomes.Count(outcome => outcome.HasOutcome),
 185                outcomes.Count(outcome => !outcome.HasOutcome),
 186                createdCount,
 187                updatedCount,
 188                unchangedCount));
 189        }
 90
 191        _logger.LogInformation(
 192            "Outcome collection evaluated current matchday {CurrentMatchday} and selected {IncompleteMatchdayCount} inco
 193            currentMatchday,
 194            incompleteMatchdays.Count,
 195            communityContext);
 96
 197        return new MatchOutcomeCollectionResult(
 198            currentMatchday,
 199            incompleteMatchdays,
 1100            summaries.AsReadOnly());
 1101    }
 102}