forked from macc704/kf6apijs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smallATK.html
830 lines (737 loc) · 27.3 KB
/
smallATK.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
<html>
<head>
<link rel="stylesheet" type="text/css" href="bower_components/dcjs/dc.css">
<style>
.link {
stroke: #ccc;
}
.node text {
pointer-events: none;
font: 10px sans-serif;
}
</style>
<script src="jquery.js"></script>
<script src="kf6api.js"></script>
<script src="bower_components/crossfilter2/crossfilter.min.js"></script>
<script src="bower_components/d3/d3.min.js"></script>
<script src="bower_components/dcjs/dc.min.js"></script>
<script>
function loginPressed() {
var serverURL = $('#serverURL').val();
kf6.setBaseURL(serverURL);
var uname = $('#uname').val();
var password = $('#password').val();
kf6.login(uname, password, function () {
getCommunities();
}, function (data) {
alert('login failed: ' + data.status);
});
}
function getCommunities() {
kf6.getCommunities(function (data) {
$('#communities').html('');
data.forEach(function (each) {
var li = '';
li += '<li>';
li += each._community.title;
li += '<input type="button" value="select" onclick="communityPressed(\'' + each.communityId + '\')">';
li += '</li>';
$('#communities').append(li);
});
});
}
function communityPressed(communityId) {
initialize(communityId, initializeDOMforCommunity);
}
function initializeDOMforCommunity() {
$('#views').html('<tr><th>Checked</th><th>Title</th><th>#ofNotes</th></tr>');
gViews.forEach(function (view) {
var tr = '';
tr += '<tr>';
tr += '<td>' + '<input id="view_' + view._id + '" type="checkbox">' + '</td>';
tr += '<td>' + view.title + '</td>';
tr += '<td>' + view.data.numOfNotes + '</td>';
view.data.weeks.forEach(function (week) {
tr += '<td>' + week.count + '</td>';
});
tr += '</tr>';
$('#views').append(tr);
});
$('#authors').html('<tr><th>Checked</th><th>Name</th><th>Nickname</th></tr>');
gAuthors.forEach(function (author) {
author.name = author.firstName;
var tr = '';
tr += '<tr>';
tr += '<td>' + '<input id="author_' + author._id + '" type="checkbox" checked>' + '</td>';
tr += '<td>' + author.firstName + ' ' + author.lastName + '</td>';
tr += '<td>' + '<input id="authorname_' + author._id + '" type="textfield" value="' + author.name + '">' + '</td>';
tr += '</tr>';
$('#authors').append(tr);
});
}
// initialize a community
function initialize(communityId, handler) {
kf6.setCommunity(communityId);
loadAuthors(function () {
loadObjects(function () {
loadRecords(function () {
loadViews(function () {
setRecords(gRecords);
initdc(gRecords);
handler();
});
});
});
});
}
var gAuthors;
var gAuthorsTable = {};
function loadAuthors(handler) {
kf6.getAuthors(function (authors) {
gAuthors = authors;
authors.forEach(function (author) {
gAuthorsTable[author._id] = author;
});
handler();
});
}
var gObjects;
var gObjectsTable = {};
function loadObjects(handler) {
var query = {
pagesize: 10000,
searchMode: 'title'
};
kf6.getObjects(query, function (objects) {
gObjects = objects;
objects.forEach(function (object) {
gObjectsTable[object._id] = object;
});
handler();
});
}
var gRecords;
function loadRecords(handler) {
var query = {};
kf6.getRecords(query, function (records) {
gRecords = records;
handler();
});
}
function setRecords(records) {
gObjects.forEach(function (object) {
object.records = [];
});
records.forEach(function (record) {
var object = gObjectsTable[record.targetId];
if (!object) {
//console.error('object not found in records. id=' + record.targetId)
return;
}
object.records.push(record);
});
}
var gViews;
var gViewsTable = {};
function loadViews(handler) {
kf6.getViews(function (data) {
gViews = data;
initViews(handler);
});
}
function initViews(handler) {
gViews.forEach(function (view) {
gViewsTable[view._id] = view;
tasks.push(function (shandler) {
initView(view, shandler);
});
});
doTask(function () {
handler();
});
}
var tasks = [];
function doTask(handler) {
var len = tasks.length;
if (len <= 0) {
handler();
return;
}
var count = 0;
tasks.forEach(function (task) {
task(function () {
count = count + 1;
if (len <= count) {
handler();
}
});
})
tasks = [];
}
function initView(view, handler) {
kf6.getLinksFrom(view._id, function (links) {
view.data = {};
view.data.links = links;
var objects = [];
links.forEach(function (link) {
if (link.type === 'contains') {
var object = gObjectsTable[link.to];
if (!object) {
console.info('object not found=' + link.to)
return;
}
objects.push(gObjectsTable[link.to]);
}
});
var numOfNotes = 0;
objects.forEach(function (object) {
if (object.type === 'Note') {
numOfNotes++;
}
});
view.data.objects = objects;
view.data.numOfNotes = numOfNotes;
initView2(view);
handler();
});
}
// Selected Views and Authors
function getSelectedAuthors() {
var authors = [];
gAuthors.forEach(function (author) {
author.name = $('#authorname_' + author._id).prop('value');
if ($('#author_' + author._id).prop('checked')) {
authors.push(author);
}
});
return authors;
}
function getSelectedViews() {
var views = [];
gViews.forEach(function (view) {
if ($('#view_' + view._id).prop('checked')) {
views.push(view);
}
});
return views;
}
function hasSelectedViews() {
return getSelectedViews().length > 0;
}
// Show data algorithm
function updateStatistics() {
var views = getSelectedViews();
if (!hasSelectedViews()) {
window.alert('No view selected.');
return;
}
var viewname = views[0].title;
for (var i = 1; i < views.length; i++) {
viewname += ', ' + views[i].title;
}
$('#viewname').html(viewname);
//initialize counter
gAuthors.forEach(function (author) {
var counter = {};
counter.notes = [];
counter.drawings = [];
counter.others = [];
counter.reads = [];
counter.modifies = [];
author.counter = counter;
});
//select objects
var objectsTable = {}
views.forEach(function (view) {
var objectsInView = view.data.objects;
objectsInView.forEach(function (object) {
objectsTable[object._id] = object;
})
});
var objects = [];
Object.keys(objectsTable).forEach(function (key) {
objects.push(objectsTable[key]);
});
//count
objects.forEach(function (object) {
object.records.forEach(function (record) {
if (object.type === 'Note') {
var counter = gAuthorsTable[record.authorId].counter;
if (record.type === 'read') {
counter.reads.push(record);
} else if (record.type === 'modified') {
counter.modifies.push(record);
}
}
});
object.authors.forEach(function (authorId) {
var counter = gAuthorsTable[authorId].counter;
if (object.type === 'Note') {
counter.notes.push(object);
} else if (object.type === 'Drawing') {
counter.drawings.push(object);
} else {
counter.others.push(object);
}
});
});
//calc % of note read
var numOfNotes = 0;
objects.forEach(function (object) {
if (object.type === 'Note') {
numOfNotes++;
}
});
var authors = getSelectedAuthors();
authors.forEach(function (author) {
var counter = author.counter;
var readinglist = {};
counter.reads.forEach(function (read) {
readinglist[read.targetId] = 1;
});
counter.readcount = Object.keys(readinglist).length;
counter.percentOfReading = Math.round((counter.readcount / numOfNotes) * 10000) / 100;
});
authors.forEach(function (author) {
var counter = author.counter;
var modifylist = {};
counter.modifies.forEach(function (modify) {
modifylist[modify.targetId] = 1;
});
counter.modifycount = Object.keys(modifylist).length;
});
//make table
$('#notes').html('<tr><th>Title</th><th># Notes</th><th># Drawings</th><th># reads</th><th># notes read</th><th>% of readings</th><th># modifies</th><th># notes modified</th></tr>');
authors.forEach(function (author) {
var counter = author.counter;
var tr = '';
tr += '<tr>';
tr += '<td>' + author.name + '</td>';
tr += '<td>' + counter.notes.length + '</td>';
tr += '<td>' + counter.drawings.length + '</td>';
//tr += '<td>' + counter.others.length + '</td>';
tr += '<td>' + counter.reads.length + '</td>';
tr += '<td>' + counter.readcount + '</td>';
tr += '<td>' + counter.percentOfReading + '</td>';
tr += '<td>' + counter.modifies.length + '</td>';
tr += '<td>' + counter.modifycount + '</td>';
tr += '</tr>';
$('#notes').append(tr);
});
}
// setting of dc.js
var hitslineChart;
var dateDim;
var minDate;
var maxDate;
var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S.%LZ").parse;
function initdc(data) {
data.forEach(function (d) {
d.date = parseDate(d.timestamp);
if (d.type === 'read') {
d.read = 1;
} else {
d.read = 0;
}
if (d.type === 'modify') {
d.modify = 1;
} else {
d.modify = 0;
}
d.total = 1;
});
var ndx = crossfilter(data);
dateDim = ndx.dimension(function (d) {
return d.date;
});
var typeDim = ndx.dimension(function (d) {
return d.type;
});
minDate = dateDim.bottom(1)[0].date;
maxDate = dateDim.top(1)[0].date;
var readGroup = dateDim.group().reduceSum(function (d) {
return d.read;
});
// var modifyGroup = dateDim.group().reduceSum(function(d) {
// return d.modify;
// });
hitslineChart = dc.barChart('#chart-line-hitsperday');
hitslineChart
.width(700).height(200)
.dimension(dateDim)
.group(readGroup, 'Read')
//.group(modifyGroup, 'Modify')
//.renderArea(true)//for line chart
.centerBar(true) //for line chart
.x(d3.time.scale().domain([minDate, maxDate]))
.legend(dc.legend().x(50).y(10).itemHeight(13).gap(5))
.yAxisLabel('Hits per day');
// dummy for update
var hits = dateDim.group().reduceSum(function (d) {
return d.total;
});
var dummy = dc.pieChart('#dummy');
dummy
.width(200).height(200)
.dimension(dateDim)
.group(hits)
.innerRadius(30);
dummy._doRedraw = function () {
var filtered = typeDim.filter().top(Infinity);
setRecords(filtered);
if (hasSelectedViews()) {
updateStatistics();
}
showRange();
};
dummy._doRender = function () {
var filtered = typeDim.filter().top(Infinity);
setRecords(filtered);
if (hasSelectedViews()) {
updateStatistics();
}
showRange();
};
dc.renderAll();
}
function fromPressed() {
var text = document.getElementById('fromDate').value;
var parser = d3.time.format("%Y%m%d").parse;
var d3time = parser(text);
if (!d3time) {
window.alert('invalid dateformat');
return;
}
if (!hitslineChart) {
return;
}
var to;
var range = getRange();
if (range) {
to = range.to;
} else {
to = dateDim.top(1)[0].date;
}
hitslineChart.filter(null);
hitslineChart.filter(dc.filters.RangedFilter(d3time, to));
dc.redrawAll();
}
function toPressed() {
var text = document.getElementById('toDate').value;
var parser = d3.time.format("%Y%m%d").parse;
var d3time = parser(text);
if (!d3time) {
window.alert('invalid dateformat');
return;
}
if (!hitslineChart) {
return;
}
var from;
var range = getRange();
if (range) {
from = range.from;
} else {
from = dateDim.bottom(1)[0].date;
}
hitslineChart.filter(null);
hitslineChart.filter(dc.filters.RangedFilter(from, d3time));
dc.redrawAll();
}
function getRange() {
if (!hitslineChart) return null;
var rangedFilter = hitslineChart.filters()[0];
if (!rangedFilter) return null;
var from = rangedFilter[0];
var to = rangedFilter[1];
return { from: from, to: to };
}
function showRange() {
var range = getRange();
if (!range) {
document.getElementById('fromCurrent').innerHTML = '';
document.getElementById('toCurrent').innerHTML = '';
return;
}
document.getElementById('fromCurrent').innerHTML = range.from;
document.getElementById('toCurrent').innerHTML = range.to;
}
// for open window
function openNewWindow() {
window.open('result.html');
}
function notify(childWindow) {
openGraph(childWindow);
}
// for graph
function openGraph(childWin) {
var authors = getSelectedAuthors();
authors.forEach(function (author) {
author.links = {};
var reads = author.counter.reads;
reads.forEach(function (read) {
var note = gObjectsTable[read.targetId];
note.authors.forEach(function (toId) {
if (!author.links[toId]) {
author.links[toId] = 0;
}
author.links[toId]++;
});
});
});
var graphdata = { nodes: [], links: [] };
var authorIds = {};
var authorId = 0;
authors.forEach(function (author) {
var name = author.name;
graphdata.nodes[authorId] = { 'name': name, 'group': '1' };
authorIds[author._id] = authorId;
authorId++;
});
authors.forEach(function (author) {
Object.keys(author.links).forEach(function (toId) {
var to = authorIds[toId];
if (!to) { return; } //filtered author
var from = authorIds[author._id];
var value = author.links[toId];
graphdata.links.push({ source: from, target: to, weight: value });
});
});
if (childWin) {
childWin.graphinit(graphdata);
} else {
graphinit(graphdata);
}
}
function graphinit(json) {
var width = 960,
height = 500
//var svg = d3.select("#graph").append("svg")
var svg = d3.select("#sna")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.gravity(0.05)
.distance(100)
.charge(-100)
.size([width, height]);
force
.nodes(json.nodes)
.links(json.links)
.start();
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function (d) { return Math.sqrt(d.weight); });
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
node.append("circle")
// .attr("xlink:href", "https://github.com/favicon.ico")
.attr("r", 7)
.attr("fill", "gray");
// .attr("x", -8)
// .attr("y", -8)
node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function (d) { return d.name });
force.on("tick", function () {
link.attr("x1", function (d) { return d.source.x; })
.attr("y1", function (d) { return d.source.y; })
.attr("x2", function (d) { return d.target.x; })
.attr("y2", function (d) { return d.target.y; });
node.attr("transform", function (d) { return "translate(" + d.x + "," + d.y + ")"; });
});
}
function toLocal() {
$('#serverURL').attr('value', 'http://localhost:9000/');
}
// localstorage
function loadSetting() {
gAuthors.forEach(function (author) {
$('#author_' + author._id).prop('checked', false);
});
var authors = JSON.parse(localStorage.getItem('authors'));
authors.forEach(function (author) {
$('#author_' + author._id).prop('checked', author.checked);
$('#authorname_' + author._id).prop('value', author.name);
});
}
function saveSetting() {
gAuthors.forEach(function (author) {
author.name = $('#authorname_' + author._id).prop('value');
author.checked = $('#author_' + author._id).prop('checked');
});
localStorage.setItem('authors', JSON.stringify(gAuthors));
}
//save and load
function save() {
var saveObject = { gAuthors, gObjects, gRecords, gViews };
var saveString = JSON.stringify(saveObject);
var blob = new Blob([saveString], { type: "text/json" });
var a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.target = '_blank';
var d = new Date();
var dStr = d.getFullYear() + ('0' + (d.getMonth() + 1)).slice(-2) + ('0' + d.getDate()).slice(-2);
a.download = dStr + '.json';
a.click();
}
function load(object) {
gAuthors = object.gAuthors;
gAuthorsTable = {};
gAuthors.forEach(function (author) {
gAuthorsTable[author._id] = author;
});
gObjects = object.gObjects;
gObjectsTable = {};
gObjects.forEach(function (object) {
gObjectsTable[object._id] = object;
});
gRecords = object.gRecords;
gViews = object.gViews;
gViews.forEach(function (view) {
initView2(view);
gViewsTable[view._id] = view;
});
setRecords(gRecords);
initdc(gRecords);
initializeDOMforCommunity();
}
$(window).on("load", function () {
var fileselector = document.getElementById("fileselector");
fileselector.addEventListener('change', function (e) {
var file = e.target.files[0];
var reader = new FileReader();
reader.addEventListener('load', function () {
var text = reader.result;
load(JSON.parse(text));
});
reader.readAsText(file);
});
});
//http://opentechnica.blogspot.jp/2012/03/javascript.html
Date.prototype.getDayOfYear = function () {
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((this - onejan) / 86400000);
};
Date.prototype.getWeekOfYear = function () {
var onejan = new Date(this.getFullYear(), 0, 1);
var offset = onejan.getDay() - 1;
var weeks = Math.floor((this.getDayOfYear() + offset) / 7);
return (onejan.getDay() == 0) ? weeks + 1 : weeks;
};
function initView2(view) {
var weeks = {};
//2000年からの週数.一年を52週と数える
//var min = 17 * 52 + 40;
//var max = 18 * 52 + 30;
var min = 15 * 52 + 40;
var max = 16 * 52 + 30;
for (var i = min; i < max; i++) {
weeks[i] = { count: 0 };
}
view.data.objects.forEach(function (object) {
if (object.type === 'Note') {
var d = parseDate(object.created);
var week = (d.getFullYear() - 2000) * 52 + d.getWeekOfYear();
if (weeks[week]) {
weeks[week].count++;
}
}
});
var weeksArray = [];
for (var i = min; i < max; i++) {
weeksArray.push(weeks[i]);
}
view.data.weeks = weeksArray;
}
</script>
</head>
<body>
<!-- login console -->
<ul>
<li>serverURL:
<!-- <input id="serverURL" type="textfield" value="http://localhost:9000/"> -->
<input id="serverURL" type="textfield" value="https://kf6.ikit.org/">
<input type="button" value="Local" onclick="toLocal()">
</li>
<li>user:
<input id="uname" type="textfield" value="yoshiaki.matsuzawa@gmail.com">
</li>
<li>password:
<input id="password" type="password" value="">
</li>
<li>
<input type="button" value="Login" onclick="loginPressed()">
</li>
</ul>
<!-- communities -->
<div>
<h1>communities:</h1>
<ul id="communities">
</ul>
</div>
<!-- community -->
<div>
<h1>Community:
<span id="communityname"></span>
</h1>
<br>
<input type="button" value="Save" onclick="save()">
<input id="fileselector" type="file">
</div>
<!-- notes -->
<!--<div style="float:left; width: 40%;">-->
<div>
<h1>views:</h1>
<table border="1" id="views">
</table>
</div>
<!--<div style="float:left; width: 40%;">-->
<div>
<h1>authors:</h1>
<table border="1" id="authors">
</table>
<br>
<input type="button" value="Save" onclick="saveSetting()">
<input type="button" value="Load" onclick="loadSetting()">
</div>
<div>
<input type="button" value="Update Statistics" onclick="updateStatistics()">
</div>
<div style="clear:both"></div>
<h1>Timeline:</h1>
<div style="width: 80%;" id="chart-line-hitsperday">
</div>
<div style="clear:both"></div>
<br> from:
<label id="fromCurrent"></label> to:
<label id="toCurrent"></label>
<br>
<input id="fromDate" type="text" value="20160301">
<input type="button" value="From" onclick="fromPressed()">
<br>
<input id="toDate" type="text" value="20160302">
<input type="button" value="To" onclick="toPressed()">
<!-- notes -->
<h1>Basic Statistics:
<span id="viewname"></span>
</h1>
<table border="1" id="notes">
</table>
<hr>
<h1>Graph:</h1>
<div id="graph">
<input type="button" value="Create Graph" onclick="openGraph()">(Press while Basic Statistics are shown)
<input type="button" value="Create Graph" onclick="openNewWindow()">
</div>
<div>
<svg id="sna"></svg>
</div>
</body>
</html>