//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;
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;
}
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;
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;
}
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) {
}
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();
}
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;
}
}
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;
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;
}