]> pilppa.com Git - lib1wire.git/commitdiff
Added support for cacheing monthly data.
authorMika Laitio <lamikr@pilppa.org>
Tue, 25 Jan 2011 21:26:38 +0000 (23:26 +0200)
committerMika Laitio <lamikr@pilppa.org>
Tue, 25 Jan 2011 21:26:38 +0000 (23:26 +0200)
Fixed various small errors in initial calculations.

Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/Data.cc
src/Data.hh
src/Date.cc
src/Date.hh
src/DeviceData.cc
src/DeviceData.hh
src/StoreCache.cc
src/StoreDay.cc
src/W1Util.cc

index 923def183ee6c553496022bc45eccb45c693434c..49dd5072f068913935315069d3d5a5f24a6de557 100644 (file)
@@ -195,6 +195,14 @@ string Data::to_string() {
        return to_string(3);
 }
 
+int Data::get_value_count() {
+       return value_arr.size();
+}
+
+double Data::get(int indx) {
+       return value_arr[indx];
+}
+
 DataRange::DataRange() {
        val_matrix      = NULL;
        column_count    = 0;
index fe819b53e3484474054ef4ce0bcd5a964326e7e0..07bea1aa4a5872fd4bfebe61741482b1911d7756 100644 (file)
@@ -31,11 +31,13 @@ namespace plp {
                        static Data *parse_string(const std::string& data_str);
                        plp::Date get_date();
                        void set_date(plp::Date *date_param);
+                       int get_value_count();
+                       double get(int indx);
                        std::string get_unit();
                        std::valarray<double>   value_arr;
                private:
-                       plp::Date               date_time;
-                       std::string             unit;
+                       plp::Date       date_time;
+                       std::string     unit;
        };
 
        class DataRange {
index 86b2cdc82a73bd28179ef36d8e9532f6dee9b0f7..e45b544fc6073d40ba431f2c6eeb0d1693cc4edf 100644 (file)
@@ -136,7 +136,7 @@ bool Date::before_or_equal_month(Date *date2) {
        string  s2;
 
        ret_val = false;
-       if (this->year < date2->month) {
+       if (this->year < date2->year) {
                ret_val = true;
        }
        else {
@@ -212,7 +212,20 @@ bool Date::before_or_equal(Date *date2) {
        return ret_val;
 }
 
-void Date::tomorrow() {
+void Date::next_hour() {
+       sec     = 0;
+       min     = 0;
+       hour++;
+       if (hour < 0) {
+               hour    = 0;
+       }
+       if (hour >= 24) {
+               hour    = 0;
+               next_day();
+       }
+}
+
+void Date::next_day() {
        if ((month > 0) &&
            (month <= 12)) {
                day++;
@@ -234,17 +247,27 @@ void Date::tomorrow() {
        }
 }
 
-void Date::next_hour() {
-       if ((hour >= 0) &&
-           (hour <= 24)) {
-               hour++;
-               if (hour > 24) {
-                       hour    = 0;
-                       tomorrow();
-               }
+void Date::next_month() {
+       sec     = 0;
+       min     = 0;
+       hour    = 0;
+       day     = 1;
+       month++;
+       if (month > 12) {
+               month   = 1;
+               year++;
        }
 }
 
+void Date::next_year() {
+       sec     = 0;
+       min     = 0;
+       hour    = 0;
+       day     = 1;
+       month   = 1;
+       year++;
+}
+
 void Date::inc_minutes(int minutes) {
        int     day_c;
        int     hour_c;
@@ -255,13 +278,13 @@ void Date::inc_minutes(int minutes) {
        hour_c  = minutes / 60;
        minutes = minutes - hour_c * 60;
        for (ii = 0; ii < day_c; ii++) {
-               tomorrow();
+               next_day();
        }
        for (ii = 0; ii < hour_c; ii++) {
                hour++;
                if (hour > 24) {
                        hour    = 0;
-                       tomorrow();
+                       next_day();
                }
        }
        min     = min + minutes;
@@ -270,7 +293,7 @@ void Date::inc_minutes(int minutes) {
                hour++;
                if (hour > 24) {
                        hour    = 0;
-                       tomorrow();
+                       next_day();
                }
        }
 }
index 2a1015ca83c707f837051d4a01f631263dde7dd6..1a578a4c96e1b77f8e0d6141a0106ef68adf558d 100644 (file)
@@ -23,8 +23,10 @@ namespace plp {
                        virtual ~Date();
                        void printout();
                        bool is_leap_year();
-                       void tomorrow();
                        void next_hour();
+                       void next_day();
+                       void next_month();
+                       void next_year();
                        void inc_minutes(int minutes);
                        void inc_seconds(int seconds);
                        Date *clone();
index d47eb005692ef6498ee0159a572bbc4976a00532..d13287c3c087796da6a654b7e14090a4071f1211 100644 (file)
@@ -212,6 +212,43 @@ DataRange *DeviceData::get_summary(Date *date_param,
        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;
+
+       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);
@@ -243,7 +280,7 @@ DataRange *DeviceData::get_daily_summary(Date *start_date,
                        }
                        delete(data);
                }
-               date->tomorrow();
+               date->next_day();
        }
        delete(date);
        return ret_val;
@@ -287,7 +324,7 @@ DataRange *DeviceData::get_hourly_summary(Date *start_date,
                                delete(data);
                        }
                }
-               date->tomorrow();
+               date->next_day();
        }
        delete(date);
        return ret_val;
@@ -305,11 +342,11 @@ DataRange *DeviceData::get_data(Date *start_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_daily_summary(start_date, end_date);
+                       ret_val = get_monthly_summary(start_date, end_date);
                        break;
                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 PERIOD_DAILY:
                        log_debug("get daily summary\n");
index 2c1641087814119a44197f4b0ce0294968ffc903..367e00f0b1195ba484e7c4b1c78c67268d22c4c3 100644 (file)
@@ -23,6 +23,13 @@ namespace w1 {
                        DeviceData(std::string device_id);
                        virtual ~DeviceData();
                        plp::DataRange *get_data_range();
+                       plp::DataRange *get_monthly_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
+                       /**
+                        * Get summary data calculated from the daily data items that is meaning full.
+                        * Depending from the device type, it may be daily mean value, daily delta, highest value, etc...
+                        */
+                       plp::DataRange *get_monthly_summary(plp::Date *date);
+                       plp::DataRange *get_monthly_summary(plp::Date *start_date, plp::Date *end_date);
                        plp::DataRange *get_daily_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
                        /**
                         * Get summary data calculated from the daily data items that is meaning full.
index 68a9f58dd5ed898dc4360b694a5bc835c4fc946e..3fc9c4b0aad1a95fcd23d1b160ac6855cbaa04d5 100644 (file)
@@ -18,12 +18,6 @@ using namespace w1;
 
 StoreCache::StoreCache(string device_id_param,
                Date *date_time_param): Store(device_id_param, date_time_param) {
-/*
-       store_fname     = get_file_name(device_id_param,
-                               date_time_param,
-                               period_type_param,
-                               calc_type_param);
-*/
 }
 
 StoreCache::~StoreCache() {
@@ -37,13 +31,22 @@ string StoreCache::get_dir_name(string device_id_param,
        char    buffer[30];
        string  bd_name;
 
-       snprintf(buffer, 30, "%d/%02d", date_time_param->year, date_time_param->month);
        bd_name = DeviceConfig::get_base_dir_name();
        bd_name = W1Util::concat_paths(bd_name, CACHE_DIR_NAME);
        bd_name = W1Util::concat_paths(bd_name, device_id_param);
        bd_name = W1Util::concat_paths(bd_name, SUMMARY_PERIOD_NAMES_ARRAY[period_type_param]);
        bd_name = W1Util::concat_paths(bd_name, CALCULATION_TYPE_NAMES_ARRAY[calc_type_param]);
-       ret_val = bd_name + "/" + buffer;
+       if (period_type_param == PERIOD_YEARLY) {
+               ret_val = bd_name;
+       }
+       else if (period_type_param == PERIOD_MONTHLY) {
+               snprintf(buffer, 30, "%d", date_time_param->year);
+               ret_val = bd_name + "/" + buffer;
+       }
+       else {
+               snprintf(buffer, 30, "%d/%02d", date_time_param->year, date_time_param->month);
+               ret_val = bd_name + "/" + buffer;
+       }
        return ret_val;
 }
 
@@ -55,10 +58,20 @@ string StoreCache::get_file_name(string device_id_param,
        string  fname;
        char    buffer[30];
 
-       snprintf(buffer, 30, "%d-%02d-%02d",
-               date_time_param->year,
-               date_time_param->month,
-               date_time_param->day);
+       if (period_type_param == PERIOD_YEARLY) {
+               snprintf(buffer, 30, "%d", date_time_param->year);
+       }
+       else if (period_type_param == PERIOD_MONTHLY) {
+               snprintf(buffer, 30, "%d-%02d",
+                       date_time_param->year,
+                       date_time_param->month);
+       }
+       else {
+               snprintf(buffer, 30, "%d-%02d-%02d",
+                       date_time_param->year,
+                       date_time_param->month,
+                       date_time_param->day);
+       }
        fname   = buffer;
        fname   = fname + DATAFILE_SUFFIX;
        ret_val = get_dir_name(device_id_param, date_time_param, period_type_param, calc_type_param);
@@ -93,10 +106,71 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
                }
        }
        if (ret_val == NULL) {
-               store   = new StoreDay(device_id, date);
-               ret_val = store->get_mean(period_type_param);
-               save(fname, ret_val, 4);
-               delete(store);
+               switch(period_type_param) {
+                       case PERIOD_YEARLY:
+                               break;
+                       case PERIOD_MONTHLY: {
+                                       Data            *cur_data;
+                                       Data            *res_data;
+                                       Date            *cur_date;
+                                       Date            *max_date;
+                                       int             ii;
+                                       int             cnt;
+                                       int             val_cnt;
+                                       DataRange       *dr;
+
+                                       cur_date        = date->clone();
+                                       max_date        = date->clone();
+                                       max_date->next_month();
+                                       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 (dr != NULL) {
+                                                       cur_data        = dr->get_first();
+                                                       if (cur_data != NULL) {
+                                                               cnt++;
+                                                               if (res_data == NULL) {
+                                                                       res_data        = cur_data;
+                                                               }
+                                                               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];
+                                                                       }
+                                                                       delete(cur_data);
+                                                               }
+                                                       }
+                                                       delete(dr);
+                                               }
+                                               delete(store);
+                                               cur_date->next_day();
+                                       }
+                                       if ((res_data != NULL) &&
+                                           (cnt > 0)) {
+                                               for (ii = 0; ii < val_cnt; ii++) {
+                                                       res_data->value_arr[ii] = res_data->value_arr[ii] / cnt;
+                                               }
+                                               ret_val = new DataRange(res_data);
+                                               save(fname, ret_val, 4);
+                                               delete(res_data);
+                                       }
+                                       delete(cur_date);
+                                       delete(max_date);
+                               }
+                               break;
+                       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);
+                               break;
+               }
        }
        return ret_val;
 }
@@ -112,7 +186,7 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
        fname   = get_file_name(device_id,
                                date,
                                period_type_param,
-                               MEAN);
+                               SUM);
        if (store_data == NULL) {
                if (access(fname.c_str(), R_OK) == 0) {
                        load(fname);
@@ -128,10 +202,68 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
                }
        }
        if (ret_val == NULL) {
-               store   = new StoreDay(device_id, date);
-               ret_val = store->get_sum(period_type_param);
-               save(fname, ret_val, 4);
-               delete(store);
+               switch(period_type_param) {
+                       case PERIOD_YEARLY:
+                               break;
+                       case PERIOD_MONTHLY: {
+                                       Data            *cur_data;
+                                       Data            *res_data;
+                                       Date            *cur_date;
+                                       Date            *max_date;
+                                       int             ii;
+                                       int             cnt;
+                                       int             val_cnt;
+                                       DataRange       *dr;
+
+                                       cur_date        = date->clone();
+                                       max_date        = date->clone();
+                                       max_date->next_month();
+                                       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 (dr != NULL) {
+                                                       cur_data        = dr->get_first();
+                                                       if (cur_data != NULL) {
+                                                               cnt++;
+                                                               if (res_data == NULL) {
+                                                                       res_data        = cur_data;
+                                                               }
+                                                               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];
+                                                                       }
+                                                                       delete(cur_data);
+                                                               }
+                                                       }
+                                                       delete(dr);
+                                               }
+                                               delete(store);
+                                               cur_date->next_day();
+                                       }
+                                       if ((res_data != NULL) &&
+                                           (cnt > 0)) {
+                                               ret_val = new DataRange(res_data);
+                                               save(fname, ret_val, 4);
+                                               delete(res_data);
+                                       }
+                                       delete(cur_date);
+                                       delete(max_date);
+                               }
+                               break;
+                       case PERIOD_DAILY:
+                       case PERIOD_HOURLY:
+                       case PERIOD_MINUTELY:
+                       case PERIOD_SECONDLY:
+                               store   = new StoreDay(device_id, date);
+                               ret_val = store->get_sum(period_type_param);
+                               save(fname, ret_val, 4);
+                               delete(store);
+                               break;
+               }
        }
        return ret_val;
 }
