[PATCH 3/7] Reimplement statistics

Michael Hanselmann public at hansmi.ch
Wed Aug 13 21:15:58 UTC 2008


---
 lib/Brackup/Backup.pm |   61 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/lib/Brackup/Backup.pm b/lib/Brackup/Backup.pm
index dfc4818..b1ab7bc 100644
--- a/lib/Brackup/Backup.pm
+++ b/lib/Brackup/Backup.pm
@@ -59,6 +59,15 @@ sub new {
     $self->{stored_files} = ();
     $self->{stored_chunks} = ();
 
+    # Number of files done
+    $self->{n_files_done} = 0;
+
+    # Size of files already done in KB (uploaded or skipped)
+    $self->{n_kb_done} = 0.0;
+
+    # Amount of uploaded data
+    $self->{n_kb_up} = 0.0;
+
     return $self;
 }
 
@@ -193,6 +202,8 @@ sub _emit_chunk {
             $self->{parent}->{target}->add_to_inventory($pchunk => $schunk);
         }
 
+        $self->{n_kb_up} += $pchunk->length / 1024;
+
         push @{$self->{stored_chunks}}, $schunk;
     }
 
@@ -219,7 +230,10 @@ sub end_file {
 
     push @{$self->{stored_files}}, [$file, $self->{stored_chunks}];
 
+    $self->{n_files_done}++;
+    $self->{n_kb_done} += $file->size / 1024;
     $self->{modecounts}{$file->type}{$file->mode}++;
+
     $self->{cur_file} = undef;
 }
 
@@ -340,6 +354,12 @@ sub new {
 
     $self->{files} = [];
 
+    # Size of all files
+    $self->{n_kb} = 0.0;
+
+    # Size of all files to be uploaded
+    $self->{n_kb_up_need} = 0.0;
+
     return $self;
 }
 
@@ -351,10 +371,30 @@ sub _begin {
     $self->debug("Discovering files in ", $root->path, " ...\n");
     $root->foreach_file(sub {
         my ($file) = @_;  # a Brackup::File
+        $self->{n_kb} += $file->size / 1024;
         push @{$self->{files}}, $file;
     });
     $self->debug("Number of files: ", scalar(@{$self->{files}}), "\n");
 
+    # calc needed chunks
+    if ($ENV{CALC_NEEDED}) {
+        my $n_files = scalar @{$self->{files}};
+        my $fn = 0;
+        foreach my $f (@{$self->{files}}) {
+            $fn++;
+            if ($fn % 100 == 0) { warn "$fn / $n_files ...\n"; }
+            foreach my $pc ($f->chunks) {
+                if ($self->{parent}->{target}->stored_chunk_from_inventory($pc)) {
+                    $pc->forget_chunkref;
+                    next;
+                }
+                $self->{n_kb_up_need} += $pc->length / 1024;
+                $pc->forget_chunkref;
+            }
+        }
+        warn 'kb need to upload = ', $self->{n_kb_up_need}, "\n";
+    }
+
     $self->SUPER::_begin();
 }
 
@@ -366,4 +406,25 @@ sub _gather_chunks {
     }
 }
 
+sub show_status {
+    my ($self) = @_;
+
+    # use either size of files in normal case, or if we pre-calculated
+    # the size-to-upload (by looking in inventory, then we'll show the
+    # more accurate percentage)
+    my $percdone = 100 * ($self->{n_kb_up_need} ?
+                          ($self->{n_kb_up} / $self->{n_kb_up_need}) :
+                          ($self->{n_kb_done} / $self->{n_kb}));
+    my $mb_remain = ($self->{n_kb_up_need} ?
+                     ($self->{n_kb_up_need} - $self->{n_kb_up}) :
+                     ($self->{n_kb} - $self->{n_kb_done})) / 1024;
+
+    $self->debug(sprintf("* %-60s %d/%d (%0.02f%%; remain: %0.01f MB)",
+                         $self->{cur_file}->path, $self->{n_files_done},
+                         scalar @{$self->{files}}, $percdone,
+                         $mb_remain));
+
+    $self->report_progress($percdone);
+};
+
 1;
-- 
1.5.5.3



More information about the brackup mailing list