]> pilppa.com Git - lib1wire.git/commitdiff
Optimisations and cleanups
authorMika Laitio <lamikr@pilppa.org>
Thu, 9 Dec 2010 16:45:27 +0000 (18:45 +0200)
committerMika Laitio <lamikr@pilppa.org>
Thu, 9 Dec 2010 16:45:27 +0000 (18:45 +0200)
- 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 <lamikr@pilppa.org>
src/Date.cc
src/W1DataList.cc
src/W1DataList.hh
src/W1Store.cc
src/W1Store.hh
src_test/test_w1_datalog_write.cc

index 8327d1513924a17afd33fd1a038d57b2aefbc73a..b00d8a78270494c71a11deb229a9b7521787072f 100644 (file)
@@ -11,6 +11,8 @@
 #include <time.h>
 #include <malloc.h>
 
+#include <plp/log.h>
+
 #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() {
index 949454cf3aa0bb2935bb1f3b2e2878b636be79f9..3e7743ba0fc96bdc84822a3571d4b27d331390b6 100644 (file)
@@ -33,7 +33,7 @@ W1DataList::~W1DataList() {
        //delete(device_dir);
 }
 
-Data *W1DataList::find_first_data(vector<string> year_vector) {
+Data *W1DataList::find_oldest_data(vector<string> year_vector) {
        int             ii;
        string          year_dir;
        string          month_dir;
@@ -58,7 +58,7 @@ Data *W1DataList::find_first_data(vector<string> 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<string> year_vector) {
        return ret_val;
 }
 
-Data *W1DataList::find_last_data(vector<string> year_vector) {
+Data *W1DataList::find_newest_data(vector<string> year_vector) {
        int             ii;
        string          year_dir;
        string          month_dir;
@@ -95,7 +95,7 @@ Data *W1DataList::find_last_data(vector<string> 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<string>  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();
        }
index dd2259a0a6f07faf36a436c13a2d164b5afd8768..967efa6d47f06db90b7c4ffc5a4f464d388071a7 100644 (file)
@@ -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<std::string> year_vector);
-                       Data *find_last_data(std::vector<std::string> year_vector);
+                       Data *find_oldest_data(std::vector<std::string> year_vector);
+                       Data *find_newest_data(std::vector<std::string> year_vector);
 
        };
 }
index d59e12121bf5999cb005cab53725f2e22f7f9296..5db7b9b2ef349ce525584193b6968f017f975655 100644 (file)
@@ -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;
 }
index 528b19a996503677ae4af2486b2c2058c5d374ed..b05d9fe15aeb0e81f7c5022bf542b85016b619c9 100644 (file)
@@ -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<std::string> *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;
        };
 }
 
index db0febca0cdaf3fa9da16df0971b98c10f11c75d..807b862bebcf62f4707583e457e631aa5e899973 100644 (file)
@@ -102,7 +102,6 @@ int main(int argc, char** argv) {
                device_list.pop_back();
                delete(device);
        }
-
        delete(scanner);
        return 0;
 }