Code

addeded documentation for json
[rrdtool-all.git] / contrib / rrdjig / rrdjig.pl
index f281706ab4a3c5a9544b0b57a1bc369754b0626c..8606e76841efce2698bf881ad1a5d3d8eb2d5780 100755 (executable)
@@ -160,15 +160,18 @@ sub fetch_data($$$){
         my $step = $start - $first;
         unshift @{$data{AVERAGE}}, [ $start, $step, [ map {undef} @{$data{AVERAGE}[0][2]} ] ];
     }
-    return \%data;
+    return (\%data);
 }
 
-sub reupdate($$$){
+sub reupdate($$$$){
     my $step = shift;
+    my $min_time = shift;
     my $dst = shift;
     my $data = shift;
-    my @hidden_rows;
-    my @fake_rows;
+    my @min;
+    my @max;
+    my @pending = map { 0 } @{$data->{AVERAGE}[0][2]};
+    my $hide_cnt = 0;
     my @up;
     while (my $av = shift @{$data->{AVERAGE}}){
         my $end = $av->[0];
@@ -183,7 +186,7 @@ sub reupdate($$$){
         for (my $t = $start+$step;$t<=$end;$t+=$step){
             my @out = @{$av->[2]};
             # lets see if we a usable a MIN or MAX entry pending
-            if (@hidden_rows < 2 and $av->[1] > $step) {
+            if ($hide_cnt <= 2 and $av->[1] > $step) {
                 for my $cf (qw(MIN MAX)){
                     my $m = $data->{$cf}[0];
                     # drop any MIN/MAX entries which we could not use
@@ -195,47 +198,74 @@ sub reupdate($$$){
                     my $cend = $m->[0];
                     my $cstep = $m->[1];
                     my $crow = $m->[2];
-                    if ($cend >= $t and $cend- $cstep <= $t - $step){
+                    if ($cend >= $t and $cend - $cstep <= $t - $step){
                         my $row = "$t:".join(':',map {defined $_ ? $_ : 'U'} @{$crow});
-                        print STDERR ($cf eq 'MIN' ? 'm' : 'M' ) ,$row,"\n" if $opt{verbose};
-                        push @up, $row;
-                        push @hidden_rows, $av->[2];
-                        push @fake_rows, $crow;
+                        if ($cf eq 'MIN'){
+                            @min = @{$crow};
+                        } else {
+                            @max = @{$crow};
+                        }
+                        if ($t > $min_time){
+                            print STDERR ($cf eq 'MIN' ? 'm' : 'M' ) ,$row,"\n" if $opt{verbose};
+                            push @up, $row;
+                        }
+                        $hide_cnt++;
+                        for (my $i = 0; $i <@$crow; $i++){
+                            if (defined $pending[$i]){
+                                if (defined $crow->[$i] and defined $out[$i]){
+                                    my $keep = ($out[$i] - $crow->[$i]);
+#                                   print STDERR " - keep $keep\n" if $opt{verbose};
+                                    $pending[$i] += $keep;
+                                }
+                                else {
+                                    $pending[$i] = undef;
+                                }                                
+                            }
+                        }
                         shift @{$data->{$cf}};
                         next STEP;
                     }
                 }
             }
-            # compensate for the AVERAGE data NOT shown
-            while (my $row = shift @hidden_rows){
-                for (my $i = 0; $i <@$row; $i++){
-                    if (not defined  $row->[$i] or not defined $out[$i]){
-                       $out[$i] = undef;
-                    } else {
-                       $out[$i] += $row->[$i];
+
+            # compensate for data not shown while insering fake MIN/MAX entries
+            for (my $i = 0; $i < @out; $i++){
+                if (defined $out[$i] and defined $pending[$i] and $pending[$i] != 0){
+                    my $new = $out[$i] + $pending[$i];
+                    if (defined $max[$i] and $new > $max[$i]) {
+                        $pending[$i] = $new - $max[$i];
+                        $out[$i] = $max[$i];
+#                       print STDERR " - maxout $i $out[$i]\n" if $opt{verbose};
                     }
-                }
-            }
-            # compensate for the MIN/MAX data shown INSTEAD
-            while (my $row = shift @fake_rows){
-                for (my $i = 0; $i <@$row; $i++){
-                    if (not defined  $row->[$i] or not defined $out[$i]){
-                       $out[$i] = undef;
-                    } else {
-                       $out[$i] -= $row->[$i];
+                    elsif (defined $min[$i] and $new < $min[$i]){
+                        $pending[$i] = $new - $min[$i];
+                        $out[$i] = $min[$i];
+#                       print STDERR " - minout $i $out[$i]\n" if $opt{verbose};
+                    }
+                    else {
+                        $pending[$i] = 0;
+                        $out[$i] = $new;
+#                       print STDERR " - combined $i $out[$i]\n" if $opt{verbose};
                     }
                 }
+                else {
+                    $pending[$i] = 0;
+                }
             }
+            $hide_cnt = 0;
             # show the result;            
             my $row = "$t:".join(':',map {defined $_ ? $_ : 'U'} @out);
-            print STDERR " ",$row,"\n" if $opt{verbose};
-            push @up, $row;
+            if ($t > $min_time){
+                print STDERR " ",$row,"\n" if $opt{verbose};            
+                push @up, $row;
+            }
         }
     }
     pop @up; # the last update is most likely one too many ...
     if (@up == 0) {
         warn "WARNING: src has no entries new enough to fill dst\n";
     } else {
+        print "$min_time $up[0]\n";       
         RRDs::update($dst,
                      $opt{'dst-tmpl'} ? '--template='.$opt{'dst-tmpl'} : (),
                      @up);
@@ -289,7 +319,7 @@ sub rrdjig($$$$){
     my ($first,$fetch_tasks) = prep_fetch_tasks($src_info,$dst_info);
     my $updates = fetch_data($src,$first,$fetch_tasks);
     set_gauge($dst,$dst_info);
-    reupdate($src_info->{step},$dst,$updates);
+    reupdate($src_info->{step},$dst_info->{last_update},$dst,$updates);
     unset_gauge($dst,$dst_info);
 }