< 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
0%
Covered lines: 0
Uncovered lines: 4
Coverable lines: 4
Total lines: 140
Line coverage: 0%
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
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
 16    public UploadKpiCommand(
 17        IAnsiConsole console,
 18        IFirebaseServiceFactory firebaseServiceFactory,
 19        ILogger<UploadKpiCommand> logger)
 20    {
 21        _console = console;
 22        _firebaseServiceFactory = firebaseServiceFactory;
 23        _logger = logger;
 24    }
 25
 26    public override async Task<int> ExecuteAsync(CommandContext context, UploadKpiSettings settings)
 27    {
 28
 29        try
 30        {
 31            _console.MarkupLine($"[green]Upload KPI command initialized for document:[/] [yellow]{settings.DocumentName}
 32            _console.MarkupLine($"[blue]Using community context:[/] [yellow]{settings.CommunityContext}[/]");
 33
 34            if (settings.Verbose)
 35            {
 36                _console.MarkupLine("[dim]Verbose mode enabled[/]");
 37            }
 38
 39            // Check if the JSON file exists in the community-context specific subfolder
 40            var jsonFilePath = Path.Combine("kpi-documents", "output", settings.CommunityContext, $"{settings.DocumentNa
 41            if (!File.Exists(jsonFilePath))
 42            {
 43                _console.MarkupLine($"[red]KPI document file not found:[/] {jsonFilePath}");
 44                _console.MarkupLine($"[dim]Run the PowerShell script with firebase mode to create the document first.[/]
 45                return 1;
 46            }
 47
 48            _console.MarkupLine($"[blue]Reading KPI document from:[/] {jsonFilePath}");
 49
 50            // Read and parse the JSON file
 51            var jsonContent = await File.ReadAllTextAsync(jsonFilePath);
 52            var kpiDocument = JsonSerializer.Deserialize<KpiDocumentJson>(jsonContent, new JsonSerializerOptions
 53            {
 54                PropertyNameCaseInsensitive = true
 55            });
 56
 57            if (kpiDocument == null)
 58            {
 59                _console.MarkupLine("[red]Failed to parse KPI document JSON[/]");
 60                return 1;
 61            }
 62
 63            if (settings.Verbose)
 64            {
 65                _console.MarkupLine($"[dim]Document Name: {kpiDocument.DocumentName}[/]");
 66                _console.MarkupLine($"[dim]Community Context: {kpiDocument.CommunityContext}[/]");
 67                _console.MarkupLine($"[dim]Content length: {kpiDocument.Content.Length} characters[/]");
 68            }
 69
 70            // Create Firebase services using factory (factory handles env var loading)
 71            var kpiRepository = _firebaseServiceFactory.CreateKpiRepository();
 72
 73            // Check if document already exists for this community context
 74            var existingDocument = await kpiRepository.GetKpiDocumentAsync(kpiDocument.DocumentName, kpiDocument.Communi
 75
 76            if (existingDocument != null)
 77            {
 78                _console.MarkupLine($"[blue]Found existing KPI document '{kpiDocument.DocumentName}' (version {existingD
 79                _console.MarkupLine($"[blue]Checking for content changes...[/]");
 80
 81                if (settings.Verbose)
 82                {
 83                    _console.MarkupLine($"[dim]Current content length: {existingDocument.Content.Length} characters[/]")
 84                    _console.MarkupLine($"[dim]New content length: {kpiDocument.Content.Length} characters[/]");
 85                }
 86            }
 87            else
 88            {
 89                _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)
 93            _console.MarkupLine($"[blue]Processing KPI document...[/]");
 94
 95            var savedVersion = await kpiRepository.SaveKpiDocumentAsync(
 96                kpiDocument.DocumentName,
 97                kpiDocument.Content,
 98                kpiDocument.Description,
 99                kpiDocument.CommunityContext);
 100
 101            if (existingDocument != null && savedVersion == existingDocument.Version)
 102            {
 103                _console.MarkupLine($"[green]✓ Content unchanged - KPI document '[/][white]{kpiDocument.DocumentName}[/]
 104            }
 105            else if (existingDocument != null)
 106            {
 107                _console.MarkupLine($"[green]✓ Content changed - Created new version {savedVersion} for KPI document '[/
 108            }
 109            else
 110            {
 111                _console.MarkupLine($"[green]✓ Successfully created KPI document '[/][white]{kpiDocument.DocumentName}[/
 112            }
 113
 114            if (settings.Verbose)
 115            {
 116                _console.MarkupLine($"[dim]Document saved to unified kpi-documents collection with community context: {k
 117                _console.MarkupLine($"[dim]Document version: {savedVersion}[/]");
 118            }
 119
 120            return 0;
 121        }
 122        catch (Exception ex)
 123        {
 124            _logger.LogError(ex, "Error in upload-kpi command");
 125            _console.MarkupLine($"[red]Error: {ex.Message}[/]");
 126            return 1;
 127        }
 128    }
 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}