-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusage.js
More file actions
161 lines (143 loc) · 3.65 KB
/
usage.js
File metadata and controls
161 lines (143 loc) · 3.65 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
//Count by schema format. Count total. Count by creator. Count by submitter. Count by keyword.
var dataTable;
var service = "submitter";
var resultCount = 0;
var NODE_URL = "http://127.0.0.1:5984";
var ajaxPool = [];
var chart;
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() {
options = {
width : 800,
height : 500,
title : 'Learning Registry Usage Stats - ' + service,
vAxis : {
title : 'Term',
titleTextStyle : {
color : 'red'
}
}
};
dataTable = new google.visualization.DataTable();
dataTable.addColumn('string', 'Term');
dataTable.addColumn('number', 'Count');
chart = new google.visualization.BarChart(document.getElementById('chart_div'));
drawChart();
startLoadingData();
}
function startLoadingData() {
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);
};
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) {
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);
//debug("compare: " + compare);
if(compare >= 0) {
insertIndex = index;
return false;
}
} else {
debug("no next row for index: " + index);
}
});
if(insertIndex >= 0) {
//debug("insert row: " + JSON.stringify(row) + " at index: " + insertIndex);
chartRows.splice(insertIndex, 0, row);
chartRows = chartRows.slice(0, MAX_ROWS - 1);
}
}
var TEST_MAX = 100000;
$.each(rows, function(index, row) {
debug2("row: " + index);
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
});
//debug("about to do first sort...");
chartRows.sort(compareRows);
} else if(index < TEST_MAX) {
insertRow({
key : row.key[0],
val : row.value
});
} else {
return false;
}
}
});
options.title = options.title + ", Top " + MAX_ROWS + " of " + rows.length;
chartRows.sort(compareRows);
//debug("finished inserting rows, about to add to data: result is: " + JSON.stringify(chartRows));
$.each(chartRows, function(index, row) {
//debug("adding data row: " + JSON.stringify(row));
//debug("adding data row: " + row.key + ", " + row.val);
dataTable.addRow([row.key, row.val]);
});
//debug("finished adding rows, abotu to draw chart. data table is: " + dataTable.toJSON());
drawChart();
}
function drawChart() {
chart.draw(dataTable, options);
}