debugwidget.cpp 6.7 KB

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