@@ -147,7 +279,7 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
        fname   = get_file_name(device_id,
                                date,
                                period_type_param,
-                               MEAN);
+                               DELTA);
        if (store_data == NULL) {
                if (access(fname.c_str(), R_OK) == 0) {
                        // read from cache file
@@ -164,10 +296,73 @@ DataRange *StoreCache::get_delta(EnumSummaryPeriod period_type_param) {
                }
        }
        if (ret_val == NULL) {
-               store   = new StoreDay(device_id, date);
-               ret_val = store->get_delta(period_type_param);
-               save(fname, ret_val, 4);
-               delete(store);
+               switch(period_type_param) {
+                       case PERIOD_YEARLY:
+                               break;
+                       case PERIOD_MONTHLY: {
+                                       Data    *first_data;
+                                       Data    *last_data;
+                                       Data    *cur_data;
+                                       Date    *cur_date;
+                                       Date    *limit_date;
+                                       int     ii;
+                                       int     cnt;
+
+                                       cur_date                = date->clone();
+                                       limit_date      = date->clone();
+                                       limit_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();
+                                                       if (cur_data != NULL) {
+                                                               first_data      = cur_data->clone();
+                                                               last_data       = cur_data->clone();
+                                                               delete(cur_data);
+                                                       }
+                                               }
+                                               cur_data        = store->get_newest_data();
+                                               if (cur_data != NULL) {
+                                                       if (last_data != NULL) {
+                                                               delete(last_data);
+                                                       }
+                                                       last_data       = cur_data;
+                                               }
+                                               delete(store);
+                                               cur_date->next_day();
+                                       }
+                                       delete(cur_date);
+                                       delete(limit_date);
+                                       if (first_data != NULL) {
+                                               if (last_data == NULL) {
+                                                       last_data       = first_data->clone();
+                                               }
+                                               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];
+                                               }
+                                               cur_date        = first_data->get_date().clone();
+                                               last_data->set_date(cur_date);
+                                               delete(cur_date);
+                                               ret_val = new DataRange(last_data);
+                                               delete(first_data);
+                                               delete(last_data);
+                                               save(fname, ret_val, 4);
+                                       }
+                               }
+                               break;
+                       case PERIOD_DAILY:
+                       case PERIOD_HOURLY:
+                       case PERIOD_MINUTELY:
+                       case PERIOD_SECONDLY:
+                               store   = new StoreDay(device_id, date);
+                               ret_val = store->get_delta(period_type_param);
+                               save(fname, ret_val, 4);
+                               delete(store);
+                               break;
+               }
        }
        return ret_val;
 }
