From a7a92143366369dfba0d87570a589728b786574d Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Thu, 9 Dec 2010 23:55:27 +0200 Subject: [PATCH] add min,max,mean,sum and delta calculations for stored data. 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 --- src/Data.cc | 9 +++ src/Data.hh | 1 + src/DeviceConfig.cc | 4 ++ src/DeviceConfig.hh | 3 + src/W1DataList.cc | 39 +++++++++---- src/W1DataList.hh | 10 ++-- src/W1Store.cc | 134 +++++++++++++++++++++++++++++++++++++++++--- src/W1Store.hh | 3 + 8 files changed, 181 insertions(+), 22 deletions(-) diff --git a/src/Data.cc b/src/Data.cc index ac7ed2d..98425c6 100644 --- a/src/Data.cc +++ b/src/Data.cc @@ -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 vector_param, Date *date_param) { int ii; int size; diff --git a/src/Data.hh b/src/Data.hh index 14ab90e..3907a9b 100644 --- a/src/Data.hh +++ b/src/Data.hh @@ -19,6 +19,7 @@ namespace w1 { class Data { public: Data(int size); + Data(int size, double default_value); Data(std::vector vector_param, plp::Date *date_param); Data(std::valarray value_arr_param, plp::Date *date_param); virtual ~Data(); diff --git a/src/DeviceConfig.cc b/src/DeviceConfig.cc index 1ecf0ba..aa191cc 100644 --- a/src/DeviceConfig.cc +++ b/src/DeviceConfig.cc @@ -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; +} diff --git a/src/DeviceConfig.hh b/src/DeviceConfig.hh index 0c9a3b5..ced5c81 100644 --- a/src/DeviceConfig.hh +++ b/src/DeviceConfig.hh @@ -11,11 +11,14 @@ #include 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; }; diff --git a/src/W1DataList.cc b/src/W1DataList.cc index 5f85db0..844db8e 100644 --- a/src/W1DataList.cc +++ b/src/W1DataList.cc @@ -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 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, diff --git a/src/W1DataList.hh b/src/W1DataList.hh index e401d8e..2d6ceda 100644 --- a/src/W1DataList.hh +++ b/src/W1DataList.hh @@ -13,6 +13,7 @@ #include "Data.hh" #include "Date.hh" +#include "DeviceConfig.hh" #include @@ -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 year_vector); Data *find_newest_data(std::vector year_vector); - }; } diff --git a/src/W1Store.cc b/src/W1Store.cc index 23adbb0..80dde72 100644 --- a/src/W1Store.cc +++ b/src/W1Store.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -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::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::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()); diff --git a/src/W1Store.hh b/src/W1Store.hh index 2d4bb46..d27768a 100644 --- a/src/W1Store.hh +++ b/src/W1Store.hh @@ -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 *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(); -- 2.41.1