-
Notifications
You must be signed in to change notification settings - Fork 6
/
Statistics.cpp
164 lines (143 loc) · 4.23 KB
/
Statistics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// QWalkingPad - Simple desktop application for controlling the Kingsmith WalkingPad over BLE
// Copyright (C) 2021 Dorian Rudolph
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
#include "Statistics.h"
#include <QLoggingCategory>
#include "Protocol.h"
namespace Pad {
Statistics::Data Statistics::Data::operator+(const Statistics::Data &other) const {
return Statistics::Data{
.duration = duration + other.duration,
.distance = distance + other.distance,
.steps = steps + other.steps,
};
}
Statistics::Data &Statistics::Data::operator+=(const Statistics::Data &rhs) {
duration += rhs.duration;
distance += rhs.distance;
steps += rhs.steps;
return *this;
}
Statistics::Data Statistics::today() {
Data dat{};
auto today = QDate::currentDate();
for (const auto &stat : stats) {
if (stat.date.date() == today) {
dat += stat.data;
}
}
return dat;
}
Statistics::Data Statistics::allTime() {
Data dat{};
for (const auto &stat : stats) {
dat += stat.data;
}
return dat;
}
static bool parseStat(const QByteArray &line, Statistics::Stat &stat) {
auto split = line.trimmed().split(',');
if (split.size() != 4 || !(stat.date = QDateTime::fromString(split[0].trimmed(), Qt::ISODate)).isValid()) {
return false;
}
bool ok1, ok2, ok3;
stat.data.duration = split[1].toInt(&ok1);
stat.data.distance = split[2].toInt(&ok2);
stat.data.steps = split[3].toInt(&ok3);
return ok1 && ok2 && ok3;
}
static QByteArray stringifyStat(const Statistics::Stat &stat) {
QByteArray b{stat.date.toString(Qt::ISODate).toUtf8()};
b.push_back(',');
b.push_back(QByteArray::number(stat.data.duration));
b.push_back(',');
b.push_back(QByteArray::number(stat.data.distance));
b.push_back(',');
b.push_back(QByteArray::number(stat.data.steps));
b.push_back('\n');
return b;
}
constexpr auto HEADER = "Date,Duration,Distance,Steps\n";
void Statistics::load(const QString &path) {
file = std::make_unique<QFile>(path);
if (!file->open(QFile::ReadWrite | QFile::Text)) {
qWarning() << "Failed to open file" << path;
file = nullptr;
return;
}
int lineNo{};
while (!file->atEnd()) {
auto line = file->readLine();
if (line.back() != '\n') {
file->write("\n");
line.append('\n');
}
if (line == "\n") continue;
if (lineNo++ == 0) {
if (line != HEADER) goto readError;
} else {
Stat stat;
if (parseStat(line, stat)) {
stats.push_back(stat);
} else {
goto readError;
}
}
}
if (lineNo) return;
readError:
stats.clear();
file->resize(0);
file->write(HEADER);
}
bool Statistics::hasFile() {
return !!file;
}
void Statistics::addStat(const Stat &stat) {
stats.push_back(stat);
if (file != nullptr) {
writeStat(stat);
}
}
void Statistics::addRecord(const Record &record) {
addStat(Stat::fromRecord(record));
}
void Statistics::changePath(const QString &path) {
file = std::make_unique<QFile>(path);
if (!file->open(QFile::WriteOnly | QFile::Text)) {
qWarning() << "Failed to open file" << path;
file = nullptr;
return;
}
file->write(HEADER);
for (const auto &stat : stats) {
writeStat(stat);
}
}
void Statistics::writeStat(const Statistics::Stat &stat) {
file->write(stringifyStat(stat));
file->flush();
}
Statistics::Stat Statistics::Stat::fromRecord(const Record &record) {
return {
.date = QDateTime::currentDateTime().addMSecs((qint64)record.startTime - (qint64)record.onTime),
.data = {
.duration = static_cast<int>(record.duration),
.distance = static_cast<int>(record.distance * 10),
.steps = static_cast<int>(record.steps),
}
};
}
}