@@ -183,7 +378,7 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
        fname   = get_file_name(device_id,
                                date,
                                period_type_param,
-                               MEAN);
+                               MAX);
        if (store_data == NULL) {
                if (access(fname.c_str(), R_OK) == 0) {
                        load(fname);
@@ -199,10 +394,78 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
                }
        }
        if (ret_val == NULL) {
-               store   = new StoreDay(device_id, date);
-               ret_val = store->get_max(period_type_param);
-               save(fname, ret_val, 4);
-               delete(store);
+               switch(period_type_param) {
+                       case PERIOD_YEARLY:
+                               break;
+                       case PERIOD_MONTHLY: {
+                                       Data            *cur_data;
+                                       Data            *res_data;
+                                       Date            *cur_date;
+                                       Date            *max_date;
+                                       int             ii;
+                                       int             cnt;
+                                       int             val_cnt;
+                                       DataRange       *dr;
+
+                                       cur_date        = date->clone();
+                                       max_date        = date->clone();
+                                       max_date->next_month();
+                                       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 (dr != NULL) {
+                                                       cur_data        = dr->get_first();
+                                                       if (cur_data != NULL) {
+                                                               cnt++;
+                                                               if (res_data == NULL) {
+                                                                       res_data        = cur_data;
+                                                               }
+                                                               else {
+                                                                       val_cnt = res_data->get_value_count();
+                                                                       int changed = 0;
+                                                                       for (ii = 0; ii < val_cnt; ii++) {
+                                                                               if (cur_data->value_arr[ii] > res_data->value_arr[ii]) {
+                                                                                       res_data->value_arr[ii] = cur_data->value_arr[ii];
+                                                                                       changed = 1;
+                                                                               }
+                                                                       }
+                                                                       if (changed == 1) {
+                                                                               Date new_date;
+
+                                                                               new_date        = cur_data->get_date();
+                                                                               res_data->set_date(&new_date);
+                                                                       }
+                                                                       delete(cur_data);
+                                                               }
+                                                       }
+                                                       delete(dr);
+                                               }
+                                               delete(store);
+                                               cur_date->next_day();
+                                       }
+                                       if ((res_data != NULL) &&
+                                           (cnt > 0)) {
+                                               ret_val = new DataRange(res_data);
+                                               save(fname, ret_val, 4);
+                                               delete(res_data);
+                                       }
+                                       delete(cur_date);
+                                       delete(max_date);
+                               }
+                               break;
+                       case PERIOD_DAILY:
+                       case PERIOD_HOURLY:
+                       case PERIOD_MINUTELY:
+                       case PERIOD_SECONDLY:
+                               store   = new StoreDay(device_id, date);
+                               ret_val = store->get_max(period_type_param);
+                               save(fname, ret_val, 4);
+                               delete(store);
+                               break;
+               }
        }
        return ret_val;
 }
