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);

View file

@ -3,7 +3,7 @@
*
* core logic for the Info & Stats page -
* char *get_minutes(int seconds);
* void process_all_dives();
* void calculate_stats_summary(struct stats_summary *out);
*/
#include "gettext.h"
#include <string.h>
@ -14,12 +14,7 @@
#include "divelist.h"
#include "statistics.h"
static stats_t stats;
stats_t stats_selection;
stats_t *stats_monthly = NULL;
stats_t *stats_yearly = NULL;
stats_t *stats_by_trip = NULL;
stats_t *stats_by_type = NULL;
static void process_temperatures(struct dive *dp, stats_t *stats)
{
@ -91,7 +86,13 @@ char *get_minutes(int seconds)
return buf;
}
void process_all_dives()
/*
* Calculate a summary of the statistics and put in the stats_summary
* structure provided in the first parameter.
* Before first use, it should be initialized with init_stats_summary().
* After use, memory must be released with free_stats_summary().
*/
void calculate_stats_summary(struct stats_summary *out)
{
int idx;
struct dive *dp;
@ -104,8 +105,8 @@ void process_all_dives()
int trip_iter = 0;
dive_trip_t *trip_ptr = 0;
unsigned int size, tsize;
stats_t stats = { 0 };
memset(&stats, 0, sizeof(stats));
if (dive_table.nr > 0) {
stats.shortest_time.seconds = dive_table.dives[0]->duration.seconds;
stats.min_depth.mm = dive_table.dives[0]->maxdepth.mm;
@ -116,37 +117,33 @@ void process_all_dives()
* case (one dive per year or all dives during
* one month) for yearly and monthly statistics*/
free(stats_yearly);
free(stats_monthly);
free(stats_by_trip);
free(stats_by_type);
size = sizeof(stats_t) * (dive_table.nr + 1);
tsize = sizeof(stats_t) * (NUM_DIVEMODE + 1);
stats_yearly = malloc(size);
stats_monthly = malloc(size);
stats_by_trip = malloc(size);
stats_by_type = malloc(tsize);
if (!stats_yearly || !stats_monthly || !stats_by_trip || !stats_by_type)
free_stats_summary(out);
out->stats_yearly = malloc(size);
out->stats_monthly = malloc(size);
out->stats_by_trip = malloc(size);
out->stats_by_type = malloc(tsize);
if (!out->stats_yearly || !out->stats_monthly || !out->stats_by_trip || !out->stats_by_type)
return;
memset(stats_yearly, 0, size);
memset(stats_monthly, 0, size);
memset(stats_by_trip, 0, size);
memset(stats_by_type, 0, tsize);
stats_yearly[0].is_year = true;
memset(out->stats_yearly, 0, size);
memset(out->stats_monthly, 0, size);
memset(out->stats_by_trip, 0, size);
memset(out->stats_by_type, 0, tsize);
out->stats_yearly[0].is_year = true;
/* Setting the is_trip to true to show the location as first
* field in the statistics window */
stats_by_type[0].location = strdup(translate("gettextFromC", "All (by type stats)"));
stats_by_type[0].is_trip = true;
stats_by_type[1].location = strdup(translate("gettextFromC", divemode_text_ui[OC]));
stats_by_type[1].is_trip = true;
stats_by_type[2].location = strdup(translate("gettextFromC", divemode_text_ui[CCR]));
stats_by_type[2].is_trip = true;
stats_by_type[3].location = strdup(translate("gettextFromC", divemode_text_ui[PSCR]));
stats_by_type[3].is_trip = true;
stats_by_type[4].location = strdup(translate("gettextFromC", divemode_text_ui[FREEDIVE]));
stats_by_type[4].is_trip = true;
out->stats_by_type[0].location = strdup(translate("gettextFromC", "All (by type stats)"));
out->stats_by_type[0].is_trip = true;
out->stats_by_type[1].location = strdup(translate("gettextFromC", divemode_text_ui[OC]));
out->stats_by_type[1].is_trip = true;
out->stats_by_type[2].location = strdup(translate("gettextFromC", divemode_text_ui[CCR]));
out->stats_by_type[2].is_trip = true;
out->stats_by_type[3].location = strdup(translate("gettextFromC", divemode_text_ui[PSCR]));
out->stats_by_type[3].is_trip = true;
out->stats_by_type[4].location = strdup(translate("gettextFromC", divemode_text_ui[FREEDIVE]));
out->stats_by_type[4].is_trip = true;
/* this relies on the fact that the dives in the dive_table
* are in chronological order */
@ -160,20 +157,20 @@ void process_all_dives()
if (current_year != tm.tm_year) {
current_year = tm.tm_year;
process_dive(dp, &(stats_yearly[++year_iter]));
stats_yearly[year_iter].is_year = true;
process_dive(dp, &(out->stats_yearly[++year_iter]));
out->stats_yearly[year_iter].is_year = true;
} else {
process_dive(dp, &(stats_yearly[year_iter]));
process_dive(dp, &(out->stats_yearly[year_iter]));
}
stats_yearly[year_iter].selection_size++;
stats_yearly[year_iter].period = current_year;
out->stats_yearly[year_iter].selection_size++;
out->stats_yearly[year_iter].period = current_year;
/* stats_by_type[0] is all the dives combined */
stats_by_type[0].selection_size++;
process_dive(dp, &(stats_by_type[0]));
out->stats_by_type[0].selection_size++;
process_dive(dp, &(out->stats_by_type[0]));
process_dive(dp, &(stats_by_type[dp->dc.divemode + 1]));
stats_by_type[dp->dc.divemode + 1].selection_size++;
process_dive(dp, &(out->stats_by_type[dp->dc.divemode + 1]));
out->stats_by_type[dp->dc.divemode + 1].selection_size++;
if (dp->divetrip != NULL) {
if (trip_ptr != dp->divetrip) {
@ -182,15 +179,15 @@ void process_all_dives()
}
/* stats_by_trip[0] is all the dives combined */
stats_by_trip[0].selection_size++;
process_dive(dp, &(stats_by_trip[0]));
stats_by_trip[0].is_trip = true;
stats_by_trip[0].location = strdup(translate("gettextFromC", "All (by trip stats)"));
out->stats_by_trip[0].selection_size++;
process_dive(dp, &(out->stats_by_trip[0]));
out->stats_by_trip[0].is_trip = true;
out->stats_by_trip[0].location = strdup(translate("gettextFromC", "All (by trip stats)"));
process_dive(dp, &(stats_by_trip[trip_iter]));
stats_by_trip[trip_iter].selection_size++;
stats_by_trip[trip_iter].is_trip = true;
stats_by_trip[trip_iter].location = dp->divetrip->location;
process_dive(dp, &(out->stats_by_trip[trip_iter]));
out->stats_by_trip[trip_iter].selection_size++;
out->stats_by_trip[trip_iter].is_trip = true;
out->stats_by_trip[trip_iter].location = dp->divetrip->location;
}
/* monthly statistics */
@ -202,14 +199,30 @@ void process_all_dives()
if (prev_month != current_month || prev_year != current_year)
month_iter++;
}
process_dive(dp, &(stats_monthly[month_iter]));
stats_monthly[month_iter].selection_size++;
stats_monthly[month_iter].period = current_month;
process_dive(dp, &(out->stats_monthly[month_iter]));
out->stats_monthly[month_iter].selection_size++;
out->stats_monthly[month_iter].period = current_month;
prev_month = current_month;
prev_year = current_year;
}
}
void free_stats_summary(struct stats_summary *stats)
{
free(stats->stats_yearly);
free(stats->stats_monthly);
free(stats->stats_by_trip);
free(stats->stats_by_type);
}
void init_stats_summary(struct stats_summary *stats)
{
stats->stats_yearly = NULL;
stats->stats_monthly = NULL;
stats->stats_by_trip = NULL;
stats->stats_by_type = NULL;
}
/* make sure we skip the selected summary entries */
void process_selected_dives(void)
{

View file

@ -12,10 +12,6 @@
#include "core/units.h"
#include "core/dive.h" // For MAX_CYLINDERS
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
int period;
@ -42,13 +38,22 @@ typedef struct
char *location;
} stats_t;
extern stats_t stats_selection;
extern stats_t *stats_yearly;
extern stats_t *stats_monthly;
extern stats_t *stats_by_trip;
extern stats_t *stats_by_type;
struct stats_summary {
stats_t *stats_yearly;
stats_t *stats_monthly;
stats_t *stats_by_trip;
stats_t *stats_by_type;
};
#ifdef __cplusplus
extern "C" {
#endif
extern char *get_minutes(int seconds);
extern void process_all_dives();
extern void init_stats_summary(struct stats_summary *stats);
extern void free_stats_summary(struct stats_summary *stats);
extern void calculate_stats_summary(struct stats_summary *stats);
extern void get_gas_used(struct dive *dive, volume_t gases[MAX_CYLINDERS]);
extern void process_selected_dives(void);
void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot);
@ -57,4 +62,25 @@ void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot);
}
#endif
/*
* For C++ code, provide a convenience version of stats_summary
* that initializes the structure on construction and frees
* resources when it goes out of scope. Apart from that, it
* can be used as a stats_summary replacement.
*/
#ifdef __cplusplus
struct stats_summary_auto_free : public stats_summary {
stats_summary_auto_free();
~stats_summary_auto_free();
};
inline stats_summary_auto_free::stats_summary_auto_free()
{
init_stats_summary(this);
}
inline stats_summary_auto_free::~stats_summary_auto_free()
{
free_stats_summary(this);
}
#endif
#endif // STATISTICS_H

