-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.js
415 lines (345 loc) · 10.6 KB
/
stats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* Copyright (c) 2012 Daniel Richman. GNU GPL 3 */
/* The stuff below is messy in places. It's only temporary anyway ;-).
* In particlar, avoid the later arguments to the data() function. */
var kill = true;
var loading = false;
var flight = false;
var page = 0;
var db = null;
function reset_ui() {
$("#intro").hide();
$("#flightlist").hide();
$("#flightlist").empty();
$("#graph").hide();
$("#pie").hide();
$("#daily").hide();
$("#daily_timeranges").hide();
$("#pie_list_container").hide();
$("#pie_list").empty();
$("#loading").hide();
$("#error").hide();
set_flightname(false);
set_flightid("*");
// Leave menu showing
}
function set_flightname(name) {
for (var i = 1; i <= 4; i++) {
if (name)
$("#flightname" + i).text("for flight \"" + name + "\"");
else
$("#flightname" + i).text("");
}
if (!name) {
$("#menu_pie").hide();
} else {
$("#menu_pie").show();
}
}
function set_flightid(id) {
$("#flight_id").text("ID: " + id);
}
function recent_flights() {
if (kill || loading) return;
reset_ui();
$("#loading").show();
loading = true;
flight = false;
db.view("payload_telemetry_stats/launch_time", {
success: got_flights,
error: error,
descending: true
});
}
function all_pie() {
if (kill || loading) return;
data("receiver", true, got_pie, false, "all");
}
function pie(f) {
if (kill || loading) return;
data("flight_receiver", true, got_pie, f, "normal");
}
function daily(max_td) {
if (kill || loading) return;
data("time_uploaded_day", true, got_daily, false, max_td);
}
function flight_pie() {
if (kill || loading) return;
reset_ui();
$("#loading").show();
loading = true;
db.view("payload_telemetry_stats/launch_time", {
success: function (flight_data) {
data("flight_receiver", 1, got_pie, false, flight_data);
},
error: error
});
}
/* view: view name.
* glvl: true for group=true, or group_level value.
* func: func to call when data loaded.
* _f: flight - if set, calls set_flightname/id and restricts startkey/endkey
* arg: argument to pass to func */
function data(view, glvl, func, _f, arg) {
reset_ui();
$("#loading").show();
loading = true;
flight = _f;
var opts = {error: error, reduce: true};
opts.success = function(data) {
func(data, arg);
}
if (glvl === true)
opts.group = true;
else
opts.group_level = glvl;
if (flight) {
set_flightname(flight.name);
set_flightid(flight._id);
opts.startkey = [flight._id, null];
opts.endkey = [flight._id, {}];
}
if (view === "time_uploaded_day" && arg !== null) {
var t = Math.floor((new Date()).getTime() / (1000 * 3600 * 24));
opts.startkey = t - arg;
}
db.view("payload_telemetry_stats/" + view, opts);
}
function got_flights(flight_data) {
if (kill) return;
loading = false;
$("#loading").hide();
var flights = {};
var now = Math.round((new Date()).getTime() / 1000);
for (var i = 0; i < flight_data.rows.length; i++) {
var f = flight_data.rows[i].value;
f._id = flight_data.rows[i].id;
var elem = $("<div class='list_item' />");
var info_elem = $("<div class='list_info' />");
var name_elem = $("<div class='list_label' />");
var extra_elem = $("<div class='list_extra' />");
var id_elem = $("<span class='list_id' />");
var td_elem = $("<span class='list_launchtime' />");
var buttons_elem = $("<div class='list_buttons' />");
var pie_elem = $("<div class='list_go'>Callsign pie</div>");
name_elem.text(f.name);
id_elem.text(f._id);
td_elem.text(f.launch.time);
(function (f) {
pie_elem.click(function () {
pie(f);
});
})(f);
info_elem.append(name_elem);
info_elem.append(extra_elem);
extra_elem.append(id_elem);
extra_elem.append(" ");
extra_elem.append(td_elem);
elem.append(info_elem);
buttons_elem.append(pie_elem);
elem.append(buttons_elem);
$("#flightlist").append(elem);
}
$("#flightlist").show();
}
/* type is either "all", "normal" or an object: the result of a
* launch_times (i.e., flights) view; in which case it's unpacked and then
* type becomes "flight" */
function got_pie(data, type) {
if (kill) return;
loading = false;
$("#loading").hide();
var flight_names = {};
if (type !== "all" && type !== "normal") {
for (var i = 0; i < type.rows.length; i++) {
var row = type.rows[i];
flight_names[row.id] = row.value.name;
}
type = "flight";
}
data.rows.sort(function (a, b) {
return (b.value - a.value);
});
var serieses = [];
var real_labels = {};
var te = $("<tr class='pie_list_title' />");
te.append($("<td class='pie_list_label'>Callsign</td>"));
te.append($("<td class='pie_list_number'>Lines</td>"));
$("#pie_list").append(te);
for (var i = 0; i < data.rows.length; i++) {
var row = data.rows[i];
var name;
if (type === "all")
name = row.key;
else if (type === "normal")
name = row.key[row.key.length - 1];
else if (type === "flight") {
var flight_id = row.key[0];
if (!flight_names[flight_id])
continue;
name = flight_names[flight_id];
}
serieses.push({label: i, data: row.value,
color: serieses.length % 5});
real_labels[i] = name;
var e = $("<tr />");
var a = $("<td class='pie_list_label' />");
a.text(name);
var b = $("<td class='pie_list_number' />");
b.text(row.value);
e.append(a);
e.append(b);
$("#pie_list").append(e);
}
if (serieses.length == 0) {
serieses.push({label: 0, data: 0, color: 0});
real_labels[0] = "";
}
$("#graph").show();
$("#pie").show();
$("#pie_list_container").show();
var settings = {
series: {
pie: {
show: true,
radius: 1,
label: {
show: true,
radius: 1,
formatter: function(label, series) {
var e = $("<div class='pie_label " +
"pie_label_select' />");
var t = $("<div class='pie_label_name' />");
t.text(real_labels[label]);
if (real_labels[label].length > 10)
t.addClass("pie_label_smaller");
e.append(t);
var i = $("<div class='pie_label_info' />");
var p = Math.round(series.percent) + "%";
i.text(series.data[0][1] + " (" + p + ")");
e.append(i);
e.prop("id", "pie_label_" + label);
return $("<div />").append(e).html();
}
}
}
},
legend: {
show: false
},
grid: {
hoverable: true
}
};
var p = $.plot($("#flot"), serieses, settings);
// Get flot's updated objects with .percent, etc.
serieses = p.getData();
// Hide after plotting: flot needs them visible to positions them
for (var i = 0; i < serieses.length; i++) {
var e = $("#pie_label_" + serieses[i].label);
e.removeClass("pie_label_select");
if (serieses[i].percent < 2)
e.hide();
}
var hover_series = null;
var hover_elem = null;
var was_visible = false;
$("#flot").bind("plothover", function (event, pos, item) {
var change = (item && item.series.label !== hover_series);
var remove = !item;
if ((change || remove) && hover_series !== null) {
hover_series = null;
hover_elem.parent().css({"z-index": 0});
hover_elem.removeClass("pie_label_select");
if (!was_visible)
hover_elem.hide();
}
if (change) {
hover_series = item.series.label;
hover_elem = $("#pie_label_" + item.series.label);
was_visible = hover_elem.is(":visible");
hover_elem.show();
hover_elem.parent().css({"z-index": 1});
hover_elem.addClass("pie_label_select");
}
});
}
function got_daily(data, arg) {
if (kill) return;
loading = false;
$("#loading").hide();
var series = [];
for (var i = 0; i < data.rows.length; i++) {
var row = data.rows[i];
var time = row.key * 3600 * 24 * 1000;
series.push([time, row.value]);
}
var settings = {
bars: {
show: true,
barWidth: 3600 * 24 * 1000
},
xaxis: {
mode: "time"
},
legend: {
show: false
}
};
$("#graph").show();
$("#daily").show();
$("#daily_timeranges").show();
var p = $.plot($("#flot"), [{data:series, color:3}], settings);
}
function show_intro(data) {
if (kill) return;
loading = false;
$("#loading").hide();
var count = data.rows.length ? data.rows[0].value : 0;
$("#totallines").text(count);
$("#intro").show();
$("#menu").show();
}
function error(msg) {
reset_ui();
kill = true;
$("#menu").hide();
$("#error").show();
$("#error_info").text(msg);
}
$(document).ready(function () {
var old_error = window.onerror;
window.onerror = function (msg, url, line) {
error(msg);
if (old_error)
old_error(msg, url, line);
}
$("#menu_list").click(recent_flights);
$("#menu_all_pie").click(all_pie);
$("#menu_flight_pie").click(flight_pie);
$("#menu_pie").click(function () {
pie(flight);
});
function daily_button(id, len) {
$(id).click(function () {
daily(len);
});
}
daily_button("#menu_daily", 31 * 4);
daily_button("#daily_week", 7);
daily_button("#daily_month", 31);
daily_button("#daily_fourm", 31 * 4);
daily_button("#daily_year", 366);
daily_button("#daily_all", null);
kill = false;
reset_ui();
$("#menu").hide();
$("#loading").show();
loading = true;
db = $.couch.db("habitat");
db.view("payload_telemetry_stats/receiver", {
reduce: true,
group: false,
error: error,
success: show_intro
});
});