OSCAR-code/SleepLib/day.cpp

285 lines
6.7 KiB
C++
Raw Normal View History

2011-06-26 08:30:44 +00:00
/********************************************************************
SleepLib Day Class Implementation
Copyright (c)2011 Mark Watkins <jedimark@users.sourceforge.net>
License: GPL
*********************************************************************/
#include "day.h"
Day::Day(Machine *m)
:machine(m)
{
d_firstsession=true;
}
Day::~Day()
{
vector<Session *>::iterator s;
for (s=sessions.begin();s!=sessions.end();s++) {
delete (*s);
}
}
MachineType Day::machine_type()
{
return machine->GetType();
};
void Day::AddSession(Session *s)
{
if (!s) {
qWarning("Day::AddSession called with NULL session object");
return;
}
if (d_firstsession) {
d_firstsession=false;
d_first=s->first();
d_last=s->last();
} else {
if (d_first > s->first()) d_first = s->first();
if (d_last < s->last()) d_last = s->last();
}
sessions.push_back(s);
}
EventDataType Day::summary_sum(MachineCode code)
{
EventDataType val=0;
vector<Session *>::iterator s;
for (s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
if (sess.summary.find(code)!=sess.summary.end()) {
val+=sess.summary[code].toDouble();
}
}
return val;
}
EventDataType Day::summary_max(MachineCode code)
{
EventDataType val=0,tmp;
vector<Session *>::iterator s;
for (s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
if (sess.summary.find(code)!=sess.summary.end()) {
tmp=sess.summary[code].toDouble();
if (tmp>val) val=tmp;
}
}
return val;
}
EventDataType Day::summary_min(MachineCode code)
{
EventDataType val=0,tmp;
bool fir=true;
// Cache this?
vector<Session *>::iterator s;
for (s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
if (sess.summary.find(code)!=sess.summary.end()) {
tmp=sess.summary[code].toDouble();
if (fir) {
val=tmp;
fir=false;
} else {
if (val<tmp) val=tmp;
}
}
}
return val;
}
EventDataType Day::summary_avg(MachineCode code)
{
EventDataType val=0,tmp=0;
int cnt=0;
vector<Session *>::iterator s;
for (s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
if (sess.summary.find(code)!=sess.summary.end()) {
tmp+=sess.summary[code].toDouble();
cnt++;
}
}
val=tmp/EventDataType(cnt);
return val;
}
EventDataType Day::summary_weighted_avg(MachineCode code)
{
double s0=0,s1=0,s2=0;
for (vector<Session *>::iterator s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
if (sess.summary.find(code)!=sess.summary.end()) {
s0=sess.hours();
s1+=sess.summary[code].toDouble()*s0;
s2+=s0;
}
}
if (s2==0) return 0;
return (s1/s2);
}
2011-07-27 09:21:53 +00:00
EventDataType Day::avg(MachineCode code)
2011-06-26 08:30:44 +00:00
{
double val=0;
// Cache this?
int cnt=0;
vector<Session *>::iterator s;
// Don't assume sessions are in order.
for (s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
2011-07-27 09:21:53 +00:00
if (sess.eventlist.find(code)!=sess.eventlist.end()) {
val+=sess.avg(code);
2011-06-26 08:30:44 +00:00
cnt++;
}
}
if (cnt==0) return 0;
return EventDataType(val/float(cnt));
}
2011-07-27 09:21:53 +00:00
EventDataType Day::sum(MachineCode code)
2011-06-26 08:30:44 +00:00
{
// Cache this?
EventDataType val=0;
vector<Session *>::iterator s;
for (s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
2011-07-27 09:21:53 +00:00
if (sess.eventlist.find(code)!=sess.eventlist.end()) {
val+=sess.sum(code);
2011-06-26 08:30:44 +00:00
}
}
return val;
}
2011-07-27 09:21:53 +00:00
EventDataType Day::weighted_avg(MachineCode code)
2011-06-26 08:30:44 +00:00
{
double s0=0,s1=0,s2=0;
for (vector<Session *>::iterator s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
2011-07-27 09:21:53 +00:00
if (sess.eventlist.find(code)!=sess.eventlist.end()) {
2011-06-26 08:30:44 +00:00
s0=sess.hours();
2011-07-27 09:21:53 +00:00
s1+=sess.weighted_avg(code)*s0;
2011-06-26 08:30:44 +00:00
s2+=s0;
}
}
if (s2==0) return 0;
return (s1/s2);
}
2011-07-03 02:43:50 +00:00
// Total session time in milliseconds
qint64 Day::total_time()
2011-06-26 08:30:44 +00:00
{
2011-07-27 09:21:53 +00:00
qint64 d_totaltime=0;
2011-06-26 08:30:44 +00:00
for (vector<Session *>::iterator s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
d_totaltime+=sess.last()-sess.first();
2011-06-26 08:30:44 +00:00
}
return d_totaltime;
}
2011-07-27 09:21:53 +00:00
EventDataType Day::percentile(MachineCode code,double percent)
2011-06-26 08:30:44 +00:00
{
double val=0;
int cnt=0;
for (vector<Session *>::iterator s=sessions.begin();s!=sessions.end();s++) {
Session & sess=*(*s);
2011-07-27 09:21:53 +00:00
if (sess.eventlist.find(code)!=sess.eventlist.end()) {
val+=sess.percentile(code,percent);
2011-06-26 08:30:44 +00:00
cnt++;
}
}
if (cnt==0) return 0;
return EventDataType(val/cnt);
}
qint64 Day::first(MachineCode code)
2011-06-26 08:30:44 +00:00
{
qint64 date=0;
qint64 tmp;
2011-06-26 08:30:44 +00:00
for (vector<Session *>::iterator s=sessions.begin();s!=sessions.end();s++) {
2011-07-27 09:21:53 +00:00
tmp=(*s)->first(code);
if (!tmp) continue;
if (!date) {
date=tmp;
} else {
if (tmp<date) date=tmp;
2011-06-26 08:30:44 +00:00
}
}
return date;
}
qint64 Day::last(MachineCode code)
2011-06-26 08:30:44 +00:00
{
qint64 date=0;
qint64 tmp;
2011-06-26 08:30:44 +00:00
for (vector<Session *>::iterator s=sessions.begin();s!=sessions.end();s++) {
2011-07-27 09:21:53 +00:00
tmp=(*s)->last(code);
if (!tmp) continue;
if (!date) {
date=tmp;
} else {
if (tmp>date) date=tmp;
2011-06-26 08:30:44 +00:00
}
}
return date;
}
2011-07-27 09:21:53 +00:00
EventDataType Day::min(MachineCode code)
2011-06-26 08:30:44 +00:00
{
2011-07-27 09:21:53 +00:00
EventDataType min=0;
EventDataType tmp;
bool first=true;
for (vector<Session *>::iterator s=sessions.begin();s!=sessions.end();s++) {
if ((*s)->eventlist.find(code)==(*s)->eventlist.end()) continue;
tmp=(*s)->min(code);
if (first) {
min=tmp;
first=false;
} else {
if (tmp<min) min=tmp;
}
2011-06-26 08:30:44 +00:00
}
2011-07-27 09:21:53 +00:00
return min;
}
2011-06-26 08:30:44 +00:00
2011-07-27 09:21:53 +00:00
EventDataType Day::max(MachineCode code)
{
EventDataType max=0;
EventDataType tmp;
bool first=true;
for (vector<Session *>::iterator s=sessions.begin();s!=sessions.end();s++) {
if ((*s)->eventlist.find(code)==(*s)->eventlist.end()) continue;
tmp=(*s)->max(code);
if (first) {
max=tmp;
first=false;
} else {
if (tmp>max) max=tmp;
}
}
return max;
2011-06-26 08:30:44 +00:00
}
2011-07-27 09:21:53 +00:00
int Day::count(MachineCode code)
{
int sum=0;
for (unsigned i=0;i<sessions.size();i++) {
sum+=sessions[i]->count(code);
}
return sum;
}
void Day::OpenEvents()
2011-06-26 08:30:44 +00:00
{
vector<Session *>::iterator s;
for (s=sessions.begin();s!=sessions.end();s++) {
2011-07-27 09:21:53 +00:00
(*s)->OpenEvents();
2011-06-26 08:30:44 +00:00
}
2011-07-27 09:21:53 +00:00
2011-06-26 08:30:44 +00:00
}