forked from benc-uk/csa-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriptionsByDay.html
163 lines (141 loc) · 4.37 KB
/
subscriptionsByDay.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
<!--
CSA Widget - Subscriptions by Day
Khaled Belghith, April 2015
-->
<!-- -------------------------------------------------------------------------------------- -->
<!-- HTML and widget display -->
<!-- -------------------------------------------------------------------------------------- -->
<div id="subscriptions_widget">
<h4>Subscriptions by Day</h4>
<hr/>
<div id="placeholder" style="width:100%;height:90%;"></div>
</div>
<!-- -------------------------------------------------------------------------------------- -->
<!-- Javascript and main code -->
<!-- -------------------------------------------------------------------------------------- -->
<script language="javascript" type="text/javascript" src="scripts/jquery.flot.js"></script>
<script language="javascript" type="text/javascript" src="scripts/jquery.flot.categories.js"></script>
<script language="javascript" type="text/javascript" src="scripts/jquery.flot.stack.js"></script>
<script language="javascript" type="text/javascript" src="scripts/jquery.flot.resize.js"></script>
<script type="text/javascript">
var timer_id;
var refresh_interval_secs = 5;
var d1 = [];
var d2 = [];
var d3 = [];
var nbrGrads = 10;
var ms_perday = 86400000;
var today = new Date();
var month_names_short = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
initData();
getAllSubscriptions();
//
// init subscriptions data to be passed as input for the diagram
//
function initData() {
for (var i = 1; i <= nbrGrads; i += 1) {
var date = new Date(today.getTime() - ((nbrGrads - i) * 24 * 60 * 60 * 1000));
var month = date.getUTCMonth();
var day = date.getUTCDate();
var newdate = month_names_short[month] + " " + day;
d1.push([newdate, 0]);
d2.push([newdate, 0]);
d3.push([newdate, 0]);
};
}
//
// reset subscriptions data to be passed as input for the diagram
//
function resetData() {
for (var i = 0; i < nbrGrads; i += 1) {
(d1[i])[1] = 0;
(d2[i])[1] = 0;
(d3[i])[1] = 0;
};
}
//
// Make AJAX call to CSA API to get all subscriptions
//
function getAllSubscriptions() {
$.ajax({
type: "GET",
url: "/api/subscription?count=256",
success: processSubscriptions,
error: errorLog,
async: false
});
}
//
// Simple error logging
//
function errorLog(apiXHR, textStatus, errorThrown) {
console.log("CSA API Error: " + errorThrown);
}
//
// Difference between two dates in nbr of days
//
function dateDiff(fromdate, todate) {
var diff = todate - fromdate;
return Math.floor(diff/ms_perday);
}
//
// increment the nbr of occurences in the data tab
// depending on the
//
function incrementValue(tab, index) {
if (index > 0){
var val = tab[index];
val[1]+= 1;
}
}
//
// Callback function used to asynchronously handle the result of getAllSubscriptions
//
function processSubscriptions(data, textStatus, apiXHR) {
// Loop over each subs instance
resetData();
subs_index = 0;
for(subs_index = 0; subs_index < data.length; subs_index++) {
subs = data[subs_index]; // Get subscruption
start = subs.subscriptionTerm.startDate.substring(0,10);
var today = new Date();
var startDate = new Date(start);
dateIndex = nbrGrads - dateDiff(startDate,today) - 1;
if (subs.status == "CANCELLED"){
incrementValue(d3, dateIndex)
}else if (subs.status == "ACTIVE"){
incrementValue(d2, dateIndex)
}else if (subs.status == "TERMINATED"){
incrementValue(d1, dateIndex)
}
}
plotWithOptions();
clearTimeout(timer_id);
timer_id = setTimeout(getAllSubscriptions, refresh_interval_secs * 1000);
}
function plotWithOptions() {
$.plot("#placeholder", [ {data: d1, label : " Terminated"},
{data: d2, label : " Active"},
{data: d3, label : " Cancelled"} ], {
xaxis: {
mode: "categories",
tickLength: 0
},
series: {
stack: 1,
bars: {
show: true,
barWidth: 0.6,
align: "center"
}
},
legend: { position: "nw" }
});
}
plotWithOptions();
</script>
<style type="text/css">
#subscriptions_widget {padding:5px; color:black; height: 100%; /* make the percentage height on placeholder work */}
#subscriptions_widget h4, #subscriptions_widget hr { margin:0 0 5px 0; padding:0px;}
#subscriptions_widget .legend .legendLabel {text-align: left; padding-left:5px}
</style>