From be53de8f94d84d96998042815c853c6a67f5b080 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 3 Feb 2023 22:19:15 +0200 Subject: initial to human conversion --- src/guprecords.raku | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/guprecords.raku b/src/guprecords.raku index 21e13db..52817f7 100644 --- a/src/guprecords.raku +++ b/src/guprecords.raku @@ -2,8 +2,14 @@ use v6.d; +constant SECOND = 1; +constant MINUTE = 60 * SECOND; +constant HOUR = 60 * MINUTE; +constant DAY = 24 * HOUR; +constant MONTH = 24 * HOUR; + class Aggregate { - has Int $.total-uptime = 0; + has Int $.total-uptime; has Int $.first-boot; has Int $.last-seen; has Int $.elems; @@ -20,6 +26,27 @@ class Aggregate { method total-downtime { $.last-seen - $.first-boot - $.total-uptime } method total-time { self.total-downtime + $.total-uptime } + + method Str returns Str { + #duration($!total-uptime) + my Str $active = self.is-active ?? '* ' !! ' '; + "$active {duration(self.total-downtime)} {date($!last-seen)}"; + } + + method is-active returns Bool { + # Last seen 3 months ago? + my Int \seconds = (DateTime.now - DateTime.new(Instant.from-posix: $!last-seen)).Int; + seconds < 3600 * 24 * 90; + } + + sub duration(Int \seconds) returns Str { + my DateTime \dt .= new(Instant.from-posix: seconds); + "{dt.year-1970} years, {dt.month} months, {dt.day} days"; + } + + sub date(Int \epoch) returns Str { + DateTime.new(Instant.from-posix: epoch).yyyy-mm-dd + } } class Aggregator { @@ -47,6 +74,7 @@ class Aggregator { sub MAIN( Str $in-dir = './stats', + Str $sort-by = 'uptime'; ) { my Aggregator $aggregator .= new; @@ -58,7 +86,7 @@ sub MAIN( say "Categoty $category"; for $aggregates.kv -> $name, $agg { my Str $plural = $agg.elems > 1 ?? 's' !! ''; - say "\t$name (" ~ $agg.elems ~ " record$plural)"; + say "\t$name $agg (" ~ $agg.elems ~ " record$plural)"; } } } -- cgit v1.2.3