stat.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "stat.h"
  19. /*!
  20. * \brief Stat::totalTime Looks up Total Blocktime in the flights database
  21. * \param yearType - Whether the calculation is based on total time, last
  22. * calendar year or the last rolling year
  23. * \return Amount of Total Block Time in minutes
  24. */
  25. QString Stat::totalTime(yearType yt)
  26. {
  27. QString query;
  28. QDate start;
  29. QString startdate;
  30. switch (yt) {
  31. case Stat::allYears:
  32. query = "SELECT SUM(tblk) FROM flights";
  33. break;
  34. case Stat::calendarYear:
  35. start.setDate(QDate::currentDate().year(), 1, 1);
  36. startdate = start.toString(Qt::ISODate);
  37. startdate.append(QLatin1Char('\''));
  38. startdate.prepend(QLatin1Char('\''));
  39. query = "SELECT SUM(tblk) FROM flights WHERE doft >= " + startdate;
  40. break;
  41. case Stat::rollingYear:
  42. start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 365);
  43. startdate = start.toString(Qt::ISODate);
  44. startdate.append(QLatin1Char('\''));
  45. startdate.prepend(QLatin1Char('\''));
  46. query = "SELECT SUM(tblk) FROM flights WHERE doft >= " + startdate;
  47. break;
  48. }
  49. QVector<QString> result = Db::customQuery(query, 1);
  50. if (!result.isEmpty()) {
  51. return result[0];
  52. } else {
  53. return QString();
  54. }
  55. }
  56. /*!
  57. * \brief Stat::currencyTakeOffLanding Returns the amount of Take Offs and
  58. * Landings performed in the last x days. Normally, 90 would be used. (EASA)
  59. * \param days Number of days to check
  60. * \return {TO,LDG}
  61. */
  62. QVector<QString> Stat::currencyTakeOffLanding(int days)
  63. {
  64. QDate start = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - days);
  65. QString startdate = start.toString(Qt::ISODate);
  66. startdate.append(QLatin1Char('\''));
  67. startdate.prepend(QLatin1Char('\''));
  68. QString query = "SELECT SUM(flights.TOday) + SUM(flights.TOnight) AS 'TO', "
  69. "SUM(flights.LDGday) + SUM(flights.LDGnight) AS 'LDG' "
  70. "FROM flights "
  71. "WHERE doft >= " + startdate;
  72. QVector<QString> result = Db::customQuery(query, 2);
  73. if (!result.isEmpty()) {
  74. return result;
  75. } else {
  76. return QVector<QString>();
  77. }
  78. }
  79. QVector<QPair<QString, QString>> Stat::totals()
  80. {
  81. QString statement = "SELECT "
  82. "printf('%02d',CAST(SUM(tblk) AS INT)/60)||':'||printf('%02d',CAST(SUM(tblk) AS INT)%60) AS 'TOTAL', "
  83. "printf('%02d',CAST(SUM(tSPSE) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSPSE) AS INT)%60) AS 'SP SE', "
  84. "printf('%02d',CAST(SUM(tSPME) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSPME) AS INT)%60) AS 'SP ME', "
  85. "printf('%02d',CAST(SUM(tNIGHT) AS INT)/60)||':'||printf('%02d',CAST(SUM(tNIGHT) AS INT)%60) AS 'NIGHT', "
  86. "printf('%02d',CAST(SUM(tIFR) AS INT)/60)||':'||printf('%02d',CAST(SUM(tIFR) AS INT)%60) AS 'IFR', "
  87. "printf('%02d',CAST(SUM(tPIC) AS INT)/60)||':'||printf('%02d',CAST(SUM(tPIC) AS INT)%60) AS 'PIC', "
  88. "printf('%02d',CAST(SUM(tPICUS) AS INT)/60)||':'||printf('%02d',CAST(SUM(tPICUS) AS INT)%60) AS 'PICUS', "
  89. "printf('%02d',CAST(SUM(tSIC) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSIC) AS INT)%60) AS 'SIC', "
  90. "printf('%02d',CAST(SUM(tDual) AS INT)/60)||':'||printf('%02d',CAST(SUM(tDual) AS INT)%60) AS 'DUAL', "
  91. "printf('%02d',CAST(SUM(tFI) AS INT)/60)||':'||printf('%02d',CAST(SUM(tFI) AS INT)%60) AS 'INSTRUCTOR', "
  92. "printf('%02d',CAST(SUM(tSIM) AS INT)/60)||':'||printf('%02d',CAST(SUM(tSIM) AS INT)%60) AS 'SIMULATOR', "
  93. "printf('%02d',CAST(SUM(tMP) AS INT)/60)||':'||printf('%02d',CAST(SUM(tMP) AS INT)%60) AS 'MultPilot', "
  94. "CAST(SUM(toDay) AS INT) AS 'TO Day', CAST(SUM(toNight) AS INT) AS 'TO Night', "
  95. "CAST(SUM(ldgDay) AS INT) AS 'LDG Day', CAST(SUM(ldgNight) AS INT) AS 'LDG Night' "
  96. "FROM flights";
  97. QVector<QString> columns = {"total", "spse", "spme", "night", "ifr",
  98. "pic", "picus", "sic", "dual", "fi", "sim", "multipilot",
  99. "today", "tonight", "ldgday", "ldgnight"
  100. };
  101. QSqlQuery q(statement);
  102. QVector<QPair<QString, QString>> output;
  103. QString value;
  104. q.next();
  105. for (const auto &column : columns) {
  106. value = q.value(columns.indexOf(column)).toString();
  107. if (!value.isEmpty()) {
  108. output << QPair{column, value};
  109. } else {
  110. output << QPair{column, QString("00:00")};
  111. }
  112. }
  113. return output;
  114. }