X-Git-Url: http://pilppa.org/gitweb/?a=blobdiff_plain;f=src%2FStoreCache.cc;h=8a9952a9394ac08f84f3b5cd4dd2525ee438f82d;hb=6e2b61568d38e2c82c9bafd7eca3af405af02ce0;hp=6e19690456d4ebb5646eb2f640035fa2e86d5a4d;hpb=063ec715601bf9662797725cb39732bea0572d26;p=libplp.git diff --git a/src/StoreCache.cc b/src/StoreCache.cc index 6e19690..8a9952a 100644 --- a/src/StoreCache.cc +++ b/src/StoreCache.cc @@ -7,6 +7,8 @@ #include #include +#include + #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; } @@ -148,14 +155,25 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) { cur_data = NULL; 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(); @@ -198,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); @@ -266,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(); @@ -308,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; } } @@ -382,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(); @@ -435,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; } } @@ -499,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(); @@ -551,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; } } @@ -615,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(); @@ -667,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; } } @@ -715,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; @@ -783,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 mon_vcr;