]> git.tdb.fi Git - ext/subsurface.git/blob - statistics.c
Merge branch 'info-split' of git://git.hohndel.org/subsurface
[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 } total_stats_widget_t;
49
50 static total_stats_widget_t stats_w;
51
52 typedef struct {
53         duration_t total_time;
54         /* avg_time is simply total_time / nr -- let's not keep this */
55         duration_t shortest_time;
56         duration_t longest_time;
57         depth_t max_depth;
58         depth_t min_depth;
59         depth_t avg_depth;
60         volume_t max_sac;
61         volume_t min_sac;
62         volume_t avg_sac;
63 } stats_t;
64
65 static stats_t stats;
66
67 static void process_all_dives(struct dive *dive, struct dive **prev_dive)
68 {
69         int idx;
70         struct dive *dp;
71         int old_tt, sac_time = 0;
72
73         *prev_dive = NULL;
74         memset(&stats, 0, sizeof(stats));
75         if (dive_table.nr > 0) {
76                 stats.shortest_time.seconds = dive_table.dives[0]->duration.seconds;
77                 stats.min_depth.mm = dive_table.dives[0]->maxdepth.mm;
78         }
79         /* this relies on the fact that the dives in the dive_table
80          * are in chronological order */
81         for (idx = 0; idx < dive_table.nr; idx++) {
82                 dp = dive_table.dives[idx];
83                 if (dp->when == dive->when) {
84                         /* that's the one we are showing */
85                         if (idx > 0)
86                                 *prev_dive = dive_table.dives[idx-1];
87                 }
88                 old_tt = stats.total_time.seconds;
89                 stats.total_time.seconds += dp->duration.seconds;
90                 if (dp->duration.seconds > stats.longest_time.seconds)
91                         stats.longest_time.seconds = dp->duration.seconds;
92                 if (dp->duration.seconds < stats.shortest_time.seconds)
93                         stats.shortest_time.seconds = dp->duration.seconds;
94                 if (dp->maxdepth.mm > stats.max_depth.mm)
95                         stats.max_depth.mm = dp->maxdepth.mm;
96                 if (dp->maxdepth.mm < stats.min_depth.mm)
97                         stats.min_depth.mm = dp->maxdepth.mm;
98                 stats.avg_depth.mm = (1.0 * old_tt * stats.avg_depth.mm +
99                                 dp->duration.seconds * dp->meandepth.mm) / stats.total_time.seconds;
100                 if (dp->sac > 2800) { /* less than .1 cuft/min (2800ml/min) is bogus */
101                         int old_sac_time = sac_time;
102                         sac_time += dp->duration.seconds;
103                         stats.avg_sac.mliter = (1.0 * old_sac_time * stats.avg_sac.mliter +
104                                                 dp->duration.seconds * dp->sac) / sac_time ;
105                         if (dp->sac > stats.max_sac.mliter)
106                                 stats.max_sac.mliter = dp->sac;
107                         if (stats.min_sac.mliter == 0 || dp->sac < stats.min_sac.mliter)
108                                 stats.min_sac.mliter = dp->sac;
109                 }
110         }
111 }
112
113 static void set_label(GtkWidget *w, const char *fmt, ...)
114 {
115         char buf[80];
116         va_list args;
117
118         va_start(args, fmt);
119         vsnprintf(buf, sizeof(buf), fmt, args);
120         va_end(args);
121         gtk_label_set_text(GTK_LABEL(w), buf);
122 }
123
124 static char * get_time_string(int seconds, int maxdays)
125 {
126         static char buf[80];
127         if (maxdays && seconds > 3600 * 24 * maxdays)
128                 snprintf(buf, sizeof(buf), "more than %d days", maxdays);
129         else {
130                 int days = seconds / 3600 / 24;
131                 int hours = (seconds - days * 3600 * 24) / 3600;
132                 int minutes = (seconds - days * 3600 * 24 - hours * 3600) / 60;
133                 if (days > 0)
134                         snprintf(buf, sizeof(buf), "%dd %dh %dmin", days, hours, minutes);
135                 else
136                         snprintf(buf, sizeof(buf), "%dh %dmin", hours, minutes);
137         }
138         return buf;
139 }
140
141 static void show_single_dive_stats(struct dive *dive)
142 {
143         char buf[80];
144         double value;
145         int decimals;
146         const char *unit;
147         int idx, offset, gas_used;
148         struct dive *prev_dive;
149         struct tm *tm;
150
151         process_all_dives(dive, &prev_dive);
152
153         tm = gmtime(&dive->when);
154         snprintf(buf, sizeof(buf),
155                 "%s, %s %d, %d %2d:%02d",
156                 weekday(tm->tm_wday),
157                 monthname(tm->tm_mon),
158                 tm->tm_mday, tm->tm_year + 1900,
159                 tm->tm_hour, tm->tm_min);
160
161         set_label(single_w.date, buf);
162         set_label(single_w.dive_time, "%d min", (dive->duration.seconds + 30) / 60);
163         if (prev_dive)
164                 set_label(single_w.surf_intv, 
165                         get_time_string(dive->when - (prev_dive->when + prev_dive->duration.seconds), 4));
166         else
167                 set_label(single_w.surf_intv, "unknown");
168         value = get_depth_units(dive->maxdepth.mm, &decimals, &unit);
169         set_label(single_w.max_depth, "%.*f %s", decimals, value, unit);
170         value = get_depth_units(dive->meandepth.mm, &decimals, &unit);
171         set_label(single_w.avg_depth, "%.*f %s", decimals, value, unit);
172         if (dive->watertemp.mkelvin) {
173                 value = get_temp_units(dive->watertemp.mkelvin, &unit);
174                 set_label(single_w.water_temp, "%.1f %s", value, unit);
175         } else
176                 set_label(single_w.water_temp, "");
177         value = get_volume_units(dive->sac, &decimals, &unit);
178         if (value > 0) {
179                 set_label(single_w.sac, "%.*f %s/min", decimals, value, unit);
180         } else
181                 set_label(single_w.sac, "");
182         set_label(single_w.otu, "%d", dive->otu);
183         offset = 0;
184         gas_used = 0;
185         buf[0] = '\0';
186         /* for the O2/He readings just create a list of them */
187         for (idx = 0; idx < MAX_CYLINDERS; idx++) {
188                 cylinder_t *cyl = &dive->cylinder[idx];
189                 unsigned int start, end;
190
191                 start = cyl->start.mbar ? : cyl->sample_start.mbar;
192                 end = cyl->end.mbar ? : cyl->sample_end.mbar;
193                 if (!cylinder_none(cyl)) {
194                         /* 0% O2 strangely means air, so 21% - I don't like that at all */
195                         int o2 = cyl->gasmix.o2.permille ? : AIR_PERMILLE;
196                         if (offset > 0) {
197                                 snprintf(buf+offset, 80-offset, ", ");
198                                 offset += 2;
199                         }
200                         snprintf(buf+offset, 80-offset, "%d/%d", (o2 + 5) / 10,
201                                 (cyl->gasmix.he.permille + 5) / 10);
202                         offset = strlen(buf);
203                 }
204                 /* and if we have size, start and end pressure, we can
205                  * calculate the total gas used */
206                 if (cyl->type.size.mliter && start && end)
207                         gas_used += cyl->type.size.mliter / 1000.0 * (start - end);
208         }
209         set_label(single_w.o2he, buf);
210         if (gas_used) {
211                 value = get_volume_units(gas_used, &decimals, &unit);
212                 set_label(single_w.gas_used, "%.*f %s", decimals, value, unit);
213         } else
214                 set_label(single_w.gas_used, "");
215 }
216
217 static void show_total_dive_stats(struct dive *dive)
218 {
219         double value;
220         int decimals;
221         const char *unit;
222
223         set_label(stats_w.total_time, get_time_string(stats.total_time.seconds, 0));
224         set_label(stats_w.avg_time, get_time_string(stats.total_time.seconds / dive_table.nr, 0));
225         set_label(stats_w.longest_time, get_time_string(stats.longest_time.seconds, 0));
226         set_label(stats_w.shortest_time, get_time_string(stats.shortest_time.seconds, 0));
227         value = get_depth_units(stats.max_depth.mm, &decimals, &unit);
228         set_label(stats_w.max_overall_depth, "%.*f %s", decimals, value, unit);
229         value = get_depth_units(stats.min_depth.mm, &decimals, &unit);
230         set_label(stats_w.min_overall_depth, "%.*f %s", decimals, value, unit);
231         value = get_depth_units(stats.avg_depth.mm, &decimals, &unit);
232         set_label(stats_w.avg_overall_depth, "%.*f %s", decimals, value, unit);
233         value = get_volume_units(stats.max_sac.mliter, &decimals, &unit);
234         set_label(stats_w.max_sac, "%.*f %s/min", decimals, value, unit);
235         value = get_volume_units(stats.min_sac.mliter, &decimals, &unit);
236         set_label(stats_w.min_sac, "%.*f %s/min", decimals, value, unit);
237         value = get_volume_units(stats.avg_sac.mliter, &decimals, &unit);
238         set_label(stats_w.avg_sac, "%.*f %s/min", decimals, value, unit);
239 }
240
241 void show_dive_stats(struct dive *dive)
242 {
243         /* they have to be called in this order, as 'total' depends on
244          * calculations done in 'single' */
245         show_single_dive_stats(dive);
246         show_total_dive_stats(dive);
247 }
248
249 void flush_dive_stats_changes(struct dive *dive)
250 {
251         /* We do nothing: we require the "Ok" button press */
252 }
253
254 static GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
255 {
256         GtkWidget *label_widget;
257         GtkWidget *frame;
258
259         frame = gtk_frame_new(label);
260         label_widget = gtk_label_new(NULL);
261         gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
262         gtk_container_add(GTK_CONTAINER(frame), label_widget);
263
264         return label_widget;
265 }
266
267 GtkWidget *total_stats_widget(void)
268 {
269         GtkWidget *vbox, *hbox, *statsframe, *framebox;
270
271         vbox = gtk_vbox_new(FALSE, 3);
272
273         statsframe = gtk_frame_new("Statistics");
274         gtk_box_pack_start(GTK_BOX(vbox), statsframe, TRUE, FALSE, 3);
275         framebox = gtk_vbox_new(FALSE, 3);
276         gtk_container_add(GTK_CONTAINER(statsframe), framebox);
277
278         /* first row */
279         hbox = gtk_hbox_new(FALSE, 3);
280         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
281
282         stats_w.total_time = new_info_label_in_frame(hbox, "Total Time");
283         stats_w.avg_time = new_info_label_in_frame(hbox, "Avg Time");
284         stats_w.longest_time = new_info_label_in_frame(hbox, "Longest Dive");
285         stats_w.shortest_time = new_info_label_in_frame(hbox, "Shortest Dive");
286
287         /* second row */
288         hbox = gtk_hbox_new(FALSE, 3);
289         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
290
291         stats_w.max_overall_depth = new_info_label_in_frame(hbox, "Max Depth");
292         stats_w.min_overall_depth = new_info_label_in_frame(hbox, "Min Depth");
293         stats_w.avg_overall_depth = new_info_label_in_frame(hbox, "Avg Depth");
294
295         /* third row */
296         hbox = gtk_hbox_new(FALSE, 3);
297         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
298
299         stats_w.max_sac = new_info_label_in_frame(hbox, "Max SAC");
300         stats_w.min_sac = new_info_label_in_frame(hbox, "Min SAC");
301         stats_w.avg_sac = new_info_label_in_frame(hbox, "Avg SAC");
302
303         return vbox;
304 }
305
306 GtkWidget *single_stats_widget(void)
307 {
308         GtkWidget *vbox, *hbox, *infoframe, *framebox;
309
310         vbox = gtk_vbox_new(FALSE, 3);
311
312         infoframe = gtk_frame_new("Dive Info");
313         gtk_box_pack_start(GTK_BOX(vbox), infoframe, TRUE, FALSE, 3);
314         framebox = gtk_vbox_new(FALSE, 3);
315         gtk_container_add(GTK_CONTAINER(infoframe), framebox);
316
317         /* first row */
318         hbox = gtk_hbox_new(FALSE, 3);
319         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
320
321         single_w.date = new_info_label_in_frame(hbox, "Date");
322         single_w.dive_time = new_info_label_in_frame(hbox, "Dive Time");
323         single_w.surf_intv = new_info_label_in_frame(hbox, "Surf Intv");
324
325         /* second row */
326         hbox = gtk_hbox_new(FALSE, 3);
327         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
328
329         single_w.max_depth = new_info_label_in_frame(hbox, "Max Depth");
330         single_w.avg_depth = new_info_label_in_frame(hbox, "Avg Depth");
331         single_w.water_temp = new_info_label_in_frame(hbox, "Water Temp");
332
333         /* third row */
334         hbox = gtk_hbox_new(FALSE, 3);
335         gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
336
337         single_w.sac = new_info_label_in_frame(hbox, "SAC");
338         single_w.otu = new_info_label_in_frame(hbox, "OTU");
339         single_w.o2he = new_info_label_in_frame(hbox, "O" UTF8_SUBSCRIPT_2 " / He");
340         single_w.gas_used = new_info_label_in_frame(hbox, "Gas Used");
341
342         return vbox;
343 }