]> pilppa.com Git - lib1wire.git/commitdiff
Implemented yearly calculations for min, max, mean, delta and sum
authorMika Laitio <lamikr@pilppa.org>
Wed, 26 Jan 2011 00:33:37 +0000 (02:33 +0200)
committerMika Laitio <lamikr@pilppa.org>
Wed, 26 Jan 2011 00:33:37 +0000 (02:33 +0200)
functions with cache support.

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

index d13287c3c087796da6a654b7e14090a4071f1211..790bcaef7140cbbc1abf1e186cc81f87ec7dc3a4 100644 (file)
@@ -25,86 +25,111 @@ using namespace plp;
 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);
+       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_dr;
+Data *DeviceData::find_newest_data(string year_name_param) {
+       int             ii;
        string          mon_dr;
        vector<string>  mon_vcr;
-       vector<string>  dta_vcr;
+       vector<string>  d_vcr;
        string          f_name;
        StoreDay        *store;
        Data            *ret_val;
+       string          year_dr;
+       int             size;
 
        ret_val = NULL;
-       if (year_vector.size() > 0) {
-               // dirs are alphabetically sorted
-               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
-                       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;
-                       }
+       year_dr = W1Util::concat_paths(device_dir, year_name_param);
+       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
+               d_vcr   = W1Util::get_data_files(mon_dr);
+               size    = d_vcr.size();
+               if (size > 0) {
+                       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;
                }
        }
        return ret_val;
 }
 
-Data *DeviceData::find_newest_data(vector<string> year_vector) {
-       int             ii;
+Data *DeviceData::find_newest_data(vector<string> year_name_vector_param) {
+       string  year_name;
+       int     size;
+       Data    *ret_val;
+
+       ret_val = NULL;
+       size    = year_name_vector_param.size();
+       if (size > 0) {
+               // dirs are alphabetically sorted
+               year_name       = year_name_vector_param.at(size - 1);
+               ret_val         = find_newest_data(year_name);
+       }
+       return ret_val;
+}
+
+Data *DeviceData::find_oldest_data(string year_name_param) {
+       int             size;
+       unsigned int    ii;
        string          year_dr;
        string          mon_dr;
        vector<string>  mon_vcr;
-       vector<string>  d_vcr;
+       vector<string>  dta_vcr;
        string          f_name;
+       StoreDay        *store;
        Data            *ret_val;
-       int             size;
-       StoreDay                *store;
 
        ret_val = NULL;
-       size    = year_vector.size();
+       year_dr = W1Util::concat_paths(device_dir, year_name_param);
+       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;
+               }
+       }
+       return ret_val;
+}
+
+Data *DeviceData::find_oldest_data(vector<string> year_name_vector_param) {
+       int     size;
+       string  year_name;
+       Data    *ret_val;
+
+       ret_val = NULL;
+       size    = year_name_vector_param.size();
        if (size > 0) {
                // dirs are alphabetically sorted
-               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
-                       d_vcr   = W1Util::get_data_files(mon_dr);
-                       size    = d_vcr.size();
-                       if (size > 0) {
-                               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;
-                       }
-               }
+               year_name       = year_name_vector_param.at(0);
+               ret_val         = find_oldest_data(year_name);
        }
        return ret_val;
 }
@@ -212,6 +237,47 @@ DataRange *DeviceData::get_summary(Date *date_param,
        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);
@@ -220,6 +286,10 @@ DataRange *DeviceData::get_monthly_summary(Date *date,
 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;
 }
@@ -257,6 +327,10 @@ DataRange *DeviceData::get_daily_summary(Date *date,
 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;
 }
@@ -294,6 +368,10 @@ DataRange *DeviceData::get_hourly_summary(Date *date,
 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;
 }
@@ -342,7 +420,7 @@ 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_monthly_summary(start_date, end_date);
+                       ret_val = get_yearly_summary(start_date, end_date);
                        break;
                case PERIOD_MONTHLY:
                        log_debug("get monthly summary\n");
index 367e00f0b1195ba484e7c4b1c78c67268d22c4c3..a85bf1476c739f7db65e7ca147be6668b7050401 100644 (file)
 
 #include <time.h>
 
