Statistics: remove global state / calculate only when needed

Statistics were calculated into global variables every time the
current dive was changed.

Calculate statistics only when needed and into a structure
provided by the caller.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-10-06 10:58:12 +02:00 committed by Dirk Hohndel
parent 68fdc0b6f4
commit 97991e2b9f
7 changed files with 141 additions and 107 deletions

View file

@ -75,42 +75,44 @@ static void exportHTMLstatistics(const QString filename, struct htmlExportSettin
QFile file(filename);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
stats_summary_auto_free stats;
stats_t total_stats;
calculate_stats_summary(&stats);
total_stats.selection_size = 0;
total_stats.total_time.seconds = 0;
int i = 0;
out << "divestat=[";
if (hes.yearlyStatistics) {
while (stats_yearly != NULL && stats_yearly[i].period) {
while (stats.stats_yearly != NULL && stats.stats_yearly[i].period) {
out << "{";
out << "\"YEAR\":\"" << stats_yearly[i].period << "\",";
out << "\"DIVES\":\"" << stats_yearly[i].selection_size << "\",";
out << "\"TOTAL_TIME\":\"" << get_dive_duration_string(stats_yearly[i].total_time.seconds,
out << "\"YEAR\":\"" << stats.stats_yearly[i].period << "\",";
out << "\"DIVES\":\"" << stats.stats_yearly[i].selection_size << "\",";
out << "\"TOTAL_TIME\":\"" << get_dive_duration_string(stats.stats_yearly[i].total_time.seconds,
gettextFromC::tr("h"), gettextFromC::tr("min"), gettextFromC::tr("sec"), " ") << "\",";
out << "\"AVERAGE_TIME\":\"" << get_minutes(stats_yearly[i].total_time.seconds / stats_yearly[i].selection_size) << "\",";
out << "\"SHORTEST_TIME\":\"" << get_minutes(stats_yearly[i].shortest_time.seconds) << "\",";
out << "\"LONGEST_TIME\":\"" << get_minutes(stats_yearly[i].longest_time.seconds) << "\",";
out << "\"AVG_DEPTH\":\"" << get_depth_string(stats_yearly[i].avg_depth) << "\",";
out << "\"MIN_DEPTH\":\"" << get_depth_string(stats_yearly[i].min_depth) << "\",";
out << "\"MAX_DEPTH\":\"" << get_depth_string(stats_yearly[i].max_depth) << "\",";
out << "\"AVG_SAC\":\"" << get_volume_string(stats_yearly[i].avg_sac) << "\",";
out << "\"MIN_SAC\":\"" << get_volume_string(stats_yearly[i].min_sac) << "\",";
out << "\"MAX_SAC\":\"" << get_volume_string(stats_yearly[i].max_sac) << "\",";
if (stats_yearly[i].combined_count) {
out << "\"AVERAGE_TIME\":\"" << get_minutes(stats.stats_yearly[i].total_time.seconds / stats.stats_yearly[i].selection_size) << "\",";
out << "\"SHORTEST_TIME\":\"" << get_minutes(stats.stats_yearly[i].shortest_time.seconds) << "\",";
out << "\"LONGEST_TIME\":\"" << get_minutes(stats.stats_yearly[i].longest_time.seconds) << "\",";
out << "\"AVG_DEPTH\":\"" << get_depth_string(stats.stats_yearly[i].avg_depth) << "\",";
out << "\"MIN_DEPTH\":\"" << get_depth_string(stats.stats_yearly[i].min_depth) << "\",";
out << "\"MAX_DEPTH\":\"" << get_depth_string(stats.stats_yearly[i].max_depth) << "\",";
out << "\"AVG_SAC\":\"" << get_volume_string(stats.stats_yearly[i].avg_sac) << "\",";
out << "\"MIN_SAC\":\"" << get_volume_string(stats.stats_yearly[i].min_sac) << "\",";
out << "\"MAX_SAC\":\"" << get_volume_string(stats.stats_yearly[i].max_sac) << "\",";
if (stats.stats_yearly[i].combined_count) {
temperature_t avg_temp;
avg_temp.mkelvin = stats_yearly[i].combined_temp.mkelvin / stats_yearly[i].combined_count;
avg_temp.mkelvin = stats.stats_yearly[i].combined_temp.mkelvin / stats.stats_yearly[i].combined_count;
out << "\"AVG_TEMP\":\"" << get_temperature_string(avg_temp) << "\",";
} else {
out << "\"AVG_TEMP\":\"0.0\",";
}
out << "\"MIN_TEMP\":\"" << (stats_yearly[i].min_temp.mkelvin == 0 ? 0 : get_temperature_string(stats_yearly[i].min_temp)) << "\",";
out << "\"MAX_TEMP\":\"" << (stats_yearly[i].max_temp.mkelvin == 0 ? 0 : get_temperature_string(stats_yearly[i].max_temp)) << "\",";
out << "\"MIN_TEMP\":\"" << (stats.stats_yearly[i].min_temp.mkelvin == 0 ? 0 : get_temperature_string(stats.stats_yearly[i].min_temp)) << "\",";
out << "\"MAX_TEMP\":\"" << (stats.stats_yearly[i].max_temp.mkelvin == 0 ? 0 : get_temperature_string(stats.stats_yearly[i].max_temp)) << "\",";
out << "},";
total_stats.selection_size += stats_yearly[i].selection_size;
total_stats.total_time.seconds += stats_yearly[i].total_time.seconds;
total_stats.selection_size += stats.stats_yearly[i].selection_size;
total_stats.total_time.seconds += stats.stats_yearly[i].total_time.seconds;
i++;
}
exportHTMLstatisticsTotal(out, &total_stats);