Browse Source

added statistics class

fiffty-50 4 years ago
parent
commit
b74d13670c
5 changed files with 148 additions and 97 deletions
  1. 0 92
      dbman.cpp
  2. 113 0
      dbstat.cpp
  3. 27 0
      dbstat.h
  4. 6 5
      homewidget.cpp
  5. 2 0
      openLog.pro

+ 0 - 92
dbman.cpp

@@ -310,98 +310,6 @@ public:
             return true;
         }
     }
-
-    /*!
-     * \brief retreiveTotalTime Looks up Total Blocktime in the flights database
-     * \return Amount of Total Block Time in blockminutes
-     */
-    static QString retreiveTotalTime()
-    {
-        QSqlQuery query;
-        query.prepare("SELECT SUM(tblk) FROM flights");
-        query.exec();
-
-        QString result;
-        while (query.next()){
-          result = query.value(0).toString();
-        }
-        return result;
-    }
-
-    /*!
-     * \brief retreiveTotalTimeThisCalendarYear Looks up Total Block Time in
-     * the current calendar year
-     * \return Total Amount of Blockminutes
-     */
-    static QString retreiveTotalTimeThisCalendarYear()
-    {
-        QDate today = QDate::currentDate();
-        QDate start;
-        start.setDate(today.year(),1,1);
-        QString startdate = start.toString(Qt::ISODate);
-
-        QSqlQuery query;
-        query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
-        query.addBindValue(startdate);
-        query.exec();
-
-        QString result;
-        while (query.next()){
-            result = query.value(0).toString();
-        }
-
-        qDebug() << "db::retreiveTotalTimeThisCalendarYear: Total minutes: " << result;
-        return result;
-    }
-    /*!
-     * \brief retreiveTotalTimeRollingYear Looks up Total Time in the last 365 days.
-     * \return Total Blockminutes
-     */
-    static QString retreiveTotalTimeRollingYear()
-    {
-        QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 365);
-        QString startdate = start.toString(Qt::ISODate);
-
-        QSqlQuery query;
-        query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
-        query.addBindValue(startdate);
-        query.exec();
-
-        QString result;
-        while (query.next()){
-            result = query.value(0).toString();
-        }
-
-        qDebug() << "db::retreiveTotalTimeRollingYear: Total minutes: " << result;
-        return result;
-    }
-    /*!
-     * \brief retreiveCurrencyTakeoffLanding Looks up the amount of Take Offs and
-     * Landings performed in the last 90 days.
-     * \return {TO,LDG}
-     */
-    static QVector<QString> retreiveCurrencyTakeoffLanding()
-    {
-        QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 90);
-
-        QSqlQuery query;
-        query.prepare("SELECT "
-                      "SUM(extras.TOday + extras.TOnight) AS 'TakeOFF', "
-                      "SUM(extras.LDGday + extras.LDGnight) AS 'Landing' "
-                      "FROM flights "
-                      "INNER JOIN extras on flights.id = extras.extras_id "
-                      "WHERE doft >= ?");
-        query.addBindValue(start.toString(Qt::ISODate));
-        query.exec();
-
-        QVector<QString> result(2,"0"); // make sure to return a valid vector even if result 0
-        while (query.next()){
-            result.insert(0,query.value(0).toString());
-            result.insert(1,query.value(1).toString());
-        }
-        qDebug() << "db::retreiveCurrencyTakeoffLanding: " << result[0] << " TO, " <<result[1] << " LDG";
-        return result;
-    }
     /*
      * Pilots Database Related Functions
      */

+ 113 - 0
dbstat.cpp

