comparison src/gui/pages/statistics.cc @ 305:91ac90a34003

core/time: remove Duration class, use regular functions instead this class was pretty useless anyway
author Paper <paper@paper.us.eu.org>
date Sun, 19 May 2024 15:56:20 -0400
parents b82841e76e79
children 6b0768158dcd
comparison
equal deleted inserted replaced
304:2115488eb302 305:91ac90a34003
1 #include "gui/pages/statistics.h" 1 #include "gui/pages/statistics.h"
2 #include "core/anime_db.h" 2 #include "core/anime_db.h"
3 #include "core/session.h" 3 #include "core/session.h"
4 #include "core/strings.h" 4 #include "core/strings.h"
5 #include "core/time.h"
5 #include "gui/pages/anime_list.h" 6 #include "gui/pages/anime_list.h"
6 #include "gui/widgets/graph.h" 7 #include "gui/widgets/graph.h"
7 #include "gui/widgets/text.h" 8 #include "gui/widgets/text.h"
8 9
9 #include <QString> 10 #include <QString>
13 #include <QVBoxLayout> 14 #include <QVBoxLayout>
14 #include <QWidget> 15 #include <QWidget>
15 16
16 #include <cmath> 17 #include <cmath>
17 #include <sstream> 18 #include <sstream>
18
19 enum class TimeUnits {
20 SECONDS,
21 MINUTES
22 };
23 19
24 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) { 20 StatisticsPage::StatisticsPage(QWidget* parent) : QFrame(parent) {
25 setBackgroundRole(QPalette::Base); 21 setBackgroundRole(QPalette::Base);
26 22
27 QVBoxLayout* layout = new QVBoxLayout(this); 23 QVBoxLayout* layout = new QVBoxLayout(this);
77 73
78 void StatisticsPage::showEvent(QShowEvent*) { 74 void StatisticsPage::showEvent(QShowEvent*) {
79 UpdateStatistics(); 75 UpdateStatistics();
80 } 76 }
81 77
82 /* [in] enum TimeUnits unit:
83 * which unit to stop on
84 * [in] int amount:
85 * amount of units to parse
86 * [in, defaults to 1.0] double unit_in_seconds:
87 * equivalent of one of 'amount' in seconds, e.g. minutes would be 60.0
88 */
89 static std::string TimeToDateString(TimeUnits unit, int amount, double unit_in_seconds = 1.0) {
90 /* avoid calculating this twice */
91 const double years_conv = (31556952.0 / unit_in_seconds);
92 const double months_conv = (2629746.0 / unit_in_seconds);
93 const double days_conv = (86400.0 / unit_in_seconds);
94 const double hours_conv = (3600.0 / unit_in_seconds);
95 const double minutes_conv = (60.0 / unit_in_seconds);
96 const double seconds_conv = (1.0 / unit_in_seconds);
97
98 const int years = amount / years_conv;
99 const int months = std::fmod(amount, years_conv) / months_conv;
100 const int days = std::fmod(amount, months_conv) / days_conv;
101 const int hours = std::fmod(amount, days_conv) / hours_conv;
102 const int minutes = std::fmod(amount, hours_conv) / minutes_conv;
103 const int seconds = std::fmod(amount, minutes_conv) / seconds_conv;
104
105 const auto add_time_segment = [](std::ostringstream& str, int amount, const std::string_view& singular,
106 const std::string_view& plural, bool always = false) {
107 if (amount > 0 || always)
108 str << amount << ((amount == 1) ? singular : plural);
109 };
110
111 /* for now, this function is very en_US specific */
112
113 std::ostringstream string;
114 add_time_segment(string, years, " year ", " years ");
115 add_time_segment(string, months, " month ", " months ");
116 add_time_segment(string, days, " day ", " days ");
117 add_time_segment(string, hours, " hour ", " hours ");
118
119 if (unit == TimeUnits::MINUTES) {
120 add_time_segment(string, minutes, " minute", " minutes", true);
121 return string.str();
122 } else {
123 add_time_segment(string, minutes, " minute ", " minutes ");
124 }
125
126 add_time_segment(string, seconds, " second", " seconds", true);
127 return string.str();
128 }
129
130 inline int GetTotalWithScore(const int score) { 78 inline int GetTotalWithScore(const int score) {
131 int count = 0; 79 int count = 0;
132 for (const auto& item : Anime::db.items) 80 for (const auto& item : Anime::db.items)
133 if (item.second.IsInUserList() && item.second.GetUserScore() == score) 81 if (item.second.IsInUserList() && item.second.GetUserScore() == score)
134 count++; 82 count++;
139 /* Anime list */ 87 /* Anime list */
140 QString string = ""; 88 QString string = "";
141 QTextStream ts(&string); 89 QTextStream ts(&string);
142 ts << Anime::db.GetTotalAnimeAmount() << '\n'; 90 ts << Anime::db.GetTotalAnimeAmount() << '\n';
143 ts << Anime::db.GetTotalEpisodeAmount() << '\n'; 91 ts << Anime::db.GetTotalEpisodeAmount() << '\n';
144 ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalWatchedAmount(), 60.0)) << '\n'; 92 ts << Strings::ToQString(Time::GetSecondsAsAbsoluteString(Time::Units::Minutes, Anime::db.GetTotalWatchedAmount(), 60.0)) << '\n';
145 ts << Strings::ToQString(TimeToDateString(TimeUnits::MINUTES, Anime::db.GetTotalPlannedAmount(), 60.0)) << '\n'; 93 ts << Strings::ToQString(Time::GetSecondsAsAbsoluteString(Time::Units::Minutes, Anime::db.GetTotalPlannedAmount(), 60.0)) << '\n';
146 ts << Anime::db.GetAverageScore() << '\n'; 94 ts << Anime::db.GetAverageScore() << '\n';
147 ts << Anime::db.GetScoreDeviation(); 95 ts << Anime::db.GetScoreDeviation();
148 _anime_list->GetData()->setText(string); 96 _anime_list->GetData()->setText(string);
149 97
150 _score_distribution_graph->Clear(); 98 _score_distribution_graph->Clear();
151 for (int i = 10; i <= 100; i += 10) 99 for (int i = 10; i <= 100; i += 10)
152 _score_distribution_graph->AddItem(i, GetTotalWithScore(i)); 100 _score_distribution_graph->AddItem(i, GetTotalWithScore(i));
153 101
154 string = ""; 102 string = "";
155 ts << Strings::ToQString(TimeToDateString(TimeUnits::SECONDS, session.uptime() / 1000)) << '\n'; 103 ts << Strings::ToQString(Time::GetSecondsAsAbsoluteString(Time::Units::Seconds, session.uptime() / 1000)) << '\n';
156 ts << session.GetRequests(); 104 ts << session.GetRequests();
157 /* Application */ 105 /* Application */
158 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000)); 106 // UiUtils::SetPlainTextEditData(application_data, QString::number(session.uptime() / 1000));
159 _application->GetData()->setText(string); 107 _application->GetData()->setText(string);
160 } 108 }