]> pilppa.org Git - lib1wire.git/blobdiff - src/DeviceData.cc
Added support for cacheing monthly data.
[lib1wire.git] / src / DeviceData.cc
index 13c28aa22f63207e323f26eb2dfd2532f87ed787..d13287c3c087796da6a654b7e14090a4071f1211 100644 (file)
@@ -11,7 +11,8 @@
 
 #include "W1Util.hh"
 #include "DeviceData.hh"
-#include "W1Store.hh"
+#include "StoreDay.hh"
+#include "StoreCache.hh"
 #include "DeviceConfig.hh"
 #include "Factory.hh"
 
@@ -22,7 +23,7 @@ using namespace std;
 using namespace plp;
 
 DeviceData::DeviceData(string device_id_param) {
-       string          base_dir;
+       string  base_dir;
 
        device_config           = Factory::get_device_config(device_id_param);
        summary_calc_type       = device_config->get_summary_calculation_type();
@@ -39,29 +40,29 @@ DeviceData::~DeviceData() {
 
 Data *DeviceData::find_oldest_data(vector<string> year_vector) {
        unsigned int    ii;
-       string          year_dir;
-       string          month_dir;
-       vector<string>  month_vector;
-       vector<string>  data_vector;
+       string          year_dr;
+       string          mon_dr;
+       vector<string>  mon_vcr;
+       vector<string>  dta_vcr;
        string          f_name;
-       W1Store         *store;
+       StoreDay        *store;
        Data            *ret_val;
 
        ret_val = NULL;
        if (year_vector.size() > 0) {
                // dirs are alphabetically sorted
-               year_dir        = year_vector.at(0);
-               year_dir        = W1Util::concat_paths(device_dir, year_dir);
-               month_vector    = W1Util::get_subdirectories(year_dir);
-               for (ii = 0; ii < month_vector.size(); ii++) {
-                       month_dir       = month_vector.at(ii);
-                       month_dir       = W1Util::concat_paths(year_dir, month_dir);
+               year_dr = year_vector.at(0);
+               year_dr = W1Util::concat_paths(device_dir, year_dr);
+               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
-                       data_vector     = W1Util::get_data_files(month_dir);
-                       if (data_vector.size() > 0) {
-                               f_name  = data_vector.at(0);
-                               f_name  = W1Util::concat_paths(month_dir, f_name);
-                               store   = new W1Store(f_name);
+                       dta_vcr = W1Util::get_data_files(mon_dr);
+                       if (dta_vcr.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;
@@ -73,32 +74,32 @@ Data *DeviceData::find_oldest_data(vector<string> year_vector) {
 
 Data *DeviceData::find_newest_data(vector<string> year_vector) {
        int             ii;
-       string          year_dir;
-       string          month_dir;
-       vector<string>  month_vector;
-       vector<string>  data_vector;
+       string          year_dr;
+       string          mon_dr;
+       vector<string>  mon_vcr;
+       vector<string>  d_vcr;
        string          f_name;
        Data            *ret_val;
        int             size;
-       W1Store         *store;
+       StoreDay                *store;
 
        ret_val = NULL;
        size    = year_vector.size();
        if (size > 0) {
                // dirs are alphabetically sorted
-               year_dir        = year_vector.at(size - 1);
-               year_dir        = W1Util::concat_paths(device_dir, year_dir);
-               month_vector    = W1Util::get_subdirectories(year_dir);
-               for (ii = month_vector.size() - 1; ii >= 0; ii--) {
-                       month_dir       = month_vector.at(ii);
-                       month_dir       = W1Util::concat_paths(year_dir, month_dir);
+               year_dr = year_vector.at(size - 1);
+               year_dr = W1Util::concat_paths(device_dir, year_dr);
+               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
-                       data_vector     = W1Util::get_data_files(month_dir);
-                       size    = data_vector.size();
+                       d_vcr   = W1Util::get_data_files(mon_dr);
+                       size    = d_vcr.size();
                        if (size > 0) {
-                               f_name  = data_vector.at(size - 1);
-                               f_name  = W1Util::concat_paths(month_dir, f_name);
-                               store   = new W1Store(f_name);
+                               f_name  = d_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;
@@ -110,57 +111,57 @@ Data *DeviceData::find_newest_data(vector<string> year_vector) {
 
 DataRange *DeviceData::get_data_range() {
        DataRange       *ret_val;
-       vector<string>  year_list;
-       Data            *first_data;
-       Data            *newest_data;
-
-       ret_val         = NULL;
-       year_list       = W1Util::get_subdirectories(device_dir);
-       first_data      = find_oldest_data(year_list);
-       if (first_data != NULL) {
-               newest_data     = find_newest_data(year_list);
-               if (newest_data != NULL) {
-                       ret_val = new DataRange(*first_data);
-                       ret_val->add_data(*newest_data);
-                       delete(newest_data);
+       vector<string>  y_list;
+       Data            *o_data;
+       Data            *n_data;
+
+       ret_val = NULL;
+       y_list  = W1Util::get_subdirectories(device_dir);
+       o_data  = find_oldest_data(y_list);
+       if (o_data != NULL) {
+               n_data  = find_newest_data(y_list);
+               if (n_data != NULL) {
+                       ret_val = new DataRange(o_data);
+                       ret_val->add(n_data);
+                       delete(n_data);
                }
-               delete(first_data);
+               delete(o_data);
        }
        return ret_val;
 }
 
-long int get_interval_type(Date *start_date,
+EnumSummaryPeriod get_period_type(Date *start_date,
                        Date *end_date) {
-       int     diff;
-       int     ret_val;
+       int                     diff;
+       EnumSummaryPeriod       ret_val;
 
-       ret_val = 0;
+       ret_val = PERIOD_YEARLY;
        diff    = end_date->year - start_date->year;
        if (diff != 0) {
-               ret_val = 0;
+               ret_val = PERIOD_YEARLY;
        }
        else {
                diff    = end_date->month - start_date->month;
                if (diff != 0) {
-                       ret_val = 1;
+                       ret_val = PERIOD_MONTHLY;
                }
                else {
                        diff    = end_date->day - start_date->day;
                        if (diff != 0) {
-                               ret_val = 2;
+                               ret_val = PERIOD_DAILY;
                        }
                        else {
                                diff    = end_date->hour - start_date->hour;
                                if (diff != 0) {
-                                       ret_val = 3;
+                                       ret_val = PERIOD_HOURLY;
                                }
                                else {
                                        diff    = end_date->min - start_date->min;
                                        if (diff != 0) {
-                                               ret_val = 4;
+                                               ret_val = PERIOD_MINUTELY;
                                        }
                                        else {
-                                               ret_val = 5;
+                                               ret_val = PERIOD_SECONDLY;
                                        }
                                }
                        }
@@ -169,43 +170,39 @@ long int get_interval_type(Date *start_date,
        return ret_val;
 }
 
-Data *DeviceData::get_day_summary(Date *date) {
-       Data    *ret_val;
-       W1Store *store;
-       bool    suc_flg;
+DataRange *DeviceData::get_summary(Date *date_param,
+                               EnumSummaryCalculationType calc_type_param,
+                               EnumSummaryPeriod period_type_param) {
+       DataRange       *ret_val;
+       StoreCache      *store;
 
        ret_val = NULL;
-       store   = new W1Store(device_id, date);
+       //store = new StoreDay(device_id, date_param);
+       store   = new StoreCache(device_id, date_param);
        if (store != NULL) {
-               suc_flg = store->load();
-               if (suc_flg == true) {
-                       switch(summary_calc_type) {
-                               case SUM:
-                                       ret_val = store->get_sum();
-                                       break;
-                               case DELTA:
-                                       ret_val = store->get_delta();
-                                       break;
-                               case MEAN:
-                               default:
-                                       ret_val = store->get_mean();
-                                       break;
-                               case MAX:
-                                       ret_val = store->get_max();
-                                       break;
-                               case MIN:
-                                       ret_val = store->get_min();
-                                       break;
-                       }
-                       if (ret_val != NULL) {
-                               ret_val->printout();
-                       }
-                       else {
-                               log_error("Could not read data log for device: %s\n", device_id.c_str());
-                       }
+               switch(calc_type_param) {
+                       case SUM:
+                               ret_val = store->get_sum(period_type_param);
+                               break;
+                       case DELTA:
+                               ret_val = store->get_delta(period_type_param);
+                               break;
+                       case MAX:
+                               ret_val = store->get_max(period_type_param);
+                               break;
+                       case MIN:
+                               ret_val = store->get_min(period_type_param);
+                               break;
+                       case MEAN:
+                       default:
+                               ret_val = store->get_mean(period_type_param);
+                               break;
+               }
+               if (ret_val != NULL) {
+                       ret_val->printout();
                }
                else {
-                       log_error("Could not read data log for device: %s. Data file open load failed.\n", device_id.c_str());
+                       log_error("Could not read data log for device: %s\n", device_id.c_str());
                }
        }
        else {
@@ -215,120 +212,155 @@ Data *DeviceData::get_day_summary(Date *date) {
        return ret_val;
 }
 
-DataRange *DeviceData::get_daily_summary(Date *start_date,
+DataRange *DeviceData::get_monthly_summary(Date *date,
+                               EnumSummaryCalculationType calc_type_param) {
+       return get_summary(date, calc_type_param, PERIOD_MONTHLY);
+}
+
+DataRange *DeviceData::get_monthly_summary(Date *date) {
+       DataRange       *ret_val;
+
+       ret_val = get_monthly_summary(date, summary_calc_type);
+       return ret_val;
+}
+
+DataRange *DeviceData::get_monthly_summary(Date *start_date,
                                        Date *end_date) {
        DataRange       *ret_val;
-       Data            *data;
+       DataRange       *data;
        Date            *date;
 
        ret_val = NULL;
        date    = start_date->clone();
-       while(date->before(*end_date)) {
-               data    = get_day_summary(date);
+       while(date->before_or_equal_month(end_date)) {
+               data    = get_monthly_summary(date);
                if (data != NULL) {
                        if (ret_val == NULL) {
-                               ret_val = new DataRange(*data);
+                               ret_val = new DataRange(data);
                        }
                        else {
-                               ret_val->add_data(*data);
+                               ret_val->add(data);
                        }
                        delete(data);
                }
-               date->tomorrow();
+               date->next_month();
        }
        delete(date);
        return ret_val;
 }
 
-vector<Data *> DeviceData::get_hourly_summary(Date *date) {
-       vector<Data *>  ret_val;
-       W1Store *       store;
+DataRange *DeviceData::get_daily_summary(Date *date,
+                               EnumSummaryCalculationType calc_type_param) {
+       return get_summary(date, calc_type_param, PERIOD_DAILY);
+}
 
-       store   = new W1Store(device_id, date);
-       store->load();
-       switch(summary_calc_type) {
-/*
-               case SUM:
-                       ret_val = store->get_sum();
-                       break;
-*/
-               case DELTA:
-                       ret_val = store->get_delta(3600);
-                       break;
-               case MEAN:
-               default:
-                       ret_val = store->get_mean(3600);
-                       break;
-/*
-               case MAX:
-                       ret_val = store->get_max();
-                       break;
-               case MIN:
-                       ret_val = store->get_min();
-                       break;
-*/
+DataRange *DeviceData::get_daily_summary(Date *date) {
+       DataRange       *ret_val;
+
+       ret_val = get_daily_summary(date, summary_calc_type);
+       return ret_val;
+}
+
+DataRange *DeviceData::get_daily_summary(Date *start_date,
+                                       Date *end_date) {
+       DataRange       *ret_val;
+       DataRange       *data;
+       Date            *date;
+
+       ret_val = NULL;
+       date    = start_date->clone();
+       while(date->before_or_equal_day(end_date)) {
+               data    = get_daily_summary(date);
+               if (data != NULL) {
+                       if (ret_val == NULL) {
+                               ret_val = new DataRange(data);
+                       }
+                       else {
+                               ret_val->add(data);
+                       }
+                       delete(data);
+               }
+               date->next_day();
        }
-       delete(store);
+       delete(date);
+       return ret_val;
+}
+
+DataRange *DeviceData::get_hourly_summary(Date *date,
+               EnumSummaryCalculationType calc_type_param) {
+       return get_summary(date, calc_type_param, PERIOD_HOURLY);
+}
+
+DataRange *DeviceData::get_hourly_summary(Date *date) {
+       DataRange       *ret_val;
+
+       ret_val = get_hourly_summary(date, summary_calc_type);
        return ret_val;
 }
 
 DataRange *DeviceData::get_hourly_summary(Date *start_date,
                                        Date *end_date) {
        DataRange       *ret_val;
-       vector<Data *>  dta_list;
+       DataRange       *dta_lst;
        Data            *data;
        Date            *date;
+       int             cnt;
+       int             ii;
 
        ret_val = NULL;
        date    = start_date->clone();
-       while(date->before(*end_date)) {
-               dta_list        = get_hourly_summary(date);
-               for(vector<Data *>::iterator list_iter = dta_list.begin(); list_iter != dta_list.end(); list_iter++) {
-                       data    = (Data *)*list_iter;
+       while(date->before_or_equal_hour(end_date)) {
+               dta_lst = get_hourly_summary(date);
+               cnt     = dta_lst->get_count();
+               for(ii = 0; ii < cnt; ii++) {
+                       data    = dta_lst->get(ii);
                        if (data != NULL) {
                                if (ret_val == NULL) {
-                                       ret_val = new DataRange(*data);
+                                       ret_val = new DataRange(data);
                                }
                                else {
-                                       ret_val->add_data(*data);
+                                       ret_val->add(data);
                                }
                                delete(data);
                        }
                }
-               date->tomorrow();
+               date->next_day();
        }
        delete(date);
        return ret_val;
 }
+
 DataRange *DeviceData::get_data(Date *start_date,
                                Date *end_date) {
-       DataRange       *ret_val;
-       int             int_type;
+       DataRange               *ret_val;
+       EnumSummaryPeriod       period;
 
-       ret_val         = NULL;
+       ret_val = NULL;
        start_date->printout();
        end_date->printout();
-       int_type        = get_interval_type(start_date, end_date);
-       switch(int_type) {
-               case 0:
-                       log_debug("get yearly summary\n");
+       period  = get_period_type(start_date, end_date);
+       switch(period) {
+               case PERIOD_YEARLY:
+                       log_debug("get yearly summary: %s - %s\n", start_date->to_string().c_str(), end_date->to_string().c_str());
+                       ret_val = get_monthly_summary(start_date, end_date);
                        break;
-               case 1:
+               case PERIOD_MONTHLY:
                        log_debug("get monthly summary\n");
-                       ret_val = get_daily_summary(start_date, end_date);
+                       ret_val = get_monthly_summary(start_date, end_date);
                        break;
-               case 2:
+               case PERIOD_DAILY:
                        log_debug("get daily summary\n");
                        ret_val = get_daily_summary(start_date, end_date);
                        break;
-               case 3:
+               case PERIOD_HOURLY:
                        log_debug("get hourly summary\n");
                        ret_val = get_hourly_summary(start_date, end_date);
                        break;
-               case 4:
+               case PERIOD_MINUTELY:
                        log_debug("get minute summary data\n");
                        break;
-               case 5:
+               case PERIOD_SECONDLY:
+               default:
                        log_debug("get second summary data\n");
                        break;
        }