-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusage-stats.js
More file actions
164 lines (143 loc) · 3.69 KB
/
usage-stats.js
File metadata and controls
164 lines (143 loc) · 3.69 KB
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
//Count by schema format. Count total. Count by creator. Count by submitter. Count by keyword.
var dataTables = {};
var services = ['submitter', 'payload_schema', 'curator', 'keyword'];
var resultCount = 0;
var NODE_URL = "http://127.0.0.1:5984";
var ajaxPool = [];
var charts = {};
var options = {};
var MAX_ROWS = 15;
$.ajaxSetup({
dataType : 'jsonp',
jsonp : 'callback'
});
google.load("visualization", "1", {
packages : ["corechart"]
});
google.setOnLoadCallback(init);
function debug2(msg) {
$('#debug2').html(msg + "<br>");
}
function debug(msg) {
$('#debug').append(msg + "<br>");
}
function init() {
$.each(services, function(index, service) {
options[service] = {
width : 800,
height : 500,
title : 'Learning Registry Usage Stats - ' + service + ', Loading Data...',
vAxis : {
title : 'Term',
titleTextStyle : {
color : 'red'
}
}
};
dataTables[service] = new google.visualization.DataTable();
dataTables[service].addColumn('string', 'Term');
dataTables[service].addColumn('number', 'Count');
var div_name = 'chart-'+service;
$('#charts').append('<div id='+div_name+'></div>');
charts[service] = new google.visualization.BarChart(document.getElementById(div_name));
drawChart(service);
startLoadingData(service);
});
}
function startLoadingData(service) {
callStats(service);
}
// http://127.0.0.1:5984/resource_data/_design/lrstats-payload_schema/_view/docs?reduce=true&group=true&group_level=2
function callStats(service) {
var callObj = {
url : NODE_URL + '/resource_data/_design/lrstats-' + service + '/_view/docs',
dataType : 'jsonp',
jsonp : 'callback',
data : {
"reduce" : true,
"group" : true,
"group_level" : 1,
}
};
callObj.success = function(ajaxData) {
buildData(ajaxData, service);
};
callObj.error = function(jqXHR, textStatus, errorThrown) {
debug("ajax error: " + errorThrown);
debug("ajax error textStatus: " + textStatus);
debug("ajax error jqXHR: " + JSON.stringify(jqXHR));
};
$.ajax(callObj);
}
function buildData(ajaxData, service) {
var rows = ajaxData.rows;
var chartRows = [];
//reverse sort by val
function compareRows(row1, row2) {
return row1.val - row2.val;
}
function insertRow(row) {
var nextRow = chartRows[MAX_ROWS - 2];
if(!nextRow) {
debug("last row does not exist");
return;
}
if(row.val < nextRow.val) {
return;
}
var insertIndex = -1;
$.each(chartRows, function(index, row) {
nextRow = chartRows[index];
if(nextRow) {
var compare = compareRows(row, nextRow);
if(compare >= 0) {
insertIndex = index;
return false;
}
} else {
debug("no next row for index: " + index);
}
});
if(insertIndex >= 0) {
chartRows.splice(insertIndex, 0, row);
chartRows = chartRows.slice(0, MAX_ROWS - 1);
}
}
var TEST_MAX = 100000;
$.each(rows, function(index, row) {
if(row.key && row.value) {
if(index < MAX_ROWS) {
chartRows.push({
key : row.key[0],
val : row.value
});
} else if(index == MAX_ROWS) {
chartRows.push({
key : row.key[0],
val : row.value
});
chartRows.sort(compareRows);
} else if(index < TEST_MAX) {
insertRow({
key : row.key[0],
val : row.value
});
} else {
return false;
}
}
});
if(rows.length>MAX_ROWS) {
options[service].title = 'Learning Registry Usage Stats - ' + service + ', Top ' + MAX_ROWS + " of " + rows.length;
} else {
options[service].title = 'Learning Registry Usage Stats - ' + service;
}
chartRows.sort(compareRows);
$.each(chartRows, function(index, row) {
dataTables[service].addRow([row.key, row.val]);
});
drawChart(service);
}
function drawChart(service) {
charts[service].draw(dataTables[service], options[service]);
}