]> pilppa.com Git - lib1wire.git/commitdiff
add min,max,mean,sum and delta calculations for stored data.
authorMika Laitio <lamikr@pilppa.org>
Thu, 9 Dec 2010 21:55:27 +0000 (23:55 +0200)
committerMika Laitio <lamikr@pilppa.org>
Thu, 9 Dec 2010 21:55:27 +0000 (23:55 +0200)
add functions for calculating from the datarange the min, max, mean, sum
and delta values... Configuration options still missing to make this
really usefull.

Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/Data.cc
src/Data.hh
src/DeviceConfig.cc
src/DeviceConfig.hh
src/W1DataList.cc
src/W1DataList.hh
src/W1Store.cc
src/W1Store.hh

index ac7ed2d38c877416844e822030a0f0c0f649c923..98425c63d64d63b0506f47b4d2602765c1129cb0 100644 (file)
@@ -35,6 +35,15 @@ Data::Data(int size) {
        value_arr.resize(size);
 }
 
+Data::Data(int size, double default_value) {
+       int ii;
+
+       value_arr.resize(size);
+       for (ii = 0; ii < size; ii++) {
+               value_arr[ii]   = default_value;
+       }
+}
+
 Data::Data(vector<double> vector_param, Date *date_param) {
        int     ii;
        int     size;
index 14ab90e0d38276c35c37cd8edf8e1ae6ae31e7c7..3907a9b384a6631f963bef88dc60ed3112b8c404 100644 (file)
@@ -19,6 +19,7 @@ namespace w1 {
        class Data {
                public:
                        Data(int size);
+                       Data(int size, double default_value);
                        Data(std::vector<double> vector_param, plp::Date *date_param);
                        Data(std::valarray<double> value_arr_param, plp::Date *date_param);
                        virtual ~Data();
index 1ecf0bae65b88e7d91c84c6f4b577cc1d92ff0ef..aa191cc009cebd7f1b89d43a86d35349e3485955 100644 (file)
@@ -20,3 +20,7 @@ DeviceConfig::~DeviceConfig() {
 string DeviceConfig::get_config_value(string key) {
        return NULL;
 }
+
+enum_summary_calculation DeviceConfig::get_summary_calculation_type() {
+       return MEAN;
+}
index 0c9a3b5917f9ae58e0e64624e8a3093b657c793a..ced5c81ae5ccfddc9af7bd041b1365f72b6494e5 100644 (file)
 #include <string>
 
 namespace w1 {
+       enum enum_summary_calculation {SUM, DELTA, MEAN, MAX, MIN};
+
        class DeviceConfig {
                public:
                        DeviceConfig(std::string device_id_param);
                        virtual ~DeviceConfig();
                        std::string get_config_value(std::string key);
+                       enum_summary_calculation get_summary_calculation_type();
                private:
                        std::string     device_id;
        };
index 5f85db07b6420b9ae18c4810ca02099bef9c3d92..844db8e34a435359b76e230e0f9a9f62befcf93b 100644 (file)
@@ -22,15 +22,17 @@ using namespace plp;
 W1DataList::W1DataList(string device_id_param) {
        string          base_dir;
 
-       device_id       = device_id_param;
-       base_dir        = W1Store::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           = new DeviceConfig(device_id_param);
+       summary_calc_type       = device_config->get_summary_calculation_type();
+       device_id               = device_id_param;
+       base_dir                = W1Store::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);
 }
 
 W1DataList::~W1DataList() {
-       //delete(device_dir);
+       delete(device_config);
 }
 
 Data *W1DataList::find_oldest_data(vector<string> year_vector) {
@@ -168,16 +170,33 @@ long int get_interval_type(Date *start_date,
 }
 
 Data *W1DataList::get_daily_summary(Date *date) {
-       Data    *data;
+       Data    *ret_val;
        W1Store *store;
 
        store   = new W1Store(device_id, date);
        store->load();
-       data    = store->get_mean();
-       data->printout();
+       switch(summary_calc_type) {
+               case SUM:
+                       ret_val = store->get_sum();
+                       break;
+               case DELTA:
+                       ret_val = store->get_delta();
+                       break;
+               case MEAN:
+               default:
+                       ret_val = store->get_mean();
+                       break;
+               case MAX:
+                       ret_val = store->get_max();
+                       break;
+               case MIN:
+                       ret_val = store->get_min();
+                       break;
+       }
+       ret_val->printout();
        delete(store);
 
-       return data;
+       return ret_val;
 }
 
 DataRange *W1DataList::get_daily_summary(Date *start_date,
index e401d8eeed32ffeee2371e674aead4d2083d70f9..2d6ceda966dea44528193acff56572a2e9cc9d2c 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "Data.hh"
 #include "Date.hh"
+#include "DeviceConfig.hh"
 
 #include <time.h>
 
@@ -30,12 +31,13 @@ namespace w1 {
                        DataRange *get_daily_summary(plp::Date *start_date, plp::Date *end_date);
                        DataRange *get_data(plp::Date *start_date, plp::Date *end_date);
                protected:
-                       std::string     device_id;
-                       std::string     device_dir;
-                       std::string     device_ch_dir;
+                       std::string                     device_id;
+                       std::string                     device_dir;
+                       std::string                     device_ch_dir;
+                       w1::DeviceConfig                *device_config;
+                       enum_summary_calculation        summary_calc_type;
                        Data *find_oldest_data(std::vector<std::string> year_vector);
                        Data *find_newest_data(std::vector<std::string> year_vector);
-
        };
 }
 
index 23adbb02da5b57d9769da7f88c2f7ccd46bd7038..80dde72388e325635b26abfda9455bf19d96ebea 100644 (file)
@@ -9,6 +9,7 @@
 #include <string>
 #include <fstream>
 #include <valarray>
+#include <limits>
 
 #include <time.h>
 #include <dirent.h>
@@ -150,6 +151,51 @@ void W1Store::load() {
        }
 }
 
+Data *W1Store::get_sum() {
+       int     row_count;
+       int     col_count;
+       double  new_val;
+       int     ii;
+       int     jj;
+       Date    *date;
+       Data    *data;
+       Data    *ret_val;
+
+       ret_val = NULL;
+       data    = NULL;
+       if (store_data == NULL) {
+               load();
+       }
+       if (store_data != NULL) {
+               row_count       = store_data->get_data_row_count();
+               log_debug("data row count: %d\n", row_count);
+               if (row_count > 0) {
+                       col_count       = store_data->get_data_column_count();
+                       log_debug("data item count per row: %d\n", col_count);
+                       ret_val         = new Data(col_count);
+                       if (col_count > 0) {
+                               for (ii = 0; ii < row_count - 1; ii++) {
+                                       data    = store_data->get_data(ii);
+                                       for (jj = 0; jj < col_count; jj++) {
+                                               new_val                 = data->value_arr[jj];
+                                               ret_val->value_arr[jj]  = ret_val->value_arr[jj] + new_val;
+                                       }
+                                       if (ii < (row_count - 2)) {
+                                               delete(data);
+                                               data    = NULL;
+                                       }
+                                       //log_debug("new val: %f, sum: %f\n", new_val, sum);
+                               }
+                       }
+                       ret_val->set_date(data->get_date());
+                       if (data != NULL) {
+                               delete(data);
+                       }
+               }
+       }
+       return ret_val;
+}
+
 Data *W1Store::get_delta() {
        int             row_count;
        int             col_count;
@@ -185,13 +231,39 @@ Data *W1Store::get_delta() {
 Data *W1Store::get_mean() {
        int     row_count;
        int     col_count;
-       double  avg;
+       int     ii;
+       Data    *ret_val;
+
+       ret_val = 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();
+                       ret_val         = get_sum();
+                       if (col_count > 0) {
+                               for (ii = 0; ii < col_count; ii++) {
+                                       ret_val->value_arr[ii]  = ret_val->value_arr[ii] / row_count;
+                                       log_debug("avg: %f\n", ret_val->value_arr[ii]);
+                               }
+                       }
+               }
+       }
+       return ret_val;
+}
+
+Data *W1Store::get_max() {
+       int     row_count;
+       int     col_count;
        double  new_val;
        int     ii;
        int     jj;
        Date    *date;
        Data    *data;
        Data    *ret_val;
+       double  min_val;
 
        ret_val = NULL;
        data    = NULL;
@@ -204,23 +276,69 @@ Data *W1Store::get_mean() {
                if (row_count > 0) {
                        col_count       = store_data->get_data_column_count();
                        log_debug("data item count per row: %d\n", col_count);
-                       ret_val         = new Data(col_count);
+                       min_val         = numeric_limits<double>::min();
+                       ret_val         = new Data(col_count, min_val);
                        if (col_count > 0) {
                                for (ii = 0; ii < row_count - 1; ii++) {
                                        data    = store_data->get_data(ii);
                                        for (jj = 0; jj < col_count; jj++) {
-                                               new_val                 = data->value_arr[jj];
-                                               ret_val->value_arr[jj]  = ret_val->value_arr[jj] + new_val;
+                                               new_val = data->value_arr[jj];
+                                               if (new_val > ret_val->value_arr[jj]) {
+                                                       ret_val->value_arr[jj]  = new_val;
+                                               }
                                        }
                                        if (ii < (row_count - 2)) {
                                                delete(data);
                                                data    = NULL;
                                        }
-                                       //log_debug("new val: %f, sum: %f\n", new_val, sum);
                                }
-                               for (ii = 0; ii < col_count; ii++) {
-                                       ret_val->value_arr[ii]  = ret_val->value_arr[ii] / row_count;
-                                       log_debug("avg: %f\n", ret_val->value_arr[ii]);
+                       }
+                       ret_val->set_date(data->get_date());
+                       if (data != NULL) {
+                               delete(data);
+                       }
+               }
+       }
+       return ret_val;
+}
+
+Data *W1Store::get_min() {
+       int     row_count;
+       int     col_count;
+       double  new_val;
+       int     ii;
+       int     jj;
+       Date    *date;
+       Data    *data;
+       Data    *ret_val;
+       double  max_val;
+
+       ret_val = NULL;
+       data    = NULL;
+       if (store_data == NULL) {
+               load();
+       }
+       if (store_data != NULL) {
+               row_count       = store_data->get_data_row_count();
+               log_debug("data row count: %d\n", row_count);
+               if (row_count > 0) {
+                       col_count       = store_data->get_data_column_count();
+                       log_debug("data item count per row: %d\n", col_count);
+                       max_val         = numeric_limits<double>::max();
+                       ret_val         = new Data(col_count, max_val);
+                       if (col_count > 0) {
+                               for (ii = 0; ii < row_count - 1; ii++) {
+                                       data    = store_data->get_data(ii);
+                                       for (jj = 0; jj < col_count; jj++) {
+                                               new_val = data->value_arr[jj];
+                                               if (new_val < ret_val->value_arr[jj]) {
+                                                       ret_val->value_arr[jj]  = new_val;
+                                               }
+                                       }
+                                       if (ii < (row_count - 2)) {
+                                               delete(data);
+                                               data    = NULL;
+                                       }
                                }
                        }
                        ret_val->set_date(data->get_date());
index 2d4bb461ec4a3457e25bd25548ead1430afde5bb..d27768a04af9fc62881a90396563f17e124b66bf 100644 (file)
@@ -27,8 +27,11 @@ namespace w1 {
                        static std::string get_file_name(std::string device_id, plp::Date *ltime);
                        static void store(std::string device_id, std::list<std::string> *string_list);
                        void load();
+                       Data *get_sum();
                        Data *get_delta();
                        Data *get_mean();
+                       Data *get_max();
+                       Data *get_min();
                        w1::Data *get_oldest_data();
                        w1::Data *get_newest_data();
                        w1::DataRange *get_oldest_and_newest_data();