@@ -218,7 +481,7 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
        fname   = get_file_name(device_id,
                                date,
                                period_type_param,
-                               MEAN);
+                               MIN);
        if (store_data == NULL) {
                if (access(fname.c_str(), R_OK) == 0) {
                        load(fname);
@@ -234,10 +497,78 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
                }
        }
        if (ret_val == NULL) {
-               store   = new StoreDay(device_id, date);
-               ret_val = store->get_min(period_type_param);
-               save(fname, ret_val, 4);
-               delete(store);
+               switch(period_type_param) {
+                       case PERIOD_YEARLY:
+                               break;
+                       case PERIOD_MONTHLY: {
+                                       Data            *cur_data;
+                                       Data            *res_data;
+                                       Date            *cur_date;
+                                       Date            *max_date;
+                                       int             ii;
+                                       int             cnt;
+                                       int             val_cnt;
+                                       DataRange       *dr;
+
+                                       cur_date        = date->clone();
+                                       max_date        = date->clone();
+                                       max_date->next_month();
+                                       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 (dr != NULL) {
+                                                       cur_data        = dr->get_first();
+                                                       if (cur_data != NULL) {
+                                                               cnt++;
+                                                               if (res_data == NULL) {
+                                                                       res_data        = cur_data;
+                                                               }
+                                                               else {
+                                                                       val_cnt = res_data->get_value_count();
+                                                                       int changed = 0;
+                                                                       for (ii = 0; ii < val_cnt; ii++) {
+                                                                               if (cur_data->value_arr[ii] < res_data->value_arr[ii]) {
+                                                                                       res_data->value_arr[ii] = cur_data->value_arr[ii];
+                                                                                       changed = 1;
+                                                                               }
+                                                                       }
+                                                                       if (changed == 1) {
+                                                                               Date new_date;
+
+                                                                               new_date        = cur_data->get_date();
+                                                                               res_data->set_date(&new_date);
+                                                                       }
+                                                                       delete(cur_data);
+                                                               }
+                                                       }
+                                                       delete(dr);
+                                               }
+                                               delete(store);
+                                               cur_date->next_day();
+                                       }
+                                       if ((res_data != NULL) &&
+                                           (cnt > 0)) {
+                                               ret_val = new DataRange(res_data);
+                                               save(fname, ret_val, 4);
+                                               delete(res_data);
+                                       }
+                                       delete(cur_date);
+                                       delete(max_date);
+                               }
+                               break;
+                       case PERIOD_DAILY:
+                       case PERIOD_HOURLY:
+                       case PERIOD_MINUTELY:
+                       case PERIOD_SECONDLY:
+                               store   = new StoreDay(device_id, date);
+                               ret_val = store->get_min(period_type_param);
+                               save(fname, ret_val, 4);
+                               delete(store);
+                               break;
+               }
        }
        return ret_val;
 }
