dbstat.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "dbstat.h"
  19. #include "dbapi.h"
  20. /*!
  21. * \brief retreiveTotalTime Looks up Total Blocktime in the flights database
  22. * \return Amount of Total Block Time in blockminutes
  23. */
  24. QString dbStat::retreiveTotalTime()
  25. {
  26. QSqlQuery query;
  27. query.prepare("SELECT SUM(tblk) FROM flights");
  28. query.exec();
  29. QString result;
  30. while (query.next()){
  31. result = query.value(0).toString();
  32. }
  33. return result;
  34. }
  35. /*!
  36. * \brief retreiveTotalTimeThisCalendarYear Looks up Total Block Time in
  37. * the current calendar year
  38. * \return Total Amount of Blockminutes
  39. */
  40. QString dbStat::retreiveTotalTimeThisCalendarYear()
  41. {
  42. QDate start;
  43. start.setDate(QDate::currentDate().year(),1,1);
  44. QString startdate = start.toString(Qt::ISODate);
  45. QSqlQuery query;
  46. query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
  47. query.addBindValue(startdate);
  48. query.exec();
  49. QString result;
  50. while (query.next()){
  51. result = query.value(0).toString();
  52. }
  53. qDebug() << "db::retreiveTotalTimeThisCalendarYear: Total minutes: " << result;
  54. return result;
  55. }
  56. /*!
  57. * \brief retreiveTotalTimeRollingYear Looks up Total Time in the last 365 days.
  58. * \return Total Blockminutes
  59. */
  60. QString dbStat::retreiveTotalTimeRollingYear()
  61. {
  62. QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 365);
  63. QString startdate = start.toString(Qt::ISODate);
  64. QSqlQuery query;
  65. query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
  66. query.addBindValue(startdate);
  67. query.exec();
  68. QString result;
  69. while (query.next()){
  70. result = query.value(0).toString();
  71. }
  72. qDebug() << "db::retreiveTotalTimeRollingYear: Total minutes: " << result;
  73. return result;
  74. }
  75. /*!
  76. * \brief retreiveCurrencyTakeoffLanding Looks up the amount of Take Offs and
  77. * Landings performed in the last 90 days.
  78. * \return {TO,LDG}
  79. */
  80. QVector<QString> dbStat::retreiveCurrencyTakeoffLanding()
  81. {
  82. QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 90);
  83. QSqlQuery query;
  84. query.prepare("SELECT SUM(extras.TOday) + SUM(extras.TOnight) AS 'TO', "
  85. "SUM(extras.LDGday) + SUM(extras.LDGnight) AS 'LDG' "
  86. "FROM flights "
  87. "INNER JOIN extras on flights.id = extras.extras_id "
  88. "WHERE doft >= ?");
  89. query.addBindValue(start.toString(Qt::ISODate));
  90. query.exec();
  91. QVector<QString> result(2,"0"); // make sure to return a valid vector even if result 0
  92. while (query.next()){
  93. result.insert(0,query.value(0).toString());
  94. result.insert(1,query.value(1).toString());
  95. }
  96. qDebug() << "db::retreiveCurrencyTakeoffLanding: " << result[0] << " TO, " <<result[1] << " LDG";
  97. return result;
  98. }