From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <t.lamprecht@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id AA83B6009E
 for <pve-devel@lists.proxmox.com>; Fri,  5 Feb 2021 14:05:29 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 992C29461
 for <pve-devel@lists.proxmox.com>; Fri,  5 Feb 2021 14:05:29 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 7CDBE9451
 for <pve-devel@lists.proxmox.com>; Fri,  5 Feb 2021 14:05:25 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 38EB5461E9
 for <pve-devel@lists.proxmox.com>; Fri,  5 Feb 2021 14:05:25 +0100 (CET)
Message-ID: <b77c044e-62c4-9f91-d66f-effaf1ab8190@proxmox.com>
Date: Fri, 5 Feb 2021 14:05:24 +0100
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:86.0) Gecko/20100101
 Thunderbird/86.0
Content-Language: en-US
To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>,
 Stefan Reiter <s.reiter@proxmox.com>
References: <20210204125224.25059-1-s.reiter@proxmox.com>
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
In-Reply-To: <20210204125224.25059-1-s.reiter@proxmox.com>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.028 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 NICE_REPLY_A           -0.182 Looks like a legit reply (A)
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [qemuserver.pm]
Subject: Re: [pve-devel] [PATCH qemu-server 1/4] vzdump: use renderers from
 Tools instead of duplicating code
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 05 Feb 2021 13:05:29 -0000

On 04.02.21 13:52, Stefan Reiter wrote:
> CLIFormatter, though maybe not perfectly named, already has functions
> doing the same task - use those.
> 
> Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
> ---
> 
> Wanted to move these to a shared location to use in the next patch, found that
> we already had functions doing pretty much the same stuff...
> 

NAK, the result needs to be a 1:1 CC of the current rendering which we spent
some time fine-tuning.

render_bytes misses a precision parameter for that and hard codes it to 2.

Adding that and moving those two plus the render_timestamp* helpers to a
separate module in common (e.g., PVE::Format ?), only having POSIX strftime
as single usage would be OK for me.

