]> git.tdb.fi Git - ext/subsurface.git/blob - statistics.c
Might as well free current_file
[ext/subsurface.git] / statistics.c
1 /* statistics.c */
2 /* creates the UI for the Info & Stats page -
3  * controlled through the following interfaces:
4  *
5  * void show_dive_stats(struct dive *dive)
6  * void flush_dive_stats_changes(struct dive *dive)
7  *
8  * called from gtk-ui:
9  * GtkWidget *stats_widget(void)
10  */
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <time.h>
16
17 #include "dive.h"
18 #include "display.h"
19 #include "display-gtk.h"
20 #include "divelist.h"
21
22 typedef struct {
23         GtkWidget *date,
24                 *dive_time,
25                 *surf_intv,
26                 *max_depth,
27                 *avg_depth,
28                 *water_temp,
29                 *sac,
30                 *otu,
31                 *o2he,
32                 *gas_used;
33 } single_stat_widget_t;
34
35 static single_stat_widget_t single_w;
36
37 typedef struct {
38         GtkWidget *total_time,
39                 *avg_time,
40                 *shortest_time,
41                 *longest_time,
42                 *max_overall_depth,
43                 *min_overall_depth,
44                 *avg_overall_depth,
45                 *min_sac,
46                 *avg_sac,
47                 *max_sac,
48                 *selection_size,
49                 *max_temp,
50                 *avg_temp,
51                 *min_temp;
52 } total_stats_widget_t;
53
54 static total_stats_widget_t stats_w;
55
56 typedef struct {
57         duration_t total_time;
58         /* avg_time is simply total_time / nr -- let's not keep this */
59         duration_t shortest_time;
60         duration_t longest_time;
61         depth_t max_depth;
62         depth_t min_depth;
63         depth_t avg_depth;
64         volume_t max_sac;
65         volume_t min_sac;
66         volume_t avg_sac;
67         int max_temp;
68         int min_temp;
69         unsigned int combined_temp;
70         unsigned int combined_count;
71         unsigned int selection_size;
72         unsigned int total_sac_time;
73 } stats_t;
74
75 static stats_t stats;
76 static stats_t stats_selection;
77
78
79 static void process_dive(struct dive *dp, stats_t *stats)
80 {
81         int old_tt, sac_time = 0;
82         const char *unit;
83
84         old_tt = stats->total_time.seconds;
85         stats->total_time.seconds += dp->duration.seconds;
86         if (dp->duration.seconds > stats->longest_time.seconds)
87                 stats->longest_time.seconds = dp->duration.seconds;
88         if (stats->shortest_time.seconds == 0 || dp->duration.seconds < stats->shortest_time.seconds)
89                 stats->shortest_time.seconds = dp->duration.seconds;
90         if (dp->maxdepth.mm > stats->max_depth.mm)
91                 stats->max_depth.mm = dp->maxdepth.mm;
92         if (stats->min_depth.mm == 0 || dp->maxdepth.mm < stats->min_depth.mm)
93                 stats->min_depth.mm = dp->maxdepth.mm;
94         if (dp->watertemp.mkelvin) {
95                 if (stats->min_temp == 0 || dp->watertemp.mkelvin < stats->min_temp)
96                         stats->min_temp = dp->watertemp.mkelvin;
97                 if (dp->watertemp.mkelvin > stats->max_temp)
98                         stats->max_temp = dp->watertemp.mkelvin;
99                 stats->combined_temp += get_temp_units(dp->watertemp.mkelvin, &unit);
100                 stats->combined_count++;
101         }
102
103         /* Maybe we should drop zero-duration dives */
104         if (!dp->duration.seconds)
105                 return;
106         stats->avg_depth.mm = (1.0 * old_tt * stats->avg_depth.mm +
107                         dp->duration.seconds * dp->meandepth.mm) / stats->total_time.seconds;
108         if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
109                 sac_time = stats->total_sac_time + dp->duration.seconds;
110                 stats->avg_sac.mliter = (1.0 * stats->total_sac_time * stats->avg_sac.mliter +
111                                 dp->duration.seconds * dp->sac) / sac_time ;
112                 if (dp->sac > stats->max_sac.mliter)
113                         stats->max_sac.mliter = dp->sac;
114                 if (stats->min_sac.mliter == 0 || dp->sac < stats->min_sac.mliter)
115                         stats->min_sac.mliter = dp->sac;
116                 stats->total_sac_time = sac_time;
117         }
118 }
119
120 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
121 {
122         int idx;
123         struct dive *dp;
124
125         *prev_dive = NULL;
126         memset(&stats, 0, sizeof(stats));
127         if (dive_table.nr > 0) {
128                 stats.shortest_time.seconds = dive_table.dives[0]->duration.seconds;
129                 stats.min_depth.mm = dive_table.dives[0]->maxdepth.mm;
130                 stats.selection_size = dive_table.nr;
131         }
132         /* this relies on the fact that the dives in the dive_table
133          * are in chronological order */
134         for (idx = 0; idx < dive_table.nr; idx++) {
135                 dp = dive_table.dives[idx];
136                 if (dp->when == dive->when) {
137                         /* that's the one we are showing */
138                         if (idx > 0)
139                                 *prev_dive = dive_table.dives[idx-1];
140                 }
141                 process_dive(dp, &stats);
142         }
143 }
144
145 /* make sure we skip the selected summary entries */
146 void process_selected_dives(void)
147 {
148         struct dive *dive;
149         unsigned int i, nr;
150
151         memset(&stats_selection, 0, sizeof(stats_selection));
152
153         nr = 0;
154         for_each_dive(i, dive) {
155                 if (dive->selected) {
156                         process_dive(dive, &stats_selection);
157                         nr++;
158                 }
159         }
160         stats_selection.selection_size = nr;
161 }
162
163 static void set_label(GtkWidget *w, const char *fmt, ...)
164 {
165         char buf[80];
166         va_list args;
167
168         va_start(args, fmt);
169         vsnprintf(buf, sizeof(buf), fmt, args);
170         va_end(args);
171         gtk_label_set_text(GTK_LABEL(w), buf);
172 }
173
174 static char * get_time_string(int seconds, int maxdays)
175 {
176         static char buf[80];
177         if (maxdays && seconds > 3600 * 24 * maxdays)
178                 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
179         else {
180                 int days = seconds / 3600 / 24;
181                 int hours = (seconds - days * 3600 * 24) / 3600;
182                 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
183                 if (days > 0)
184                         snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
185                 else
186                         snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
187         }
188         return buf;
189 }
190
191 static void show_single_dive_stats(struct dive *dive)
192 {
193         char buf[80];
194         double value;
195         int decimals;
196         const char *unit;
197         int idx, offset, gas_used;
198         struct dive *prev_dive;
199         struct tm *tm;
200
201         process_all_dives(dive, &prev_dive);
202
203         tm = gmtime(&dive->when);
204         snprintf(buf, sizeof(buf),
205                 "%s, %s %d, %d %2d:%02d",
206                 weekday(tm->tm_wday),
207                 monthname(tm->tm_mon),
208                 tm->tm_mday, tm->tm_year + 1900,
209                 tm->tm_hour, tm->tm_min);
210
211         set_label(single_w.date, buf);
212         set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
213         if (prev_dive)
214                 set_label(single_w.surf_intv,
215                         get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
216         else
217                 set_label(single_w.surf_intv, "unknown");
218         value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
219         set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
220         value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
221         set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
222         if (dive->watertemp.mkelvin) {
223                 value = get_temp_units(dive->watertemp.mkelvin, &unit);
224                 set_label(single_w.water_temp, "%.1f %s", value, unit);
225         } else
226                 set_label(single_w.water_temp, "");
227         value = get_volume_units(dive->sac, &decimals, &unit);
228         if (value > 0) {
229                 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
230         } else
231                 set_label(single_w.sac, "");
232         set_label(single_w.otu, "%d", dive->otu);
233         offset = 0;
234         gas_used = 0;
235         buf[0] = '\0';
236         /* for the O2/He readings just create a list of them */
237         for (idx = 0; idx < MAX_CYLINDERS; idx++) {
238                 cylinder_t *cyl = &dive->cylinder[idx];
239                 unsigned int start, end;
240
241                 start = cyl->start.mbar ? : cyl->sample_start.mbar;
242                 end = cyl->end.mbar ? : cyl->sample_end.mbar;
243                 if (!cylinder_none(cyl)) {
244                         /* 0% O2 strangely means air, so 21% - I don't like that at all */
245                         int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
246                         if (offset > 0) {
247                                 snprintf(buf+offset, 80-offset, ", ");
248                                 offset += 2;
249                         }
250                         snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
251                                 (cyl->gasmix.he.permille + 5) / 10);
252                         offset = strlen(buf);
253                 }
254                 /* and if we have size, start and end pressure, we can
255                  * calculate the total gas used */
256                 if (cyl->type.size.mliter && start && end)
257                         gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
258         }
259         set_label(single_w.o2he, buf);
260         if (gas_used) {
261                 value = get_volume_units(gas_used, &decimals, &unit);
262                 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
263         } else
264                 set_label(single_w.gas_used, "");
265 }
266
267 static void show_total_dive_stats(struct dive *dive)
268 {
269         double value;
270         int decimals, seconds;
271         const char *unit;
272         stats_t *stats_ptr;
273
274         stats_ptr = &stats_selection;
275
276         set_label(stats_w.selection_size, "%d", stats_ptr->selection_size);
277         if (stats_ptr->min_temp) {
278                 value = get_temp_units(stats_ptr->min_temp, &unit);
279                 set_label(stats_w.min_temp, "%.1f %s", value, unit);
280         }
281         if (stats_ptr->combined_temp && stats_ptr->combined_count)
282                 set_label(stats_w.avg_temp, "%.1f %s", stats_ptr->combined_temp / (stats_ptr->combined_count * 1.0), unit);
283         if (stats_ptr->max_temp) {
284                 value = get_temp_units(stats_ptr->max_temp, &unit);
285                 set_label(stats_w.max_temp, "%.1f %s", value, unit);
286         }
287         set_label(stats_w.total_time, get_time_string(stats_ptr->total_time.seconds, 0));
288         seconds = stats_ptr->total_time.seconds;
289         if (stats_ptr->selection_size)
290                 seconds /= stats_ptr->selection_size;
291         set_label(stats_w.avg_time, get_time_string(seconds, 0));
292         set_label(stats_w.longest_time, get_time_string(stats_ptr->longest_time.seconds, 0));
293         set_label(stats_w.shortest_time, get_time_string(stats_ptr->shortest_time.seconds, 0));
294         value = get_depth_units(stats_ptr->max_depth.mm, &decimals, &unit);
295         set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
296         value = get_depth_units(stats_ptr->min_depth.mm, &decimals, &unit);
297         set_label(stats_w.min_overall_depth, "%.*f %s", decimals, value, unit);
298         value = get_depth_units(stats_ptr->avg_depth.mm, &decimals, &unit);
299         set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
300         value = get_volume_units(stats_ptr->max_sac.mliter, &decimals, &unit);
301         set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
302         value = get_volume_units(stats_ptr->min_sac.mliter, &decimals, &unit);
303         set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
304         value = get_volume_units(stats_ptr->avg_sac.mliter, &decimals, &unit);
305         set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
306 }
307
308 void show_dive_stats(struct dive *dive)
309 {
310         /* they have to be called in this order, as 'total' depends on
311          * calculations done in 'single' */
312         show_single_dive_stats(dive);
313         show_total_dive_stats(dive);
314 }
315
316 void flush_dive_stats_changes(struct dive *dive)
317 {
318         /* We do nothing: we require the "Ok" button press */
319 }
320
321 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
322 {
323         GtkWidget *label_widget;
324         GtkWidget *frame;
325
326         frame = gtk_frame_new(label);
327         label_widget = gtk_label_new(NULL);
328         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
329         gtk_container_add(GTK_CONTAINER(frame), label_widget);
330
331         return label_widget;
332 }
333
334 GtkWidget *total_stats_widget(void)
335 {
336         GtkWidget *vbox, *hbox, *statsframe, *framebox;
337
338         vbox = gtk_vbox_new(FALSE, 3);
339
340         statsframe = gtk_frame_new("Statistics");
341         gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
342         framebox = gtk_vbox_new(FALSE, 3);
343         gtk_container_add(GTK_CONTAINER(statsframe), framebox);
344
345         /* first row */
346         hbox = gtk_hbox_new(FALSE, 3);
347         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
348         stats_w.selection_size = new_info_label_in_frame(hbox, "Dives");
349         stats_w.max_temp = new_info_label_in_frame(hbox, "Max Temp");
350         stats_w.min_temp = new_info_label_in_frame(hbox, "Min Temp");
351         stats_w.avg_temp = new_info_label_in_frame(hbox, "Avg Temp");
352
353         /* second row */
354         hbox = gtk_hbox_new(FALSE, 3);
355         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
356
357         stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
358         stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
359         stats_w.longest_time = new_info_label_in_frame(hbox, "Longest Dive");
360         stats_w.shortest_time = new_info_label_in_frame(hbox, "Shortest Dive");
361
362         /* third row */
363         hbox = gtk_hbox_new(FALSE, 3);
364         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
365
366         stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
367         stats_w.min_overall_depth = new_info_label_in_frame(hbox, "Min Depth");
368         stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
369
370         /* fourth row */
371         hbox = gtk_hbox_new(FALSE, 3);
372         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
373
374         stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
375         stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
376         stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
377
378         return vbox;
379 }
380
381 GtkWidget *single_stats_widget(void)
382 {
383         GtkWidget *vbox, *hbox, *infoframe, *framebox;
384
385         vbox = gtk_vbox_new(FALSE, 3);
386
387         infoframe = gtk_frame_new("Dive Info");
388         gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
389         framebox = gtk_vbox_new(FALSE, 3);
390         gtk_container_add(GTK_CONTAINER(infoframe), framebox);
391
392         /* first row */
393         hbox = gtk_hbox_new(FALSE, 3);
394         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
395
396         single_w.date = new_info_label_in_frame(hbox, "Date");
397         single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
398         single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
399
400         /* second row */
401         hbox = gtk_hbox_new(FALSE, 3);
402         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
403
404         single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
405         single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
406         single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
407
408         /* third row */
409         hbox = gtk_hbox_new(FALSE, 3);
410         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
411
412         single_w.sac = new_info_label_in_frame(hbox, "SAC");
413         single_w.otu = new_info_label_in_frame(hbox, "OTU");
414         single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
415         single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");
416
417         return vbox;
418 }