]> pilppa.org Git - lib1wire.git/blobdiff - src/StoreCache.cc
Implemented yearly calculations for min, max, mean, delta and sum
[lib1wire.git] / src / StoreCache.cc
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;
+}