@@ -0,0 +1,113 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *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 <https://www.gnu.org/licenses/>.
+ */
+#include "dbstat.h"
+#include "dbman.cpp"
+
+/*!
+ * \brief retreiveTotalTime Looks up Total Blocktime in the flights database
+ * \return Amount of Total Block Time in blockminutes
+ */
+QString dbStat::retreiveTotalTime()
+{
+    QSqlQuery query;
+    query.prepare("SELECT SUM(tblk) FROM flights");
+    query.exec();
+
+    QString result;
+    while (query.next()){
+      result = query.value(0).toString();
+    }
+    return result;
+}
+
+/*!
+ * \brief retreiveTotalTimeThisCalendarYear Looks up Total Block Time in
+ * the current calendar year
+ * \return Total Amount of Blockminutes
+ */
+QString dbStat::retreiveTotalTimeThisCalendarYear()
+{
+    QDate today = QDate::currentDate();
+    QDate start;
+    start.setDate(today.year(),1,1);
+    QString startdate = start.toString(Qt::ISODate);
+
+    QSqlQuery query;
+    query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
+    query.addBindValue(startdate);
+    query.exec();
+
+    QString result;
+    while (query.next()){
+        result = query.value(0).toString();
+    }
+
+    qDebug() << "db::retreiveTotalTimeThisCalendarYear: Total minutes: " << result;
+    return result;
+}
+
+/*!
+ * \brief retreiveTotalTimeRollingYear Looks up Total Time in the last 365 days.
+ * \return Total Blockminutes
+ */
+QString dbStat::retreiveTotalTimeRollingYear()
+{
+    QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 365);
+    QString startdate = start.toString(Qt::ISODate);
+
+    QSqlQuery query;
+    query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
+    query.addBindValue(startdate);
+    query.exec();
+
+    QString result;
+    while (query.next()){
+        result = query.value(0).toString();
+    }
+
+    qDebug() << "db::retreiveTotalTimeRollingYear: Total minutes: " << result;
+    return result;
+}
+
+/*!
+ * \brief retreiveCurrencyTakeoffLanding Looks up the amount of Take Offs and
+ * Landings performed in the last 90 days.
+ * \return {TO,LDG}
+ */
+QVector<QString> dbStat::retreiveCurrencyTakeoffLanding()
+{
+    QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 90);
+
+    QSqlQuery query;
+    query.prepare("SELECT "
+                  "SUM(extras.TOday + extras.TOnight) AS 'TakeOFF', "
+                  "SUM(extras.LDGday + extras.LDGnight) AS 'Landing' "
+                  "FROM flights "
+                  "INNER JOIN extras on flights.id = extras.extras_id "
+                  "WHERE doft >= ?");
+    query.addBindValue(start.toString(Qt::ISODate));
+    query.exec();
+
+    QVector<QString> result(2,"0"); // make sure to return a valid vector even if result 0
+    while (query.next()){
+        result.insert(0,query.value(0).toString());
+        result.insert(1,query.value(1).toString());
+    }
+    qDebug() << "db::retreiveCurrencyTakeoffLanding: " << result[0] << " TO, " <<result[1] << " LDG";
+    return result;
+}

+ 27 - 0
dbstat.h

@@ -0,0 +1,27 @@
+#ifndef DBSTAT_H
+#define DBSTAT_H
+
+#include <QCoreApplication>
+#include <QDebug>
+#include <QSqlDatabase>
+#include <QSqlDriver>
+#include <QSqlError>
+#include <QSqlQuery>
+
+/*!
+ * \brief The dbStat class provides functionality for retreiving various statistics
+ * from the database. In general, most values are provided as either QString or
+ * QVector<QString>.
+ *
+ */
+class dbStat
+{
+public:
+    dbStat();
+    static QString retreiveTotalTime();
+    static QString retreiveTotalTimeThisCalendarYear();
+    static QString retreiveTotalTimeRollingYear();
+    static QVector<QString> retreiveCurrencyTakeoffLanding();
+};
+
+#endif // DBSTAT_H

+ 6 - 5
homewidget.cpp

@@ -2,6 +2,7 @@
 #include "ui_homewidget.h"
 #include "calc.h"
 #include "dbman.cpp"
+#include "dbstat.h"
 
 #include <QDebug>
 
@@ -17,17 +18,17 @@ homeWidget::homeWidget(QWidget *parent) :
      * To Do: Functions to retreive values from DB
      */
 
-    ui->totalTimeDisplayLabel->setText(calc::minutes_to_string(db::retreiveTotalTime()));
+    ui->totalTimeDisplayLabel->setText(calc::minutes_to_string(dbStat::retreiveTotalTime()));
 
-    QString blockMinutesThisYear = db::retreiveTotalTimeThisCalendarYear();
+    QString blockMinutesThisYear = dbStat::retreiveTotalTimeThisCalendarYear();
     ui->blockHoursCalDisplayLabel->setText(calc::minutes_to_string(blockMinutesThisYear));
     if (blockMinutesThisYear.toInt() > 900*60) {
         qDebug() << "More than 900 block hours this calendar year!";
         // set Text Red
     }
-    QString blockMinutesRollingYear = db::retreiveTotalTimeRollingYear();
+    QString blockMinutesRollingYear = dbStat::retreiveTotalTimeRollingYear();
     ui->blockHoursRolDisplayLabel->setText(calc::minutes_to_string(blockMinutesRollingYear));
-    QVector<QString> currency = db::retreiveCurrencyTakeoffLanding();
+    QVector<QString> currency = dbStat::retreiveCurrencyTakeoffLanding();
     ui->currencyDisplayLabel->setText(currency[0] + " Take Offs\n" + currency[1] + " Landings");
     if (currency[0].toInt() < 3 || currency[1].toInt() < 3){
         qDebug() << "Less than 3 TO/LDG in last 90 days!";
@@ -42,5 +43,5 @@ homeWidget::~homeWidget()
 
 void homeWidget::on_debugButton_clicked()
 {
-    ui->debugLineEdit->setText(db::retreiveTotalTimeRollingYear());
+    ui->debugLineEdit->setText(dbStat::retreiveTotalTimeRollingYear());
 }

+ 2 - 0
openLog.pro

@@ -24,6 +24,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
 SOURCES += \
     calc.cpp \
     dbman.cpp \
+    dbstat.cpp \
     easaview.cpp \
     editflight.cpp \
     homewidget.cpp \
@@ -37,6 +38,7 @@ SOURCES += \
 
 HEADERS += \
     calc.h \
+    dbstat.h \
     easaview.h \
     editflight.h \
     homewidget.h \