< Summary

Information
Class: Orchestrator.Commands.Utility.UploadKpi.UploadKpiCommand
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Commands/Utility/UploadKpi/UploadKpiCommand.cs
Line coverage
100%
Covered lines: 66
Uncovered lines: 0
Coverable lines: 66
Total lines: 146
Line coverage: 100%
Branch coverage
100%
Covered branches: 20
Total branches: 20
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%
ExecuteAsync()100%2020100%
get_DocumentName()100%11100%
set_DocumentName(...)100%11100%
.ctor()100%11100%
get_Content()100%11100%
set_Content(...)100%11100%
get_Description()100%11100%
set_Description(...)100%11100%
get_CommunityContext()100%11100%
set_CommunityContext(...)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
 120    public UploadKpiCommand(
 121        IAnsiConsole console,
 122        IFirebaseServiceFactory firebaseServiceFactory,
 123        [FromKeyedServices(ServiceRegistrationExtensions.KpiDocumentsFileProviderKey)] IFileProvider fileProvider,
 124        ILogger<UploadKpiCommand> logger)
 25    {
 126        _console = console;
 127        _firebaseServiceFactory = firebaseServiceFactory;
 128        _fileProvider = fileProvider;
 129        _logger = logger;
 130    }
 31
 32    public override async Task<int> ExecuteAsync(CommandContext context, UploadKpiSettings settings)
 33    {
 34        try
 35        {
 136            _console.MarkupLine($"[green]Upload KPI command initialized for document:[/] [yellow]{settings.DocumentName}
 137            _console.MarkupLine($"[blue]Using community context:[/] [yellow]{settings.CommunityContext}[/]");
 38
 139            if (settings.Verbose)
 40            {
 141                _console.MarkupLine("[dim]Verbose mode enabled[/]");
 42            }
 43
 44            // Check if the JSON file exists in the community-context specific subfolder
 145            var jsonFilePath = $"output/{settings.CommunityContext}/{settings.DocumentName}.json";
 146            var fileInfo = _fileProvider.GetFileInfo(jsonFilePath);
 147            if (!fileInfo.Exists)
 48            {
 149                _console.MarkupLine($"[red]KPI document file not found:[/] {jsonFilePath}");
 150                _console.MarkupLine($"[dim]Run the PowerShell script with firebase mode to create the document first.[/]
 151                return 1;
 52            }
 53
 154            _console.MarkupLine($"[blue]Reading KPI document from:[/] {jsonFilePath}");
 55
 56            // Read and parse the JSON file
 157            using var stream = fileInfo.CreateReadStream();
 158            var kpiDocument = await JsonSerializer.DeserializeAsync<KpiDocumentJson>(stream, new JsonSerializerOptions
 159            {
 160                PropertyNameCaseInsensitive = true
 161            });
 62
 163            if (kpiDocument == null)
 64            {
 165                _console.MarkupLine("[red]Failed to parse KPI document JSON[/]");
 166                return 1;
 67            }
 68
 169            if (settings.Verbose)
 70            {
 171                _console.MarkupLine($"[dim]Document Name: {kpiDocument.DocumentName}[/]");
 172                _console.MarkupLine($"[dim]Community Context: {kpiDocument.CommunityContext}[/]");
 173                _console.MarkupLine($"[dim]Content length: {kpiDocument.Content.Length} characters[/]");
 74            }
 75
 76            // Create Firebase services using factory (factory handles env var loading)
 177            var kpiRepository = _firebaseServiceFactory.CreateKpiRepository();
 78
 79            // Check if document already exists for this community context
 180            var existingDocument = await kpiRepository.GetKpiDocumentAsync(kpiDocument.DocumentName, kpiDocument.Communi
 81
 182            if (existingDocument != null)
 83            {
 184                _console.MarkupLine($"[blue]Found existing KPI document '{kpiDocument.DocumentName}' (version {existingD
 185                _console.MarkupLine($"[blue]Checking for content changes...[/]");
 86
 187                if (settings.Verbose)
 88                {
 189                    _console.MarkupLine($"[dim]Current content length: {existingDocument.Content.Length} characters[/]")
 190                    _console.MarkupLine($"[dim]New content length: {kpiDocument.Content.Length} characters[/]");
 91                }
 92            }
 93            else
 94            {
 195                _console.MarkupLine($"[blue]No existing KPI document found for '{kpiDocument.DocumentName}' - will creat
 96            }
 97
 98            // Upload the document (versioning is handled automatically by the repository)
 199            _console.MarkupLine($"[blue]Processing KPI document...[/]");
 100
 1101            var savedVersion = await kpiRepository.SaveKpiDocumentAsync(
 1102                kpiDocument.DocumentName,
 1103                kpiDocument.Content,
 1104                kpiDocument.Description,
 1105                kpiDocument.CommunityContext);
 106
 1107            if (existingDocument != null && savedVersion == existingDocument.Version)
 108            {
 1109                _console.MarkupLine($"[green]✓ Content unchanged - KPI document '[/][white]{kpiDocument.DocumentName}[/]
 110            }
 1111            else if (existingDocument != null)
 112            {
 1113                _console.MarkupLine($"[green]✓ Content changed - Created new version {savedVersion} for KPI document '[/
 114            }
 115            else
 116            {
 1117                _console.MarkupLine($"[green]✓ Successfully created KPI document '[/][white]{kpiDocument.DocumentName}[/
 118            }
 119
 1120            if (settings.Verbose)
 121            {
 1122                _console.MarkupLine($"[dim]Document saved to unified kpi-documents collection with community context: {k
 1123                _console.MarkupLine($"[dim]Document version: {savedVersion}[/]");
 124            }
 125
 1126            return 0;
 127        }
 1128        catch (Exception ex)
 129        {
 1130            _logger.LogError(ex, "Error in upload-kpi command");
 1131            _console.MarkupLine($"[red]Error: {ex.Message}[/]");
 1132            return 1;
 133        }
 1134    }
 135
 136    /// <summary>
 137    /// JSON model for deserializing KPI document files.
 138    /// </summary>
 139    private class KpiDocumentJson
 140    {
 1141        public string DocumentName { get; set; } = string.Empty;
 1142        public string Content { get; set; } = string.Empty;
 1143        public string Description { get; set; } = string.Empty;
 1144        public string CommunityContext { get; set; } = string.Empty;
 145    }
 146}