debugwidget.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "debugwidget.h"
  2. #include "ui_debugwidget.h"
  3. DebugWidget::DebugWidget(QWidget *parent) :
  4. QWidget(parent),
  5. ui(new Ui::DebugWidget)
  6. {
  7. ui->setupUi(this);
  8. for (const auto& table : DbInfo().tables) {
  9. if( table != "sqlite_sequence") {
  10. ui->tableComboBox->addItem(table);
  11. }
  12. }
  13. }
  14. DebugWidget::~DebugWidget()
  15. {
  16. delete ui;
  17. }
  18. void DebugWidget::on_resetUserTablesPushButton_clicked()
  19. {
  20. ATimer timer(this);
  21. QMessageBox result;
  22. if (ADataBaseSetup::resetToDefault()){
  23. result.setText("Database successfully reset.\n\nRestarting app.");
  24. result.exec();
  25. qApp->quit();
  26. QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
  27. } else {
  28. result.setText("Errors have occurred. Check console for Debug output. ");
  29. result.exec();
  30. }
  31. }
  32. void DebugWidget::on_resetDatabasePushButton_clicked()
  33. {
  34. ATimer timer(this);
  35. QMessageBox mb(this);
  36. //check if template dir exists and create if needed.
  37. QDir dir("data/templates");
  38. DEB(dir.path());
  39. if (!dir.exists())
  40. dir.mkpath(".");
  41. // download latest csv
  42. QStringList templateTables = {"aircraft", "airports", "changelog"};
  43. const auto& linkStub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/develop/assets/database/templates/";
  44. for (const auto& table : templateTables) {
  45. QEventLoop loop;
  46. ADownload* dl = new ADownload;
  47. connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
  48. dl->setTarget(QUrl(linkStub + table + ".csv"));
  49. dl->setFileName("data/templates/" + table + ".csv");
  50. dl->download();
  51. loop.exec(); // event loop waits for download done signal before allowing loop to continue
  52. dl->deleteLater();
  53. }
  54. //close database connection
  55. Db::disconnect();
  56. // back up old database
  57. auto oldDatabase = QFile("data/logbook.db");
  58. if (oldDatabase.exists()) {
  59. auto dateString = QDateTime::currentDateTime().toString(Qt::ISODate);
  60. DEB("Backing up old database as: " << "logbook-backup-" + dateString);
  61. if (!oldDatabase.rename("data/logbook-backup-" + dateString)) {
  62. DEB("Warning: Creating backup of old database has failed.");
  63. }
  64. }
  65. // re-connct and create new database
  66. Db::connect();
  67. if (ADataBaseSetup::createDatabase()) {
  68. mb.setText("Database has been successfully reset.\n\nRestarting application.");
  69. mb.exec();
  70. qApp->quit();
  71. QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
  72. } else {
  73. mb.setText("Errors have ocurred. Check console for details.");
  74. }
  75. }
  76. void DebugWidget::downloadFinished()
  77. {
  78. }
  79. void DebugWidget::on_fillUserDataPushButton_clicked()
  80. {
  81. ATimer timer(this);
  82. QMessageBox mb(this);
  83. //check if template dir exists and create if needed.
  84. QDir dir("data/templates");
  85. DEB(dir.path());
  86. if (!dir.exists())
  87. dir.mkpath(".");
  88. // download latest csv
  89. QStringList userTables = {"pilots", "tails", "flights"};
  90. const auto& linkStub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/develop/assets/database/templates/sample_";
  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("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. QVector<bool> allGood;
  102. for (const auto& table : userTables) {
  103. auto data = aReadCsv("data/templates/sample_" + table + ".csv");
  104. allGood.append(ADataBaseSetup::commitData(data, table));
  105. }
  106. for (const auto& item : allGood) {
  107. if (!item) {
  108. mb.setText("Errors have ocurred. Check console for details.");
  109. mb.exec();
  110. return;
  111. }
  112. }
  113. mb.setText("User tables successfully populated.\n\nRestarting app.");
  114. mb.exec();
  115. qApp->quit();
  116. QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
  117. }
  118. void DebugWidget::on_selectCsvPushButton_clicked()
  119. {
  120. auto fileName = QFileDialog::getOpenFileName(this,
  121. tr("Open CSV File for import"), QDir::homePath(), tr("CSV files (*.csv)"));
  122. ui->importCsvLineEdit->setText(fileName);
  123. }
  124. void DebugWidget::on_importCsvPushButton_clicked()
  125. {
  126. ATimer timer(this);
  127. auto file = QFileInfo(ui->importCsvLineEdit->text());
  128. DEB("File exists/is file: " << file.exists() << file.isFile() << " Path: " << file.absoluteFilePath());
  129. if (file.exists() && file.isFile()) {
  130. if (ADataBaseSetup::commitData(aReadCsv(file.absoluteFilePath()), ui->tableComboBox->currentText())) {
  131. auto mb = QMessageBox(this);
  132. mb.setText("Data inserted successfully.");
  133. mb.exec();
  134. } else {
  135. auto mb = QMessageBox(this);
  136. mb.setText("Errors have ocurred. Check console for details.");
  137. mb.exec();
  138. }
  139. } else {
  140. auto mb = QMessageBox(this);
  141. mb.setText("Please select a valid file.");
  142. mb.exec();
  143. }
  144. }
  145. void DebugWidget::on_debugPushButton_clicked()
  146. {
  147. qlonglong number_of_runs = 5000;
  148. long time1 = 0;
  149. long time2 = 0;
  150. using namespace experimental;
  151. {
  152. ATimer timer;
  153. for (int i = 0; i < number_of_runs; i++) {
  154. // first block, do stuff here...
  155. aDB()->getEntry({"pilots", i});
  156. }
  157. time1 = timer.timeNow();
  158. }
  159. {
  160. ATimer timer;
  161. for (int i = 0; i < number_of_runs; i++) {
  162. // second block, do stuff here...
  163. aDB()->getPilotEntry(i);
  164. }
  165. time2 = timer.timeNow();
  166. }
  167. DEB("First block executed " << number_of_runs << " times for a total of " << time1 << " milliseconds.");
  168. DEB("Second block executed " << number_of_runs << " times for a total of " << time2 << " milliseconds.");
  169. }
  170. /*
  171. qlonglong number_of_runs = 5000;
  172. long time1 = 0;
  173. long time2 = 0;
  174. using namespace experimental;
  175. {
  176. ATimer timer;
  177. for (int i = 0; i < number_of_runs; i++) {
  178. // first block, do stuff here...
  179. }
  180. time1 = timer.timeNow();
  181. }
  182. {
  183. ATimer timer;
  184. for (int i = 0; i < number_of_runs; i++) {
  185. // second block, do stuff here...
  186. }
  187. time2 = timer.timeNow();
  188. }
  189. DEB("First block executed " << number_of_runs << " times for a total of " << time1 << " milliseconds.");
  190. DEB("Second block executed " << number_of_runs << " times for a total of " << time2 << " milliseconds.");
  191. */