From: Andrew Ruthven Date: Thu, 6 Dec 2018 21:52:34 +0000 (+1300) Subject: When making a human readable disk size, keep the original. X-Git-Tag: 1.0.0~49 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b6bac7704c85eaac11fbe410d328b26bd4d8fdb9;p=mythtv-status.git When making a human readable disk size, keep the original. This fixes a bug where we wouldn't show the disk space warning if the total and used spaces were in different units. It also stops us having to convert from the SI format to a common unit (MB) to calculate the used percentage. --- diff --git a/bin/mythtv-status b/bin/mythtv-status index 5c82378..49ba0f4 100755 --- a/bin/mythtv-status +++ b/bin/mythtv-status @@ -398,14 +398,14 @@ my @blocks = ( 'xpath' => '//Status/MachineInfo/Storage', 'protocol_version' => [ "<= 31" ], 'attrs' => [ qw/_total_total _total_used/ ], - 'commify' => [ qw/_total_total _total_used/ ], + 'commify' => [ qw/si__total_total si__total_used/ ], 'human_readable_sizes' => [ qw/_total_total _total_used/ ], - 'template' => "Total space is ___total_total__ ___total_total_unit__, with ___total_used__ ___total_used_unit__ used (__percent__)", + 'template' => "Total space is __si__total_total__ __si__total_total_unit__, with __si__total_used__ __si__total_used_unit__ used (__percent__)", 'format' => 'one line', 'optional' => 1, 'subs' => { 'percent' => sub { - calc_disk_space_percentage("$_[0]->{'_total_used'} $_[0]->{'_total_used_unit'}", "$_[0]->{'_total_total'} $_[0]->{'_total_total_unit'}") + calc_disk_space_percentage($_[0]->{'_total_used'}, $_[0]->{'_total_total'}) }, }, }, @@ -418,14 +418,14 @@ my @blocks = ( 'protocol_version' => [ ">= 32" ], 'xml_version' => [ "== 0" ], 'attrs' => [ qw/drive_total_total drive_total_used/ ], - 'commify' => [ qw/drive_total_total drive_total_used/ ], + 'commify' => [ qw/si_drive_total_total si_drive_total_used/ ], 'human_readable_sizes' => [ qw/drive_total_total drive_total_used/ ], - 'template' => "Total space is __drive_total_total__ __drive_total_total_unit__, with __drive_total_used__ __drive_total_used_unit__ used (__percent__)", + 'template' => "Total space is __si_drive_total_total__ __si_drive_total_total_unit__, with __si_drive_total_used__ __si_drive_total_used_unit__ used (__percent__)", 'format' => 'one line', 'optional' => 1, 'subs' => { 'percent' => sub { - calc_disk_space_percentage("$_[0]->{'drive_total_used'} $_[0]->{'drive_total_used_unit'}", "$_[0]->{'drive_total_total'} $_[0]->{'drive_total_total_unit'}") + calc_disk_space_percentage($_[0]->{'drive_total_used'}, $_[0]->{'drive_total_total'}) }, }, }, @@ -437,14 +437,14 @@ my @blocks = ( 'xpath' => '//Status/MachineInfo/Storage/Group[@id="total"]', 'protocol_version' => [ ">= 39" ], 'attrs' => [ qw/total used/ ], - 'commify' => [ qw/total used/ ], + 'commify' => [ qw/si_total used/ ], 'human_readable_sizes' => [ qw/total used/ ], - 'template' => "Total space is __total__ __total_unit__, with __used__ __used_unit__ used (__percent__)", + 'template' => "Total space is __si_total__ __si_total_unit__, with __si_used__ __si_used_unit__ used (__percent__)", 'format' => 'one line', 'optional' => 1, 'subs' => { 'percent' => sub { - calc_disk_space_percentage("$_[0]->{'used'} $_[0]->{'used_unit'}", "$_[0]->{'total'} $_[0]->{'total_unit'}") + calc_disk_space_percentage($_[0]->{'used'}, $_[0]->{'total'}) }, }, }, @@ -456,9 +456,9 @@ my @blocks = ( 'xpath' => '//Status/MachineInfo/Storage/Group', 'protocol_version' => [ ">= 39" ], 'attrs' => [ qw/id total used/ ], - 'commify' => [ qw/total used/ ], + 'commify' => [ qw/si_total used/ ], 'human_readable_sizes' => [ qw/total used/ ], - 'template' => "Total space for group __id__ is __total__ __total_unit__, with __used__ __used_unit__ used (__percent__)", + 'template' => "Total space for group __id__ is __si_total__ __si_total_unit__, with __si_used__ __si_used_unit__ used (__percent__)", 'filter' => { 'id' => sub { return $_[0] eq 'total' }, 'used' => sub { @@ -469,7 +469,7 @@ my @blocks = ( }, 'subs' => { 'percent' => sub { - calc_disk_space_percentage("$_[0]->{'used'} $_[0]->{'used_unit'}", "$_[0]->{'total'} $_[0]->{'total_unit'}") + calc_disk_space_percentage($_[0]->{'used'}, $_[0]->{'total'}) }, }, }, @@ -1027,14 +1027,15 @@ sub substitute_vars { for my $unit (@size_thresholds) { if (defined $vars->{$key} && defined $unit->{'threshold'}) { if ($vars->{$key} > $unit->{'threshold'}) { - $vars->{$key} = sprintf("%.1f", $vars->{$key} / $unit->{'conversion'}); - $vars->{$key} =~ s/\.0//; - $vars->{"${key}_unit"} = $unit->{'unit'}; + $vars->{"si_$key"} = sprintf("%.1f", $vars->{$key} / $unit->{'conversion'}); + $vars->{"si_$key"} =~ s/\.0//; + $vars->{"si_${key}_unit"} = $unit->{'unit'}; last; } } else { - $vars->{"${key}_unit"} = $unit->{'unit'}; + $vars->{"si_${key}"} = $vars->{$key}; + $vars->{"si_${key}_unit"} = $unit->{'unit'}; } } } @@ -1122,7 +1123,9 @@ sub calc_disk_space_percentage { # Make sure that the disk space is in a common unit. # Currently that is MB. sub normalise_disk_space { - if ($_[0] =~ /^([.0-9]+) (\w+)$/) { + if ($_[0] =~ /^[.0-9]+$/) { + return $_[0]; + } elsif ($_[0] =~ /^([.0-9]+) (\w+)$/) { my $space = $1; my $unit = $2;