]> pilppa.org Git - libplp.git/blobdiff - src/StoreCache.cc
include unistd
[libplp.git] / src / StoreCache.cc
index 3506292c36a01fabb7d3f4ce1cc74f35ed6b74b2..8a9952a9394ac08f84f3b5cd4dd2525ee438f82d 100644 (file)
@@ -7,6 +7,8 @@
 #include <sstream>
 #include <fstream>
 
+#include <unistd.h>
+
 #include "log.h"
 #include "Store.hh"
 #include "StoreCache.hh"
@@ -31,12 +33,17 @@ string StoreCache::get_dir_name(string device_id_param,
        string  ret_val;
        char    buffer[30];
        string  bd_name;
+       string  period_name;
+       string  calc_type;
+
+       period_name     = DataSummary::get_summary_period_name(period_type_param);
+       calc_type       = DataSummary::get_summary_calculation_name(calc_type_param);
 
        bd_name = DeviceConfig::get_base_dir_name();
        bd_name = FileUtil::concat_paths(bd_name, CACHE_DIR_NAME);
        bd_name = FileUtil::concat_paths(bd_name, device_id_param);
-       bd_name = FileUtil::concat_paths(bd_name, SUMMARY_PERIOD_NAMES_ARRAY[period_type_param]);
-       bd_name = FileUtil::concat_paths(bd_name, CALCULATION_TYPE_NAMES_ARRAY[calc_type_param]);
+       bd_name = FileUtil::concat_paths(bd_name, period_name);
+       bd_name = FileUtil::concat_paths(bd_name, calc_type);
        if (period_type_param == PERIOD_YEARLY) {
                ret_val = bd_name;
        }
@@ -149,14 +156,24 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
                                        res_data        = NULL;
                                        cnt             = 0;
                                        val_cnt         = 0;
+                                       dr              = NULL;
                                        while(cur_date->before(max_date)) {
                                                if (period_type_param == PERIOD_YEARLY) {
                                                        store   = new StoreCache(device_id, cur_date);
                                                        dr      = store->get_mean(PERIOD_MONTHLY);
                                                }
                                                else {
-                                                       store   = new StoreDay(device_id, cur_date);
-                                                       dr      = store->get_mean(PERIOD_DAILY);
+                                                       if (StoreDay::exist(device_id, cur_date, false)) {
+                                                               store   = new StoreDay(device_id, cur_date);
+                                                               dr      = store->get_mean(PERIOD_DAILY);
+                                                       }
+                                                       if (dr == NULL) {
+                                                               // data not found for that day, try to find next date containing data
+                                                               StoreDay::get_next_date_with_data(device_id,
+                                                                                       cur_date,
+                                                                                       max_date);
+                                                               continue;
+                                                       }
                                                }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
@@ -199,12 +216,14 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
                        case PERIOD_DAILY:
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
-                       case PERIOD_SECONDLY: {
+                       case PERIOD_SECONDLY:
+                               if (StoreDay::exist(device_id, date, false)) {
                                        StoreDay        *store;
 
                                        store   = new StoreDay(device_id, date);
                                        ret_val = store->get_mean(period_type_param);
-                                       if ((period_type_param != PERIOD_MINUTELY) ||
+                                       if ((ret_val != NULL) &&
+                                           (period_type_param != PERIOD_MINUTELY) &&
                                            (period_type_param != PERIOD_SECONDLY)) {
                                                // no need cache second or minute data
                                                save(fname, ret_val, 4);
@@ -267,8 +286,17 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
                                                        dr      = store->get_sum(PERIOD_MONTHLY);
                                                }
                                                else {
-                                                       store   = new StoreDay(device_id, cur_date);
-                                                       dr      = store->get_sum(PERIOD_DAILY);
+                                                       if (StoreDay::exist(device_id, cur_date, false)) {
+                                                               store   = new StoreDay(device_id, cur_date);
+                                                               dr      = store->get_sum(PERIOD_DAILY);
+                                                       }
+                                                       if (dr == NULL) {
+                                                               // data not found for that day, try to find next date containing data
+                                                               StoreDay::get_next_date_with_data(device_id,
+                                                                                               cur_date,
+                                                                                               max_date);
+                                                               continue;
+                                                       }
                                                }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
@@ -309,14 +337,17 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
                        case PERIOD_SECONDLY:
-                               store   = new StoreDay(device_id, date);
-                               ret_val = store->get_sum(period_type_param);
-                               if ((period_type_param != PERIOD_MINUTELY) ||
-                                   (period_type_param != PERIOD_SECONDLY)) {
-                                       // no need cache second or minute data
-                                       save(fname, ret_val, 4);
+                               if (StoreDay::exist(device_id, date, false)) {
+                                       store   = new StoreDay(device_id, date);
+                                       ret_val = store->get_sum(period_type_param);
+                                       if ((ret_val != NULL) &&
+                                           (period_type_param != PERIOD_MINUTELY) &&
+                                           (period_type_param != PERIOD_SECONDLY)) {
+                                               // no need cache second or minute data
+                                               save(fname, ret_val, 4);
+                                       }
+                                       delete(store);
                                }
-                               delete(store);
                                break;
                }
        }
@@ -383,37 +414,47 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
                                        Data    *last_data;
                                        Data    *cur_data;
                                        Date    *cur_date;
-                                       Date    *limit_date;
+                                       Date    *max_date;
                                        int     ii;
                                        int     cnt;
 
                                        cur_date        = date->clone();
-                                       limit_date      = date->clone();
-                                       limit_date->next_month();
+                                       max_date        = date->clone();
+                                       max_date->next_month();
                                        first_data      = NULL;
                                        last_data       = NULL;
-                                       while(cur_date->before(limit_date)) {
-                                               store   = new StoreDay(device_id, cur_date);
-                                               if (first_data == NULL) {
-                                                       cur_data        = store->get_oldest_data();
+                                       while(cur_date->before(max_date)) {
+                                               if (StoreDay::exist(device_id,
+                                                               cur_date,
+                                                               false)) {
+                                                       store   = new StoreDay(device_id, cur_date);
+                                                       if (first_data == NULL) {
+                                                               cur_data        = store->get_oldest_data();
+                                                               if (cur_data != NULL) {
+                                                                       first_data      = cur_data->clone();
+                                                                       last_data       = cur_data->clone();
+                                                                       delete(cur_data);
+                                                               }
+                                                       }
+                                                       cur_data        = store->get_latest_data();
                                                        if (cur_data != NULL) {
-                                                               first_data      = cur_data->clone();
-                                                               last_data       = cur_data->clone();
-                                                               delete(cur_data);
+                                                               if (last_data != NULL) {
+                                                                       delete(last_data);
+                                                               }
+                                                               last_data       = cur_data;
                                                        }
+                                                       delete(store);
+                                                       cur_date->next_day();
                                                }
-                                               cur_data        = store->get_latest_data();
-                                               if (cur_data != NULL) {
-                                                       if (last_data != NULL) {
-                                                               delete(last_data);
-                                                       }
-                                                       last_data       = cur_data;
+                                               else {
+                                                       // data not found for that day, try to find next date containing data
+                                                       StoreDay::get_next_date_with_data(device_id,
+                                                                               cur_date,
+                                                                               max_date);
                                                }
-                                               delete(store);
-                                               cur_date->next_day();
                                        }
                                        delete(cur_date);
-                                       delete(limit_date);
+                                       delete(max_date);
                                        if (first_data != NULL) {
                                                if (last_data == NULL) {
                                                        last_data       = first_data->clone();
@@ -436,14 +477,16 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
                        case PERIOD_SECONDLY:
-                               store   = new StoreDay(device_id, date);
-                               ret_val = store->get_delta(period_type_param);
-                               if ((period_type_param != PERIOD_MINUTELY) ||
-                                   (period_type_param != PERIOD_SECONDLY)) {
-                                       // no need cache second or minute data
-                                       save(fname, ret_val, 4);
+                               if (StoreDay::exist(device_id, date, false)) {
+                                       store   = new StoreDay(device_id, date);
+                                       ret_val = store->get_delta(period_type_param);
+                                       if ((period_type_param != PERIOD_MINUTELY) ||
+                                           (period_type_param != PERIOD_SECONDLY)) {
+                                               // no need cache second or minute data
+                                               save(fname, ret_val, 4);
+                                       }
+                                       delete(store);
                                }
-                               delete(store);
                                break;
                }
        }
@@ -500,8 +543,16 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
                                                        dr      = store->get_max(PERIOD_MONTHLY);
                                                }
                                                else {
-                                                       store   = new StoreDay(device_id, cur_date);
-                                                       dr      = store->get_max(PERIOD_DAILY);
+                                                       if (StoreDay::exist(device_id, cur_date, false)) {
+                                                               store   = new StoreDay(device_id, cur_date);
+                                                               dr      = store->get_max(PERIOD_DAILY);
+                                                       }
+                                                       else {
+                                                               StoreDay::get_next_date_with_data(device_id,
+                                                                                               cur_date,
+                                                                                               max_date);
+                                                               continue;
+                                                       }
                                                }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
@@ -552,14 +603,16 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
                        case PERIOD_SECONDLY:
-                               store   = new StoreDay(device_id, date);
-                               ret_val = store->get_max(period_type_param);
-                               if ((period_type_param != PERIOD_MINUTELY) ||
-                                   (period_type_param != PERIOD_SECONDLY)) {
-                                       // no need cache second or minute data
-                                       save(fname, ret_val, 4);
+                               if (StoreDay::exist(device_id, date, false)) {
+                                       store   = new StoreDay(device_id, date);
+                                       ret_val = store->get_max(period_type_param);
+                                       if ((period_type_param != PERIOD_MINUTELY) ||
+                                           (period_type_param != PERIOD_SECONDLY)) {
+                                               // no need cache second or minute data
+                                               save(fname, ret_val, 4);
+                                       }
+                                       delete(store);
                                }
-                               delete(store);
                                break;
                }
        }
@@ -616,8 +669,16 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
                                                        dr      = store->get_min(PERIOD_MONTHLY);
                                                }
                                                else {
-                                                       store   = new StoreDay(device_id, cur_date);
-                                                       dr      = store->get_min(PERIOD_DAILY);
+                                                       if (StoreDay::exist(device_id, cur_date, false)) {
+                                                               store   = new StoreDay(device_id, cur_date);
+                                                               dr      = store->get_min(PERIOD_DAILY);
+                                                       }
+                                                       else {
+                                                               StoreDay::get_next_date_with_data(device_id,
+                                                                                               cur_date,
+                                                                                               max_date);
+                                                               continue;
+                                                       }
                                                }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
@@ -668,14 +729,16 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
                        case PERIOD_SECONDLY:
-                               store   = new StoreDay(device_id, date);
-                               ret_val = store->get_min(period_type_param);
-                               if ((period_type_param != PERIOD_MINUTELY) ||
-                                   (period_type_param != PERIOD_SECONDLY)) {
-                                       // no need cache second or minute data
-                                       save(fname, ret_val, 4);
+                               if (StoreDay::exist(device_id, date, false)) {
+                                       store   = new StoreDay(device_id, date);
+                                       ret_val = store->get_min(period_type_param);
+                                       if ((period_type_param != PERIOD_MINUTELY) ||
+                                           (period_type_param != PERIOD_SECONDLY)) {
+                                               // no need cache second or minute data
+                                               save(fname, ret_val, 4);
+                                       }
+                                       delete(store);
                                }
-                               delete(store);
                                break;
                }
        }
@@ -716,7 +779,9 @@ void StoreCache::save(std::string fname_param, plp::DataRange *datarange_param,
        }
 }
 
-Data *StoreCache::get_oldest_data(Date *date_param, string device_id_param, EnumSummaryPeriod period_type_param) {
+Data *StoreCache::get_oldest_data(Date *date_param,
+                       string device_id_param,
+                       EnumSummaryPeriod period_type_param) {
        int             size;
        unsigned int    ii;
        string          year_dr;
@@ -784,7 +849,9 @@ Data *StoreCache::get_oldest_data(Date *date_param, string device_id_param, Enum
        return ret_val;
 }
 
-Data *StoreCache::get_latest_data(Date *date_param, string device_id_param, EnumSummaryPeriod period_type_param) {
+Data *StoreCache::get_latest_data(Date *date_param,
+                       string device_id_param,
+                       EnumSummaryPeriod period_type_param) {
        int             ii;
        string          mon_dr;
        vector<string>  mon_vcr;