]> pilppa.org Git - lib1wire.git/blobdiff - src/DeviceData.cc
Optimization for year and monthly data calculation.
[lib1wire.git] / src / DeviceData.cc
index 259d821ae38310cebc583baeab048c4e637af82c..c564377a146d747b9c051092e3ef55bae5664fa1 100644 (file)
@@ -4,6 +4,8 @@
  *  Created on: Nov 7, 2010
  *      Author: lamikr
  */
+#include <sstream>
+
 #include <dirent.h>
 #include <malloc.h>
 #include <errno.h>
@@ -11,7 +13,8 @@
 
 #include "W1Util.hh"
 #include "DeviceData.hh"
-#include "W1Store.hh"
+#include "StoreDay.hh"
+#include "StoreCache.hh"
 #include "DeviceConfig.hh"
 #include "Factory.hh"
 
@@ -21,146 +24,124 @@ using namespace w1;
 using namespace std;
 using namespace plp;
 
+template <class NumberDataType>
+bool string_to_number(NumberDataType& result,
+                 const std::string& string_param,
+                 std::ios_base& (*format)(std::ios_base&))
+{
+       istringstream iss(string_param);
+       return !(iss >> format >> result).fail();
+}
+
 DeviceData::DeviceData(string device_id_param) {
-       string          base_dir;
-
-       device_config           = Factory::get_device_config(device_id_param);
-       summary_calc_type       = device_config->get_summary_calculation_type();
-       device_id               = device_id_param;
-       base_dir                = DeviceConfig::get_base_dir_name();
-       device_dir              = W1Util::concat_paths(base_dir, device_id);
-       device_ch_dir           = W1Util::concat_paths(base_dir, "cache");
-       device_ch_dir           = W1Util::concat_paths(device_ch_dir, device_id);
+       string  base_dir;
+
+       device_config   = NULL;
+       device_id       = device_id_param;
+       base_dir        = DeviceConfig::get_base_dir_name();
+       device_dir      = W1Util::concat_paths(base_dir, device_id);
+       device_ch_dir   = W1Util::concat_paths(base_dir, "cache");
+       device_ch_dir   = W1Util::concat_paths(device_ch_dir, device_id);
 }
 
 DeviceData::~DeviceData() {
-       delete(device_config);
+       if (device_config != NULL) {
+               delete(device_config);
+               device_config   = NULL;
+       }
 }
 
-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          f_name;
-       W1Store         *store;
-       Data            *ret_val;
+Data *DeviceData::find_newest_data(vector<string> year_name_vector_param) {
+       string  year_name;
+       int     size;
+       Data    *ret_val;
+       Date    date;
+       int     val_int;
 
        ret_val = NULL;
-       if (year_vector.size() > 0) {
+       size    = year_name_vector_param.size();
+       if (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);
-                       // 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);
-                               ret_val = store->get_oldest_data();
-                               delete(store);
-                               break;
-                       }
-               }
+               year_name       = year_name_vector_param.at(size - 1);
+               string_to_number<int>(val_int, year_name, dec);
+               date.year       = val_int;
+               ret_val         = StoreCache::get_newest_data(&date, device_id, PERIOD_YEARLY);
        }
        return ret_val;
 }
 
-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          f_name;
-       Data            *ret_val;
-       int             size;
-       W1Store         *store;
+Data *DeviceData::find_oldest_data(vector<string> year_name_vector_param) {
+       int     size;
+       string  year_name;
+       Data    *ret_val;
+       Date    date;
+       int     val_int;
 
        ret_val = NULL;
-       size    = year_vector.size();
+       size    = year_name_vector_param.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);
-                       // scan data files from month dir
-                       data_vector     = W1Util::get_data_files(month_dir);
-                       size    = data_vector.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);
-                               ret_val = store->get_newest_data();
-                               delete(store);
-                               break;
-                       }
-               }
+               year_name       = year_name_vector_param.at(0);
+               string_to_number<int>(val_int, year_name, dec);
+               date.year       = val_int;
+               ret_val         = StoreCache::get_oldest_data(&date, device_id, PERIOD_YEARLY);
        }
        return ret_val;
 }
 
 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,54 +150,214 @@ long int get_interval_type(Date *start_date,
        return ret_val;
 }
 
