diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-10-06 10:58:12 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-10-07 17:50:51 +0300 |
commit | 97991e2b9fff4254c2b40417bf6d7496ba0d849f (patch) | |
tree | a2b9af681e9ac5e72e4846736df001f75f7cc788 /core | |
parent | 68fdc0b6f49c81cb61387b7d006807509280c9df (diff) | |
download | subsurface-97991e2b9fff4254c2b40417bf6d7496ba0d849f.tar.gz |
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>
Diffstat (limited to 'core')
-rw-r--r-- | core/divelogexportlogic.cpp | 40 | ||||
-rw-r--r-- | core/statistics.c | 121 | ||||
-rw-r--r-- | core/statistics.h | 44 |
3 files changed, 123 insertions, 82 deletions
diff --git a/core/divelogexportlogic.cpp b/core/divelogexportlogic.cpp index cb700e79a..27a42fbf7 100644 --- a/core/divelogexportlogic.cpp +++ b/core/divelogexportlogic.cpp @@ -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); diff --git a/core/statistics.c b/core/statistics.c index b25080a9f..b7e4e1f4c 100644 --- a/core/statistics.c +++ b/core/statistics.c @@ -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)")); - - 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; + 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, &(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) { diff --git a/core/statistics.h b/core/statistics.h index 7766d3154..4639ecf3f 100644 --- a/core/statistics.h +++ b/core/statistics.h @@ -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 |