diff options
author | Arseniy-Movshev <dodoradio@outlook.com> | 2023-03-12 00:33:58 +0000 |
---|---|---|
committer | Arseniy-Movshev <dodoradio@outlook.com> | 2023-03-12 11:51:49 +0000 |
commit | 55339aacf03642657e1f9c7d087f9b9fd319de4a (patch) | |
tree | cabe8dcc069d7d63a4978e22b4baab0df861d41b /logger.cpp | |
parent | 47ed16926ac86295000a51fe1360a0ff9e21d84a (diff) |
Change to having one record file per day and rewrite file access infrastructure to serve this.
I believe this is a useful improvement. The main two advantages are that a) it is very cheap to check whether a record exists for a given day - just check for file presence and b) there is a reasonable limit to the size of these files (never more than a few hundred records per day) which means that they can just be loaded into ram for processing without any complex splitting operations
Currently, the necessary `~/asteroid-healthloggerd/stepCounter` and `...loggerd/heartrateMonitor` directories are not automatically created and the code just vomits errors into log if it can't write to them.
Diffstat (limited to 'logger.cpp')
-rw-r--r-- | logger.cpp | 26 |
1 files changed, 18 insertions, 8 deletions
@@ -4,6 +4,7 @@ #include <QDateTime> #include <QFile> #include <QTextStream> +#include <QStandardPaths> #include <QTimer> #include <QDebug> @@ -52,33 +53,42 @@ void Logger::displayOn(QString displayState) { } } -void writeReadingToFile(QString data, QString filename) { - QFile file(filename); +void fileAddRecord(QString sensorPrefix, QString logdata, QDateTime recordTime) { //adds a record to today's log file for the given sensor + qDebug() << fileNameForDate(recordTime.date(), sensorPrefix); + QFile file(fileNameForDate(recordTime.date(), sensorPrefix)); if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) { qDebug() << "failed to open file"; return; } file.seek(file.size()); QTextStream out(&file); - out << data; + out << QString::number(recordTime.currentSecsSinceEpoch()) + ":" + logdata + "\n"; file.close(); } +bool dayFileExists(QString sensorPrefix, QDateTime dateTime) { + return QFile::exists(fileNameForDate(dateTime.date(), sensorPrefix)); +} -QString getLineFromFile(int lineNumber, QString filename) { - QFile file(filename); +QStringList fileGetPrevRecord(QString sensorPrefix, QDateTime recordTime) { + qDebug() << fileNameForDate(recordTime.date(), sensorPrefix); + QFile file(fileNameForDate(recordTime.date(), sensorPrefix)); if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) { qDebug() << "failed to open file"; - return "0 : 0"; + return {0,0}; } QTextStream inStream(&file); QString line; int i; - while(!inStream.atEnd() & (i < lineNumber | i < 0)) + while(!inStream.atEnd()) { line = inStream.readLine(); qDebug() << line; i++; } file.close(); - return line; + return line.split(":"); +} + +QString fileNameForDate(QDate date, QString prefix) { + return QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/asteroid-healthloggerd/" + prefix + "/" + date.toString("yyyy-MM-dd.log"); } |