]> pilppa.com Git - lib1wire.git/commitdiff
support for sum, min and max queryes from the collected data.
authorMika Laitio <lamikr@pilppa.org>
Thu, 6 Jan 2011 09:53:29 +0000 (11:53 +0200)
committerMika Laitio <lamikr@pilppa.org>
Thu, 6 Jan 2011 09:53:29 +0000 (11:53 +0200)
Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/DeviceData.cc
src/DeviceData.hh
src/W1Store.cc
src/W1Store.hh

index 5f95966edae83aa8b5156ab58289bd49bd8ded33..cfb29a7b8576cc4559c13e437fe1a492bea69fbf 100644 (file)
@@ -22,7 +22,7 @@ using namespace std;
 using namespace plp;
 
 DeviceData::DeviceData(string device_id_param) {
-       string          base_dir;
+       string  base_dir;
 
        device_config           = Factory::get_device_config(device_id_param);
        summary_calc_type       = device_config->get_summary_calculation_type();
@@ -42,7 +42,7 @@ Data *DeviceData::find_oldest_data(vector<string> year_vector) {
        string          year_dr;
        string          mon_dr;
        vector<string>  mon_vcr;
-       vector<string>  d_vcr;
+       vector<string>  dta_vcr;
        string          f_name;
        W1Store         *store;
        Data            *ret_val;
@@ -57,9 +57,9 @@ Data *DeviceData::find_oldest_data(vector<string> year_vector) {
                        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);
-                       if (d_vcr.size() > 0) {
-                               f_name  = d_vcr.at(0);
+                       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 W1Store(f_name);
                                ret_val = store->get_oldest_data();
@@ -169,7 +169,8 @@ long int get_interval_type(Date *start_date,
        return ret_val;
 }
 
-Data *DeviceData::get_day_summary(Date *date) {
+Data *DeviceData::get_daily_summary(Date *date,
+                               int calc_type) {
        Data    *ret_val;
        W1Store *store;
        bool    suc_flg;
@@ -179,7 +180,7 @@ Data *DeviceData::get_day_summary(Date *date) {
        if (store != NULL) {
                suc_flg = store->load();
                if (suc_flg == true) {
-                       switch(summary_calc_type) {
+                       switch(calc_type) {
                                case SUM:
                                        ret_val = store->get_sum();
                                        break;
@@ -215,6 +216,13 @@ Data *DeviceData::get_day_summary(Date *date) {
        return ret_val;
 }
 
+Data *DeviceData::get_daily_summary(Date *date) {
+       Data    *ret_val;
+
+       ret_val = get_daily_summary(date, summary_calc_type);
+       return ret_val;
+}
+
 DataRange *DeviceData::get_daily_summary(Date *start_date,
                                        Date *end_date) {
        DataRange       *ret_val;
@@ -224,7 +232,7 @@ DataRange *DeviceData::get_daily_summary(Date *start_date,
        ret_val = NULL;
        date    = start_date->clone();
        while(date->before(*end_date)) {
-               data    = get_day_summary(date);
+               data    = get_daily_summary(date);
                if (data != NULL) {
                        if (ret_val == NULL) {
                                ret_val = new DataRange(data);
@@ -240,18 +248,18 @@ DataRange *DeviceData::get_daily_summary(Date *start_date,
        return ret_val;
 }
 
-vector<Data *> DeviceData::get_hourly_summary(Date *date) {
-       vector<Data *>  ret_val;
-       W1Store *       store;
+vector<Data *> *DeviceData::get_hourly_summary(Date *date,
+                                       int calc_type) {
+       vector<Data *>  *ret_val;
+       W1Store         *store;
 
+       ret_val = NULL;
        store   = new W1Store(device_id, date);
        store->load();
-       switch(summary_calc_type) {
-/*
+       switch(calc_type) {
                case SUM:
-                       ret_val = store->get_sum();
+                       ret_val = store->get_sum(3600);
                        break;
-*/
                case DELTA:
                        ret_val = store->get_delta(3600);
                        break;
@@ -259,23 +267,28 @@ vector<Data *> DeviceData::get_hourly_summary(Date *date) {
                default:
                        ret_val = store->get_mean(3600);
                        break;
-/*
                case MAX:
-                       ret_val = store->get_max();
+                       ret_val = store->get_max(3600);
                        break;
                case MIN:
-                       ret_val = store->get_min();
+                       ret_val = store->get_min(3600);
                        break;
-*/
        }
        delete(store);
        return ret_val;
 }
 
+vector<Data *> *DeviceData::get_hourly_summary(Date *date) {
+       vector<Data *>  *ret_val;
+
+       ret_val = get_hourly_summary(date, summary_calc_type);
+       return ret_val;
+}
+
 DataRange *DeviceData::get_hourly_summary(Date *start_date,
                                        Date *end_date) {
        DataRange                       *ret_val;
-       vector<Data *>                  dta_list;
+       vector<Data *>                  *dta_lst;
        Data                            *data;
        Date                            *date;
        vector<Data *>::iterator        iter;
@@ -283,8 +296,8 @@ DataRange *DeviceData::get_hourly_summary(Date *start_date,
        ret_val = NULL;
        date    = start_date->clone();
        while(date->before(*end_date)) {
-               dta_list        = get_hourly_summary(date);
-               for(iter = dta_list.begin(); iter != dta_list.end(); iter++) {
+               dta_lst = get_hourly_summary(date);
+               for(iter = dta_lst->begin(); iter != dta_lst->end(); iter++) {
                        data    = (Data *)*iter;
                        if (data != NULL) {
                                if (ret_val == NULL) {
@@ -301,6 +314,7 @@ DataRange *DeviceData::get_hourly_summary(Date *start_date,
        delete(date);
        return ret_val;
 }
+
 DataRange *DeviceData::get_data(Date *start_date,
                                Date *end_date) {
        DataRange       *ret_val;
index a4c06ac8ef5d3eb8c98caa23f8a7d0aabc66d466..e3b3727d482e08bd737d195dc91459780476858d 100644 (file)
@@ -23,13 +23,15 @@ namespace w1 {
                        DeviceData(std::string device_id);
                        virtual ~DeviceData();
                        DataRange *get_data_range();
+                       Data *get_daily_summary(plp::Date *date, int 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...
                         */
-                       Data *get_day_summary(plp::Date *date);
+                       Data *get_daily_summary(plp::Date *date);
                        DataRange *get_daily_summary(plp::Date *start_date, plp::Date *end_date);
-                       std::vector<Data *> get_hourly_summary(plp::Date *date);
+                       std::vector<Data *> *get_hourly_summary(plp::Date *date, int calc_type);
+                       std::vector<Data *> *get_hourly_summary(plp::Date *date);
                        DataRange *get_hourly_summary(plp::Date *start_date, plp::Date *end_date);
                        DataRange *get_data(plp::Date *start_date, plp::Date *end_date);
                protected:
index 83e16150fdb388565f8c5c77a3b53647210eeb88..effea2e27e47df37adcb3ecd8da623444542ca59 100644 (file)
@@ -367,7 +367,74 @@ Data *W1Store::get_min() {
        return ret_val;
 }
 
-vector<Data *> W1Store::get_mean(int freq_sec) {
+vector<Data *> *W1Store::get_sum(int freq_sec) {
+       int             row_count;
+       int             col_count;
+       int             jj;
+       int             ii;
+       Data            *data;
+       Data            *calc;
+       Date            *limit_d;
+       Date            date;
+       vector<Data *>  *ret_val;
+
+       ret_val = new vector<Data *>();
+       calc    = NULL;
+       limit_d = NULL;
+       if (store_data == NULL) {
+               load();
+       }
+       if (store_data != NULL) {
+               row_count       = store_data->get_data_row_count();
+               if (row_count > 0) {
+                       col_count       = store_data->get_data_column_count();
+                       if (col_count > 0) {
+                               for (ii = 0; ii < row_count; ii++) {
+                                       data    = store_data->get_data(ii);
+                                       if (data != NULL) {
+                                               if (calc == NULL) {
+                                                       calc            = data->clone();
+                                                       limit_d         = data->get_date().clone();
+                                                       limit_d->min    = 0;
+                                                       limit_d->sec    = 0;
+                                                       limit_d->inc_seconds(freq_sec);
+                                               }
+                                               else {
+                                                       date    = data->get_date();
+                                                       if (date.before(*limit_d)) {
+                                                               for (jj = 0; jj < col_count; jj++) {
+                                                                       calc->value_arr[jj]     = calc->value_arr[jj] + data->value_arr[jj];
+                                                               }
+                                                       }
+                                                       else {
+                                                               ret_val->push_back(calc);
+                                                               calc            = data->clone();
+                                                               if (limit_d != NULL) {
+                                                                       delete(limit_d);
+                                                               }
+                                                               limit_d         = data->get_date().clone();
+                                                               limit_d->min    = 0;
+                                                               limit_d->sec    = 0;
+                                                               limit_d->inc_seconds(freq_sec);
+                                                       }
+                                               }
+                                               delete(data);
+                                       }
+                               }
+                               if (calc != NULL) {
+                                       delete(calc);
+                                       calc    = NULL;
+                               }
+                               if (limit_d != NULL) {
+                                       delete(limit_d);
+                               }
+                       }
+               }
+       }
+       return ret_val;
+}
+
+vector<Data *> *W1Store::get_mean(int freq_sec) {
        int             row_count;
        int             col_count;
        int             d_count;
@@ -377,8 +444,9 @@ vector<Data *> W1Store::get_mean(int freq_sec) {
        Data            *calc;
        Date            *limit_d;
        Date            date;
-       vector<Data *>  ret_val;
+       vector<Data *>  *ret_val;
 
+       ret_val = new vector<Data *>();
        calc    = NULL;
        limit_d = NULL;
        d_count = 1;
@@ -413,7 +481,7 @@ vector<Data *> W1Store::get_mean(int freq_sec) {
                                                                for (jj = 0; jj < col_count; jj++) {
                                                                        calc->value_arr[jj]     = calc->value_arr[jj] / d_count;
                                                                }
-                                                               ret_val.push_back(calc);
+                                                               ret_val->push_back(calc);
                                                                d_count         = 1;
                                                                calc            = data->clone();
                                                                if (limit_d != NULL) {
@@ -441,7 +509,7 @@ vector<Data *> W1Store::get_mean(int freq_sec) {
        return ret_val;
 }
 
-vector<Data *> W1Store::get_delta(int freq_sec) {
+vector<Data *> *W1Store::get_delta(int freq_sec) {
        int             row_count;
        int             col_count;
        int             jj;
@@ -451,8 +519,9 @@ vector<Data *> W1Store::get_delta(int freq_sec) {
        Data            *calc2;
        Date            *limit_d;
        Date            date;
-       vector<Data *>  ret_val;
+       vector<Data *>  *ret_val;
 
+       ret_val = new vector<Data *>();
        calc1   = NULL;
        calc2   = NULL;
        limit_d = NULL;
@@ -493,7 +562,7 @@ vector<Data *> W1Store::get_delta(int freq_sec) {
                                                                for (jj = 0; jj < col_count; jj++) {
                                                                        calc2->value_arr[jj]    = calc2->value_arr[jj] - calc1->value_arr[jj];
                                                                }
-                                                               ret_val.push_back(calc2);
+                                                               ret_val->push_back(calc2);
                                                                delete(calc1);
                                                                calc1   = data->clone();
                                                                calc2   = NULL; // do not delete calc2 as it's stored to array
@@ -526,6 +595,98 @@ vector<Data *> W1Store::get_delta(int freq_sec) {
        return ret_val;
 }
 
+vector<Data *> *W1Store::get_max_or_min(int freq_sec, bool max) {
+       int             row_count;
+       int             col_count;
+       int             jj;
+       int             ii;
+       Data            *data;
+       Data            *calc;
+       Date            *limit_d;
+       Date            date;
+       vector<Data *>  *ret_val;
+
+       ret_val = new vector<Data *>();
+       calc    = NULL;
+       limit_d = NULL;
+       if (store_data == NULL) {
+               load();
+       }
+       if (store_data != NULL) {
+               row_count       = store_data->get_data_row_count();
+               if (row_count > 0) {
+                       col_count       = store_data->get_data_column_count();
+                       if (col_count > 0) {
+                               for (ii = 0; ii < row_count; ii++) {
+                                       data    = store_data->get_data(ii);
+                                       if (data != NULL) {
+                                               if (calc == NULL) {
+                                                       calc            = data->clone();
+                                                       limit_d         = data->get_date().clone();
+                                                       limit_d->min    = 0;
+                                                       limit_d->sec    = 0;
+                                                       limit_d->inc_seconds(freq_sec);
+                                               }
+                                               else {
+                                                       date    = data->get_date();
+                                                       if (date.before(*limit_d)) {
+                                                               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];
+                                                                               }
+                                                                       }
+                                                               }
+                                                               else {
+                                                                       for (jj = 0; jj < col_count; jj++) {
+                                                                               if (data->value_arr[jj] < calc->value_arr[jj]) {
+                                                                                       calc->value_arr[jj]     = data->value_arr[jj];
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                                       else {
+                                                               ret_val->push_back(calc);
+                                                               calc            = data->clone();
+                                                               if (limit_d != NULL) {
+                                                                       delete(limit_d);
+                                                               }
+                                                               limit_d         = data->get_date().clone();
+                                                               limit_d->min    = 0;
+                                                               limit_d->sec    = 0;
+                                                               limit_d->inc_seconds(freq_sec);
+                                                       }
+                                               }
+                                               delete(data);
+                                       }
+                               }
+                               if (calc != NULL) {
+                                       delete(calc);
+                                       calc    = NULL;
+                               }
+                               if (limit_d != NULL) {
+                                       delete(limit_d);
+                               }
+                       }
+               }
+       }
+       return ret_val;
+}
+
+vector<Data *> *W1Store::get_max(int freq_sec) {
+       vector<Data *>  *ret_val;
+
+       ret_val = get_max_or_min(freq_sec, true);
+       return ret_val;
+}
+
+vector<Data *> *W1Store::get_min(int freq_sec) {
+       vector<Data *>  *ret_val;
+
+       ret_val = get_max_or_min(freq_sec, false);
+       return ret_val;
+}
+
 DataRange *W1Store::get_oldest_and_newest_data() {
        DataRange       *ret_val;
        ifstream        in;
index 5f3dff94888e8d279d66e1e95de62ede509fa935..b4a6b1a1a6f4426d0f74971fbb9502d56a576858 100644 (file)
@@ -33,8 +33,11 @@ namespace w1 {
                        Data *get_mean();
                        Data *get_max();
                        Data *get_min();
-                       std::vector<w1::Data *> get_mean(int freq_sec);
-                       std::vector<w1::Data *> get_delta(int freq_sec);
+                       std::vector<w1::Data *> *get_sum(int freq_sec);
+                       std::vector<w1::Data *> *get_mean(int freq_sec);
+                       std::vector<w1::Data *> *get_delta(int freq_sec);
+                       std::vector<w1::Data *> *get_max(int freq_sec);
+                       std::vector<w1::Data *> *get_min(int freq_sec);
                        w1::Data *get_oldest_data();
                        w1::Data *get_newest_data();
                        w1::DataRange *get_oldest_and_newest_data();
@@ -42,6 +45,7 @@ namespace w1 {
                        std::string             store_file_name;
                        DataRange               *store_data;
                        DataRange               *range_data;
+                       std::vector<Data *> *get_max_or_min(int freq_sec, bool max);
        };
 }