debugwidget.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2022 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 "debugwidget.h"
  19. #include "src/testing/importCrewlounge/processaircraft.h"
  20. #include "src/testing/importCrewlounge/processflights.h"
  21. #include "src/testing/importCrewlounge/processpilots.h"
  22. #include "ui_debugwidget.h"
  23. #include <QtGlobal>
  24. #include "src/classes/downloadhelper.h"
  25. #include "src/functions/readcsv.h"
  26. #include "src/database/database.h"
  27. #include "src/database/row.h"
  28. #include "src/testing/atimer.h"
  29. #include "src/functions/log.h"
  30. void DebugWidget::on_debugPushButton_clicked()
  31. {
  32. auto rawCsvData = CSV::readCsvAsRows("/home/felix/git/importMCC/assets/data/felix.csv");
  33. // Process Pilots
  34. auto proc_pilots = ProcessPilots(rawCsvData);
  35. proc_pilots.init();
  36. const auto p_maps = proc_pilots.getProcessedPilotMaps();
  37. // Process Tails
  38. auto proc_tails = ProcessAircraft(rawCsvData);
  39. proc_tails.init();
  40. const auto t_maps = proc_tails.getProcessedTailMaps();
  41. // Process Flights
  42. auto proc_flights = ProcessFlights(rawCsvData,proc_pilots.getProcessedPilotsIds(), proc_tails.getProcessedTailIds());
  43. proc_flights.init();
  44. auto flights = proc_flights.getProcessedFlights();
  45. DEB << "Flight:" << flights[1000];
  46. }
  47. DebugWidget::DebugWidget(QWidget *parent) :
  48. QWidget(parent),
  49. ui(new Ui::DebugWidget)
  50. {
  51. ui->setupUi(this);
  52. for (const auto& table : DB->getTableNames()) {
  53. if( table != "sqlite_sequence") {
  54. ui->tableComboBox->addItem(table);
  55. }
  56. }
  57. }
  58. DebugWidget::~DebugWidget()
  59. {
  60. delete ui;
  61. }
  62. void DebugWidget::on_resetUserTablesPushButton_clicked()
  63. {
  64. ATimer timer(this);
  65. if (DB->resetUserData()){
  66. LOG << "Database successfully reset";
  67. emit DB->dataBaseUpdated(OPL::DbTable::Any);
  68. } else
  69. LOG <<"Errors have occurred. Check console for Debug output. ";
  70. }
  71. void DebugWidget::on_resetDatabasePushButton_clicked()
  72. {
  73. // disconnect and remove old database
  74. DB->disconnect();
  75. QFile db_file(OPL::Paths::databaseFileInfo().absoluteFilePath());
  76. if (!db_file.remove()) {
  77. WARN(tr("Unable to delete existing database file."));
  78. return;
  79. }
  80. // Download templates
  81. QString branch_name = ui->branchLineEdit->text();
  82. // Create url string
  83. auto template_url_string = QStringLiteral("https://raw.githubusercontent.com/fiffty-50/openpilotlog/");
  84. template_url_string.append(branch_name);
  85. template_url_string.append(QLatin1String("/assets/database/templates/"));
  86. QDir template_dir(OPL::Paths::directory(OPL::Paths::Templates));
  87. QStringList template_table_names;
  88. for (const auto table : DB->getTemplateTables())
  89. template_table_names.append(OPL::GLOBALS->getDbTableName(table));
  90. // Download json files
  91. for (const auto& table_name : template_table_names) {
  92. QEventLoop loop;
  93. DownloadHelper* dl = new DownloadHelper;
  94. QObject::connect(dl, &DownloadHelper::done, &loop, &QEventLoop::quit );
  95. dl->setTarget(QUrl(template_url_string + table_name + QLatin1String(".json")));
  96. dl->setFileName(template_dir.absoluteFilePath(table_name + QLatin1String(".json")));
  97. DEB << "Downloading: " << template_url_string + table_name + QLatin1String(".json");
  98. dl->download();
  99. dl->deleteLater();
  100. loop.exec(); // event loop waits for download done signal before allowing loop to continue
  101. QFileInfo downloaded_file(template_dir.filePath(table_name + QLatin1String(".json")));
  102. if (downloaded_file.size() == 0)
  103. LOG << "ssl/network error";
  104. }
  105. // Download checksum files
  106. for (const auto& table : template_table_names) {
  107. QEventLoop loop;
  108. DownloadHelper* dl = new DownloadHelper;
  109. QObject::connect(dl, &DownloadHelper::done, &loop, &QEventLoop::quit );
  110. dl->setTarget(QUrl(template_url_string + table + QLatin1String(".md5")));
  111. dl->setFileName(template_dir.absoluteFilePath(table + QLatin1String(".md5")));
  112. DEB << "Downloading: " << template_url_string + table + QLatin1String(".md5");
  113. dl->download();
  114. dl->deleteLater();
  115. loop.exec(); // event loop waits for download done signal before allowing loop to continue
  116. QFileInfo downloaded_file(template_dir.filePath(table + QLatin1String(".md5")));
  117. if (downloaded_file.size() == 0)
  118. LOG << "ssl/network error";
  119. }
  120. // Create Database
  121. if (!DB->createSchema()) {
  122. WARN(QString("Unable to create database.<br>%1").arg(DB->lastError.text()));
  123. return;
  124. }
  125. // Load ressources
  126. bool use_ressource_data = false; // do not use local data, download from github
  127. if(!DB->importTemplateData(use_ressource_data)) {
  128. WARN(tr("Database creation has been unsuccessful. Unable to fill template data.<br><br>%1")
  129. .arg(DB->lastError.text()));
  130. return ;
  131. }
  132. DB->connect();
  133. }
  134. void DebugWidget::downloadFinished()
  135. {
  136. }
  137. void DebugWidget::on_fillUserDataPushButton_clicked()
  138. {
  139. TODO << "Create JSON sample data and import";
  140. /*
  141. ATimer timer(this);
  142. QMessageBox message_box(this);
  143. // download latest json
  144. QStringList userTables = {"pilots", "tails", "flights"};
  145. QString linkStub = "https://raw.githubusercontent.com/fiffty-50/openpilotlog/";
  146. linkStub.append(ui->branchLineEdit->text());
  147. linkStub.append("/assets/database/templates/sample_");
  148. QDir template_dir(AStandardPaths::directory(AStandardPaths::Templates));
  149. for (const auto& table : userTables) {
  150. QEventLoop loop;
  151. ADownload* dl = new ADownload;
  152. connect(dl, &ADownload::done, &loop, &QEventLoop::quit );
  153. dl->setTarget(QUrl(linkStub + table + ".json"));
  154. dl->setFileName(template_dir.filePath("sample_" % table % QStringLiteral(".json")));
  155. dl->download();
  156. loop.exec(); // event loop waits for download done signal before allowing loop to continue
  157. dl->deleteLater();
  158. }
  159. QBitArray allGood;
  160. allGood.resize(userTables.size());
  161. for (const auto& table : userTables) {
  162. auto data = readcsv(AStandardPaths::directory(AStandardPaths::Templates).absoluteFilePath(
  163. + "sample_" + table + ".json"));
  164. allGood.setBit(userTables.indexOf(table), aDbSetup::commitData(data, table));
  165. }
  166. if (allGood.count(true) != userTables.size()) {
  167. message_box.setText("Errors have ocurred. Check console for details.");
  168. message_box.exec();
  169. return;
  170. }
  171. message_box.setText("User tables successfully populated.");
  172. message_box.exec();
  173. emit DB->dataBaseUpdated();
  174. */
  175. }
  176. void DebugWidget::on_selectCsvPushButton_clicked()
  177. {
  178. auto fileName = QFileDialog::getOpenFileName(this,
  179. tr("Open CSV File for import"),
  180. OPL::Paths::directory(OPL::Paths::Templates).absolutePath(),
  181. tr("CSV files (*.csv)"));
  182. ui->importCsvLineEdit->setText(fileName);
  183. }
  184. void DebugWidget::on_importCsvPushButton_clicked()
  185. {
  186. WARN("Not currently working...");
  187. /*
  188. ATimer timer(this);
  189. auto file = QFileInfo(ui->importCsvLineEdit->text());
  190. DEB << "File exists/is file:" << file.exists() << file.isFile() << " Path:" << file.absoluteFilePath();
  191. if (file.exists() && file.isFile()) {
  192. if (DataBaseSetup::commitData(aReadCsv(file.absoluteFilePath()), ui->tableComboBox->currentText())) {
  193. QMessageBox message_box(this);
  194. message_box.setText("Data inserted successfully.");
  195. message_box.exec();
  196. } else {
  197. QMessageBox message_box(this);
  198. message_box.setText("Errors have ocurred. Check console for details.");
  199. message_box.exec();
  200. }
  201. } else {
  202. QMessageBox message_box(this);
  203. message_box.setText("Please select a valid file.");
  204. message_box.exec();
  205. }
  206. */
  207. }
  208. void DebugWidget::changeEvent(QEvent *event)
  209. {
  210. if (event != nullptr)
  211. if(event->type() == QEvent::LanguageChange)
  212. ui->retranslateUi(this);
  213. }
  214. /* //Comparing two functions template
  215. qlonglong number_of_runs = 5000;
  216. long time1 = 0;
  217. long time2 = 0;
  218. {
  219. ATimer timer;
  220. for (int i = 0; i < number_of_runs; i++) {
  221. // first block, do stuff here...
  222. }
  223. time1 = timer.timeNow();
  224. }
  225. {
  226. ATimer timer;
  227. for (int i = 0; i < number_of_runs; i++) {
  228. // second block, do stuff here...
  229. }
  230. time2 = timer.timeNow();
  231. }
  232. DEB << "First block executed " << number_of_runs << " times for a total of " << time1 << " milliseconds.");
  233. DEB << "Second block executed " << number_of_runs << " times for a total of " << time2 << " milliseconds.");
  234. */
  235. /*
  236. *#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
  237. * DEB << QT_VERSION_MAJOR << QT_VERSION_MINOR << "At least 5.12";
  238. *#else
  239. * DEB << QT_VERSION_MAJOR << QT_VERSION_MINOR << "Less than 5.12";
  240. * #endif
  241. */