* Created on: Nov 7, 2010
* Author: lamikr
*/
+#include <sstream>
+
#include <dirent.h>
#include <malloc.h>
#include <errno.h>
using namespace std;
using namespace plp;
+template <class NumberDataType>
+bool string_to_number(NumberDataType& result,
+ const std::string& string_param,
+ std::ios_base& (*format)(std::ios_base&))
+{
+ istringstream iss(string_param);
+ return !(iss >> format >> result).fail();
+}
+
DeviceData::DeviceData(string device_id_param) {
string base_dir;
}
}
-Data *DeviceData::find_newest_data(string year_name_param) {
- int ii;
- string mon_dr;
- vector<string> mon_vcr;
- vector<string> d_vcr;
- string f_name;
- StoreDay *store;
- Data *ret_val;
- string year_dr;
- int size;
-
- ret_val = NULL;
- year_dr = W1Util::concat_paths(device_dir, year_name_param);
- mon_vcr = W1Util::get_subdirectories(year_dr);
- for (ii = mon_vcr.size() - 1; ii >= 0; ii--) {
- 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);
- size = d_vcr.size();
- if (size > 0) {
- f_name = d_vcr.at(size - 1);
- f_name = W1Util::concat_paths(mon_dr, f_name);
- store = new StoreDay(f_name);
- ret_val = store->get_newest_data();
- delete(store);
- break;
- }
- }
- return ret_val;
-}
-
Data *DeviceData::find_newest_data(vector<string> year_name_vector_param) {
string year_name;
int size;
Data *ret_val;
+ Date date;
+ int val_int;
ret_val = NULL;
size = year_name_vector_param.size();
if (size > 0) {
// dirs are alphabetically sorted
year_name = year_name_vector_param.at(size - 1);
- ret_val = find_newest_data(year_name);
- }
- return ret_val;
-}
-
-Data *DeviceData::find_oldest_data(string year_name_param) {
- int size;
- unsigned int ii;
- string year_dr;
- string mon_dr;
- vector<string> mon_vcr;
- vector<string> dta_vcr;
- string f_name;
- StoreDay *store;
- Data *ret_val;
-
- ret_val = NULL;
- year_dr = W1Util::concat_paths(device_dir, year_name_param);
- mon_vcr = W1Util::get_subdirectories(year_dr);
- for (ii = 0; ii < mon_vcr.size(); ii++) {
- mon_dr = mon_vcr.at(ii);
- mon_dr = W1Util::concat_paths(year_dr, mon_dr);
- // scan data files from month dir
- dta_vcr = W1Util::get_data_files(mon_dr);
- size = dta_vcr.size();
- if (size > 0) {
- f_name = dta_vcr.at(0);
- f_name = W1Util::concat_paths(mon_dr, f_name);
- store = new StoreDay(f_name);
- ret_val = store->get_oldest_data();
- delete(store);
- break;
- }
+ string_to_number<int>(val_int, year_name, dec);
+ date.year = val_int;
+ ret_val = StoreCache::get_newest_data(&date, device_id, PERIOD_YEARLY);
}
return ret_val;
}
int size;
string year_name;
Data *ret_val;
+ Date date;
+ int val_int;
ret_val = NULL;
size = year_name_vector_param.size();
if (size > 0) {
// dirs are alphabetically sorted
year_name = year_name_vector_param.at(0);
- ret_val = find_oldest_data(year_name);
+ string_to_number<int>(val_int, year_name, dec);
+ date.year = val_int;
+ ret_val = StoreCache::get_oldest_data(&date, device_id, PERIOD_YEARLY);
}
return ret_val;
}
return ret_val;
}
+plp::Date *StoreCache::get_scanning_limit_date(EnumSummaryPeriod period_type_param) {
+ Data *cur_data;;
+ Date *ret_val;
+
+ cur_data = get_newest_data(date, device_id, period_type_param);
+ if (cur_data != NULL) {
+ ret_val = cur_data->get_date().clone();
+ ret_val->next_second();
+ delete(cur_data);
+ cur_data = NULL;
+ }
+ else {
+ ret_val = date->clone();
+ if (period_type_param == PERIOD_YEARLY) {
+ ret_val->next_year();
+ }
+ else {
+ ret_val->next_month();
+ }
+ }
+ return ret_val;
+}
+
DataRange *StoreCache::get_mean(EnumSummaryPeriod period_type_param) {
int row_count;
DataRange *ret_val;
Store *store;
cur_date = date->clone();
- max_date = date->clone();
- if (period_type_param == PERIOD_YEARLY) {
- max_date->next_year();
- }
- else {
- max_date->next_month();
- }
+ max_date = get_scanning_limit_date(period_type_param);
cur_data = NULL;
res_data = NULL;
cnt = 0;
DataRange *dr;
cur_date = date->clone();
- max_date = date->clone();
- if (period_type_param == PERIOD_YEARLY) {
- max_date->next_year();
- }
- else {
- max_date->next_month();
- }
+ max_date = get_scanning_limit_date(period_type_param);
cur_data = NULL;
res_data = NULL;
cnt = 0;
int ii;
int cnt;
- first_data = get_year_oldest_data();
+ first_data = get_oldest_data(date, device_id, PERIOD_YEARLY);
if (first_data != NULL) {
- last_data = get_year_newest_data();
+ last_data = get_newest_data(date, device_id, PERIOD_YEARLY);
if (last_data != NULL) {
ret_val = new DataRange();
cnt = last_data->get_value_count();
DataRange *dr;
cur_date = date->clone();
- max_date = date->clone();
- if (period_type_param == PERIOD_YEARLY) {
- max_date->next_year();
- }
- else {
- max_date->next_month();
- }
+ max_date = get_scanning_limit_date(period_type_param);
cur_data = NULL;
res_data = NULL;
cnt = 0;
DataRange *dr;
cur_date = date->clone();
- max_date = date->clone();
- if (period_type_param == PERIOD_YEARLY) {
- max_date->next_year();
- }
- else {
- max_date->next_month();
- }
+ max_date = get_scanning_limit_date(period_type_param);
cur_data = NULL;
res_data = NULL;
cnt = 0;
}
}
-/**
- * Find oldest data in certain year
- */
-Data *StoreCache::get_year_oldest_data() {
+Data *StoreCache::get_oldest_data(Date *date_param, string device_id_param, EnumSummaryPeriod period_type_param) {
+ int size;
+ unsigned int ii;
+ string year_dr;
+ string mon_dr;
+ vector<string> mon_vcr;
+ vector<string> dta_vcr;
+ string f_name;
+ StoreDay *store;
Data *ret_val;
+ string device_dir;
char buffer[30];
- DeviceData *dta_finder;
- string str;
-
- dta_finder = new DeviceData(device_id);
- snprintf(buffer, 30, "%d", date->year);
- str.append(buffer);
- ret_val = dta_finder->find_oldest_data(str);
- delete(dta_finder);
+ string year_name;
+
+ ret_val = NULL;
+ if (period_type_param == PERIOD_YEARLY) {
+ snprintf(buffer, 30, "%d", date_param->year);
+ year_name.append(buffer);
+ device_dir = DeviceConfig::get_base_dir_name();
+ device_dir = W1Util::concat_paths(device_dir, device_id_param);
+ year_dr = W1Util::concat_paths(device_dir, year_name);
+ mon_vcr = W1Util::get_subdirectories(year_dr);
+ for (ii = 0; ii < mon_vcr.size(); ii++) {
+ mon_dr = mon_vcr.at(ii);
+ mon_dr = W1Util::concat_paths(year_dr, mon_dr);
+ // scan data files from month dir
+ dta_vcr = W1Util::get_data_files(mon_dr);
+ size = dta_vcr.size();
+ if (size > 0) {
+ f_name = dta_vcr.at(0);
+ f_name = W1Util::concat_paths(mon_dr, f_name);
+ store = new StoreDay(f_name);
+ ret_val = store->get_oldest_data();
+ delete(store);
+ break;
+ }
+ }
+ }
+ else if (period_type_param == PERIOD_MONTHLY) {
+ ret_val = NULL;
+ snprintf(buffer, 30, "%d", date_param->year);
+ year_name.append(buffer);
+ device_dir = DeviceConfig::get_base_dir_name();
+ device_dir = W1Util::concat_paths(device_dir, device_id_param);
+ year_dr = W1Util::concat_paths(device_dir, year_name);
+ snprintf(buffer, 30, "%02d", date_param->month);
+ mon_dr.append(buffer);
+ mon_dr = W1Util::concat_paths(year_dr, mon_dr);
+ // scan data files from month dir
+ dta_vcr = W1Util::get_data_files(mon_dr);
+ size = dta_vcr.size();
+ if (size > 0) {
+ f_name = dta_vcr.at(0);
+ f_name = W1Util::concat_paths(mon_dr, f_name);
+ store = new StoreDay(f_name);
+ ret_val = store->get_oldest_data();
+ delete(store);
+ }
+
+ }
+ else {
+ store = new StoreDay(device_id_param, date_param);
+ ret_val = store->get_oldest_data();
+ delete(store);
+ }
return ret_val;
}
-/**
- * Find newest data in certain year
- */
-Data *StoreCache::get_year_newest_data() {
+Data *StoreCache::get_newest_data(Date *date_param, string device_id_param, EnumSummaryPeriod period_type_param) {
+ int ii;
+ string mon_dr;
+ vector<string> mon_vcr;
+ vector<string> dta_vcr;
+ string f_name;
+ StoreDay *store;
Data *ret_val;
+ string year_dr;
+ int size;
+ string device_dir;
char buffer[30];
- DeviceData *dta_finder;
- string str;
-
- dta_finder = new DeviceData(device_id);
- snprintf(buffer, 30, "%d", date->year);
- str.append(buffer);
- ret_val = dta_finder->find_newest_data(str);
- delete(dta_finder);
+ string year_name;
+
+ ret_val = NULL;
+ if (period_type_param == PERIOD_YEARLY) {
+ snprintf(buffer, 30, "%d", date_param->year);
+ year_name.append(buffer);
+ device_dir = DeviceConfig::get_base_dir_name();
+ device_dir = W1Util::concat_paths(device_dir, device_id_param);
+ year_dr = W1Util::concat_paths(device_dir, year_name);
+ mon_vcr = W1Util::get_subdirectories(year_dr);
+ for (ii = mon_vcr.size() - 1; ii >= 0; ii--) {
+ mon_dr = mon_vcr.at(ii);
+ mon_dr = W1Util::concat_paths(year_dr, mon_dr);
+ // scan data files from month dir
+ dta_vcr = W1Util::get_data_files(mon_dr);
+ size = dta_vcr.size();
+ if (size > 0) {
+ f_name = dta_vcr.at(size - 1);
+ f_name = W1Util::concat_paths(mon_dr, f_name);
+ store = new StoreDay(f_name);
+ ret_val = store->get_newest_data();
+ delete(store);
+ break;
+ }
+ }
+ }
+ else if (period_type_param == PERIOD_MONTHLY) {
+ ret_val = NULL;
+ snprintf(buffer, 30, "%d", date_param->year);
+ year_name.append(buffer);
+ device_dir = DeviceConfig::get_base_dir_name();
+ device_dir = W1Util::concat_paths(device_dir, device_id_param);
+ year_dr = W1Util::concat_paths(device_dir, year_name);
+ snprintf(buffer, 30, "%02d", date_param->month);
+ mon_dr.append(buffer);
+ mon_dr = W1Util::concat_paths(year_dr, mon_dr);
+ // scan data files from month dir
+ dta_vcr = W1Util::get_data_files(mon_dr);
+ size = dta_vcr.size();
+ if (size > 0) {
+ f_name = dta_vcr.at(size - 1);
+ f_name = W1Util::concat_paths(mon_dr, f_name);
+ store = new StoreDay(f_name);
+ ret_val = store->get_newest_data();
+ delete(store);
+ }
+
+ }
+ else {
+ store = new StoreDay(device_id_param, date_param);
+ ret_val = store->get_newest_data();
+ delete(store);
+ }
return ret_val;
}