-namespace w1 {
+namespace plp {
        class DeviceData {
                public:
                        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.
+                        * Get monthly summary data.
+                        * Depending from the device type, it may be daily mean value, daily delta, highest value, etc...
+                        */
+                       plp::DataRange *get_yearly_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
+                       plp::DataRange *get_yearly_summary(plp::Date *date);
+                       plp::DataRange *get_yearly_summary(plp::Date *start_date, plp::Date *end_date);
+                       /**
+                        * Get monthly summary data.
                         * Depending from the device type, it may be daily mean value, daily delta, highest value, etc...
                         */
+                       plp::DataRange *get_monthly_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
                        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.
+                        * Get daily summary data calculated from the daily data items.
                         * Depending from the device type, it may be daily mean value, daily delta, highest value, etc...
                         */
+                       plp::DataRange *get_daily_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
                        plp::DataRange *get_daily_summary(plp::Date *date);
                        plp::DataRange *get_daily_summary(plp::Date *start_date, plp::Date *end_date);
                        plp::DataRange *get_hourly_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
                        plp::DataRange *get_hourly_summary(plp::Date *date);
                        plp::DataRange *get_hourly_summary(plp::Date *start_date, plp::Date *end_date);
                        plp::DataRange *get_data(plp::Date *start_date, plp::Date *end_date);
+                       plp::Data *find_newest_data(std::string year_name_param);
+                       plp::Data *find_oldest_data(std::string year_name_param);
                protected:
                        std::string                     device_id;
                        std::string                     device_dir;
index b10c3eaa46342ec9e5d3def1b9fe2fbd16f9a866..76287eb17f553ad89f4a7b24c47fb683c6fa75df 100644 (file)
@@ -18,6 +18,7 @@
 #include "W1Util.hh"
 
 using namespace w1;
+using namespace plp;
 using namespace std;
 
 template <class NumberDataType>
index 415ce9584717c7cb23194fbaeb06c9c1b80deac3..6e2beb2b337617b21fbf80c1a1608df8468e2933 100644 (file)
@@ -27,7 +27,7 @@ namespace w1 {
                        virtual ~Factory();
                        static W1Device *get_device(int family_code, std::string device_id, dirent *direntry_param);
                        static std::list<W1Device *> get_device_list();
-                       static std::list<DeviceData *> get_device_data_list();
+                       static std::list<plp::DeviceData *> get_device_data_list();
                        static DeviceConfig *get_device_config(std::string device_id);
                private:
                        //int parse_family_code(std::string folder_name);
index 288f7f2fec80c7e9507769aff2d463a93adf15f9..a33465da51bd650d8b9d8b4801903729d90550c3 100644 (file)
@@ -72,8 +72,5 @@ bool Store::load(string fname_param) {
                        log_error("Could not open data file: %s\n", fname_param.c_str());
                }
        }
-       else {
-               log_warning("Could not open data file, file does not exist or user does not have read access for it: %s\n", fname_param.c_str());
-       }
        return ret_val;
 }
index 51252ad322d2f6702df8321630f774f64b6c4481..02ce3a7226a83a0b18168be46d2cf14610e44665 100644 (file)
 #include <string>
 #include "Data.hh"
 #include "Date.hh"
+#include "W1Configure.hh"
 
 namespace plp {
        class Store {
                public:
                        Store(std::string device_id, plp::Date *date_time);
                        virtual ~Store();
+                       virtual plp::DataRange *get_sum(EnumSummaryPeriod period_type_param) = 0;
+                       virtual plp::DataRange *get_mean(EnumSummaryPeriod period_type_param) = 0;
+                       virtual plp::DataRange *get_delta(EnumSummaryPeriod period_type_param) = 0;
+                       virtual plp::DataRange *get_max(EnumSummaryPeriod period_type_param) = 0;
+                       virtual plp::DataRange *get_min(EnumSummaryPeriod period_type_param) = 0;
                protected:
                        std::string     device_id;
                        plp::Date       *date;
index 3fc9c4b0aad1a95fcd23d1b160ac6855cbaa04d5..b444f32f90f7ed41228a24dc86fa26f1978d387b 100644 (file)
@@ -10,6 +10,7 @@
 #include "StoreCache.hh"
 #include "StoreDay.hh"
 #include "DeviceConfig.hh"
+#include "DeviceData.hh"
 #include "W1Util.hh"
 
 using namespace std;
@@ -17,7 +18,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() {
@@ -84,7 +85,6 @@ DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
        DataRange       *ret_val;
        Data            *dta;
        string          fname;
-       StoreDay        *store;
 
        ret_val = NULL;
        fname   = get_file_name(device_id,
@@ -108,8 +108,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 +118,37 @@ 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();
+                                       if (period_type_param == PERIOD_YEARLY) {
+                                               max_date->next_year();
+                                       }
+                                       else {
+                                               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 (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 +158,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 +181,14 @@ 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);
+                                       save(fname, ret_val, 4);
+                                       delete(store);
+                               }
                                break;
                }
        }
@@ -180,7 +200,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 +224,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;
@@ -217,13 +236,24 @@ DataRange *StoreCache::get_sum(EnumSummaryPeriod period_type_param) {
 
                                        cur_date        = date->clone();
                                        max_date        = date->clone();
-                                       max_date->next_month();
+                                       if (period_type_param == PERIOD_YEARLY) {
+                                               max_date->next_year();
+                                       }
+                                       else {
+                                               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 (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 +272,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)) {
@@ -273,7 +308,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 +332,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_year_oldest_data();
+                                       if (first_data != NULL) {
+                                               last_data       = get_year_newest_data();
+                                               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 +367,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;
@@ -372,7 +431,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 +455,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;
@@ -409,13 +467,24 @@ DataRange *StoreCache::get_max(EnumSummaryPeriod period_type_param) {
 
                                        cur_date        = date->clone();
                                        max_date        = date->clone();
-                                       max_date->next_month();
+                                       if (period_type_param == PERIOD_YEARLY) {
+                                               max_date->next_year();
+                                       }
+                                       else {
+                                               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 (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 +513,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)) {
@@ -475,7 +549,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 +573,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;
@@ -512,13 +585,24 @@ DataRange *StoreCache::get_min(EnumSummaryPeriod period_type_param) {
 
                                        cur_date        = date->clone();
                                        max_date        = date->clone();
-                                       max_date->next_month();
+                                       if (period_type_param == PERIOD_YEARLY) {
+                                               max_date->next_year();
+                                       }
+                                       else {
+                                               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 (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 +631,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)) {
@@ -582,7 +671,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 +695,37 @@ void StoreCache::save(std::string fname_param, plp::DataRange *datarange_param,
                delete(ostream);
        }
 }
+
+/**
+ * Find oldest data in certain year
+ */
+Data *StoreCache::get_year_oldest_data() {
+       Data            *ret_val;
+       char            buffer[30];
+       DeviceData      *dta_finder;
+       string          str;
+
+       dta_finder      = new DeviceData(device_id);
+       snprintf(buffer, 30, "%d", date->year);
+       str.append(buffer);
+       ret_val         = dta_finder->find_oldest_data(str);
+       delete(dta_finder);
+       return ret_val;
+}
+
+/**
+ * Find newest data in certain year
+ */
+Data *StoreCache::get_year_newest_data() {
+       Data            *ret_val;
+       char            buffer[30];
+       DeviceData      *dta_finder;
+       string          str;
+
+       dta_finder      = new DeviceData(device_id);
+       snprintf(buffer, 30, "%d", date->year);
+       str.append(buffer);
+       ret_val         = dta_finder->find_newest_data(str);
+       delete(dta_finder);
+       return ret_val;
+}
index dadd6b261f8873b58e59d40270cc57f3987f29d4..fdf3fe028a8391853e1ecdbedffb84b5d1dc6710 100644 (file)
@@ -28,6 +28,8 @@ namespace plp {
                        plp::DataRange *get_max(EnumSummaryPeriod period_type_param);
                        plp::DataRange *get_min(EnumSummaryPeriod period_type_param);
                protected:
+                       plp::Data *get_year_oldest_data();
+                       plp::Data *get_year_newest_data();
                        static std::string get_dir_name(std::string device_id_param,
                                                Date *date_time_param,
                                                EnumSummaryPeriod period_type_param,
index fb5c1757f3b8f20575b49d21478c3b494d29a832..089f8846156cbd98bcde77b8d8ecae812b47d91d 100644 (file)
@@ -520,16 +520,16 @@ DataRange *StoreDay::get_oldest_and_newest_data(string fname_param) {
        if (store_data != NULL) {
                row_count       = store_data->get_count();
                if (row_count > 0) {
-                       o_data          = store_data->get(0);
-                       n_data          = store_data->get(row_count - 1);
+                       o_data  = store_data->get(0);
+                       n_data  = store_data->get(row_count - 1);
                }
        }
        else {
                if (range_data != NULL) {
                        row_count       = range_data->get_count();
                        if (row_count > 0) {
-                               o_data          = range_data->get(0);
-                               n_data          = range_data->get(row_count - 1);
+                               o_data  = range_data->get(0);
+                               n_data  = range_data->get(row_count - 1);
                        }
                }
                else {