functions with cache support.
Signed-off-by: Mika Laitio <lamikr@pilppa.org>
DeviceData::DeviceData(string device_id_param) {
string base_dir;
- device_config = Factory::get_device_config(device_id_param);
- summary_calc_type = device_config->get_summary_calculation_type();
- device_id = device_id_param;
- base_dir = DeviceConfig::get_base_dir_name();
- device_dir = W1Util::concat_paths(base_dir, device_id);
- device_ch_dir = W1Util::concat_paths(base_dir, "cache");
- device_ch_dir = W1Util::concat_paths(device_ch_dir, device_id);
+ device_config = NULL;
+ device_id = device_id_param;
+ base_dir = DeviceConfig::get_base_dir_name();
+ device_dir = W1Util::concat_paths(base_dir, device_id);
+ device_ch_dir = W1Util::concat_paths(base_dir, "cache");
+ device_ch_dir = W1Util::concat_paths(device_ch_dir, device_id);
}
DeviceData::~DeviceData() {
- delete(device_config);
+ if (device_config != NULL) {
+ delete(device_config);
+ device_config = NULL;
+ }
}
-Data *DeviceData::find_oldest_data(vector<string> year_vector) {
- unsigned int ii;
- string year_dr;
+Data *DeviceData::find_newest_data(string year_name_param) {
+ int ii;
string mon_dr;
vector<string> mon_vcr;
- vector<string> dta_vcr;
+ vector<string> d_vcr;
string f_name;
StoreDay *store;
Data *ret_val;
+ string year_dr;
+ int size;
ret_val = NULL;
- if (year_vector.size() > 0) {
- // dirs are alphabetically sorted
- year_dr = year_vector.at(0);
- year_dr = W1Util::concat_paths(device_dir, year_dr);
- 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);
- if (dta_vcr.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;
- }
+ 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_vector) {
- int ii;
+Data *DeviceData::find_newest_data(vector<string> year_name_vector_param) {
+ string year_name;
+ int size;
+ Data *ret_val;
+
+ 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> d_vcr;
+ vector<string> dta_vcr;
string f_name;
+ StoreDay *store;
Data *ret_val;
- int size;
- StoreDay *store;
ret_val = NULL;
- size = year_vector.size();
+ 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;
+ }
+ }
+ return ret_val;
+}
+
+Data *DeviceData::find_oldest_data(vector<string> year_name_vector_param) {
+ int size;
+ string year_name;
+ Data *ret_val;
+
+ ret_val = NULL;
+ size = year_name_vector_param.size();
if (size > 0) {
// dirs are alphabetically sorted
- year_dr = year_vector.at(size - 1);
- year_dr = W1Util::concat_paths(device_dir, year_dr);
- 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;
- }
- }
+ year_name = year_name_vector_param.at(0);
+ ret_val = find_oldest_data(year_name);
}
return ret_val;
}
return ret_val;
}
+DataRange *DeviceData::get_yearly_summary(Date *date,
+ EnumSummaryCalculationType calc_type_param) {
+ return get_summary(date, calc_type_param, PERIOD_YEARLY);
+}
+
+DataRange *DeviceData::get_yearly_summary(Date *date) {
+ DataRange *ret_val;
+
+ if (device_config == NULL) {
+ device_config = Factory::get_device_config(device_id);
+ summary_calc_type = device_config->get_summary_calculation_type();
+ }
+ ret_val = get_yearly_summary(date, summary_calc_type);
+ return ret_val;
+}
+
+DataRange *DeviceData::get_yearly_summary(Date *start_date,
+ Date *end_date) {
+ DataRange *ret_val;
+ DataRange *data;
+ Date *date;
+
+ ret_val = NULL;
+ date = start_date->clone();
+ while(date->before_or_equal_year(end_date)) {
+ data = get_yearly_summary(date);
+ if (data != NULL) {
+ if (ret_val == NULL) {
+ ret_val = new DataRange(data);
+ }
+ else {
+ ret_val->add(data);
+ }
+ delete(data);
+ }
+ date->next_year();
+ }
+ delete(date);
+ return ret_val;
+}
+
DataRange *DeviceData::get_monthly_summary(Date *date,
EnumSummaryCalculationType calc_type_param) {
return get_summary(date, calc_type_param, PERIOD_MONTHLY);
DataRange *DeviceData::get_monthly_summary(Date *date) {
DataRange *ret_val;
+ if (device_config == NULL) {
+ device_config = Factory::get_device_config(device_id);
+ summary_calc_type = device_config->get_summary_calculation_type();
+ }
ret_val = get_monthly_summary(date, summary_calc_type);
return ret_val;
}
DataRange *DeviceData::get_daily_summary(Date *date) {
DataRange *ret_val;
+ if (device_config == NULL) {
+ device_config = Factory::get_device_config(device_id);
+ summary_calc_type = device_config->get_summary_calculation_type();
+ }
ret_val = get_daily_summary(date, summary_calc_type);
return ret_val;
}
DataRange *DeviceData::get_hourly_summary(Date *date) {
DataRange *ret_val;
+ if (device_config == NULL) {
+ device_config = Factory::get_device_config(device_id);
+ summary_calc_type = device_config->get_summary_calculation_type();
+ }
ret_val = get_hourly_summary(date, summary_calc_type);
return ret_val;
}
switch(period) {
case PERIOD_YEARLY:
log_debug("get yearly summary: %s - %s\n", start_date->to_string().c_str(), end_date->to_string().c_str());
- ret_val = get_monthly_summary(start_date, end_date);
+ ret_val = get_yearly_summary(start_date, end_date);
break;
case PERIOD_MONTHLY:
log_debug("get monthly summary\n");
#include <time.h>
-namespace w1 {
+namespace plp {
class DeviceData {
public:
DeviceData(std::string device_id);
virtual ~DeviceData();
plp::DataRange *get_data_range();
- plp::DataRange *get_monthly_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
/**
- * Get summary data calculated from the daily data items that is meaning full.
+ * Get monthly summary data.
+ * Depending from the device type, it may be daily mean value, daily delta, highest value, etc...
+ */
+ plp::DataRange *get_yearly_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
+ plp::DataRange *get_yearly_summary(plp::Date *date);
+ plp::DataRange *get_yearly_summary(plp::Date *start_date, plp::Date *end_date);
+ /**
+ * Get monthly summary data.
* Depending from the device type, it may be daily mean value, daily delta, highest value, etc...
*/
+ plp::DataRange *get_monthly_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
plp::DataRange *get_monthly_summary(plp::Date *date);
plp::DataRange *get_monthly_summary(plp::Date *start_date, plp::Date *end_date);
- plp::DataRange *get_daily_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
/**
- * Get summary data calculated from the daily data items that is meaning full.
+ * Get daily summary data calculated from the daily data items.
* Depending from the device type, it may be daily mean value, daily delta, highest value, etc...
*/
+ plp::DataRange *get_daily_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
plp::DataRange *get_daily_summary(plp::Date *date);
plp::DataRange *get_daily_summary(plp::Date *start_date, plp::Date *end_date);
plp::DataRange *get_hourly_summary(plp::Date *date, EnumSummaryCalculationType calc_type);
plp::DataRange *get_hourly_summary(plp::Date *date);
plp::DataRange *get_hourly_summary(plp::Date *start_date, plp::Date *end_date);
plp::DataRange *get_data(plp::Date *start_date, plp::Date *end_date);
+ plp::Data *find_newest_data(std::string year_name_param);
+ plp::Data *find_oldest_data(std::string year_name_param);
protected:
std::string device_id;
std::string device_dir;
#include "W1Util.hh"
using namespace w1;
+using namespace plp;
using namespace std;
template <class NumberDataType>
virtual ~Factory();
static W1Device *get_device(int family_code, std::string device_id, dirent *direntry_param);
static std::list<W1Device *> get_device_list();
- static std::list<DeviceData *> get_device_data_list();
+ static std::list<plp::DeviceData *> get_device_data_list();
static DeviceConfig *get_device_config(std::string device_id);
private:
//int parse_family_code(std::string folder_name);
log_error("Could not open data file: %s\n", fname_param.c_str());
}
}
- else {
- log_warning("Could not open data file, file does not exist or user does not have read access for it: %s\n", fname_param.c_str());
- }
return ret_val;
}
#include <string>
#include "Data.hh"
#include "Date.hh"
+#include "W1Configure.hh"
namespace plp {
class Store {
public:
Store(std::string device_id, plp::Date *date_time);
virtual ~Store();
+ virtual plp::DataRange *get_sum(EnumSummaryPeriod period_type_param) = 0;
+ virtual plp::DataRange *get_mean(EnumSummaryPeriod period_type_param) = 0;
+ virtual plp::DataRange *get_delta(EnumSummaryPeriod period_type_param) = 0;
+ virtual plp::DataRange *get_max(EnumSummaryPeriod period_type_param) = 0;
+ virtual plp::DataRange *get_min(EnumSummaryPeriod period_type_param) = 0;
protected:
std::string device_id;
plp::Date *date;
#include "StoreCache.hh"
#include "StoreDay.hh"
#include "DeviceConfig.hh"
+#include "DeviceData.hh"
#include "W1Util.hh"
using namespace std;
using namespace w1;
StoreCache::StoreCache(string device_id_param,
- Date *date_time_param): Store(device_id_param, date_time_param) {
+ Date *date_time_param): Store(device_id_param, date_time_param) {
}
StoreCache::~StoreCache() {
DataRange *ret_val;
Data *dta;
string fname;
- StoreDay *store;
ret_val = NULL;
fname = get_file_name(device_id,
if (ret_val == NULL) {
switch(period_type_param) {
case PERIOD_YEARLY:
- break;
- case PERIOD_MONTHLY: {
+ case PERIOD_MONTHLY:
+ {
Data *cur_data;
Data *res_data;
Date *cur_date;
int cnt;
int val_cnt;
DataRange *dr;
+ Store *store;
cur_date = date->clone();
max_date = date->clone();
- max_date->next_month();
+ if (period_type_param == PERIOD_YEARLY) {
+ max_date->next_year();
+ }
+ else {
+ max_date->next_month();
+ }
cur_data = NULL;
res_data = NULL;
cnt = 0;
while(cur_date->before(max_date)) {
- store = new StoreDay(device_id, cur_date);
- dr = store->get_mean(PERIOD_DAILY);
+ if (period_type_param == PERIOD_YEARLY) {
+ store = new StoreCache(device_id, cur_date);
+ dr = store->get_mean(PERIOD_MONTHLY);
+ }
+ else {
+ store = new StoreDay(device_id, cur_date);
+ dr = store->get_mean(PERIOD_DAILY);
+ }
if (dr != NULL) {
cur_data = dr->get_first();
if (cur_data != NULL) {
cnt++;
if (res_data == NULL) {
res_data = cur_data;
+ val_cnt = res_data->get_value_count();
}
else {
- val_cnt = res_data->get_value_count();
for (ii = 0; ii < val_cnt; ii++) {
res_data->value_arr[ii] = res_data->value_arr[ii] + cur_data->value_arr[ii];
}
delete(dr);
}
delete(store);
- cur_date->next_day();
+ if (period_type_param == PERIOD_YEARLY) {
+ cur_date->next_month();
+ }
+ else {
+ cur_date->next_day();
+ }
}
if ((res_data != NULL) &&
(cnt > 0)) {
case PERIOD_DAILY:
case PERIOD_HOURLY:
case PERIOD_MINUTELY:
- case PERIOD_SECONDLY:
- store = new StoreDay(device_id, date);
- ret_val = store->get_mean(period_type_param);
- save(fname, ret_val, 4);
- delete(store);
+ case PERIOD_SECONDLY: {
+ StoreDay *store;
+
+ store = new StoreDay(device_id, date);
+ ret_val = store->get_mean(period_type_param);
+ save(fname, ret_val, 4);
+ delete(store);
+ }
break;
}
}
DataRange *ret_val;
Data *dta;
string fname;
- StoreDay *store;
+ Store *store;
ret_val = NULL;
fname = get_file_name(device_id,
if (ret_val == NULL) {
switch(period_type_param) {
case PERIOD_YEARLY:
- break;
case PERIOD_MONTHLY: {
Data *cur_data;
Data *res_data;
cur_date = date->clone();
max_date = date->clone();
- max_date->next_month();
+ if (period_type_param == PERIOD_YEARLY) {
+ max_date->next_year();
+ }
+ else {
+ max_date->next_month();
+ }
cur_data = NULL;
res_data = NULL;
cnt = 0;
while(cur_date->before(max_date)) {
- store = new StoreDay(device_id, cur_date);
- dr = store->get_sum(PERIOD_DAILY);
+ if (period_type_param == PERIOD_YEARLY) {
+ store = new StoreCache(device_id, cur_date);
+ dr = store->get_sum(PERIOD_MONTHLY);
+ }
+ else {
+ store = new StoreDay(device_id, cur_date);
+ dr = store->get_sum(PERIOD_DAILY);
+ }
if (dr != NULL) {
cur_data = dr->get_first();
if (cur_data != NULL) {
delete(dr);
}
delete(store);
- cur_date->next_day();
+ if (period_type_param == PERIOD_YEARLY) {
+ cur_date->next_month();
+ }
+ else {
+ cur_date->next_day();
+ }
}
if ((res_data != NULL) &&
(cnt > 0)) {
DataRange *ret_val;
Data *dta;
string fname;
- StoreDay *store;
+ StoreDay *store;
ret_val = NULL;
fname = get_file_name(device_id,
}
if (ret_val == NULL) {
switch(period_type_param) {
- case PERIOD_YEARLY:
+ case PERIOD_YEARLY: {
+ Data *first_data;
+ Data *last_data;
+ int ii;
+ int cnt;
+
+ first_data = get_year_oldest_data();
+ if (first_data != NULL) {
+ last_data = get_year_newest_data();
+ if (last_data != NULL) {
+ ret_val = new DataRange();
+ cnt = last_data->get_value_count();
+ for (ii = 0; ii < cnt; ii++) {
+ last_data->value_arr[ii] = last_data->value_arr[ii] - first_data->value_arr[ii];
+ }
+ ret_val->add(last_data);
+ delete(last_data);
+ }
+ delete(first_data);
+ save(fname, ret_val, 4);
+ }
+ else {
+ log_error("Could not read first or last data item from device %s for year: %d", device_id.c_str(), date->year);
+ }
+ }
break;
case PERIOD_MONTHLY: {
Data *first_data;
int ii;
int cnt;
- cur_date = date->clone();
+ cur_date = date->clone();
limit_date = date->clone();
limit_date->next_month();
first_data = NULL;
DataRange *ret_val;
Data *dta;
string fname;
- StoreDay *store;
+ Store *store;
ret_val = NULL;
fname = get_file_name(device_id,
if (ret_val == NULL) {
switch(period_type_param) {
case PERIOD_YEARLY:
- break;
case PERIOD_MONTHLY: {
Data *cur_data;
Data *res_data;
cur_date = date->clone();
max_date = date->clone();
- max_date->next_month();
+ if (period_type_param == PERIOD_YEARLY) {
+ max_date->next_year();
+ }
+ else {
+ max_date->next_month();
+ }
cur_data = NULL;
res_data = NULL;
cnt = 0;
while(cur_date->before(max_date)) {
- store = new StoreDay(device_id, cur_date);
- dr = store->get_max(PERIOD_DAILY);
+ if (period_type_param == PERIOD_YEARLY) {
+ store = new StoreCache(device_id, cur_date);
+ dr = store->get_max(PERIOD_MONTHLY);
+ }
+ else {
+ store = new StoreDay(device_id, cur_date);
+ dr = store->get_max(PERIOD_DAILY);
+ }
if (dr != NULL) {
cur_data = dr->get_first();
if (cur_data != NULL) {
delete(dr);
}
delete(store);
- cur_date->next_day();
+ if (period_type_param == PERIOD_YEARLY) {
+ cur_date->next_month();
+ }
+ else {
+ cur_date->next_day();
+ }
}
if ((res_data != NULL) &&
(cnt > 0)) {
DataRange *ret_val;
Data *dta;
string fname;
- StoreDay *store;
+ Store *store;
ret_val = NULL;
fname = get_file_name(device_id,
if (ret_val == NULL) {
switch(period_type_param) {
case PERIOD_YEARLY:
- break;
case PERIOD_MONTHLY: {
Data *cur_data;
Data *res_data;
cur_date = date->clone();
max_date = date->clone();
- max_date->next_month();
+ if (period_type_param == PERIOD_YEARLY) {
+ max_date->next_year();
+ }
+ else {
+ max_date->next_month();
+ }
cur_data = NULL;
res_data = NULL;
cnt = 0;
while(cur_date->before(max_date)) {
- store = new StoreDay(device_id, cur_date);
- dr = store->get_min(PERIOD_DAILY);
+ if (period_type_param == PERIOD_YEARLY) {
+ store = new StoreCache(device_id, cur_date);
+ dr = store->get_min(PERIOD_MONTHLY);
+ }
+ else {
+ store = new StoreDay(device_id, cur_date);
+ dr = store->get_min(PERIOD_DAILY);
+ }
if (dr != NULL) {
cur_data = dr->get_first();
if (cur_data != NULL) {
delete(dr);
}
delete(store);
- cur_date->next_day();
+ if (period_type_param == PERIOD_YEARLY) {
+ cur_date->next_month();
+ }
+ else {
+ cur_date->next_day();
+ }
}
if ((res_data != NULL) &&
(cnt > 0)) {
cnt = datarange_param->get_count();
ostream = NULL;
- log_info("[%s] cacheing %d data values.\n", device_id.c_str(), cnt);
+ //log_info("[%s] cacheing %d data values.\n", device_id.c_str(), cnt);
ostream = W1Util::open_for_writing(fname_param.c_str());
if ((ostream != NULL) &&
(ostream->is_open() == true)) {
delete(ostream);
}
}
+
+/**
+ * Find oldest data in certain year
+ */
+Data *StoreCache::get_year_oldest_data() {
+ Data *ret_val;
+ 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);
+ return ret_val;
+}
+
+/**
+ * Find newest data in certain year
+ */
+Data *StoreCache::get_year_newest_data() {
+ Data *ret_val;
+ 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);
+ return ret_val;
+}
plp::DataRange *get_max(EnumSummaryPeriod period_type_param);
plp::DataRange *get_min(EnumSummaryPeriod period_type_param);
protected:
+ plp::Data *get_year_oldest_data();
+ plp::Data *get_year_newest_data();
static std::string get_dir_name(std::string device_id_param,
Date *date_time_param,
EnumSummaryPeriod period_type_param,
if (store_data != NULL) {
row_count = store_data->get_count();
if (row_count > 0) {
- o_data = store_data->get(0);
- n_data = store_data->get(row_count - 1);
+ o_data = store_data->get(0);
+ n_data = store_data->get(row_count - 1);
}
}
else {
if (range_data != NULL) {
row_count = range_data->get_count();
if (row_count > 0) {
- o_data = range_data->get(0);
- n_data = range_data->get(row_count - 1);
+ o_data = range_data->get(0);
+ n_data = range_data->get(row_count - 1);
}
}
else {