From 48abe9533c0e7910105d94caad77bce99ee7b283 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 19 Mar 2023 18:11:11 +0200 Subject: prepare output-format --- guprecords.raku | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/guprecords.raku b/guprecords.raku index c0cb22e..281630e 100644 --- a/guprecords.raku +++ b/guprecords.raku @@ -4,6 +4,7 @@ use v6.d; enum Category ; enum Metric ; +enum OutputFormat ; subset MetricSubset of Metric where * ne any (Downtime, Lifespan); subset Natural of Int where * >= 0; @@ -93,6 +94,7 @@ class Aggregator { } class Reporter { + has OutputFormat $.output-format is required; has Category $.category = Host; has Metric $.metric is required; has Natural $.limit is required; @@ -100,17 +102,21 @@ class Reporter { method report { say "Top {$.limit} {$.metric}'s by {$.category}:\n"; + with self!table -> (@table, %size) { my Str \format = '|' ~ join '|', " %{%size}s ", " %{%size}s ", " %{%size}s ", "\n"; my Str \border = '+' ~ join '+', '-' x (2+%size), '-' x (2+%size), '-' x (2+%size), "\n"; + print border; printf format, 'Pos', $.category, $.metric; print border; + for @table -> \position, \name, \value { printf format, position, name, value; } + print border; } } @@ -176,11 +182,14 @@ multi sub MAIN( Category :$category = Host, #= The category, one of Host, OS, OSMajor, Uname [default: 'Host'] Metric :$metric = Uptime, #= The metric, one of Boots, Uptime, MetaScore, Downtime, Lifespan Natural :$limit = 20, #= Limit output to num of entries. + Bool :$md, #= Output Markdown format. ) { + my OutputFormat $output-format = $md ?? MarkdownSingle !! PlaintextSingle; + if $category ~~ Host { - do-it($stats-dir, HostReporter.new(:$metric, :$limit)); + do-it($stats-dir, HostReporter.new(:$metric, :$limit, :$output-format)); } elsif $metric ~~ MetricSubset { - do-it($stats-dir, Reporter.new(:$category, :$metric, :$limit)); + do-it($stats-dir, Reporter.new(:$category, :$metric, :$limit, :$output-format)); } else { die "Category $category only supports the following metrics: {Metric.^enum_value_list.grep: * ~~ MetricSubset}"; } @@ -190,12 +199,15 @@ multi sub MAIN( Str :$stats-dir is required, Bool :$all, #= Generate all possible stats Natural :$limit = 20, + Bool :$md, ) { + my OutputFormat $output-format = $md ?? MarkdownMulti !! PlaintextMulti; + for Category.^enum_value_list X Metric.^enum_value_list -> (Category $category, Metric $metric) { next if $category !~~ Host and $metric !~~ MetricSubset; do-it($stats-dir, $category ~~ Host - ?? HostReporter.new(:$metric, :$limit) - !! Reporter.new(:$category, :$metric, :$limit)); + ?? HostReporter.new(:$metric, :$limit, :$output-format) + !! Reporter.new(:$category, :$metric, :$limit, :$output-format)); say ''; } } -- cgit v1.2.3