Skip to content

Commit

Permalink
refactor: use Spectre.Console in summary table
Browse files Browse the repository at this point in the history
  • Loading branch information
melotic committed Sep 15, 2023
1 parent 570ac31 commit b4fc923
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Microsoft.ComponentDetection.Orchestrator.Services;
using Microsoft.ComponentDetection.Orchestrator.Experiments;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Spectre.Console;
using static System.Environment;

public class DetectorProcessingService : IDetectorProcessingService
Expand Down Expand Up @@ -131,7 +132,7 @@ public async Task<DetectorProcessingResult> ProcessDetectorsAsync(
var detectorProcessingResult = this.ConvertDetectorResultsIntoResult(results, exitCode);

var totalElapsedTime = stopwatch.Elapsed.TotalSeconds;
this.LogTabularOutput(this.logger, providerElapsedTime, totalElapsedTime);
this.LogTabularOutput(providerElapsedTime, totalElapsedTime);

// If there are components which are skipped due to connection or parsing
// errors, log them by detector.
Expand Down Expand Up @@ -298,8 +299,35 @@ private bool IsOSLinuxOrMac()
return OSVersion.Platform == PlatformID.MacOSX || OSVersion.Platform == PlatformID.Unix;
}

private void LogTabularOutput(ILogger logger, ConcurrentDictionary<string, DetectorRunResult> providerElapsedTime, double totalElapsedTime)
private void LogTabularOutput(ConcurrentDictionary<string, DetectorRunResult> providerElapsedTime, double totalElapsedTime)
{
var table = new Table();
table.Title("Detection Summary")
.AddColumn("[bold]Component Detector Id[/]", x => x.Width(30))
.AddColumn("[bold]Detection Time[/]", x => x.Width(30))
.AddColumn("[bold]# Components Found[/]", x => x.Width(30))
.AddColumn("[bold]# Explicitly Referenced[/]", x => x.Width(30));

static string MarkupRelevantDetectors(int count, string tag, string value) => count == 0 ? value : $"[{tag}]{value}[/]";

foreach (var (detectorId, detectorRunResult) in providerElapsedTime.OrderBy(x => x.Key))
{
table.AddRow(
MarkupRelevantDetectors(detectorRunResult.ComponentsFoundCount, "cyan", detectorId),
$"{detectorRunResult.ExecutionTime.TotalSeconds:g2} seconds",
detectorRunResult.ComponentsFoundCount.ToString(),
detectorRunResult.ExplicitlyReferencedComponentCount.ToString());
}

table.AddRow(Enumerable.Range(0, 4).Select(x => new Rule()));
table.AddRow(
"[bold underline]Total[/]",
$"{totalElapsedTime:g2} seconds",
providerElapsedTime.Sum(x => x.Value.ComponentsFoundCount).ToString(),
providerElapsedTime.Sum(x => x.Value.ExplicitlyReferencedComponentCount).ToString());

AnsiConsole.Write(table);

var tsf = new TabularStringFormat(new Column[]
{
new Column { Header = "Component Detector Id", Width = 30 },
Expand Down Expand Up @@ -328,10 +356,9 @@ private void LogTabularOutput(ILogger logger, ConcurrentDictionary<string, Detec
providerElapsedTime.Sum(x => x.Value.ExplicitlyReferencedComponentCount),
});

foreach (var line in tsf.GenerateString(rows)
.Split(new string[] { NewLine }, StringSplitOptions.None))
foreach (var line in tsf.GenerateString(rows).Split(new[] { NewLine }, StringSplitOptions.None))
{
this.logger.LogInformation("{Line}", line);
this.logger.LogInformation("{DetectionTimeLine}", line);
}
}
}
7 changes: 6 additions & 1 deletion src/Microsoft.ComponentDetection/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Filters;
using Spectre.Console.Cli;

if (args.Contains("--debug", StringComparer.OrdinalIgnoreCase))
Expand All @@ -32,7 +33,11 @@
1) // sinkMapCountLimit
.WriteTo.Map<bool>(
LoggingEnricher.PrintStderrPropertyName,
(printLogsToStderr, wt) => wt.Console(standardErrorFromLevel: printLogsToStderr ? LogEventLevel.Debug : null),
(printLogsToStderr, wt) => wt.Logger(lc => lc
.WriteTo.Console(standardErrorFromLevel: printLogsToStderr ? LogEventLevel.Debug : null)

// Don't write the detection times table from DetectorProcessingService to the console, only the log file
.Filter.ByExcluding(Matching.WithProperty<string>("DetectionTimeLine", x => !string.IsNullOrEmpty(x)))),
1) // sinkMapCountLimit
.CreateLogger()));

Expand Down

0 comments on commit b4fc923

Please sign in to comment.