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