dbstat.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /*!
  20. * \brief retreiveTotalTime Looks up Total Blocktime in the flights database
  21. * \return Amount of Total Block Time in blockminutes
  22. */
  23. QString dbStat::retreiveTotalTime()
  24. {
  25. QSqlQuery query;
  26. query.prepare("SELECT SUM(tblk) FROM flights");
  27. query.exec();
  28. QString result;
  29. while (query.next()){
  30. result = query.value(0).toString();
  31. }
  32. return result;
  33. }
  34. /*!
  35. * \brief retreiveTotalTimeThisCalendarYear Looks up Total Block Time in
  36. * the current calendar year
  37. * \return Total Amount of Blockminutes
  38. */
  39. QString dbStat::retreiveTotalTimeThisCalendarYear()
  40. {
  41. QDate start;
  42. start.setDate(QDate::currentDate().year(),1,1);
  43. QString startdate = start.toString(Qt::ISODate);
  44. QSqlQuery query;
  45. query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
  46. query.addBindValue(startdate);
  47. query.exec();
  48. QString result;
  49. while (query.next()){
  50. result = query.value(0).toString();
  51. }
  52. qDebug() << "db::retreiveTotalTimeThisCalendarYear: Total minutes: " << result;
  53. return result;
  54. }
  55. /*!
  56. * \brief retreiveTotalTimeRollingYear Looks up Total Time in the last 365 days.
  57. * \return Total Blockminutes
  58. */
  59. QString dbStat::retreiveTotalTimeRollingYear()
  60. {
  61. QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 365);
  62. QString startdate = start.toString(Qt::ISODate);
  63. QSqlQuery query;
  64. query.prepare("SELECT SUM(tblk) FROM flights WHERE doft >= ?");
  65. query.addBindValue(startdate);
  66. query.exec();
  67. QString result;
  68. while (query.next()){
  69. result = query.value(0).toString();
  70. }
  71. qDebug() << "db::retreiveTotalTimeRollingYear: Total minutes: " << result;
  72. return result;
  73. }
  74. /*!
  75. * \brief retreiveCurrencyTakeoffLanding Looks up the amount of Take Offs and
  76. * Landings performed in the last 90 days.
  77. * \return {TO,LDG}
  78. */
  79. QVector<QString> dbStat::retreiveCurrencyTakeoffLanding()
  80. {
  81. QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 90);
  82. QSqlQuery query;
  83. query.prepare("SELECT SUM(extras.TOday) + SUM(extras.TOnight) AS 'TO', "
  84. "SUM(extras.LDGday) + SUM(extras.LDGnight) AS 'LDG' "
  85. "FROM flights "
  86. "INNER JOIN extras on flights.id = extras.extras_id "
  87. "WHERE doft >= ?");
  88. query.addBindValue(start.toString(Qt::ISODate));
  89. query.exec();
  90. QVector<QString> result(2,"0"); // make sure to return a valid vector even if result 0
  91. while (query.next()){
  92. result.insert(0,query.value(0).toString());
  93. result.insert(1,query.value(1).toString());
  94. }
  95. qDebug() << "db::retreiveCurrencyTakeoffLanding: " << result[0] << " TO, " <<result[1] << " LDG";
  96. return result;
  97. }