index 8d35952f5a3321f60501172fdbdc40445f989b58..fb5c1757f3b8f20575b49d21478c3b494d29a832 100644 (file)
@@ -436,10 +436,12 @@ plp::DataRange *StoreDay::get_max_or_min(EnumSummaryPeriod period_type_param, bo
                                                        date    = data->get_date();
                                                        if ((ii <= (row_count -1)) &&
                                                            ((frq_sec == -1) || (date.before(limit_d)))) {
+                                                               int changed = 0;
                                                                if (max == true) {
                                                                        for (jj = 0; jj < col_count; jj++) {
                                                                                if (calc->value_arr[jj] < data->value_arr[jj]) {
                                                                                        calc->value_arr[jj]     = data->value_arr[jj];
+                                                                                       changed = 1;
                                                                                }
                                                                        }
                                                                }
@@ -447,9 +449,16 @@ plp::DataRange *StoreDay::get_max_or_min(EnumSummaryPeriod period_type_param, bo
                                                                        for (jj = 0; jj < col_count; jj++) {
                                                                                if (data->value_arr[jj] < calc->value_arr[jj]) {
                                                                                        calc->value_arr[jj]     = data->value_arr[jj];
+                                                                                       changed = 1;
                                                                                }
                                                                        }
                                                                }
+                                                               if (changed == 1) {
+                                                                       Date new_date;
+
+                                                                       new_date        = data->get_date();
+                                                                       calc->set_date(&new_date);
+                                                               }
                                                        }
                                                        if ((ii >= (row_count -1)) ||
                                                            ((frq_sec != -1) && (date.before(limit_d) == false))) {
@@ -525,17 +534,19 @@ DataRange *StoreDay::get_oldest_and_newest_data(string fname_param) {
                }
                else {
                        in.open(fname_param.c_str());
-                       while (in.eof() == false) {
-                               getline(in, line);
-                               if (line.empty() == false) {
-                                       if (o_data == NULL) {
-                                               o_data  = Data::parse_string(line);
+                       if (in.is_open() == true) {
+                               while (in.eof() == false) {
+                                       getline(in, line);
+                                       if (line.empty() == false) {
+                                               if (o_data == NULL) {
+                                                       o_data  = Data::parse_string(line);
+                                               }
+                                               prev_line       = line;
                                        }
-                                       prev_line       = line;
                                }
-                       }
-                       if (prev_line.empty() == false) {
-                               n_data  = Data::parse_string(prev_line);
+                               if (prev_line.empty() == false) {
+                                       n_data  = Data::parse_string(prev_line);
+                               }
                        }
                }
        }
index 452d7290758e7b1eee203e5d27e39c9a03a548a0..b375a352494933ccb26056cbffef57c71c0dde93 100644 (file)
@@ -78,9 +78,10 @@ bool W1Util::mkdirs(const char *path) {
                                *p = '\0';
                                // if dir does not exist, create it
                                if (access(path, F_OK) != 0) {
-                                       log_debug("trying to create directory: %s\n", path);
+                                       //log_debug("trying to create directory: %s\n", path);
                                        err_flg = mkdir(path, S_IRWXU);
                                        if (err_flg != 0) {
+                                               log_error("Could not create directory: %s\n", path);
                                                ret_val = false;
                                                break;
                                        }
@@ -91,9 +92,10 @@ bool W1Util::mkdirs(const char *path) {
                if (ret_val == true) {
                        // if dir does not exist, create it
                        if (access(path, F_OK) != 0) {
-                               log_debug("trying to create directory: %s\n", path);
+                               //log_debug("trying to create directory: %s\n", path);
                                err_flg = mkdir(path, S_IRWXU);
                                if (err_flg != 0) {
+                                       log_error("Could not create directory: %s\n", path);
                                        ret_val = false;
                                }
                        }