]> pilppa.org Git - libplp.git/blobdiff - src/DataReader.cc
Data read optimizations
[libplp.git] / src / DataReader.cc
index 1c7e9a36121bc85403f80d5552c4c74765c96fe7..c1dfb489c65ecd51b9388e616916f437e71f64e0 100644 (file)
@@ -34,12 +34,14 @@ bool string_to_number(NumberDataType& result,
 DataReader::DataReader(string device_id_param) {
        string  base_dir;
 
-       device_config   = NULL;
-       device_id       = device_id_param;
-       base_dir        = DeviceConfig::get_base_dir_name();
-       device_dir      = FileUtil::concat_paths(base_dir, device_id);
-       device_ch_dir   = FileUtil::concat_paths(base_dir, "cache");
-       device_ch_dir   = FileUtil::concat_paths(device_ch_dir, device_id);
+       log_debug("device_id: %s\n", device_id_param.c_str());
+       summary_calc_type       = MEAN;
+       device_config           = NULL;
+       device_id               = device_id_param;
+       base_dir                = DeviceConfig::get_base_dir_name();
+       device_dir              = FileUtil::concat_paths(base_dir, device_id);
+       device_ch_dir           = FileUtil::concat_paths(base_dir, "cache");
+       device_ch_dir           = FileUtil::concat_paths(device_ch_dir, device_id);
 }
 
 DataReader::~DataReader() {
@@ -74,7 +76,9 @@ Data *DataReader::get_latest_data() {
 
        ret_val = NULL;
        y_list  = FileUtil::get_subdirectories(device_dir);
-       ret_val = find_latest_data(y_list);
+       if (y_list.size() > 0) {
+               ret_val = find_latest_data(y_list);
+       }
        return ret_val;
 }
 
@@ -190,11 +194,15 @@ DataRange *DataReader::get_summary(Date *date_param,
                        ret_val->printout();
                }
                else {
-                       log_error("Could not read data log for device: %s\n", device_id.c_str());
+                       log_error("%s: Could not read data log for device %s. Data not found.\n",
+                               date_param->to_string().c_str(),
+                               device_id.c_str());
                }
        }
        else {
-               log_error("Could not read data log for device: %s\n", device_id.c_str());
+               log_error("%s: Could not read data log for device: %s. Memory allocation error..\n",
+                       date_param->to_string().c_str(),
+                       device_id.c_str());
        }
        delete(store);
        return ret_val;
@@ -225,15 +233,17 @@ DataRange *DataReader::get_yearly_summary(Date *start_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);
+               if (StoreDay::exist_in_year(device_id, date, false)) {
+                       data    = get_yearly_summary(date);
+                       if (data != NULL) {
+                               if (ret_val == NULL) {
+                                       ret_val = new DataRange(data);
+                               }
+                               else {
+                                       ret_val->add(data);
+                               }
+                               delete(data);
                        }
-                       delete(data);
                }
                date->next_year();
        }
@@ -266,22 +276,53 @@ DataRange *DataReader::get_monthly_summary(Date *start_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);
+               if (StoreDay::exist_in_month(device_id, date, false)) {
+                       data    = get_monthly_summary(date);
+                       if (data != NULL) {
+                               if (ret_val == NULL) {
+                                       ret_val = new DataRange(data);
+                               }
+                               else {
+                                       ret_val->add(data);
+                               }
+                               delete(data);
                        }
-                       delete(data);
+                       date->next_month();
                }
-               date->next_month();
        }
        delete(date);
        return ret_val;
 }
 
+/*
+ * Get Daily summary from the latest date
+ */
+DataRange *DataReader::get_daily_summary() {
+       if (device_config == NULL) {
+               device_config           = DeviceConfig::get_device_config(device_id);
+               summary_calc_type       = device_config->get_summary_calculation_type();
+       }
+       return get_daily_summary(summary_calc_type);
+}
+
+/*
+ * Get Daily summary specified by the calc_type from the latest date.
+ */
+DataRange *DataReader::get_daily_summary(EnumSummaryCalculationType calc_type_param) {
+       Data            *data;
+       Date            date;
+       DataRange       *ret_val;
+
+       ret_val = NULL;
+       data    = get_latest_data();
+       if (data != NULL) {
+               date    = data->get_date();
+               ret_val = get_daily_summary(&date, calc_type_param);
+               delete(data);
+       }
+       return ret_val;
+}
+
 DataRange *DataReader::get_daily_summary(Date *date,
                                EnumSummaryCalculationType calc_type_param) {
        return get_summary(date, calc_type_param, PERIOD_DAILY);
@@ -307,15 +348,17 @@ DataRange *DataReader::get_daily_summary(Date *start_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);
+               if (StoreDay::exist(device_id, date, false)) {
+                       data    = get_daily_summary(date);
+                       if (data != NULL) {
+                               if (ret_val == NULL) {
+                                       ret_val = new DataRange(data);
+                               }
+                               else {
+                                       ret_val->add(data);
+                               }
+                               delete(data);
                        }
-                       delete(data);
                }
                date->next_day();
        }
@@ -377,8 +420,6 @@ DataRange *DataReader::get_data(Date *start_date,
        EnumSummaryPeriod       period;
 
        ret_val = NULL;
-       start_date->printout();
-       end_date->printout();
        period  = get_period_type(start_date, end_date);
        switch(period) {
                case PERIOD_YEARLY:
@@ -413,14 +454,20 @@ string DataReader::get_device_id() {
 }
 
 /**
- * Read device type from the device specific config file
+ * Read device type from the device specific config file.
+ *
+ * @rerurn string representing device type.
+ * In the case of error, an empty string is returned.
  */
-string DataReader::get_device_type() {
-       string  ret_val;
+bool DataReader::get_device_type(string& type_param) {
+       bool    ret_val;
 
+       ret_val = false;
        if (device_config == NULL) {
                device_config   = DeviceConfig::get_device_config(device_id);
-               ret_val         = device_config->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
+       }
+       if (device_config != NULL) {
+               ret_val = device_config->get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, type_param);
        }
        return ret_val;
 }