]> pilppa.org Git - lib1wire.git/blobdiff - src/StoreCache.cc
Api cleanups for device querying
[lib1wire.git] / src / StoreCache.cc
index 3fc9c4b0aad1a95fcd23d1b160ac6855cbaa04d5..f3f2ac29bc553ea1f50a2f30e80adbbb97226da0 100644 (file)
@@ -17,7 +17,7 @@ using namespace plp;
 using namespace w1;
 
 StoreCache::StoreCache(string device_id_param,
-               Date *date_time_param): Store(device_id_param, date_time_param) {
+       Date *date_time_param): Store(device_id_param, date_time_param) {
 }
 
 StoreCache::~StoreCache() {
@@ -79,12 +79,34 @@ string StoreCache::get_file_name(string device_id_param,
        return ret_val;
 }
 
+plp::Date *StoreCache::get_scanning_limit_date(EnumSummaryPeriod period_type_param) {
+       Data    *cur_data;;
+       Date    *ret_val;
+
+       cur_data        = get_newest_data(date, device_id, period_type_param);
+       if (cur_data != NULL) {
+               ret_val = cur_data->get_date().clone();
+               ret_val->next_second();
+               delete(cur_data);
+               cur_data        = NULL;
+       }
+       else {
+               ret_val = date->clone();
+               if (period_type_param == PERIOD_YEARLY) {
+                       ret_val->next_year();
+               }
+               else {
+                       ret_val->next_month();
+               }
+       }
+       return ret_val;
+}
+
 DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
        int             row_count;
        DataRange       *ret_val;
        Data            *dta;
        string          fname;
-       StoreDay        *store;
 
        ret_val = NULL;
        fname   = get_file_name(device_id,
@@ -108,8 +130,8 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
        if (ret_val == NULL) {
                switch(period_type_param) {
                        case PERIOD_YEARLY:
-                               break;
-                       case PERIOD_MONTHLY: {
+                       case PERIOD_MONTHLY:
+                       {
                                        Data            *cur_data;
                                        Data            *res_data;
                                        Date            *cur_date;
@@ -118,25 +140,31 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
                                        int             cnt;
                                        int             val_cnt;
                                        DataRange       *dr;
+                                       Store           *store;
 
                                        cur_date        = date->clone();
-                                       max_date        = date->clone();
-                                       max_date->next_month();
+                                       max_date        = get_scanning_limit_date(period_type_param);
                                        cur_data        = NULL;
                                        res_data        = NULL;
                                        cnt             = 0;
                                        while(cur_date->before(max_date)) {
-                                               store   = new StoreDay(device_id, cur_date);
-                                               dr      = store->get_mean(PERIOD_DAILY);
+                                               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 (dr != NULL) {
                                                        cur_data        = dr->get_first();
                                                        if (cur_data != NULL) {
                                                                cnt++;
                                                                if (res_data == NULL) {
                                                                        res_data        = cur_data;
+                                                                       val_cnt         = res_data->get_value_count();
                                                                }
                                                                else {
-                                                                       val_cnt = res_data->get_value_count();
                                                                        for (ii = 0; ii < val_cnt; ii++) {
                                                                                res_data->value_arr[ii] = res_data->value_arr[ii] + cur_data->value_arr[ii];
                                                                        }
@@ -146,7 +174,12 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
                                                        delete(dr);
                                                }
                                                delete(store);
-                                               cur_date->next_day();
+                                               if (period_type_param == PERIOD_YEARLY) {
+                                                       cur_date->next_month();
+                                               }
+                                               else {
+                                                       cur_date->next_day();
+                                               }
                                        }
                                        if ((res_data != NULL) &&
                                            (cnt > 0)) {
@@ -164,11 +197,18 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
                        case PERIOD_DAILY:
                        case PERIOD_HOURLY:
                        case PERIOD_MINUTELY:
-                       case PERIOD_SECONDLY:
-                               store   = new StoreDay(device_id, date);
-                               ret_val = store->get_mean(period_type_param);
-                               save(fname, ret_val, 4);
-                               delete(store);
+                       case PERIOD_SECONDLY: {
+                                       StoreDay        *store;
+
+                                       store   = new StoreDay(device_id, date);
+                                       ret_val = store->get_mean(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);
+                               }
                                break;
                }
        }
@@ -180,7 +220,7 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
        DataRange       *ret_val;
        Data            *dta;
        string          fname;
-       StoreDay        *store;
+       Store           *store;
 
        ret_val = NULL;
        fname   = get_file_name(device_id,
@@ -204,7 +244,6 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
        if (ret_val == NULL) {
                switch(period_type_param) {
                        case PERIOD_YEARLY:
-                               break;
                        case PERIOD_MONTHLY: {
                                        Data            *cur_data;
                                        Data            *res_data;
@@ -216,14 +255,19 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
                                        DataRange       *dr;
 
                                        cur_date        = date->clone();
-                                       max_date        = date->clone();
-                                       max_date->next_month();
+                                       max_date        = get_scanning_limit_date(period_type_param);
                                        cur_data        = NULL;
                                        res_data        = NULL;
                                        cnt             = 0;
                                        while(cur_date->before(max_date)) {
-                                               store   = new StoreDay(device_id, cur_date);
-                                               dr      = store->get_sum(PERIOD_DAILY);
+                                               if (period_type_param == PERIOD_YEARLY) {
+                                                       store   = new StoreCache(device_id, cur_date);
+                                                       dr      = store->get_sum(PERIOD_MONTHLY);
+                                               }
+                                               else {
+                                                       store   = new StoreDay(device_id, cur_date);
+                                                       dr      = store->get_sum(PERIOD_DAILY);
+                                               }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
                                                        if (cur_data != NULL) {
@@ -242,7 +286,12 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
                                                        delete(dr);
                                                }
                                                delete(store);
-                                               cur_date->next_day();
+                                               if (period_type_param == PERIOD_YEARLY) {
+                                                       cur_date->next_month();
+                                               }
+                                               else {
+                                                       cur_date->next_day();
+                                               }
                                        }
                                        if ((res_data != NULL) &&
                                            (cnt > 0)) {
@@ -260,7 +309,11 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
                        case PERIOD_SECONDLY:
                                store   = new StoreDay(device_id, date);
                                ret_val = store->get_sum(period_type_param);
-                               save(fname, ret_val, 4);
+                               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);
                                break;
                }
@@ -273,7 +326,7 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
        DataRange       *ret_val;
        Data            *dta;
        string          fname;
-       StoreDay        *store;
+       StoreDay        *store;
 
        ret_val = NULL;
        fname   = get_file_name(device_id,
@@ -297,7 +350,31 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
        }
        if (ret_val == NULL) {
                switch(period_type_param) {
-                       case PERIOD_YEARLY:
+                       case PERIOD_YEARLY: {
+                                       Data    *first_data;
+                                       Data    *last_data;
+                                       int     ii;
+                                       int     cnt;
+
+                                       first_data      = get_oldest_data(date, device_id, PERIOD_YEARLY);
+                                       if (first_data != NULL) {
+                                               last_data       = get_newest_data(date, device_id, PERIOD_YEARLY);
+                                               if (last_data != NULL) {
+                                                       ret_val         = new DataRange();
+                                                       cnt             = last_data->get_value_count();
+                                                       for (ii = 0; ii < cnt; ii++) {
+                                                               last_data->value_arr[ii]        = last_data->value_arr[ii] - first_data->value_arr[ii];
+                                                       }
+                                                       ret_val->add(last_data);
+                                                       delete(last_data);
+                                               }
+                                               delete(first_data);
+                                               save(fname, ret_val, 4);
+                                       }
+                                       else {
+                                               log_error("Could not read first or last data item from device %s for year: %d", device_id.c_str(), date->year);
+                                       }
+                               }
                                break;
                        case PERIOD_MONTHLY: {
                                        Data    *first_data;
@@ -308,7 +385,7 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
                                        int     ii;
                                        int     cnt;
 
-                                       cur_date                = date->clone();
+                                       cur_date        = date->clone();
                                        limit_date      = date->clone();
                                        limit_date->next_month();
                                        first_data      = NULL;
@@ -359,7 +436,11 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
                        case PERIOD_SECONDLY:
                                store   = new StoreDay(device_id, date);
                                ret_val = store->get_delta(period_type_param);
-                               save(fname, ret_val, 4);
+                               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);
                                break;
                }
@@ -372,7 +453,7 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
        DataRange       *ret_val;
        Data            *dta;
        string          fname;
-       StoreDay        *store;
+       Store           *store;
 
        ret_val = NULL;
        fname   = get_file_name(device_id,
@@ -396,7 +477,6 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
        if (ret_val == NULL) {
                switch(period_type_param) {
                        case PERIOD_YEARLY:
-                               break;
                        case PERIOD_MONTHLY: {
                                        Data            *cur_data;
                                        Data            *res_data;
@@ -408,14 +488,19 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
                                        DataRange       *dr;
 
                                        cur_date        = date->clone();
-                                       max_date        = date->clone();
-                                       max_date->next_month();
+                                       max_date        = get_scanning_limit_date(period_type_param);
                                        cur_data        = NULL;
                                        res_data        = NULL;
                                        cnt             = 0;
                                        while(cur_date->before(max_date)) {
-                                               store   = new StoreDay(device_id, cur_date);
-                                               dr      = store->get_max(PERIOD_DAILY);
+                                               if (period_type_param == PERIOD_YEARLY) {
+                                                       store   = new StoreCache(device_id, cur_date);
+                                                       dr      = store->get_max(PERIOD_MONTHLY);
+                                               }
+                                               else {
+                                                       store   = new StoreDay(device_id, cur_date);
+                                                       dr      = store->get_max(PERIOD_DAILY);
+                                               }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
                                                        if (cur_data != NULL) {
@@ -444,7 +529,12 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
                                                        delete(dr);
                                                }
                                                delete(store);
-                                               cur_date->next_day();
+                                               if (period_type_param == PERIOD_YEARLY) {
+                                                       cur_date->next_month();
+                                               }
+                                               else {
+                                                       cur_date->next_day();
+                                               }
                                        }
                                        if ((res_data != NULL) &&
                                            (cnt > 0)) {
@@ -462,7 +552,11 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
                        case PERIOD_SECONDLY:
                                store   = new StoreDay(device_id, date);
                                ret_val = store->get_max(period_type_param);
-                               save(fname, ret_val, 4);
+                               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);
                                break;
                }
@@ -475,7 +569,7 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
        DataRange       *ret_val;
        Data            *dta;
        string          fname;
-       StoreDay        *store;
+       Store           *store;
 
        ret_val = NULL;
        fname   = get_file_name(device_id,
@@ -499,7 +593,6 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
        if (ret_val == NULL) {
                switch(period_type_param) {
                        case PERIOD_YEARLY:
-                               break;
                        case PERIOD_MONTHLY: {
                                        Data            *cur_data;
                                        Data            *res_data;
@@ -511,14 +604,19 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
                                        DataRange       *dr;
 
                                        cur_date        = date->clone();
-                                       max_date        = date->clone();
-                                       max_date->next_month();
+                                       max_date        = get_scanning_limit_date(period_type_param);
                                        cur_data        = NULL;
                                        res_data        = NULL;
                                        cnt             = 0;
                                        while(cur_date->before(max_date)) {
-                                               store   = new StoreDay(device_id, cur_date);
-                                               dr      = store->get_min(PERIOD_DAILY);
+                                               if (period_type_param == PERIOD_YEARLY) {
+                                                       store   = new StoreCache(device_id, cur_date);
+                                                       dr      = store->get_min(PERIOD_MONTHLY);
+                                               }
+                                               else {
+                                                       store   = new StoreDay(device_id, cur_date);
+                                                       dr      = store->get_min(PERIOD_DAILY);
+                                               }
                                                if (dr != NULL) {
                                                        cur_data        = dr->get_first();
                                                        if (cur_data != NULL) {
@@ -547,7 +645,12 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
                                                        delete(dr);
                                                }
                                                delete(store);
-                                               cur_date->next_day();
+                                               if (period_type_param == PERIOD_YEARLY) {
+                                                       cur_date->next_month();
+                                               }
+                                               else {
+                                                       cur_date->next_day();
+                                               }
                                        }
                                        if ((res_data != NULL) &&
                                            (cnt > 0)) {
@@ -565,7 +668,11 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
                        case PERIOD_SECONDLY:
                                store   = new StoreDay(device_id, date);
                                ret_val = store->get_min(period_type_param);
-                               save(fname, ret_val, 4);
+                               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);
                                break;
                }
@@ -582,7 +689,7 @@ void StoreCache::save(std::string fname_param, plp::DataRange *datarange_param,
 
        cnt     = datarange_param->get_count();
        ostream =  NULL;
-       log_info("[%s] cacheing %d data values.\n", device_id.c_str(), cnt);
+       //log_info("[%s] cacheing %d data values.\n", device_id.c_str(), cnt);
        ostream = W1Util::open_for_writing(fname_param.c_str());
        if ((ostream != NULL) &&
            (ostream->is_open() == true)) {
@@ -606,3 +713,139 @@ void StoreCache::save(std::string fname_param, plp::DataRange *datarange_param,
                delete(ostream);
        }
 }
+
+Data *StoreCache::get_oldest_data(Date *date_param, string device_id_param, EnumSummaryPeriod period_type_param) {
+       int             size;
+       unsigned int    ii;
+       string          year_dr;
+       string          mon_dr;
+       vector<string>  mon_vcr;
+       vector<string>  dta_vcr;
+       string          f_name;
+       StoreDay        *store;
+       Data            *ret_val;
+       string          device_dir;
+       char            buffer[30];
+       string          year_name;
+
+       ret_val = NULL;
+       if (period_type_param == PERIOD_YEARLY) {
+               snprintf(buffer, 30, "%d", date_param->year);
+               year_name.append(buffer);
+               device_dir      = DeviceConfig::get_base_dir_name();
+               device_dir      = W1Util::concat_paths(device_dir, device_id_param);
+               year_dr = W1Util::concat_paths(device_dir, year_name);
+               mon_vcr = W1Util::get_subdirectories(year_dr);
+               for (ii = 0; ii < mon_vcr.size(); ii++) {
+                       mon_dr  = mon_vcr.at(ii);
+                       mon_dr  = W1Util::concat_paths(year_dr, mon_dr);
+                       // scan data files from month dir
+                       dta_vcr = W1Util::get_data_files(mon_dr);
+                       size    = dta_vcr.size();
+                       if (size > 0) {
+                               f_name  = dta_vcr.at(0);
+                               f_name  = W1Util::concat_paths(mon_dr, f_name);
+                               store   = new StoreDay(f_name);
+                               ret_val = store->get_oldest_data();
+                               delete(store);
+                               break;
+                       }
+               }
+       }
+       else if (period_type_param == PERIOD_MONTHLY) {
+               ret_val         = NULL;
+               snprintf(buffer, 30, "%d", date_param->year);
+               year_name.append(buffer);
+               device_dir      = DeviceConfig::get_base_dir_name();
+               device_dir      = W1Util::concat_paths(device_dir, device_id_param);
+               year_dr         = W1Util::concat_paths(device_dir, year_name);
+               snprintf(buffer, 30, "%02d", date_param->month);
+               mon_dr.append(buffer);
+               mon_dr          = W1Util::concat_paths(year_dr, mon_dr);
+               // scan data files from month dir
+               dta_vcr         = W1Util::get_data_files(mon_dr);
+               size            = dta_vcr.size();
+               if (size > 0) {
+                       f_name  = dta_vcr.at(0);
+                       f_name  = W1Util::concat_paths(mon_dr, f_name);
+                       store   = new StoreDay(f_name);
+                       ret_val = store->get_oldest_data();
+                       delete(store);
+               }
+
+       }
+       else {
+               store   = new StoreDay(device_id_param, date_param);
+               ret_val = store->get_oldest_data();
+               delete(store);
+       }
+       return ret_val;
+}
+
+Data *StoreCache::get_newest_data(Date *date_param, string device_id_param, EnumSummaryPeriod period_type_param) {
+       int             ii;
+       string          mon_dr;
+       vector<string>  mon_vcr;
+       vector<string>  dta_vcr;
+       string          f_name;
+       StoreDay        *store;
+       Data            *ret_val;
+       string          year_dr;
+       int             size;
+       string          device_dir;
+       char            buffer[30];
+       string          year_name;
+
+       ret_val = NULL;
+       if (period_type_param == PERIOD_YEARLY) {
+               snprintf(buffer, 30, "%d", date_param->year);
+               year_name.append(buffer);
+               device_dir      = DeviceConfig::get_base_dir_name();
+               device_dir      = W1Util::concat_paths(device_dir, device_id_param);
+               year_dr = W1Util::concat_paths(device_dir, year_name);
+               mon_vcr = W1Util::get_subdirectories(year_dr);
+               for (ii = mon_vcr.size() - 1; ii >= 0; ii--) {
+                       mon_dr  = mon_vcr.at(ii);
+                       mon_dr  = W1Util::concat_paths(year_dr, mon_dr);
+                       // scan data files from month dir
+                       dta_vcr = W1Util::get_data_files(mon_dr);
+                       size    = dta_vcr.size();
+                       if (size > 0) {
+                               f_name  = dta_vcr.at(size - 1);
+                               f_name  = W1Util::concat_paths(mon_dr, f_name);
+                               store   = new StoreDay(f_name);
+                               ret_val = store->get_newest_data();
+                               delete(store);
+                               break;
+                       }
+               }
+       }
+       else if (period_type_param == PERIOD_MONTHLY) {
+               ret_val         = NULL;
+               snprintf(buffer, 30, "%d", date_param->year);
+               year_name.append(buffer);
+               device_dir      = DeviceConfig::get_base_dir_name();
+               device_dir      = W1Util::concat_paths(device_dir, device_id_param);
+               year_dr         = W1Util::concat_paths(device_dir, year_name);
+               snprintf(buffer, 30, "%02d", date_param->month);
+               mon_dr.append(buffer);
+               mon_dr          = W1Util::concat_paths(year_dr, mon_dr);
+               // scan data files from month dir
+               dta_vcr         = W1Util::get_data_files(mon_dr);
+               size            = dta_vcr.size();
+               if (size > 0) {
+                       f_name  = dta_vcr.at(size - 1);
+                       f_name  = W1Util::concat_paths(mon_dr, f_name);
+                       store   = new StoreDay(f_name);
+                       ret_val = store->get_newest_data();
+                       delete(store);
+               }
+
+       }
+       else {
+               store   = new StoreDay(device_id_param, date_param);
+               ret_val = store->get_newest_data();
+               delete(store);
+       }
+       return ret_val;
+}