From 5e4bcec460603b9755094ddc1fe8408fdba11eb6 Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Thu, 9 Dec 2010 18:45:27 +0200 Subject: [PATCH] Optimisations and cleanups - nicer printout for dates - store data read cleanups - new method: get_oldest_and_newest_data - rewrote other methods to use cached data if possible Signed-off-by: Mika Laitio --- src/Date.cc | 4 +- src/W1DataList.cc | 70 +++-------- src/W1DataList.hh | 4 +- src/W1Store.cc | 195 +++++++++++++++++++++++------- src/W1Store.hh | 14 ++- src_test/test_w1_datalog_write.cc | 1 - 6 files changed, 180 insertions(+), 108 deletions(-) diff --git a/src/Date.cc b/src/Date.cc index 8327d15..b00d8a7 100644 --- a/src/Date.cc +++ b/src/Date.cc @@ -11,6 +11,8 @@ #include #include +#include + #include "Date.hh" using namespace std; @@ -62,7 +64,7 @@ bool Date::is_leap_year() { } void Date::printout() { - cout << "date: " << year << " " << month << " " << day << " " << hour << " " << min << " " << sec << endl; + log_debug("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, min, sec); } Date *Date::clone() { diff --git a/src/W1DataList.cc b/src/W1DataList.cc index 949454c..3e7743b 100644 --- a/src/W1DataList.cc +++ b/src/W1DataList.cc @@ -33,7 +33,7 @@ W1DataList::~W1DataList() { //delete(device_dir); } -Data *W1DataList::find_first_data(vector year_vector) { +Data *W1DataList::find_oldest_data(vector year_vector) { int ii; string year_dir; string month_dir; @@ -58,7 +58,7 @@ Data *W1DataList::find_first_data(vector year_vector) { f_name = data_vector.at(0); f_name = W1Util::concat_paths(month_dir, f_name); store = new W1Store(f_name); - ret_val = store->load_first_data_row(); + ret_val = store->get_oldest_data(); delete(store); break; } @@ -67,7 +67,7 @@ Data *W1DataList::find_first_data(vector year_vector) { return ret_val; } -Data *W1DataList::find_last_data(vector year_vector) { +Data *W1DataList::find_newest_data(vector year_vector) { int ii; string year_dir; string month_dir; @@ -95,7 +95,7 @@ Data *W1DataList::find_last_data(vector year_vector) { f_name = data_vector.at(size - 1); f_name = W1Util::concat_paths(month_dir, f_name); store = new W1Store(f_name); - ret_val = store->load_last_data_row(); + ret_val = store->get_newest_data(); delete(store); break; } @@ -110,32 +110,22 @@ DataRange *W1DataList::get_data_range() { struct dirent *year_dirent; vector year_list; Data *first_data; - Data *last_data; + Data *newest_data; ret_val = NULL; year_list = W1Util::get_subdirectories(device_dir); - first_data = find_first_data(year_list); + first_data = find_oldest_data(year_list); if (first_data != NULL) { - last_data = find_last_data(year_list); - if (last_data != NULL) { + newest_data = find_newest_data(year_list); + if (newest_data != NULL) { ret_val = new DataRange(*first_data); - ret_val->add_data(*last_data); - delete(last_data); + ret_val->add_data(*newest_data); + delete(newest_data); } delete(first_data); } return ret_val; } -/* -long int get_date_as_seconds(struct tm *date) { - long int ret_val; - - ret_val = date->tm - date->tm_hour * 3600 + - date->tm_min * 60 + - date->tm_sec; -} -*/ long int get_interval_type(Date *start_date, Date *end_date) { @@ -185,56 +175,34 @@ string W1DataList::get_day_data(Date *date) { } Data *W1DataList::get_avg_day_data(Date *date) { - return NULL; -} + Data *data; + W1Store *store; -/* -time_t to_seconds2(const char *date) -{ - struct tm storage={0,0,0,0,0,0,0,0,0}; - char *p=NULL; - time_t retval=0; + store = new W1Store(device_id, date); + store->load(); + data = store->get_mean(); + data->printout(); + delete(store); - p=(char *)strptime(date,"%d-%b-%Y",&storage); - if(p==NULL) { - retval=0; - } - else - { - errno = 0; - retval =mktime(&storage); - if (retval == ((time_t)-1)) { - log_error("Error2 converting struct tm to seconds: %s\n", strerror(errno)); - } - else { - log_debug("succ2: %ld\n", retval); - } - } - return retval; + return data; } -*/ DataRange *W1DataList::get_avg_day_data(Date *start_date, Date *end_date) { DataRange *ret_val; Data *data; - W1Store *store; Date *date; ret_val = NULL; date = start_date->clone(); while(date->before(*end_date)) { - store = new W1Store(device_id, date); - store->load(); - data = store->get_mean(); - data->printout(); + data = get_avg_day_data(date); if (ret_val == NULL) { ret_val = new DataRange(*data); } else { ret_val->add_data(*data); } - delete(store); delete(data); date->tomorrow(); } diff --git a/src/W1DataList.hh b/src/W1DataList.hh index dd2259a..967efa6 100644 --- a/src/W1DataList.hh +++ b/src/W1DataList.hh @@ -30,8 +30,8 @@ namespace w1 { std::string device_dir; std::string device_ch_dir; std::string get_day_data(plp::Date *date); - Data *find_first_data(std::vector year_vector); - Data *find_last_data(std::vector year_vector); + 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 d59e121..5db7b9b 100644 --- a/src/W1Store.cc +++ b/src/W1Store.cc @@ -27,24 +27,25 @@ using namespace w1; using namespace plp; std::string W1Store::store_base_dir = DEFAULT_STORAGE_BASE_DIR; -DataRange *data_range = NULL; W1Store::W1Store(string device_id, Date *date_time) { - data_range = NULL; + store_data = NULL; + range_data = NULL; store_file_name = get_store_file_name(device_id, date_time); log_debug("data file name: %s\n", store_file_name.c_str()); } W1Store::W1Store(string file_name_param) { - data_range = NULL; + store_data = NULL; + range_data = NULL; store_file_name = file_name_param; } W1Store::~W1Store() { - if (data_range != NULL) { - delete(data_range); - data_range = NULL; + if (store_data != NULL) { + delete(store_data); + store_data = NULL; } } @@ -129,29 +130,61 @@ void W1Store::load() { ifstream in; string line; - if (data_range != NULL) { - delete(data_range); - data_range = NULL; + if (store_data != NULL) { + delete(store_data); + store_data = NULL; } in.open(store_file_name.c_str()); if (in.is_open() == true) { while (in.eof() == false) { getline(in, line); data = W1Util::parse_data_line(line); - if (data_range == NULL) { - data_range = new DataRange(*data); + if (store_data == NULL) { + store_data = new DataRange(*data); } else { - data_range->add_data(*data); + store_data->add_data(*data); } delete(data); } } } +Data *W1Store::get_delta() { + int row_count; + int col_count; + Data *o_data; + Data *n_data; + Data *ret_val; + int ii; + DataRange *dr; + + ret_val = NULL; + dr = get_oldest_and_newest_data(); + if (dr != NULL) { + row_count = dr->get_data_row_count(); + if (row_count == 2) { + o_data = dr->get_data(0); + n_data = dr->get_data(1); + col_count = dr->get_data_column_count(); + ret_val = new Data(col_count); + if (col_count > 0) { + for (ii = 0; ii < col_count; ii++) { + ret_val->value_arr[ii] = n_data->value_arr[ii] - o_data->value_arr[ii]; + } + } + ret_val->set_date(n_data->get_date()); + delete(o_data); + delete(n_data); + } + delete(dr); + } + return ret_val; +} + Data *W1Store::get_mean() { - int d_count; - int i_count; + int row_count; + int col_count; double avg; double new_val; int ii; @@ -161,70 +194,138 @@ Data *W1Store::get_mean() { Data *ret_val; ret_val = NULL; - if (data_range == NULL) { + data = NULL; + if (store_data == NULL) { load(); } - if (data_range != NULL) { - d_count = data_range->get_data_row_count(); - log_debug("data row count: %d\n", d_count); - if (d_count > 0) { - i_count = data_range->get_data_column_count(); - log_debug("data item count per row: %d\n", i_count); - ret_val = new Data(i_count); - if (i_count > 0) { - for (ii = 0; ii < d_count - 1; ii++) { - data = data_range->get_data(ii); - for (jj = 0; jj < i_count; jj++) { + 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 < (d_count - 2)) { + if (ii < (row_count - 2)) { delete(data); + data = NULL; } //log_debug("new val: %f, sum: %f\n", new_val, sum); } - for (ii = 0; ii < i_count; ii++) { - ret_val->value_arr[ii] = ret_val->value_arr[ii] / d_count; + 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()); - delete(data); + if (data != NULL) { + delete(data); + } } } return ret_val; } -Data *W1Store::load_first_data_row() { - Data *ret_val; +DataRange *W1Store::get_oldest_and_newest_data() { + DataRange *ret_val; ifstream in; + Data *o_data; + Data *n_data; + string latest; + int row_count; string line; + string prev_line; ret_val = NULL; - in.open(store_file_name.c_str()); - if (in.eof() == false) { - getline(in, line); - ret_val = W1Util::parse_data_line(line); + o_data = NULL; + n_data = NULL; + if (store_data != NULL) { + row_count = store_data->get_data_row_count(); + if (row_count > 0) { + o_data = store_data->get_data(0); + n_data = store_data->get_data(row_count - 1); + } + } + else { + if (range_data != NULL) { + row_count = range_data->get_data_row_count(); + if (row_count > 0) { + o_data = range_data->get_data(0); + n_data = range_data->get_data(row_count - 1); + } + } + else { + in.open(store_file_name.c_str()); + while (in.eof() == false) { + getline(in, line); + if (line.empty() == false) { + if (o_data == NULL) { + o_data = W1Util::parse_data_line(line); + } + prev_line = line; + } + } + if (prev_line.empty() == false) { + n_data = W1Util::parse_data_line(prev_line); + } + } + } + if ((o_data != NULL) && + (n_data != NULL)) { + ret_val = new DataRange(*o_data); + ret_val->add_data(*n_data); + if (range_data != NULL) { + range_data = new DataRange(*o_data); + range_data->add_data(*n_data); + } + } + if (o_data != NULL) { + delete(o_data); + } + if (n_data != NULL) { + delete(n_data); } return ret_val; } -Data *W1Store::load_last_data_row() { +Data *W1Store::get_oldest_data() { + int row_count; + int col_count; Data *ret_val; - ifstream in; - string line; - string prev_line; + DataRange *dr; ret_val = NULL; - in.open(store_file_name.c_str()); - while (in.eof() == false) { - getline(in, line); - if (line.empty() == false) { - prev_line = line; + dr = get_oldest_and_newest_data(); + if (dr != NULL) { + row_count = dr->get_data_row_count(); + if (row_count >= 1) { + ret_val = dr->get_data(0); } + delete(dr); } - if (prev_line.empty() == false) { - ret_val = W1Util::parse_data_line(prev_line); + return ret_val; +} + +Data *W1Store::get_newest_data() { + int row_count; + int col_count; + Data *ret_val; + DataRange *dr; + + ret_val = NULL; + dr = get_oldest_and_newest_data(); + if (dr != NULL) { + row_count = dr->get_data_row_count(); + if (row_count == 2) { + ret_val = dr->get_data(1); + } + delete(dr); } return ret_val; } diff --git a/src/W1Store.hh b/src/W1Store.hh index 528b19a..b05d9fe 100644 --- a/src/W1Store.hh +++ b/src/W1Store.hh @@ -27,14 +27,16 @@ namespace w1 { static std::string get_store_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(); - w1::Data *load_first_data_row(); - w1::Data *load_last_data_row(); + w1::Data *get_oldest_data(); + w1::Data *get_newest_data(); + w1::DataRange *get_oldest_and_newest_data(); protected: - static std::string store_base_dir; - std::string store_file_name; - DataRange *data_range; + static std::string store_base_dir; + std::string store_file_name; + DataRange *store_data; + DataRange *range_data; }; } diff --git a/src_test/test_w1_datalog_write.cc b/src_test/test_w1_datalog_write.cc index db0febc..807b862 100644 --- a/src_test/test_w1_datalog_write.cc +++ b/src_test/test_w1_datalog_write.cc @@ -102,7 +102,6 @@ int main(int argc, char** argv) { device_list.pop_back(); delete(device); } - delete(scanner); return 0; } -- 2.41.1