forked from karpathy/ulogme
-
Notifications
You must be signed in to change notification settings - Fork 14
/
overview.html
568 lines (501 loc) · 20.4 KB
/
overview.html
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>uLogMe - Overview</title>
<meta content="uLogMe Overview page - See https://GitHub.com/Naereen/uLogMe/" name="description" />
<link type="text/css" rel="stylesheet" href="css/overview_style.css" />
<link rel="stylesheet" media="screen and (min-width: 280px)" type="text/css" href="css/nprogress.css" />
<link rel="stylesheet" type="text/css" href="css/headerlink.css" />
<link rel="icon" href="favicon.png" type="image/png" />
<script type="text/javascript" src="js/jquery-1.8.3.min.js" charset="utf-8"></script>
<script type="text/javascript" src="js/spin.min.js" charset="utf-8"></script>
<script type="text/javascript" src="js/render_utils.js" charset="utf-8"></script>
<script type="text/javascript" src="js/render_settings.js" charset="utf-8"></script>
<script type="text/javascript" src="js/d3.min.js" charset="utf-8"></script>
<script type="text/javascript" src="js/d3utils.js" charset="utf-8"></script>
<script type="text/javascript" src="js/underscore.min.js" charset="utf-8"></script>
<script type="text/javascript" src="js/ulogme_common.js" charset="utf-8"></script>
<script type="text/javascript" src="js/nprogress.min.js" charset="utf-8"></script>
<script type="text/javascript" src="js/mousetrap.min.js" charset="utf-8"></script>
<script type="text/javascript" src="js/all-easter-eggs.js" charset="utf-8"></script>
<script type="text/javascript">
key_stats_all = [] // global
var skipdraw = {}; // global...
function drawEvents() {
$("#content").empty();
// draw the legend on top of the svg
var d3div = d3.select("#content");
var ldiv = d3div.append("div").attr("class", "legenddiv");
for(var i=0;i<etypes.length;i++) {
var pi = ldiv.append("p").text(etypes[i]).attr("style", "color:" + color_hash[etypes[i]]);
var m = etypes[i];
if(skipdraw[m]) { pi.attr("class", "skipdrawyes"); }
else { pi.attr("class", "skipdrawno"); }
pi.on("click", function(i) { // close over index i
return function() {
// toggle whether this one gets drawn
var m = etypes[i];
if(skipdraw[m] === false) { skipdraw[m] = true; }
else { skipdraw[m] = false; }
drawEvents(); // and redraw the graph!
}
}(i));
}
var margin = {top: 10, right: 10, bottom: 100, left: 40};
var fullwidth = 1200;
var fullheight = 800;
var width = fullwidth - margin.left - margin.right;
var height = fullheight - margin.top - margin.bottom;
var svg = d3div.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
var yscale = 0.008;
// draw y axis labels
var yoff = 0;
var yn = 0;
while(yoff < height) {
var yy = (height + margin.top - yoff);
svg.append("text")
.attr("transform", "translate(0," + (yy-3) + ")")
.text(yn + (yn < 10 ? " " : "") + "h");
svg.append("line")
.attr("x1", 0)
.attr("x2", width + margin.left)
.attr("y1", yy)
.attr("y2", yy)
.attr("stroke", "#EEE")
.attr("stroke-width", 1);
yn++;
yoff += 3600 * yscale;
}
// draw x axis labels
var N = edur.length;
var dsz = width / N;
svg.selectAll(".xlabel")
.data(event_list)
.enter()
.append("text")
.attr("transform", function(d, i) {
var x = margin.left + i * dsz;
var y = height + margin.top + 3;
return "translate(" + x + "," + y + ")rotate(90)";})
.attr("fill", "#333")
.attr("font-family", "arial")
.attr("font-size", "14px")
.text(function(d) { var dobj = new Date(d.t0 * 1000); return ppDateShort(dobj); })
// draw vertical lines at week boundaries for easier visual consumption
svg.selectAll(".yd")
.data(event_list)
.enter()
.append("line")
.attr("stroke", function(d) {
var dobj = new Date(d.t0 * 1000);
var isMonday = dobj.getDay() === 1;
return isMonday ? "#BBB" : "#EEE"
})
.attr("x1", function(d, i) { return margin.left + i * dsz; })
.attr("x2", function(d, i) { return margin.left + i * dsz; })
.attr("y1", height + margin.top)
.attr("y2", margin.top);
// draw the data
for(var k=0;k<N;k++) {
// convert from kv to list
var dtimes = [];
for(var i=0;i<etypes.length;i++) {
var m = etypes[i];
if(skipdraw[m]) continue; // skip!
dtimes.push({
val: edur[k].hasOwnProperty(m) ? edur[k][m] : 0,
col: color_hash[m]
});
}
svgg = svg.append("g")
.attr("style", "cursor:pointer;")
.on("click", function(q){
return function(){
window.location.href = "index.html?gotoday=" + q;
};
}(k)); // have to closure k
var gh = 0;
svgg.selectAll(".day"+k)
.data(dtimes)
.enter()
.append("rect")
.attr("width", dsz)
.attr("height", function(d) { return d.val * yscale; })
.attr("x", margin.left + k * dsz)
.attr("y", function(d) { gh += d.val; return height + margin.top - gh * yscale; })
.attr("fill", function(d) { return d.col; } );
}
}
// enter .m field and build up etypes[]
var etypes = []
function mapEvents(es) {
for(var i=0,N=es.length;i<N;i++) {
var e = es[i];
e.m = mapwin(e.s);
if(etypes.indexOf(e.m) === -1) {
etypes.push(e.m);
skipdraw[e.m] = false;
}
}
}
function statEvents(es, ecounts) {
if(es.length === 0) return; // empty case
var t0 = es[0].t;
var ixprev = 0;
for(var i=1,N=es.length;i<N;i++) {
var e = es[i];
var dt = es[i].t - es[ixprev].t; // length of time for last event
es[ixprev].dt = dt;
var tmap = es[ixprev].m; // mapped title of previous events
if(ecounts.hasOwnProperty(tmap)) {
ecounts[tmap] += dt;
} else {
ecounts[tmap] = 0;
}
ixprev = i;
}
es[N-1].dt = 1; // last event we dont know how long lasted. assume 1 second?
}
var edur = []; // stores durations for events for all days. Core structure!
var color_hash = {};
function analyzeEvents() {
edur = []; // reset global var
for(var k=0;k<events.length;k++) {
var es = events[k]["window_events"]; // window events for day k
mapEvents(es); // assign group names to structure in field .m, build etypes[]
}
color_hash = colorHashStrings(etypes);
for(var k=0;k<events.length;k++) {
edur.push({}) // hmmm
var es = events[k]["window_events"]; // window events for day k
statEvents(es, edur[k]);
}
}
function drawKeyEvents() {
var W = $("#keystats").width();
var H = 15;
$("#keystats").empty();
var wmargin = 100;
var time_bin = 10*60; // in seconds
var allkevents = [];
var d0s = [];
var ktots = [];
var maxs = 0;
var maxktot = 0;
var kevents_global = [];
var max_kevents_global = 0;
var sum_kevents_global = 0;
for(var k=0;k<events.length;k++) {
var es = events[k]["keyfreq_events"]; // window events for day k
// get 7am time on this day
var d0 = new Date(es[0].t * 1000);
d0.setHours(7);
d0.setMinutes(0);
d0.setSeconds(0);
d0.setMilliseconds(0);
var t00 = d0.getTime() / 1000; // morning at 7am
var ft = t00 + 60*60*24; // 7am the next day
var kevents = [];
var t = t00;
while(t <= ft) {
kevents.push(0); t += time_bin; // create time bins
if(k===0) { kevents_global.push(0); }
}
// bucket up the events
var ktot = 0;
for(var q=0,n=es.length;q<n;q++) {
var kw = es[q];
var binix = Math.floor((kw.t - t00)/time_bin);
var news = kevents[binix] + kw.s;
kevents[binix] = news;
var newg = kevents_global[binix] + kw.s;
if(news > maxs) { maxs = news; }
kevents_global[binix] = newg;
if(newg > max_kevents_global) { max_kevents_global = newg; }
sum_kevents_global += kw.s;
ktot += kw.s;
}
allkevents.push(kevents);
d0s.push(d0);
ktots.push(ktot);
if(ktot > maxktot) { maxktot = ktot; }
}
// draw global key events across all days as line
var sx = kevents_global.length;
var bar_width = (W - wmargin) / sx;
var div = d3.select("#keystats").append("div");
var svg = div.append("svg")
.attr("width", W)
.attr("height", H*2);
var line = d3.svg.line()
.x(function(d,i) { return (W - 2*wmargin) * i / sx + wmargin; })
.y(function(d) { return 2*H - d / max_kevents_global * H * 2; });
svg.append("path")
.datum(kevents_global)
.attr("class", "line")
.attr("d", line);
// draw x axis: times of the day
var div = d3.select("#keystats").append("div");
var svg = div.append("svg")
.attr("width", W)
.attr("height", 20);
for(var q=0;q<24;q++) {
svg.append("text")
.attr("font-size", 14)
.attr("font-family", "arial")
.attr("transform", "translate(" + (q/24*(W-2*wmargin)+2+wmargin) + ",16)")
.text(function(d, i) { return ((q + 7) % 24) + ":00"; });
svg.append("line")
.attr("x1", q/24*(W-2*wmargin)+wmargin)
.attr("x2", q/24*(W-2*wmargin)+wmargin)
.attr("y1", 0)
.attr("y2", 20)
.attr("stroke", "#000")
.attr("stroke-width", 2);
}
for(var k=0;k<events.length;k++) {
var kevents = allkevents[k];
var div = d3.select("#keystats").append("div").attr("class", "divkeys");
var svg = div.append("svg")
.attr("width", W)
.attr("height", H);
var sx = kevents.length;
svg.selectAll(".ke")
.data(kevents)
.enter()
.append("rect")
.attr("x", function(d,i) { return (W - 2*wmargin) * i / sx + wmargin; })
.attr("width", bar_width)
.attr("y", 0)
.attr("height", H)
.attr("fill", function(d) {
var e = d / maxs;
var r = Math.floor(Math.max(0, 255 - e*255));
var g = Math.floor(Math.max(0, 255 - e*255));
var b = 255;
return "rgb(" + r + "," + g + "," + b + ")";
});
// draw y axis: time
svg.append("text")
.attr("font-size", 14)
.attr("transform", "translate(0,12)")
.attr("font-family", "arial")
.text(ppDateShort(d0s[k]));
// draw y axis: total number of keys
svg.append("rect")
.attr("x", W - wmargin + 5)
.attr("y", 0)
.attr("width", function(d) { return ktots[k]/maxktot * wmargin; })
.attr("height", H)
.attr("fill", "rgb(255, 100, 100)");
svg.append("text")
.attr("transform", "translate(" + (W - wmargin + 7) + ", " + 13 + ")")
.attr("font-size", 14)
.attr("font-family", "arial")
.text(ktots[k]);
}
var kevents_global
div.append("p").text("Total keys pressed: " + sum_kevents_global + " in " + events.length + " days (" + Math.floor(sum_kevents_global/events.length) + " per day average)");
}
function loadAllEvents() {
// load the master json file and all the other jsons
getJSON_CACHEHACK("json/export_list.json").then(function(days_list) {
event_list = days_list; // global variable assign
console.log("uLogMe (overview.html): fetched export_list OK.")
return Promise.all(days_list.map(function(x) { return getJSON_CACHEHACK(x.fname); }));
}).then(function(days) {
events = days; // global variable assign
}).catch(function(err){
console.log("uLogMe (overview.html): some error happened: " + err);
}).then(function() {
analyzeEvents(); // all events have been loaded. Analyze!
drawEvents(); // and d3js draw!
key_stats_all = mergeWindowKeyEvents();
visualizeKeySummary(key_stats_all);
visualizeTimeSummary(edur);
drawKeyEvents(); // draw key events
});
}
function mergeWindowKeyEvents() {
// iterate over all events and compute key_stats
var key_stats_all = [];
for(var k=0;k<events.length;k++) {
var es = events[k]["window_events"]; // window events for day k
var ek = events[k]["keyfreq_events"]; // key events
key_stats = computeKeyStats(es, ek); // defined in ulogme_common
key_stats_all.push(key_stats);
}
return key_stats_all;
}
function visualizeKeySummary(key_stats_all) {
$("#keysummary").empty();
// merge all keystats into a single global key stats
var gstats = {};
_.each(etypes, function(m) { gstats[m] = {name: m, val: 0, n:0, col: color_hash[m]}; });
var n = key_stats_all.length;
for(var i=0;i<n;i++) {
var key_stats = key_stats_all[i];
for(var j=0;j<etypes.length;j++) {
var e = etypes[j];
if(key_stats.hasOwnProperty(e)) {
gstats[e].val += key_stats[e].f;
gstats[e].n += key_stats[e].n;
}
}
}
gstats = _.filter(gstats, function(d) { return d.val > 0; }); // cutoff at 0 keys
_.each(gstats, function(d) { d.text = d.val + " (" + (d.val/(d.n*9)).toFixed(2) + "/s) (" + d.name + ")"; });
gstats = _.sortBy(gstats, "val").reverse();
// visualize as chart
var chart_data = {};
chart_data.width = 600;
chart_data.barheight = 30;
chart_data.textpad = 300;
chart_data.textmargin = 10;
chart_data.title = "Total keys per window";
chart_data.data = gstats;
d3utils.drawHorizontalBarChart(d3.select("#keysummary"), chart_data);
}
function visualizeTimeSummary(edur) {
$("#timesummary").empty();
var gstats = {};
_.each(etypes, function(m) { gstats[m] = {name: m, val: 0, n:0, col: color_hash[m]}; });
var n = edur.length;
for(var i=0;i<n;i++) {
var key_stats = edur[i];
for(var j=0;j<etypes.length;j++) {
var e = etypes[j];
if(key_stats.hasOwnProperty(e)) {
gstats[e].val += key_stats[e];
}
}
}
gstats = _.filter(gstats, function(d) { return d.val > 0; }); // cutoff at 0 keys
_.each(gstats, function(d) { d.text = (d.val/60/60).toFixed(2) + "hr (" + d.name + ")"; });
gstats = _.sortBy(gstats, "val").reverse();
// visualize as chart
var chart_data = {};
chart_data.width = 600;
chart_data.barheight = 30;
chart_data.textpad = 300;
chart_data.textmargin = 10;
chart_data.title = "Total time per window";
chart_data.data = gstats;
d3utils.drawHorizontalBarChart(d3.select("#timesummary"), chart_data);
}
function startSpinner() {
NProgress.start();
// create a spinner object
var target = document.getElementById("spinnerdiv");
opts = {left:"30px", top:"40px", radius: 10, color: "#FFF" };
var spinner = new Spinner(opts).spin(target);
NProgress.inc();
}
function stopSpinner() {
$("#spinnerdiv").empty();
NProgress.done();
}
var event_list;
var events;
function start() {
NProgress.start();
loadAllEvents();
NProgress.inc();
$("#reloadbutton").click(reload);
// XXX setInterval(redraw, 1000); // in case of window resize, we can redraw
// Automatic reload, can be configurer in the render_settings.js file
// Idea and code from https://github.com/blackdaemon/ulogme/commit/b356854
if (auto_reload_interval > 0) {
setInterval(auto_reload, auto_reload_interval*60*1000);
}
NProgress.done();
}
// setup refresh handler to create a post request to /reload
// reload, with or without animation
function reload(animate=true) {
startSpinner();
$.post("/refresh",
{"time" : 0},
function(data,status){
console.log("uLogMe (overview.html): Data: " + data + "\nStatus: " + status);
if(data === 'OK') {
// everything went well, refresh current view
loadAllEvents(); // reload all events
}
stopSpinner();
});
}
// auto-reload every XXX minutes, cf. render_settings.js, without animation
function auto_reload() {
reload(false);
}
// MouseTrap keyboard shortcuts (cf. http://craig.is/killing/mice)
Mousetrap.bind(["h", "?"], function() {
console.log("uLogMe (overview.html): Help alert window ...");
window.alert(
"This « uLogMe Overview » page has the following keyboard shortcuts:"
+ "\n - h or ? : for this help window."
+ "\n - r : for refreshing the view by reloading the data (you can also refresh the page)."
+ "\n - i or s : goes to the more recent day of the Single-day view page for uLogMe."
+ "\n\nuLogMe is a free and open-source software, see https://github.com/Naereen/uLogMe/ for more details and the latest version."
+ "\nBuilt with ♥ by Lilian Besson (Naereen) and other contributors on GitHub."
);
aRandomEasterEgg();
});
Mousetrap.bind(["r"], function() {
console.log("uLogMe (overview.html): Refreshing data ...");
reload();
aRandomEasterEgg();
});
Mousetrap.bind(["i", "s"], function() {
console.log("uLogMe (overview.html): Going to daily-vew page ...");
location.href = "index.html";
aRandomEasterEgg();
});
</script>
</head>
<body onload="start()">
<!-- <script type="text/javascript">NProgress.start();</script> -->
<div id="spinnerdiv" title="Wait for the data to be loaded..."></div>
<div id="reloadbutton" title="Reload the data (override cache)">⟲</div>
<div id="indexlink"><a href="index.html" title="Switch to the Single-day view page">Single-day View</a></div>
<h1><a title="Check-out my version of uLogMe on GitHub" href="https://github.com/Naereen/uLogMe/">uLogMe</a> by <a href="https://github.com/Naereen/">Naereen</a></h1>
<div id="wrap">
<div id="graphopts">
</div>
<h2 id="totalperday">Total amount of time per day <a class="headerlink" href="#totalperday" title="Anchor for the « Total amount of time per day » chart">¶</a></h2>
<div id="content"></div>
<h2 id="perwindowsummary">Per-window summaries <a class="headerlink" href="#perwindowsummary" title="Anchor for the « Per-window summaries » chart">¶</a></h2>
<div id="summaries">
<div id="timesummary"></div>
<div id="keysummary"></div>
</div>
<h2 id="keystatseachday">Key frequencies during each day <a class="headerlink" href="#keystatseachday" title="Anchor for the « Key frequencies during each day » chart">¶</a></h2>
<div id="keystats"></div>
</div>
<br><br><br><br>
<footer>
<br><br><br>
<div class="linksfooter">
<h3 id="about">About <a class="headerlink" href="#about" title="Anchor for the about section">¶</a></h3>
<a href="https://github.com/Naereen/uLogMe/">Full documentation can be found on GitHub.</a>
<a href="https://lbesson.mit-license.org/"><img src="svg/license.svg" alt="GitHub license"></a>
<br>
Thanks to <a href="https://GitHub.com/karpathy/">@karpathy</a> and <a href="https://GitHub.com/karpathy/ulogme/graphs/contributors">contributors</a> for the <a href="https://GitHub.com/karpathy/ulogme">initial project</a>.
<br>
Hacked and tweaked with <span class="love">♥</span> by <a href="https://GitHub.com/Naereen/">Lilian Besson (Naereen)</a>, © 2016-2018.
<br>
<a href="https://GitHub.com/Naereen/"><img src="svg/built-with-love.svg" alt="ForTheBadge built-with-love"></a>
</div>
<!-- Google Analytics code -->
<script type="text/javascript" src="js/ga.js"></script>
<noscript>
<img style="visibility: hidden; display: none;" src="https://ga-beacon.appspot.com/UA-38514290-1/ulogme/overview.html?pixel"/>
</noscript>
</footer>
</body>
</html>