more detailed calculation of points to be returned
Costa Tsaousis (ktsaou) committed
Dec 2, 2015 at 02:13 UTC
8eba58f29b56f7ac714508cfd26792d796a0ab40
1 file changed
+35
-27
src/rrd2json.c
+35
-27
@@ -851,8 +851,11 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
851
}
852
853
// allow relative for before and after
854
- if(before <= st->update_every * st->entries) before = last_entry_t + before;
855
- if(after <= st->update_every * st->entries) after = last_entry_t + after;
854
+ if(before <= st->update_every * st->entries)
855
+ before = last_entry_t + before;
856
+
857
+ if(after <= st->update_every * st->entries)
858
+ after = last_entry_t + after;
859
860
// make sure they are within our timeframe
861
if(before > last_entry_t) before = last_entry_t;
@@ -872,16 +875,39 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
875
time_t duration = before - after;
876
if(duration <= 0) return NULL;
877
875
- // check the required points
876
- if(points > duration / st->update_every) points = 0;
877
- if(points <= 0) points = duration / st->update_every;
878
+ // how many points does the chart has?
879
+ long available_points = duration / st->update_every;
880
+
881
+ // check the wanted points
882
+ if(points < 0) points = -points;
883
+ if(points > available_points) points = available_points;
884
885
// calculate proper grouping of source data
880
- long group = duration / points;
886
+ long group = available_points / points;
887
if(group <= 0) group = 1;
882
- if(duration / group > points) group++;
888
884
- // error("NEW: points=%d after=%d before=%d group=%d, duration=%d", points, after, before, group, duration);
889
+ // round group to the closest integer
890
+ if(available_points % points > points / 2) group++;
891
+
892
+ // find the starting and ending slots in our round robin db
893
+ long start_at_slot = rrdset_time2slot(st, before),
894
+ stop_at_slot = rrdset_time2slot(st, after);
895
+
896
+ // align them, so that panning on the data
897
+ // will always produce the same results
898
+ start_at_slot -= start_at_slot % group;
899
+ stop_at_slot -= stop_at_slot % group;
900
+
901
+ time_t after_new = rrdset_slot2time(st, stop_at_slot);
902
+ time_t before_new = rrdset_slot2time(st, start_at_slot);
903
+ long points_new = (before_new - after_new) / st->update_every / group;
904
+
905
+ //error("SHIFT: %s: wanted %ld points, got %ld - group=%ld, wanted duration=%u, got %u - wanted %ld - %ld, got %ld - %ld", st->id, points, points_new, group, before - after, before_new - after_new, after, before, after_new, before_new);
906
+
907
+ after = after_new;
908
+ before = before_new;
909
+ duration = before - after;
910
+ points = points_new;
911
912
// Now we have:
913
// before = the end time of the calculation
@@ -945,29 +971,10 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
971
// -------------------------------------------------------------------------
972
// the main loop
973
948
- long start_at_slot = rrdset_time2slot(st, before), // rrdset_last_slot(st),
949
- stop_at_slot = rrdset_time2slot(st, after);
950
-
974
time_t now = rrdset_slot2time(st, start_at_slot),
975
dt = st->update_every,
976
group_start_t = 0;
977
955
- if(unlikely(debug)) debug(D_RRD_STATS, "INFO %s after_t: %lu (stop_at_t: %ld), before_t: %lu (start_at_t: %ld), start_t(now): %lu, current_entry: %ld, entries: %ld"
956
- , st->id
957
- , after
958
- , stop_at_slot
959
- , before
960
- , start_at_slot
961
- , now
962
- , st->current_entry
963
- , st->entries
964
- );
965
-
966
- // align to group for proper panning of data
967
- start_at_slot -= start_at_slot % group;
968
- stop_at_slot -= stop_at_slot % group;
969
- now = rrdset_slot2time(st, start_at_slot);
970
-
978
if(unlikely(debug)) debug(D_RRD_STATS, "BEGIN %s after_t: %lu (stop_at_t: %ld), before_t: %lu (start_at_t: %ld), start_t(now): %lu, current_entry: %ld, entries: %ld"
979
, st->id
980
, after
@@ -1081,6 +1088,7 @@ RRDR *rrd2rrdr(RRDSET *st, long points, long long after, long long before, int g
1088
}
1089
1090
rrdr_done(r);
1091
+ //error("SHIFT: %s: wanted %ld points, got %ld", st->id, points, rrdr_rows(r));
1092
return r;
1093
}
1094