< 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
0%
Covered lines: 0
Uncovered lines: 63
Coverable lines: 63
Total lines: 140
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 20
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
ExecuteAsync()0%420200%
get_DocumentName()100%210%
set_DocumentName(...)100%210%
.ctor()100%210%
get_Content()100%210%
set_Content(...)100%210%
get_Description()100%210%
set_Description(...)100%210%
get_CommunityContext()100%210%
set_CommunityContext(...)100%210%

File(s)

/home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/Commands/Utility/UploadKpi/UploadKpiCommand.cs

#LineLine coverage
 1using Microsoft.Extensions.Logging;
 2using Spectre.Console.Cli;
 3using Spectre.Console;
 4using System.Text.Json;
 5using EHonda.KicktippAi.Core;
 6using Orchestrator.Infrastructure.Factories;
 7
 8namespace Orchestrator.Commands.Utility.UploadKpi;
 9
 10public class UploadKpiCommand : AsyncCommand<UploadKpiSettings>
 11{
 12    private readonly IAnsiConsole _console;
 13    private readonly IFirebaseServiceFactory _firebaseServiceFactory;
 14    private readonly ILogger<UploadKpiCommand> _logger;
 15
 016    public UploadKpiCommand(
 017        IAnsiConsole console,
 018        IFirebaseServiceFactory firebaseServiceFactory,
 019        ILogger<UploadKpiCommand> logger)
 20    {
 021        _console = console;
 022        _firebaseServiceFactory = firebaseServiceFactory;
 023        _logger = logger;
 024    }
 25
 26    public override async Task<int> ExecuteAsync(CommandContext context, UploadKpiSettings settings)
 27    {
 28
 29        try
 30        {
 031            _console.MarkupLine($"[green]Upload KPI command initialized for document:[/] [yellow]{settings.DocumentName}
 032            _console.MarkupLine($"[blue]Using community context:[/] [yellow]{settings.CommunityContext}[/]");
 33
 034            if (settings.Verbose)
 35            {
 036                _console.MarkupLine("[dim]Verbose mode enabled[/]");
 37            }
 38
 39            // Check if the JSON file exists in the community-context specific subfolder
 040            var jsonFilePath = Path.Combine("kpi-documents", "output", settings.CommunityContext, $"{settings.DocumentNa
 041            if (!File.Exists(jsonFilePath))
 42            {
 043                _console.MarkupLine($"[red]KPI document file not found:[/] {jsonFilePath}");
 044                _console.MarkupLine($"[dim]Run the PowerShell script with firebase mode to create the document first.[/]
 045                return 1;
 46            }
 47
 048            _console.MarkupLine($"[blue]Reading KPI document from:[/] {jsonFilePath}");
 49
 50            // Read and parse the JSON file
 051            var jsonContent = await File.ReadAllTextAsync(jsonFilePath);
 052            var kpiDocument = JsonSerializer.Deserialize<KpiDocumentJson>(jsonContent, new JsonSerializerOptions
 053            {
 054                PropertyNameCaseInsensitive = true
 055            });
 56
 057            if (kpiDocument == null)
 58            {
 059                _console.MarkupLine("[red]Failed to parse KPI document JSON[/]");
 060                return 1;
 61            }
 62
 063            if (settings.Verbose)
 64            {
 065                _console.MarkupLine($"[dim]Document Name: {kpiDocument.DocumentName}[/]");
 066                _console.MarkupLine($"[dim]Community Context: {kpiDocument.CommunityContext}[/]");
 067                _console.MarkupLine($"[dim]Content length: {kpiDocument.Content.Length} characters[/]");
 68            }
 69
 70            // Create Firebase services using factory (factory handles env var loading)
 071            var kpiRepository = _firebaseServiceFactory.CreateKpiRepository();
 72
 73            // Check if document already exists for this community context
 074            var existingDocument = await kpiRepository.GetKpiDocumentAsync(kpiDocument.DocumentName, kpiDocument.Communi
 75
 076            if (existingDocument != null)
 77            {
 078                _console.MarkupLine($"[blue]Found existing KPI document '{kpiDocument.DocumentName}' (version {existingD
 079                _console.MarkupLine($"[blue]Checking for content changes...[/]");
 80
 081                if (settings.Verbose)
 82                {
 083                    _console.MarkupLine($"[dim]Current content length: {existingDocument.Content.Length} characters[/]")
 084                    _console.MarkupLine($"[dim]New content length: {kpiDocument.Content.Length} characters[/]");
 85                }
 86            }
 87            else
 88            {
 089                _console.MarkupLine($"[blue]No existing KPI document found for '{kpiDocument.DocumentName}' - will creat
 90            }
 91
 92            // Upload the document (versioning is handled automatically by the repository)
 093            _console.MarkupLine($"[blue]Processing KPI document...[/]");
 94
 095            var savedVersion = await kpiRepository.SaveKpiDocumentAsync(
 096                kpiDocument.DocumentName,
 097                kpiDocument.Content,
 098                kpiDocument.Description,
 099                kpiDocument.CommunityContext);
 100
 0101            if (existingDocument != null && savedVersion == existingDocument.Version)
 102            {
 0103                _console.MarkupLine($"[green]✓ Content unchanged - KPI document '[/][white]{kpiDocument.DocumentName}[/]
 104            }
 0105            else if (existingDocument != null)
 106            {
 0107                _console.MarkupLine($"[green]✓ Content changed - Created new version {savedVersion} for KPI document '[/
 108            }
 109            else
 110            {
 0111                _console.MarkupLine($"[green]✓ Successfully created KPI document '[/][white]{kpiDocument.DocumentName}[/
 112            }
 113
 0114            if (settings.Verbose)
 115            {
 0116                _console.MarkupLine($"[dim]Document saved to unified kpi-documents collection with community context: {k
 0117                _console.MarkupLine($"[dim]Document version: {savedVersion}[/]");
 118            }
 119
 0120            return 0;
 121        }
 0122        catch (Exception ex)
 123        {
 0124            _logger.LogError(ex, "Error in upload-kpi command");
 0125            _console.MarkupLine($"[red]Error: {ex.Message}[/]");
 0126            return 1;
 127        }
 0128    }
 129
 130    /// <summary>
 131    /// JSON model for deserializing KPI document files.
 132    /// </summary>
 133    private class KpiDocumentJson
 134    {
 0135        public string DocumentName { get; set; } = string.Empty;
 0136        public string Content { get; set; } = string.Empty;
 0137        public string Description { get; set; } = string.Empty;
 0138        public string CommunityContext { get; set; } = string.Empty;
 139    }
 140}