-Data *DeviceData::get_daily_summary(Date *date) {
-       Data    *ret_val;
-       W1Store *store;
+DataRange *DeviceData::get_summary(Date *date_param,
+                               EnumSummaryCalculationType calc_type_param,
+                               EnumSummaryPeriod period_type_param) {
+       DataRange       *ret_val;
+       StoreCache      *store;
 
-       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();
-                       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;
+       ret_val = NULL;
+       //store = new StoreDay(device_id, date_param);
+       store   = new StoreCache(device_id, date_param);
+       if (store != NULL) {
+               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\n", device_id.c_str());
+               }
+       }
+       else {
+               log_error("Could not read data log for device: %s\n", device_id.c_str());
        }
-       ret_val->printout();
        delete(store);
+       return ret_val;
+}
+
+DataRange *DeviceData::get_yearly_summary(Date *date,
+                               EnumSummaryCalculationType calc_type_param) {
+       return get_summary(date, calc_type_param, PERIOD_YEARLY);
+}
+
+DataRange *DeviceData::get_yearly_summary(Date *date) {
+       DataRange       *ret_val;
+
+       if (device_config == NULL) {
+               device_config           = Factory::get_device_config(device_id);
+               summary_calc_type       = device_config->get_summary_calculation_type();
+       }
+       ret_val = get_yearly_summary(date, summary_calc_type);
+       return ret_val;
+}
 
+DataRange *DeviceData::get_yearly_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_year(end_date)) {
+               data    = get_yearly_summary(date);
+               if (data != NULL) {
+                       if (ret_val == NULL) {
+                               ret_val = new DataRange(data);
+                       }
+                       else {
+                               ret_val->add(data);
+                       }
+                       delete(data);
+               }
+               date->next_year();
+       }
+       delete(date);
+       return ret_val;
+}
+
+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;
+
+       if (device_config == NULL) {
+               device_config           = Factory::get_device_config(device_id);
+               summary_calc_type       = device_config->get_summary_calculation_type();
+       }
+       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;
+       DataRange       *data;
+       Date            *date;
+
+       ret_val = NULL;
+       date    = start_date->clone();
+       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);
+                       }
+                       else {
+                               ret_val->add(data);
+                       }
+                       delete(data);
+               }
+               date->next_month();
+       }
+       delete(date);
+       return ret_val;
+}
+
+DataRange *DeviceData::get_daily_summary(Date *date,
+                               EnumSummaryCalculationType calc_type_param) {
+       return get_summary(date, calc_type_param, PERIOD_DAILY);
+}
+
+DataRange *DeviceData::get_daily_summary(Date *date) {
+       DataRange       *ret_val;
+
+       if (device_config == NULL) {
+               device_config           = Factory::get_device_config(device_id);
+               summary_calc_type       = device_config->get_summary_calculation_type();
+       }
+       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;
-       Data            *data;
+       DataRange       *data;
        Date            *date;
 
        ret_val = NULL;
        date    = start_date->clone();
-       while(date->before(*end_date)) {
+       while(date->before_or_equal_day(end_date)) {
                data    = get_daily_summary(date);
-               if (ret_val == NULL) {
-                       ret_val = new DataRange(*data);
+               if (data != NULL) {
+                       if (ret_val == NULL) {
+                               ret_val = new DataRange(data);
+                       }
+                       else {
+                               ret_val->add(data);
+                       }
+                       delete(data);
                }
-               else {
-                       ret_val->add_data(*data);
+               date->next_day();
+       }
+       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;
+
+       if (device_config == NULL) {
+               device_config           = Factory::get_device_config(device_id);
+               summary_calc_type       = device_config->get_summary_calculation_type();
+       }
+       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;
+       DataRange       *dta_lst;
+       Data            *data;
+       Date            *date;
+       int             cnt;
+       int             ii;
+
+       ret_val = NULL;
+       date    = start_date->clone();
+       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);
+                               }
+                               else {
+                                       ret_val->add(data);
+                               }
+                               delete(data);
+                       }
                }
-               delete(data);
-               date->tomorrow();
+               date->next_day();
        }
        delete(date);
        return ret_val;
@@ -224,31 +365,35 @@ DataRange *DeviceData::get_daily_summary(Date *start_date,
 
 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_yearly_summary(start_date, end_date);
                        break;
-               case 1:
+               case PERIOD_MONTHLY:
                        log_debug("get monthly summary\n");
+                       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;
        }