< Summary

Information
Class: Orchestrator.Commands.Utility.UploadKpi.UploadKpiCommand.KpiDocumentJson
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Commands/Utility/UploadKpi/UploadKpiCommand.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 149
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/Commands/Utility/UploadKpi/UploadKpiCommand.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.FileProviders;
 3using Microsoft.Extensions.Logging;
 4using Spectre.Console.Cli;
 5using Spectre.Console;
 6using System.Text.Json;
 7using EHonda.KicktippAi.Core;
 8using Orchestrator.Infrastructure;
 9using Orchestrator.Infrastructure.Factories;
 10
 11namespace Orchestrator.Commands.Utility.UploadKpi;
 12
 13public class UploadKpiCommand : AsyncCommand<UploadKpiSettings>
 14{
 15    private readonly IAnsiConsole _console;
 16    private readonly IFirebaseServiceFactory _firebaseServiceFactory;
 17    private readonly IFileProvider _fileProvider;
 18    private readonly ILogger<UploadKpiCommand> _logger;
 19
 20    public UploadKpiCommand(
 21        IAnsiConsole console,
 22        IFirebaseServiceFactory firebaseServiceFactory,
 23        [FromKeyedServices(ServiceRegistrationExtensions.KpiDocumentsFileProviderKey)] IFileProvider fileProvider,
 24        ILogger<UploadKpiCommand> logger)
 25    {
 26        _console = console;
 27        _firebaseServiceFactory = firebaseServiceFactory;
 28        _fileProvider = fileProvider;
 29        _logger = logger;
 30    }
 31
 32    protected override async Task<int> ExecuteAsync(CommandContext context, UploadKpiSettings settings, CancellationToke
 33    {
 34        try
 35        {
 36            _console.MarkupLine($"[green]Upload KPI command initialized for document:[/] [yellow]{settings.DocumentName}
 37            _console.MarkupLine($"[blue]Using community context:[/] [yellow]{settings.CommunityContext}[/]");
 38            var competition = CompetitionResolver.ResolveCompetition(settings.Competition, settings.CommunityContext, se
 39            var repositoryCompetition = CompetitionResolver.ToRepositoryCompetitionArgument(competition);
 40            _console.MarkupLine($"[blue]Using competition:[/] [yellow]{competition}[/]");
 41
 42            if (settings.Verbose)
 43            {
 44                _console.MarkupLine("[dim]Verbose mode enabled[/]");
 45            }
 46
 47            // Check if the JSON file exists in the community-context specific subfolder
 48            var jsonFilePath = $"output/{settings.CommunityContext}/{settings.DocumentName}.json";
 49            var fileInfo = _fileProvider.GetFileInfo(jsonFilePath);
 50            if (!fileInfo.Exists)
 51            {
 52                _console.MarkupLine($"[red]KPI document file not found:[/] {jsonFilePath}");
 53                _console.MarkupLine($"[dim]Run the PowerShell script with firebase mode to create the document first.[/]
 54                return 1;
 55            }
 56
 57            _console.MarkupLine($"[blue]Reading KPI document from:[/] {jsonFilePath}");
 58
 59            // Read and parse the JSON file
 60            using var stream = fileInfo.CreateReadStream();
 61            var kpiDocument = await JsonSerializer.DeserializeAsync<KpiDocumentJson>(stream, new JsonSerializerOptions
 62            {
 63                PropertyNameCaseInsensitive = true
 64            });
 65
 66            if (kpiDocument == null)
 67            {
 68                _console.MarkupLine("[red]Failed to parse KPI document JSON[/]");
 69                return 1;
 70            }
 71
 72            if (settings.Verbose)
 73            {
 74                _console.MarkupLine($"[dim]Document Name: {kpiDocument.DocumentName}[/]");
 75                _console.MarkupLine($"[dim]Community Context: {kpiDocument.CommunityContext}[/]");
 76                _console.MarkupLine($"[dim]Content length: {kpiDocument.Content.Length} characters[/]");
 77            }
 78
 79            // Create Firebase services using factory (factory handles env var loading)
 80            var kpiRepository = _firebaseServiceFactory.CreateKpiRepository(repositoryCompetition);
 81
 82            // Check if document already exists for this community context
 83            var existingDocument = await kpiRepository.GetKpiDocumentAsync(kpiDocument.DocumentName, kpiDocument.Communi
 84
 85            if (existingDocument != null)
 86            {
 87                _console.MarkupLine($"[blue]Found existing KPI document '{kpiDocument.DocumentName}' (version {existingD
 88                _console.MarkupLine($"[blue]Checking for content changes...[/]");
 89
 90                if (settings.Verbose)
 91                {
 92                    _console.MarkupLine($"[dim]Current content length: {existingDocument.Content.Length} characters[/]")
 93                    _console.MarkupLine($"[dim]New content length: {kpiDocument.Content.Length} characters[/]");
 94                }
 95            }
 96            else
 97            {
 98                _console.MarkupLine($"[blue]No existing KPI document found for '{kpiDocument.DocumentName}' - will creat
 99            }
 100
 101            // Upload the document (versioning is handled automatically by the repository)
 102            _console.MarkupLine($"[blue]Processing KPI document...[/]");
 103
 104            var savedVersion = await kpiRepository.SaveKpiDocumentAsync(
 105                kpiDocument.DocumentName,
 106                kpiDocument.Content,
 107                kpiDocument.Description,
 108                kpiDocument.CommunityContext);
 109
 110            if (existingDocument != null && savedVersion == existingDocument.Version)
 111            {
 112                _console.MarkupLine($"[green]✓ Content unchanged - KPI document '[/][white]{kpiDocument.DocumentName}[/]
 113            }
 114            else if (existingDocument != null)
 115            {
 116                _console.MarkupLine($"[green]✓ Content changed - Created new version {savedVersion} for KPI document '[/
 117            }
 118            else
 119            {
 120                _console.MarkupLine($"[green]✓ Successfully created KPI document '[/][white]{kpiDocument.DocumentName}[/
 121            }
 122
 123            if (settings.Verbose)
 124            {
 125                _console.MarkupLine($"[dim]Document saved to unified kpi-documents collection with community context: {k
 126                _console.MarkupLine($"[dim]Document version: {savedVersion}[/]");
 127            }
 128
 129            return 0;
 130        }
 131        catch (Exception ex)
 132        {
 133            _logger.LogError(ex, "Error in upload-kpi command");
 134            _console.MarkupLine($"[red]Error: {ex.Message}[/]");
 135            return 1;
 136        }
 137    }
 138
 139    /// <summary>
 140    /// JSON model for deserializing KPI document files.
 141    /// </summary>
 142    private class KpiDocumentJson
 143    {
 1144        public string DocumentName { get; set; } = string.Empty;
 1145        public string Content { get; set; } = string.Empty;
 1146        public string Description { get; set; } = string.Empty;
 1147        public string CommunityContext { get; set; } = string.Empty;
 148    }
 149}

Methods/Properties

.ctor()