From 93b082fe6bc861578fe695e979dc707814fd3b3f Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 18 May 2025 01:51:38 +0300 Subject: also show last kernel --- guprecords.raku | 64 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/guprecords.raku b/guprecords.raku index 65e2650..e6d91f0 100644 --- a/guprecords.raku +++ b/guprecords.raku @@ -65,6 +65,9 @@ class Aggregate { } class HostAggregate is Aggregate { + has Str $.last-kernel; + + method new (Str:D $name, Str:D $last-kernel) { self.bless: :$name, :$last-kernel } method lifespan returns UInt { $.last-seen - $.first-boot } method downtime returns UInt { self.lifespan - $.uptime } method meta-score returns UInt { UInt(self.downtime / 2000000) + callsame } @@ -88,10 +91,14 @@ class Aggregator { die "Record file for $host already processed - duplicate inputs?" if %!aggregates{$host}:exists; - %!aggregates{$host} = HostAggregate.new: $host; + %!aggregates{$host} = HostAggregate.new: $host, self!last-kernel($file); self!add-line: :line($_), :$host for $file.IO.lines; } + method !last-kernel(IO::Path:D $file) { + $file.lines.map({ [.split(':')] }).max({ $_[1] }).[2] + } + method !add-line(Str:D :$line, Str:D :$host) { my ($uptime, $boot-time, $os) = $line.trim.split: ':'; my $uname = $os.split(' ').first; @@ -146,22 +153,40 @@ class Reporter does OutputHelper { method report returns Str { join '', gather { with self!table -> (@table, %size) { - my \format = '|' ~ join '|', " %{%size}s ", - " %{%size}s ", " %{%size}s ", "\n"; - my \border = '+' ~ join '+', '-' x (2+%size), - '-' x (2+%size), '-' x (2+%size), "\n"; + my $format = '|' ~ join '|', + " %{%size}s "," %{%size}s "," %{%size}s "; + my $border = '+' ~ join '+', + '-' x (2+%size), '-' x (2+%size), '-' x (2+%size); + + # Works only for HostReporter reports + my \last-kernel-header = 'Last Kernel'; + if %size > 0 { + %size = last-kernel-header.chars + if %size < last-kernel-header.chars; + $format ~= "| %{%size}s "; + $border ~= '+' ~ '-' x (2+%size); + } + + $format ~= "|\n" and $border ~= "+\n"; + take "{self.output-header}Top {$.limit} {$.metric}'s by {$.category}\n\n", - self.output-trim(%DESCRIPTION{$.metric}, border.chars), "\n\n", - self.output-block, border, - sprintf(format, 'Pos', $.category, $.metric), - border; - - for @table -> \position, \name, \value { - take sprintf format, position, name, value; + self.output-trim(%DESCRIPTION{$.metric}, $border.chars), "\n\n", + self.output-block, $border, + (%size > 0 + ?? sprintf($format, 'Pos', $.category, $.metric, last-kernel-header) + !! sprintf($format, 'Pos', $.category, $.metric)), + $border; + + for @table -> \position, \name, \value, \last-kernel { + if last-kernel.chars > 0 { + take sprintf $format, position, name, value, last-kernel; + } else { + take sprintf $format, position, name, value; + } } - take border, self.output-block; + take $border, self.output-block; } } } @@ -173,18 +198,25 @@ class Reporter does OutputHelper { # Initial table size my %size = :count('Pos'.chars), :name($.category.chars), - :value($.metric.chars); + :value($.metric.chars), :last-kernel(0); for self.sort-by($.metric) -> Aggregate \what { my \active = what.is-active ?? '*' !! ' '; my \name = active ~ what.name; my \value = self.human-str($.metric, what).Str; + my $last-kernel = ''; + + if what ~~ HostAggregate { + my HostAggregate $ha = what; + $last-kernel = $ha.last-kernel; + } # Adjust size %size{.key} = .value if %size{.key} < .value - for :count($count.Str.chars+1), :name(name.chars), :value(value.chars); + for :count($count.Str.chars+1), :name(name.chars), + :value(value.chars), :last-kernel($last-kernel.chars); - @table.push: "{$count+1}.", name, value; + @table.push: "{$count+1}.", name, value, $last-kernel; last if ++$count == $.limit; } -- cgit v1.2.3