From: Mika Laitio Date: Thu, 6 Jan 2011 09:53:29 +0000 (+0200) Subject: support for sum, min and max queryes from the collected data. X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=6b8da0110c1dc3c720b6000b95b401b87e78d3f2;p=lib1wire.git support for sum, min and max queryes from the collected data. Signed-off-by: Mika Laitio --- diff --git a/src/DeviceData.cc b/src/DeviceData.cc index 5f95966..cfb29a7 100644 --- a/src/DeviceData.cc +++ b/src/DeviceData.cc @@ -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 year_vector) { string year_dr; string mon_dr; vector mon_vcr; - vector d_vcr; + vector dta_vcr; string f_name; W1Store *store; Data *ret_val; @@ -57,9 +57,9 @@ Data *DeviceData::find_oldest_data(vector 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 DeviceData::get_hourly_summary(Date *date) { - vector ret_val; - W1Store * store; +vector *DeviceData::get_hourly_summary(Date *date, + int calc_type) { + vector *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 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 *DeviceData::get_hourly_summary(Date *date) { + vector *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 dta_list; + vector *dta_lst; Data *data; Date *date; vector::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; diff --git a/src/DeviceData.hh b/src/DeviceData.hh index a4c06ac..e3b3727 100644 --- a/src/DeviceData.hh +++ b/src/DeviceData.hh @@ -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 get_hourly_summary(plp::Date *date); + std::vector *get_hourly_summary(plp::Date *date, int calc_type); + std::vector *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: diff --git a/src/W1Store.cc b/src/W1Store.cc index 83e1615..effea2e 100644 --- a/src/W1Store.cc +++ b/src/W1Store.cc @@ -367,7 +367,74 @@ Data *W1Store::get_min() { return ret_val; } -vector W1Store::get_mean(int freq_sec) { +vector *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 *ret_val; + + ret_val = new vector(); + 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 *W1Store::get_mean(int freq_sec) { int row_count; int col_count; int d_count; @@ -377,8 +444,9 @@ vector W1Store::get_mean(int freq_sec) { Data *calc; Date *limit_d; Date date; - vector ret_val; + vector *ret_val; + ret_val = new vector(); calc = NULL; limit_d = NULL; d_count = 1; @@ -413,7 +481,7 @@ vector 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 W1Store::get_mean(int freq_sec) { return ret_val; } -vector W1Store::get_delta(int freq_sec) { +vector *W1Store::get_delta(int freq_sec) { int row_count; int col_count; int jj; @@ -451,8 +519,9 @@ vector W1Store::get_delta(int freq_sec) { Data *calc2; Date *limit_d; Date date; - vector ret_val; + vector *ret_val; + ret_val = new vector(); calc1 = NULL; calc2 = NULL; limit_d = NULL; @@ -493,7 +562,7 @@ vector 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 W1Store::get_delta(int freq_sec) { return ret_val; } +vector *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 *ret_val; + + ret_val = new vector(); + 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 *W1Store::get_max(int freq_sec) { + vector *ret_val; + + ret_val = get_max_or_min(freq_sec, true); + return ret_val; +} + +vector *W1Store::get_min(int freq_sec) { + vector *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; diff --git a/src/W1Store.hh b/src/W1Store.hh index 5f3dff9..b4a6b1a 100644 --- a/src/W1Store.hh +++ b/src/W1Store.hh @@ -33,8 +33,11 @@ namespace w1 { Data *get_mean(); Data *get_max(); Data *get_min(); - std::vector get_mean(int freq_sec); - std::vector get_delta(int freq_sec); + std::vector *get_sum(int freq_sec); + std::vector *get_mean(int freq_sec); + std::vector *get_delta(int freq_sec); + std::vector *get_max(int freq_sec); + std::vector *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 *get_max_or_min(int freq_sec, bool max); }; }