>  PVE/VZDump/QemuServer.pm | 64 ++++++++--------------------------------
>  1 file changed, 13 insertions(+), 51 deletions(-)
> 
> diff --git a/PVE/VZDump/QemuServer.pm b/PVE/VZDump/QemuServer.pm
> index b5e74d3..a34bdf5 100644
> --- a/PVE/VZDump/QemuServer.pm
> +++ b/PVE/VZDump/QemuServer.pm
> @@ -21,6 +21,7 @@ use PVE::Storage::PBSPlugin;
>  use PVE::Storage;
>  use PVE::Tools;
>  use PVE::VZDump;
> +use PVE::CLIFormatter qw(render_duration render_bytes);
>  
>  use PVE::QemuConfig;
>  use PVE::QemuServer;
> @@ -262,45 +263,6 @@ sub archive {
>      }
>  }
>  
> -# number, [precision=1]
> -my $num2str = sub {
> -    return sprintf( "%." . ( $_[1] || 1 ) . "f", $_[0] );
> -};
> -my sub bytes_to_human {
> -    my ($bytes, $precission) = @_;
> -
> -    return $num2str->($bytes, $precission) . ' B' if $bytes < 1024;
> -    my $kb = $bytes/1024;
> -
> -    return $num2str->($kb, $precission) . " KiB" if $kb < 1024;
> -    my $mb = $kb/1024;
> -
> -    return $num2str->($mb, $precission) . " MiB" if $mb < 1024;
> -    my $gb = $mb/1024;
> -
> -    return $num2str->($gb, $precission) . " GiB" if $gb < 1024;
> -    my $tb = $gb/1024;
> -
> -    return $num2str->($tb, $precission) . " TiB";
> -}
> -my sub duration_to_human {
> -    my ($seconds) = @_;
> -
> -    return sprintf('%2ds', $seconds) if $seconds < 60;
> -    my $minutes = $seconds / 60;
> -    $seconds = $seconds % 60;
> -
> -    return sprintf('%2dm %2ds', $minutes, $seconds) if $minutes < 60;
> -    my $hours = $minutes / 60;
> -    $minutes = $minutes % 60;
> -
> -    return sprintf('%2dh %2dm %2ds', $hours, $minutes, $seconds) if $hours < 24;
> -    my $days = $hours / 24;
> -    $hours = $hours % 24;
> -
> -    return sprintf('%2dd %2dh %2dm', $days, $hours, $minutes);
> -}
> -
>  my $bitmap_action_to_human = sub {
>      my ($self, $info) = @_;
>  
> @@ -316,8 +278,8 @@ my $bitmap_action_to_human = sub {
>  	if ($info->{dirty} == 0) {
>  	    return "OK (drive clean)";
>  	} else {
> -	    my $size = bytes_to_human($info->{size});
> -	    my $dirty = bytes_to_human($info->{dirty});
> +	    my $size = render_bytes($info->{size});
> +	    my $dirty = render_bytes($info->{dirty});
>  	    return "OK ($dirty of $size dirty)";
>  	}
>      } elsif ($action eq "invalid") {
> @@ -339,7 +301,7 @@ my $query_backup_status_loop = sub {
>  	my ($mb, $delta) = @_;
>  	return "0 B/s" if $mb <= 0;
>  	my $bw = int(($mb / $delta));
> -	return bytes_to_human($bw) . "/s";
> +	return render_bytes($bw) . "/s";
>      };
>  
>      my $target = 0;
> @@ -361,8 +323,8 @@ my $query_backup_status_loop = sub {
>  	    $last_reused += $info->{size} - $info->{dirty};
>  	}
>  	if ($target < $total) {
> -	    my $total_h = bytes_to_human($total);
> -	    my $target_h = bytes_to_human($target);
> +	    my $total_h = render_bytes($total);
> +	    my $target_h = render_bytes($target);
>  	    $self->loginfo("using fast incremental mode (dirty-bitmap), $target_h dirty of $total_h total");
>  	}
>      }
> @@ -397,16 +359,16 @@ my $query_backup_status_loop = sub {
>  	my $timediff = ($ctime - $last_time) || 1; # fixme
>  	my $mbps_read = $get_mbps->($rbytes, $timediff);
>  	my $mbps_write = $get_mbps->($wbytes, $timediff);
> -	my $target_h = bytes_to_human($target);
> -	my $transferred_h = bytes_to_human($transferred);
> +	my $target_h = render_bytes($target);
> +	my $transferred_h = render_bytes($transferred);
>  
>  	if (!$has_query_bitmap && $first_round && $target != $total) { # FIXME: remove with PVE 7.0
> -	    my $total_h = bytes_to_human($total);
> +	    my $total_h = render_bytes($total);
>  	    $self->loginfo("using fast incremental mode (dirty-bitmap), $target_h dirty of $total_h total");
>  	}
>  
>  	my $statusline = sprintf("%3d%% ($transferred_h of $target_h) in %s"
> -	    .", read: $mbps_read, write: $mbps_write", $percent, duration_to_human($duration));
> +	    .", read: $mbps_read, write: $mbps_write", $percent, render_duration($duration));
>  
>  	my $res = $status->{status} || 'unknown';
>  	if ($res ne 'active') {
> @@ -446,16 +408,16 @@ my $query_backup_status_loop = sub {
>  
>      if ($last_zero) {
>  	my $zero_per = $last_target ? int(($last_zero * 100)/$last_target) : 0;
> -	my $zero_h = bytes_to_human($last_zero, 2);
> +	my $zero_h = render_bytes($last_zero);
>  	$self->loginfo("backup is sparse: $zero_h (${zero_per}%) total zero data");
>      }
>      if ($reused) {
> -	my $reused_h = bytes_to_human($reused, 2);
> +	my $reused_h = render_bytes($reused);
>  	my $reuse_per = int($reused * 100 / $last_total);
>  	$self->loginfo("backup was done incrementally, reused $reused_h (${reuse_per}%)");
>      }
>      if ($transferred) {
> -	my $transferred_h = bytes_to_human($transferred, 2);
> +	my $transferred_h = render_bytes($transferred);
>  	if ($duration) {
>  	    my $mbps = $get_mbps->($transferred, $duration);
>  	    $self->loginfo("transferred $transferred_h in $duration seconds ($mbps)");
>