debugwidget.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include <QtGlobal>
  8. DebugWidget::DebugWidget(QWidget *parent) :
  9. QWidget(parent),
  10. ui(new Ui::DebugWidget)
  11. {
  12. ui->setupUi(this);
  13. for (const auto& table : aDB->getTableNames()) {
  14. if( table != "sqlite_sequence") {
  15. ui->tableComboBox->addItem(table);
  16. }
  17. }
  18. }
  19. DebugWidget::~DebugWidget()
  20. {
  21. delete ui;
  22. }
  23. void DebugWidget::on_resetUserTablesPushButton_clicked()
  24. {
  25. ATimer timer(this);
  26. QMessageBox message_box(this);
  27. if (ADataBaseSetup::resetToDefault()){
  28. message_box.setText("Database successfully reset");
  29. message_box.exec();
  30. emit aDB->dataBaseUpdated();
  31. } else {
  32. message_box.setText("Errors have occurred. Check console for Debug output. ");
  33. message_box.exec();
  34. }
  35. }
  36. void DebugWidget::on_resetDatabasePushButton_clicked()
  37. {
  38. ATimer timer(this);
  39. QMessageBox message_box(this);
  40. // download latest csv
  41. QString link_stub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
  42. link_stub.append(ui->branchLineEdit->text()); // optionally select branch for development
  43. link_stub.append("/assets/database/templates/");
  44. QStringList template_tables = {"aircraft", "airports", "changelog"};
  45. QDir template_dir(AStandardPaths::absPathOf(AStandardPaths::Templates));
  46. for (const auto& table : template_tables) {
  47. QEventLoop loop;
  48. ADownload* dl = new ADownload;
  49. QObject::connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
  50. dl->setTarget(QUrl(link_stub % table % QStringLiteral(".csv")));
  51. dl->setFileName(template_dir.filePath(table % QStringLiteral(".csv")));
  52. dl->download();
  53. loop.exec(); // event loop waits for download done signal before allowing loop to continue
  54. dl->deleteLater();
  55. }
  56. // back up old db
  57. aDB->disconnect();
  58. ADataBaseSetup::backupOldData();
  59. // re-connct and create new database
  60. aDB->connect();
  61. if (ADataBaseSetup::createDatabase()) {
  62. DEB << "Database has been successfully created.";
  63. } else {
  64. message_box.setText("Errors have ocurred creating the database.<br>"
  65. "Check console for details.");
  66. message_box.exec();
  67. }
  68. if (ADataBaseSetup::importDefaultData()) {
  69. message_box.setText("Database has been successfully reset.");
  70. emit aDB->dataBaseUpdated();
  71. message_box.exec();
  72. } else {
  73. message_box.setText("Errors have ocurred while importing templates.<br>"
  74. "Check console for details.");
  75. message_box.exec();
  76. }
  77. }
  78. void DebugWidget::downloadFinished()
  79. {
  80. }
  81. void DebugWidget::on_fillUserDataPushButton_clicked()
  82. {
  83. ATimer timer(this);
  84. QMessageBox message_box(this);
  85. // download latest csv
  86. QStringList userTables = {"pilots", "tails", "flights"};
  87. QString linkStub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
  88. linkStub.append(ui->branchLineEdit->text());
  89. linkStub.append("/assets/database/templates/sample_");
  90. QDir template_dir(AStandardPaths::absPathOf(AStandardPaths::Templates));
  91. for (const auto& table : userTables) {
  92. QEventLoop loop;
  93. ADownload* dl = new ADownload;
  94. connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
  95. dl->setTarget(QUrl(linkStub + table + ".csv"));
  96. dl->setFileName(template_dir.filePath("sample_" % table % QStringLiteral(".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. emit aDB->dataBaseUpdated();
  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. QMessageBox message_box(this);
  133. message_box.setText("Data inserted successfully.");
  134. message_box.exec();
  135. } else {
  136. QMessageBox message_box(this);
  137. message_box.setText("Errors have ocurred. Check console for details.");
  138. message_box.exec();
  139. }
  140. } else {
  141. QMessageBox message_box(this);
  142. message_box.setText("Please select a valid file.");
  143. message_box.exec();
  144. }
  145. }
  146. void DebugWidget::on_debugPushButton_clicked()
  147. {
  148. // space for debugging
  149. }
  150. /* //Comparing two functions template
  151. qlonglong number_of_runs = 5000;
  152. long time1 = 0;
  153. long time2 = 0;
  154. {
  155. ATimer timer;
  156. for (int i = 0; i < number_of_runs; i++) {
  157. // first block, do stuff here...
  158. }
  159. time1 = timer.timeNow();
  160. }
  161. {
  162. ATimer timer;
  163. for (int i = 0; i < number_of_runs; i++) {
  164. // second block, do stuff here...
  165. }
  166. time2 = timer.timeNow();
  167. }
  168. DEB << "First block executed " << number_of_runs << " times for a total of " << time1 << " milliseconds.");
  169. DEB << "Second block executed " << number_of_runs << " times for a total of " << time2 << " milliseconds.");
  170. */
  171. /*
  172. *#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
  173. * DEB << QT_VERSION_MAJOR << QT_VERSION_MINOR << "At least 5.12";
  174. *#else
  175. * DEB << QT_VERSION_MAJOR << QT_VERSION_MINOR << "Less than 5.12";
  176. * #endif
  177. */