View file

@ -424,13 +424,7 @@ void MainTab::updateDiveInfo(bool clear)
setEnabled(false);
editMode = IGNORE; // don't trigger on changes to the widgets
// This method updates ALL tabs whenever a new dive or trip is
// selected.
// If exactly one trip has been selected, we show the location / notes
// for the trip in the Info tab, otherwise we show the info of the
// selected_dive
process_selected_dives();
process_all_dives();
for (auto widget : extraWidgets) {
widget->updateData();

View file

@ -207,8 +207,10 @@ QString TemplateLayout::generateStatistics()
QVariantList years;
int i = 0;
while (stats_yearly != NULL && stats_yearly[i].period) {
YearInfo year(stats_yearly[i]);
stats_summary_auto_free stats;
calculate_stats_summary(&stats);
while (stats.stats_yearly != NULL && stats.stats_yearly[i].period) {
YearInfo year(stats.stats_yearly[i]);
years.append(QVariant::fromValue(year));
i++;
}

View file

@ -49,13 +49,9 @@ int main(int argc, char **argv)
// this should have set up the informational preferences - let's grab
// the units from there
prefs.unit_system = git_prefs.unit_system;
prefs.units = git_prefs.units;
// populate the statistics
process_all_dives();
// now set up the export settings to create the HTML export
struct htmlExportSetting hes;
hes.themeFile = "sand.css";

View file

@ -176,13 +176,15 @@ void YearlyStatisticsModel::update_yearly_stats()
{
int i, month = 0;
unsigned int j, combined_months;
stats_summary_auto_free stats;
calculate_stats_summary(&stats);
for (i = 0; stats_yearly != NULL && stats_yearly[i].period; ++i) {
YearStatisticsItem *item = new YearStatisticsItem(stats_yearly[i]);
for (i = 0; stats.stats_yearly != NULL && stats.stats_yearly[i].period; ++i) {
YearStatisticsItem *item = new YearStatisticsItem(stats.stats_yearly[i]);
combined_months = 0;
for (j = 0; combined_months < stats_yearly[i].selection_size; ++j) {
combined_months += stats_monthly[month].selection_size;
YearStatisticsItem *iChild = new YearStatisticsItem(stats_monthly[month]);
for (j = 0; combined_months < stats.stats_yearly[i].selection_size; ++j) {
combined_months += stats.stats_monthly[month].selection_size;
YearStatisticsItem *iChild = new YearStatisticsItem(stats.stats_monthly[month]);
item->children.append(iChild);
iChild->parent = item;
month++;
@ -191,11 +193,10 @@ void YearlyStatisticsModel::update_yearly_stats()
item->parent = rootItem.get();
}
if (stats_by_trip != NULL && stats_by_trip[0].is_trip == true) {
YearStatisticsItem *item = new YearStatisticsItem(stats_by_trip[0]);
for (i = 1; stats_by_trip != NULL && stats_by_trip[i].is_trip; ++i) {
YearStatisticsItem *iChild = new YearStatisticsItem(stats_by_trip[i]);
if (stats.stats_by_trip != NULL && stats.stats_by_trip[0].is_trip == true) {
YearStatisticsItem *item = new YearStatisticsItem(stats.stats_by_trip[0]);
for (i = 1; stats.stats_by_trip != NULL && stats.stats_by_trip[i].is_trip; ++i) {
YearStatisticsItem *iChild = new YearStatisticsItem(stats.stats_by_trip[i]);
item->children.append(iChild);
iChild->parent = item;
}
@ -204,12 +205,12 @@ void YearlyStatisticsModel::update_yearly_stats()
}
/* Show the statistic sorted by dive type */
if (stats_by_type != NULL && stats_by_type[0].selection_size) {
YearStatisticsItem *item = new YearStatisticsItem(stats_by_type[0]);
if (stats.stats_by_type != NULL && stats.stats_by_type[0].selection_size) {
YearStatisticsItem *item = new YearStatisticsItem(stats.stats_by_type[0]);
for (i = 1; i <= NUM_DIVEMODE; ++i) {
if (stats_by_type[i].selection_size == 0)
if (stats.stats_by_type[i].selection_size == 0)
continue;
YearStatisticsItem *iChild = new YearStatisticsItem(stats_by_type[i]);
YearStatisticsItem *iChild = new YearStatisticsItem(stats.stats_by_type[i]);
item->children.append(iChild);
iChild->parent = item;
}