-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
266 lines (225 loc) · 12.3 KB
/
index.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
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdn.mxpnl.com/libs/mixpanel-platform/css/reset.css">
<link rel="stylesheet" type="text/css" href="https://cdn.mxpnl.com/libs/mixpanel-platform/build/mixpanel-platform.v0.latest.min.css">
<script src="https://cdn.mxpnl.com/libs/mixpanel-platform/build/mixpanel-platform.v0.latest.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/JSXTransformer.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.12.2/react.js"></script>
<style type="text/css">
.eventSelect {
float: left;
}
.sessionSelectLabel {
float: right;
color: #747D94;
font-size: 15px;
font-weight: bold;
margin-right: 11px;
margin-top: 5px;
}
.sessionSelect {
float: right;
}
.note {
font-size: 12px;
margin-top: 10px;
color: #747D94;
}
.feedHeader {
margin-top: 20px;
padding: 15px;
background-color: #747D94;
color: rgb(240, 242, 246);
font-size: 20px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
.feedRow:nth-child(odd) {
background: #F0F2F6;
}
.userRow {
border-bottom: 1px solid #747D94;
padding-bottom: 3px;
padding-top: 3px;
}
.userRow img {
vertical-align: middle;
height: 50px;
width: 50px;
}
.userRow span {
padding-left: 20px;
font-size: 20px;
padding-top: 10px;
}
</style>
</head>
<body class="mixpanel-platform-body">
<div class="controls mixpanel-platform-section">
<div class="eventSelect"></div>
<div class="sessionSelect"></div>
<div class="sessionSelectLabel">Session length:</div>
<div style="clear: both;"></div>
</div>
<div id="chart"></div>
<div class="feedHeader">Active users</div>
<div class="feed"></div>
<div class="note">*Note: This data is pulled from the "live view" endpoint which only returns a max of 200 events. If you are collecting more than 200 events of the same type over three seconds, this data could be slightly inaccurate.</div>
<script type="text/jsx">
var SESSION_ACTIVITY_LIMIT_SECONDS = 30;
var QUERY_INTERVAL_SECONDS = 3;
var MAX_VALUES = 30
var initializedAt = moment();
var startTime = 0;
var activeUsers = {};
var pollInterval = null;
var UserRow = React.createClass({
render: function() {
return <div className="userRow"><img src={this.props.avatar} /><span>{this.props.name}</span></div>;
}
});
var chart = new Highcharts.Chart({
chart: {
renderTo: 'chart',
type: 'area'
},
title: {
text: ''
},
xAxis: {
type: 'category'
},
yAxis: {
title: {
text: 'Active users'
},
},
tooltip: {
pointFormat: '{point.y:,.0f} active users'
},
series: [{name: 'Active users', data: []}]
});
var initialize = function() {
clearInterval(pollInterval);
chart.series[0].setData([]);
chart.setTitle({text: ""});
initializedAt = moment();
MP.api.query("/api/2.0/live", {client_version: 3}).done(function(resp) {
const maxTs = Math.max(...resp.event_list.map(ev => ev.$ts));
console.log(resp);
console.log(maxTs);
startTime = maxTs - QUERY_INTERVAL_SECONDS;
});
getActiveUsers();
pollInterval = setInterval(getActiveUsers, QUERY_INTERVAL_SECONDS * 1000);
}
var getActiveUsers = function() {
MP.api.query("/api/2.0/live", {client_version: 3, start_time: startTime, search: $(".eventSelect").val() || ""}).done(function(resp) {
var usersInBatch = _.reduce(resp, function(results, event) {
eventTimestamp = parseInt(event.$ts);
startTime = Math.max(startTime, eventTimestamp)
var distinctId = event.properties.distinct_id;
if (distinctId) {
if (!results[distinctId]) {
results[distinctId] = {
email: event.properties.$email || event.properties.Email,
lastAction: 0
}
}
if (results[distinctId].lastAction < eventTimestamp) {
results[distinctId].lastAction = eventTimestamp;
}
}
return results
}, {});
activeUsers = _.extend(activeUsers, usersInBatch);
_.each(activeUsers, function(userObj, distinctId) {
if (userObj.lastAction < startTime - SESSION_ACTIVITY_LIMIT_SECONDS * 1000) {
console.log('deleting stale user session ' + distinctId + '. Last activity was ' + parseInt((startTime - userObj.lastAction) / 1000) + ' seconds ago');
removeUser(distinctId);
} else {
addToFeed(distinctId);
}
});
chart.series[0].addPoint({name: moment().format('HH:mm:ss'), y: _.keys(activeUsers).length}, true, chart.series[0].data.length > MAX_VALUES);
});
var eventLabel = "performing any event";
if ($('.eventSelect').val()) {
eventLabel = "performing \"" + $('.eventSelect').val() + "\"";
}
var secondsOnPage = parseInt(Math.max(1, (moment() - initializedAt) / 1000));
chart.setTitle({text: _.sprintf("Concurrent users %s in the last %s seconds", eventLabel, SESSION_ACTIVITY_LIMIT_SECONDS)});
}
var removeUser = function(distinctId) {
delete activeUsers[distinctId];
$('.feed .uid_' + distinctId).remove();
}
var addToFeed = function(distinctId) {
var email = activeUsers[distinctId].email;
var $dfr = $.Deferred();
if (email) {
MP.api.query('/api/2.0/engage/', {where: 'properties["$email"] == "' + email + '"'}).done(function(resp) {
_.each(resp.results, function(profile) {
if (profile.$distinct_id == distinctId) {
var props = profile.$properties;
var name = props['$name'] ? props['$name'] : props['$first_name'] ? props['$first_name'] + ' ' + props['$last_name'] : 'Unknown';
$dfr.resolve(name, gravatar(email, {secure: true, backup: 'https://cdn.mxpnl.com/site_media/images/engage/default_user_icon.png'}));
}
});
});
} else {
$dfr.resolve('Unknown', 'https://i0.wp.com/cdn.mxpnl.com/site_media/images/engage/default_user_icon.png?ssl=1')
}
$dfr.done(function(name, avatar) {
if ($('.feed .uid_' + distinctId).length == 0) {
$('.feed').append($("<div>").addClass('feedRow').addClass('uid_' + distinctId));
}
var attrs = {
name: name,
avatar: avatar
}
React.render(<UserRow {...attrs} />, $('.feed .uid_' + distinctId)[0]);
});
}
MP.api.ready(function() {
$('.eventSelect').MPEventSelect().on('change', function() {
_.each(activeUsers, function(userObj, distinctId) {
removeUser(distinctId)
});
initialize();
});
$('.sessionSelect').MPSelect({items: [[30, '30 seconds'], [60, '1 minute'], [300, '5 minutes'], [900, '15 minutes'], [1800, '30 minutes']]}).val(SESSION_ACTIVITY_LIMIT_SECONDS).on('change', function() {
SESSION_ACTIVITY_LIMIT_SECONDS = $('.sessionSelect').val();
initialize();
});
initialize();
});
function gravatar(email, options) {
// using md5() from here: http://www.myersdaily.org/joseph/javascript/md5-text.html
function md5cycle(e,t){var n=e[0],r=e[1],i=e[2],s=e[3];n=ff(n,r,i,s,t[0],7,-680876936);s=ff(s,n,r,i,t[1],12,-389564586);i=ff(i,s,n,r,t[2],17,606105819);r=ff(r,i,s,n,t[3],22,-1044525330);n=ff(n,r,i,s,t[4],7,-176418897);s=ff(s,n,r,i,t[5],12,1200080426);i=ff(i,s,n,r,t[6],17,-1473231341);r=ff(r,i,s,n,t[7],22,-45705983);n=ff(n,r,i,s,t[8],7,1770035416);s=ff(s,n,r,i,t[9],12,-1958414417);i=ff(i,s,n,r,t[10],17,-42063);r=ff(r,i,s,n,t[11],22,-1990404162);n=ff(n,r,i,s,t[12],7,1804603682);s=ff(s,n,r,i,t[13],12,-40341101);i=ff(i,s,n,r,t[14],17,-1502002290);r=ff(r,i,s,n,t[15],22,1236535329);n=gg(n,r,i,s,t[1],5,-165796510);s=gg(s,n,r,i,t[6],9,-1069501632);i=gg(i,s,n,r,t[11],14,643717713);r=gg(r,i,s,n,t[0],20,-373897302);n=gg(n,r,i,s,t[5],5,-701558691);s=gg(s,n,r,i,t[10],9,38016083);i=gg(i,s,n,r,t[15],14,-660478335);r=gg(r,i,s,n,t[4],20,-405537848);n=gg(n,r,i,s,t[9],5,568446438);s=gg(s,n,r,i,t[14],9,-1019803690);i=gg(i,s,n,r,t[3],14,-187363961);r=gg(r,i,s,n,t[8],20,1163531501);n=gg(n,r,i,s,t[13],5,-1444681467);s=gg(s,n,r,i,t[2],9,-51403784);i=gg(i,s,n,r,t[7],14,1735328473);r=gg(r,i,s,n,t[12],20,-1926607734);n=hh(n,r,i,s,t[5],4,-378558);s=hh(s,n,r,i,t[8],11,-2022574463);i=hh(i,s,n,r,t[11],16,1839030562);r=hh(r,i,s,n,t[14],23,-35309556);n=hh(n,r,i,s,t[1],4,-1530992060);s=hh(s,n,r,i,t[4],11,1272893353);i=hh(i,s,n,r,t[7],16,-155497632);r=hh(r,i,s,n,t[10],23,-1094730640);n=hh(n,r,i,s,t[13],4,681279174);s=hh(s,n,r,i,t[0],11,-358537222);i=hh(i,s,n,r,t[3],16,-722521979);r=hh(r,i,s,n,t[6],23,76029189);n=hh(n,r,i,s,t[9],4,-640364487);s=hh(s,n,r,i,t[12],11,-421815835);i=hh(i,s,n,r,t[15],16,530742520);r=hh(r,i,s,n,t[2],23,-995338651);n=ii(n,r,i,s,t[0],6,-198630844);s=ii(s,n,r,i,t[7],10,1126891415);i=ii(i,s,n,r,t[14],15,-1416354905);r=ii(r,i,s,n,t[5],21,-57434055);n=ii(n,r,i,s,t[12],6,1700485571);s=ii(s,n,r,i,t[3],10,-1894986606);i=ii(i,s,n,r,t[10],15,-1051523);r=ii(r,i,s,n,t[1],21,-2054922799);n=ii(n,r,i,s,t[8],6,1873313359);s=ii(s,n,r,i,t[15],10,-30611744);i=ii(i,s,n,r,t[6],15,-1560198380);r=ii(r,i,s,n,t[13],21,1309151649);n=ii(n,r,i,s,t[4],6,-145523070);s=ii(s,n,r,i,t[11],10,-1120210379);i=ii(i,s,n,r,t[2],15,718787259);r=ii(r,i,s,n,t[9],21,-343485551);e[0]=add32(n,e[0]);e[1]=add32(r,e[1]);e[2]=add32(i,e[2]);e[3]=add32(s,e[3])}function cmn(e,t,n,r,i,s){t=add32(add32(t,e),add32(r,s));return add32(t<<i|t>>>32-i,n)}function ff(e,t,n,r,i,s,o){return cmn(t&n|~t&r,e,t,i,s,o)}function gg(e,t,n,r,i,s,o){return cmn(t&r|n&~r,e,t,i,s,o)}function hh(e,t,n,r,i,s,o){return cmn(t^n^r,e,t,i,s,o)}function ii(e,t,n,r,i,s,o){return cmn(n^(t|~r),e,t,i,s,o)}function md51(e){txt="";var t=e.length,n=[1732584193,-271733879,-1732584194,271733878],r;for(r=64;r<=e.length;r+=64){md5cycle(n,md5blk(e.substring(r-64,r)))}e=e.substring(r-64);var i=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(r=0;r<e.length;r++)i[r>>2]|=e.charCodeAt(r)<<(r%4<<3);i[r>>2]|=128<<(r%4<<3);if(r>55){md5cycle(n,i);for(r=0;r<16;r++)i[r]=0}i[14]=t*8;md5cycle(n,i);return n}function md5blk(e){var t=[],n;for(n=0;n<64;n+=4){t[n>>2]=e.charCodeAt(n)+(e.charCodeAt(n+1)<<8)+(e.charCodeAt(n+2)<<16)+(e.charCodeAt(n+3)<<24)}return t}function rhex(e){var t="",n=0;for(;n<4;n++)t+=hex_chr[e>>n*8+4&15]+hex_chr[e>>n*8&15];return t}function hex(e){for(var t=0;t<e.length;t++)e[t]=rhex(e[t]);return e.join("")}function md5(e){return hex(md51(e))}function add32(e,t){return e+t&4294967295}var hex_chr="0123456789abcdef".split("");if(md5("hello")!="5d41402abc4b2a76b9719d911017c592"){function add32(e,t){var n=(e&65535)+(t&65535),r=(e>>16)+(t>>16)+(n>>16);return r<<16|n&65535}}
//check to make sure you gave us something
var options = options || {},
base,
params = [];
//set some defaults, just in case
options = {
size: options.size || "50",
rating: options.rating || "g",
secure: options.secure || (location.protocol === 'https:'),
backup: options.backup || ""
};
//setup the email address
email = email.trim().toLowerCase();
//determine which base to use
base = options.secure ? 'https://secure.gravatar.com/avatar/' : 'http://www.gravatar.com/avatar/';
//add the params
if (options.rating) {params.push("r=" + options.rating)};
if (options.backup) {params.push("d=" + encodeURIComponent(options.backup))};
if (options.size) {params.push("s=" + options.size)};
//now throw it all together
return base + md5(email) + "?" + params.join("&");
}
</script>
</body>
</html>