debugwidget.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "debugwidget.h"
  2. #include "ui_debugwidget.h"
  3. #include "src/classes/astandardpaths.h"
  4. #include "src/gui/widgets/logbookwidget.h"
  5. #include "src/gui/widgets/pilotswidget.h"
  6. #include "src/gui/widgets/aircraftwidget.h"
  7. DebugWidget::DebugWidget(QWidget *parent) :
  8. QWidget(parent),
  9. ui(new Ui::DebugWidget)
  10. {
  11. ui->setupUi(this);
  12. for (const auto& table : aDB()->getTableNames()) {
  13. if( table != "sqlite_sequence") {
  14. ui->tableComboBox->addItem(table);
  15. }
  16. }
  17. }
  18. DebugWidget::~DebugWidget()
  19. {
  20. delete ui;
  21. }
  22. void DebugWidget::on_resetUserTablesPushButton_clicked()
  23. {
  24. ATimer timer(this);
  25. QMessageBox result;
  26. if (ADataBaseSetup::resetToDefault()){
  27. result.setText("Database successfully reset");
  28. result.exec();
  29. touchDatabase();
  30. } else {
  31. result.setText("Errors have occurred. Check console for Debug output. ");
  32. result.exec();
  33. }
  34. }
  35. void DebugWidget::on_resetDatabasePushButton_clicked()
  36. {
  37. ATimer timer(this);
  38. QMessageBox message_box(this);
  39. // download latest csv
  40. QString link_stub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
  41. link_stub.append(ui->branchLineEdit->text()); // optionally select branch for development
  42. link_stub.append("/assets/database/templates/");
  43. QStringList template_tables = {"aircraft", "airports", "changelog"};
  44. QDir template_dir(AStandardPaths::absPathOf(AStandardPaths::Templates));
  45. for (const auto& table : template_tables) {
  46. QEventLoop loop;
  47. ADownload* dl = new ADownload;
  48. QObject::connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
  49. dl->setTarget(QUrl(link_stub % table % QStringLiteral(".csv")));
  50. dl->setFileName(template_dir.filePath(table % QStringLiteral(".csv")));
  51. dl->download();
  52. loop.exec(); // event loop waits for download done signal before allowing loop to continue
  53. dl->deleteLater();
  54. }
  55. // back up old db
  56. aDB()->disconnect();
  57. ADataBaseSetup::backupOldData();
  58. // re-connct and create new database
  59. aDB()->connect();
  60. if (ADataBaseSetup::createDatabase()) {
  61. DEB << "Database has been successfully created.";
  62. } else {
  63. message_box.setText("Errors have ocurred creating the database.<br>"
  64. "Check console for details.");
  65. message_box.exec();
  66. }
  67. if (ADataBaseSetup::importDefaultData()) {
  68. message_box.setText("Database has been successfully reset.");
  69. touchDatabase();
  70. message_box.exec();
  71. } else {
  72. message_box.setText("Errors have ocurred while importing templates.<br>"
  73. "Check console for details.");
  74. message_box.exec();
  75. }
  76. }
  77. void DebugWidget::downloadFinished()
  78. {
  79. }
  80. void DebugWidget::on_fillUserDataPushButton_clicked()
  81. {
  82. ATimer timer(this);
  83. QMessageBox message_box(this);
  84. // download latest csv
  85. QStringList userTables = {"pilots", "tails", "flights"};
  86. QString linkStub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
  87. linkStub.append(ui->branchLineEdit->text());
  88. linkStub.append("/assets/database/templates/sample_");
  89. QDir template_dir(AStandardPaths::absPathOf(AStandardPaths::Templates));
  90. for (const auto& table : userTables) {
  91. QEventLoop loop;
  92. ADownload* dl = new ADownload;
  93. connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
  94. dl->setTarget(QUrl(linkStub + table + ".csv"));
  95. dl->setFileName(template_dir.filePath("sample_" + table % QStringLiteral(".csv")));
  96. //dl->setFileName("data/templates/sample_" + table + ".csv");
  97. dl->download();
  98. loop.exec(); // event loop waits for download done signal before allowing loop to continue
  99. dl->deleteLater();
  100. }
  101. QBitArray allGood;
  102. allGood.resize(userTables.size());
  103. for (const auto& table : userTables) {
  104. auto data = aReadCsv(AStandardPaths::absPathOf(AStandardPaths::Templates)
  105. + "/sample_" + table + ".csv");
  106. allGood.setBit(userTables.indexOf(table), ADataBaseSetup::commitData(data, table));
  107. }
  108. if (allGood.count(true) != userTables.size()) {
  109. message_box.setText("Errors have ocurred. Check console for details.");
  110. message_box.exec();
  111. return;
  112. }
  113. message_box.setText("User tables successfully populated.");
  114. message_box.exec();
  115. touchDatabase();
  116. }
  117. void DebugWidget::on_selectCsvPushButton_clicked()
  118. {
  119. auto fileName = QFileDialog::getOpenFileName(this,
  120. tr("Open CSV File for import"),
  121. AStandardPaths::absPathOf(AStandardPaths::Templates),
  122. tr("CSV files (*.csv)"));
  123. ui->importCsvLineEdit->setText(fileName);
  124. }
  125. void DebugWidget::on_importCsvPushButton_clicked()
  126. {
  127. ATimer timer(this);
  128. auto file = QFileInfo(ui->importCsvLineEdit->text());
  129. DEB << "File exists/is file: " << file.exists() << file.isFile() << " Path: " << file.absoluteFilePath();
  130. if (file.exists() && file.isFile()) {
  131. if (ADataBaseSetup::commitData(aReadCsv(file.absoluteFilePath()), ui->tableComboBox->currentText())) {
  132. auto mb = QMessageBox(this);
  133. mb.setText("Data inserted successfully.");
  134. mb.exec();
  135. } else {
  136. auto mb = QMessageBox(this);
  137. mb.setText("Errors have ocurred. Check console for details.");
  138. mb.exec();
  139. }
  140. } else {
  141. auto mb = QMessageBox(this);
  142. mb.setText("Please select a valid file.");
  143. mb.exec();
  144. }
  145. }
  146. void DebugWidget::on_debugPushButton_clicked()
  147. {
  148. touchDatabase();
  149. }
  150. /*!
  151. * \brief Acess the database to trigger aDB()::databaseUpdated
  152. */
  153. void DebugWidget::touchDatabase()
  154. {
  155. QMap<QString, QVariant> debugData;
  156. debugData.insert("lastname","debugLastName");
  157. debugData.insert("firstname","debugFirstName");
  158. auto pilot = APilotEntry(1);
  159. pilot.setData(debugData);
  160. aDB()->commit(pilot);
  161. }
  162. /* //Comparing two functions template
  163. qlonglong number_of_runs = 5000;
  164. long time1 = 0;
  165. long time2 = 0;
  166. {
  167. ATimer timer;
  168. for (int i = 0; i < number_of_runs; i++) {
  169. // first block, do stuff here...
  170. }
  171. time1 = timer.timeNow();
  172. }
  173. {
  174. ATimer timer;
  175. for (int i = 0; i < number_of_runs; i++) {
  176. // second block, do stuff here...
  177. }
  178. time2 = timer.timeNow();
  179. }
  180. DEB << "First block executed " << number_of_runs << " times for a total of " << time1 << " milliseconds.");
  181. DEB << "Second block executed " << number_of_runs << " times for a total of " << time2 << " milliseconds.");
  182. */
  183. /*qlonglong number_of_runs = 500;
  184. long time1 = 0;
  185. {
  186. ATimer timer;
  187. for (int i = 0; i < number_of_runs; i++) {
  188. // first block, do stuff here...
  189. auto acft = aDB()->getTailEntry(5);
  190. auto pilot = aDB()->getPilotEntry(7);
  191. auto flight = aDB()->getFlightEntry(15);
  192. QList<AEntry> list = {acft, pilot, flight};
  193. for (auto entry : list) {
  194. for (auto column : entry.getData()) {
  195. QString value = column.toString();
  196. }
  197. }
  198. }
  199. time1 = timer.timeNow();
  200. }
  201. DEB << "First block executed " << number_of_runs << " times for a total of " << time1 << " milliseconds.");
  202. // 108 - 134 milliseconds with legacy exp db api
  203. // 108 - 110 milliseconds with improved exp api
  204. // to do: with string literals*/