- for(i=0;i<rrd.stat_head->rra_cnt;i++){
- if(cf_conv(rrd.rra_def[i].cf_nam) == cf_idx){
-
-
- cal_end = (rrd.live_head->last_up - (rrd.live_head->last_up
- % (rrd.rra_def[i].pdp_cnt
- * rrd.stat_head->pdp_step)));
- cal_start = (cal_end
- - (rrd.rra_def[i].pdp_cnt
- * rrd.rra_def[i].row_cnt
- * rrd.stat_head->pdp_step));
-
- full_match = *end -*start;
- /* best full match */
- if(cal_end >= *end
- && cal_start <= *start){
- tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step
- * rrd.rra_def[i].pdp_cnt));
- if (first_full || (tmp_step_diff < best_step_diff)){
- first_full=0;
- best_step_diff = tmp_step_diff;
- best_full_rra=i;
- }
-
- } else {
- /* best partial match */
- tmp_match = full_match;
- if (cal_start>*start)
- tmp_match -= (cal_start-*start);
- if (cal_end<*end)
- tmp_match -= (*end-cal_end);
- if (first_part || best_match < tmp_match){
- first_part=0;
- best_match = tmp_match;
- best_part_rra =i;
- }
- }
- }
+ for (i = 0; (unsigned) i < rrd.stat_head->rra_cnt; i++) {
+ enum cf_en rratype=cf_conv(rrd.rra_def[i].cf_nam);
+ /* handle this RRA */
+ if (
+ /* if we found a direct match */
+ (rratype == cf_idx)
+ ||
+ /*if we found a DS with interval 1
+ and CF (requested,available) are MIN,MAX,AVERAGE,LAST
+ */
+ (
+ /* only if we are on interval 1 */
+ (rrd.rra_def[i].pdp_cnt==1)
+ && (
+ /* and requested CF is MIN,MAX,AVERAGE,LAST */
+ (cf_idx == CF_MINIMUM)
+ ||(cf_idx == CF_MAXIMUM)
+ ||(cf_idx == CF_AVERAGE)
+ ||(cf_idx == CF_LAST)
+ )
+ && (
+ /* and found CF is MIN,MAX,AVERAGE,LAST */
+ (rratype == CF_MINIMUM)
+ ||(rratype == CF_MAXIMUM)
+ ||(rratype == CF_AVERAGE)
+ ||(rratype == CF_LAST)
+ )
+ )
+ ){
+
+ cal_end = (rrd.live_head->last_up - (rrd.live_head->last_up
+ % (rrd.rra_def[i].pdp_cnt
+ *
+ rrd.stat_head->
+ pdp_step)));
+ cal_start =
+ (cal_end -
+ (rrd.rra_def[i].pdp_cnt * rrd.rra_def[i].row_cnt *
+ rrd.stat_head->pdp_step));
+
+ full_match = *end - *start;
+#ifdef DEBUG
+ fprintf(stderr, "Considering: start %10lu end %10lu step %5lu ",
+ cal_start, cal_end,
+ rrd.stat_head->pdp_step * rrd.rra_def[i].pdp_cnt);
+#endif
+ /* we need step difference in either full or partial case */
+ tmp_step_diff = labs(*step - (rrd.stat_head->pdp_step
+ * rrd.rra_def[i].pdp_cnt));
+ /* best full match */
+ if (cal_start <= *start) {
+ if (first_full || (tmp_step_diff < best_full_step_diff)) {
+ first_full = 0;
+ best_full_step_diff = tmp_step_diff;
+ best_full_rra = i;
+#ifdef DEBUG
+ fprintf(stderr, "best full match so far\n");
+ } else {
+ fprintf(stderr, "full match, not best\n");
+#endif
+ }
+
+ } else {
+ /* best partial match */
+ tmp_match = full_match;
+ if (cal_start > *start)
+ tmp_match -= (cal_start - *start);
+ if (first_part ||
+ (best_match < tmp_match) ||
+ (best_match == tmp_match &&
+ tmp_step_diff < best_part_step_diff)) {
+#ifdef DEBUG
+ fprintf(stderr, "best partial so far\n");
+#endif
+ first_part = 0;
+ best_match = tmp_match;
+ best_part_step_diff = tmp_step_diff;
+ best_part_rra = i;
+ } else {
+#ifdef DEBUG
+ fprintf(stderr, "partial match, not best\n");
+#endif
+ }
+ }
+ }