Mercurial > minori
annotate src/gui/pages/statistics.cc @ 187:9613d72b097e
*: multiple performance improvements
like marking `static const` when it makes sense...
date: change old stupid heap-based method to a structure which should
make copying the thing actually make a copy.
also many performance-based changes, like removing the std::tie
dependency and forward-declaring nlohmann json
*: replace every instance of QString::fromUtf8 to Strings::ToQString.
the main difference is that our function will always convert exactly
what is in the string, while some other times it would only convert
up to the nearest NUL byte
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Wed, 06 Dec 2023 13:43:54 -0500 |
| parents | bc8d2ccff09c |
| children | 649786bae914 |
| rev | line source |
|---|---|
| 9 | 1 #include "gui/pages/statistics.h" |
| 15 | 2 #include "core/anime_db.h" |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
3 #include "core/strings.h" |
| 15 | 4 #include "core/session.h" |
| 9 | 5 #include "gui/pages/anime_list.h" |
| 46 | 6 #include "gui/widgets/text.h" |
| 93 | 7 #include "gui/widgets/graph.h" |
|
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
172
diff
changeset
|
8 |
| 9 | 9 #include <QString> |
| 10 #include <QTextDocument> | |
| 11 #include <QTextStream> | |
| 12 #include <QTimer> | |
| 13 #include <QVBoxLayout> | |
| 14 #include <QWidget> | |
|
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
172
diff
changeset
|
15 |
| 9 | 16 #include <sstream> |
| 17 | |
| 64 | 18 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) { |
| 19 QVBoxLayout* layout = new QVBoxLayout(this); | |
| 9 | 20 |
| 64 | 21 setFrameShape(QFrame::Box); |
| 9 | 22 setFrameShadow(QFrame::Sunken); |
| 23 | |
| 46 | 24 setAutoFillBackground(true); |
|
37
9ae9365dd4ea
window.cpp: fix sidebar on Linux
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
25 |
| 93 | 26 _anime_list.reset(new TextWidgets::LabelledSection( |
| 51 | 27 tr("Anime list"), |
| 63 | 28 tr("Anime count:\nEpisode count:\nTime spent watching:\nTime to complete:\nAverage score:\nScore deviation:"), |
| 93 | 29 "", this)); |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
30 layout->addWidget(_anime_list.get()); |
| 93 | 31 |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
32 { |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
33 QWidget* score_dist_widget = new QWidget(this); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
34 QVBoxLayout* score_dist_layout = new QVBoxLayout(score_dist_widget); |
| 9 | 35 |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
36 score_dist_layout->addWidget(new TextWidgets::Header(tr("Score distribution"), score_dist_widget)); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
37 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
38 /* Ew */ |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
39 { |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
40 QWidget* score_graph_parent = new QWidget(score_dist_widget); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
41 QVBoxLayout* score_parent_layout = new QVBoxLayout(score_graph_parent); |
|
94
2f373d48f889
*: etc changes to graph stuff
Paper <mrpapersonic@gmail.com>
parents:
93
diff
changeset
|
42 |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
43 _score_distribution_graph.reset(new Graph<int>(score_graph_parent)); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
44 score_parent_layout->addWidget(_score_distribution_graph.get()); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
45 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
46 score_parent_layout->setSpacing(0); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
47 score_parent_layout->setContentsMargins(12, 0, 0, 0); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
48 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
49 score_graph_parent->setContentsMargins(3, 0, 0, 0); |
| 9 | 50 |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
51 score_dist_layout->addWidget(score_graph_parent); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
52 } |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
53 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
54 score_dist_layout->setContentsMargins(0, 0, 0, 0); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
55 |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
56 layout->addWidget(score_dist_widget); |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
57 } |
|
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
58 |
| 93 | 59 |
| 60 _application.reset(new TextWidgets::LabelledSection(tr("Minori"), tr("Uptime:\nRequests made:"), "\n\n", this)); | |
|
172
45a0967485f1
graph, statistics: make my code a little less messy
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
61 layout->addWidget(_application.get()); |
| 93 | 62 |
| 64 | 63 layout->addStretch(); |
| 9 | 64 |
| 65 QTimer* timer = new QTimer(this); | |
| 66 connect(timer, &QTimer::timeout, this, [this] { | |
| 67 if (isVisible()) | |
| 68 UpdateStatistics(); | |
| 69 }); | |
| 70 timer->start(1000); // update statistics every second | |
| 71 } | |
| 72 | |
| 64 | 73 void StatisticsPage::showEvent(QShowEvent*) { |
| 9 | 74 UpdateStatistics(); |
| 75 } | |
| 76 | |
| 77 /* me abusing macros :) */ | |
| 15 | 78 #define ADD_TIME_SEGMENT(r, x, s, p) \ |
| 64 | 79 if (x > 0) \ |
| 80 r << x << ((x == 1) ? s : p) | |
| 102 | 81 std::string StatisticsPage::MinutesToDateString(const int minutes) { |
| 9 | 82 /* ew */ |
| 83 int years = (minutes * (1 / 525949.2F)); | |
| 84 int months = (minutes * (1 / 43829.1F)) - (years * 12); | |
| 85 int days = (minutes * (1 / 1440.0F)) - (years * 365.2425F) - (months * 30.436875F); | |
| 86 int hours = (minutes * (1 / 60.0F)) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
| 87 int rest_minutes = (minutes) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440) - (hours * 60); | |
| 88 std::ostringstream return_stream; | |
| 89 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); | |
| 90 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
| 91 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
| 92 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
| 93 if (rest_minutes > 0 || return_stream.str().size() == 0) | |
| 94 return_stream << rest_minutes << ((rest_minutes == 1) ? " minute" : " minutes"); | |
| 95 return return_stream.str(); | |
| 96 } | |
| 97 | |
| 102 | 98 std::string StatisticsPage::SecondsToDateString(const int sec) { |
| 9 | 99 /* this is all fairly unnecessary, but works:tm: */ |
| 10 | 100 int years = sec * (1 / 31556952.0F); |
| 101 int months = sec * (1 / 2629746.0F) - (years * 12); | |
| 102 int days = sec * (1 / 86400.0F) - (years * 365.2425F) - (months * 30.436875F); | |
| 103 int hours = sec * (1 / 3600.0F) - (years * 8765.82F) - (months * 730.485F) - (days * 24); | |
| 104 int minutes = (sec) * (1 / 60.0F) - (years * 525949.2F) - (months * 43829.1F) - (days * 1440.0F) - (hours * 60.0F); | |
| 15 | 105 int seconds = |
| 106 sec - (years * 31556952.0F) - (months * 2629746.0F) - (days * 86400.0F) - (hours * 3600.0F) - (minutes * 60.0F); | |
| 9 | 107 std::ostringstream return_stream; |
| 10 | 108 ADD_TIME_SEGMENT(return_stream, years, " year ", " years "); |
| 109 ADD_TIME_SEGMENT(return_stream, months, " month ", " months "); | |
| 110 ADD_TIME_SEGMENT(return_stream, days, " day ", " days "); | |
| 111 ADD_TIME_SEGMENT(return_stream, hours, " hour ", " hours "); | |
| 112 ADD_TIME_SEGMENT(return_stream, minutes, " minute ", " minutes "); | |
| 113 if (seconds > 0 || return_stream.str().size() == 0) | |
| 114 return_stream << seconds << ((seconds == 1) ? " second" : " seconds"); | |
| 9 | 115 return return_stream.str(); |
| 116 } | |
| 117 #undef ADD_TIME_SEGMENT | |
| 118 | |
| 102 | 119 inline int GetTotalWithScore(const int score) { |
| 93 | 120 int count = 0; |
| 121 for (const auto& item : Anime::db.items) | |
| 122 if (item.second.IsInUserList() && item.second.GetUserScore() == score) | |
| 123 count++; | |
| 124 return count; | |
| 125 } | |
| 126 | |
| 64 | 127 void StatisticsPage::UpdateStatistics() { |
| 9 | 128 /* Anime list */ |
| 129 QString string = ""; | |
| 130 QTextStream ts(&string); | |
| 10 | 131 ts << Anime::db.GetTotalAnimeAmount() << '\n'; |
| 132 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; | |
| 133 ts << MinutesToDateString(Anime::db.GetTotalWatchedAmount()).c_str() << '\n'; | |
| 134 ts << MinutesToDateString(Anime::db.GetTotalPlannedAmount()).c_str() << '\n'; | |
| 135 ts << Anime::db.GetAverageScore() << '\n'; | |
| 136 ts << Anime::db.GetScoreDeviation(); | |
| 93 | 137 _anime_list->GetParagraph()->SetText(string); |
| 138 | |
| 139 _score_distribution_graph->Clear(); | |
| 140 for (int i = 10; i <= 100; i += 10) | |
| 141 _score_distribution_graph->AddItem(i, GetTotalWithScore(i)); | |
| 9 | 142 |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
143 string = ""; |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
144 ts << Strings::ToQString(SecondsToDateString(session.uptime() / 1000)) << '\n'; |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
145 ts << session.GetRequests(); |
| 9 | 146 /* Application */ |
| 147 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); | |
| 93 | 148 _application->GetParagraph()->SetText(string); |
| 9 | 149 } |
| 150 | |
| 10 | 151 #include "gui/pages/moc_statistics.cpp" |
