Skip to content

Instantly share code, notes, and snippets.

@robertz
Created August 17, 2014 00:53
Show Gist options
  • Save robertz/b62561d896a92fe6800e to your computer and use it in GitHub Desktop.
Save robertz/b62561d896a92fe6800e to your computer and use it in GitHub Desktop.
WhosonTracker.cfc
component name="WhosonTracker" {
function init(){
variables.clients = [];
return this;
}
function trackHit(required Event){
var updateFlag = 0;
var userCount = arrayLen(variables.clients);
try{
lock name="WhosonTrackerLockForWritingUserData" type="exclusive" timeout="5" {
if(userCount){
for(var i = userCount; i>= 1; i--){
if(dateDiff('s', variables.clients[i].lastUpdated, now()) >= 300) arrayDeleteAt(variables.clients, i);
}
userCount = arrayLen(variables.clients);
}
for(i = 1; i <= userCount; i++){
if(variables.clients[i].sessionID == session.sessionID) updateFlag = i;
}
if(!updateFlag){
// client not found
arrayAppend(variables.clients, {
'sessionID' = session.sessionID
,'created' = now()
,'lastUpdated' = now()
,'referer' = CGI.HTTP_REFERER
,'remoteAddr' = CGI.REMOTE_ADDR
,'hostName' = getHostName(CGI.REMOTE_ADDR)
,'currentRoute' = Event.getSESBaseURL() & CGI.PATH_INFO & (len(CGI.QUERY_STRING) ? '?' & CGI.QUERY_STRING : '')
,'userAgent' = CGI.HTTP_USER_AGENT
});
}
else{
// update client
if(!findNoCase('/coldbox/', CGI.PATH_INFO)){
variables.clients[updateFlag].lastUpdated = now();
variables.clients[updateFlag].currentRoute = Event.getSESBaseURL() & CGI.PATH_INFO & (len(CGI.QUERY_STRING) ? '?' & CGI.QUERY_STRING : '');
}
}
}
}
catch(any e){
// writeDump(var = e, abort = 1);
}
return;
}
public array function getData(){
return variables.clients;
}
private string function getHostName(required string address){
var iaClass = createObject('java', 'java.net.InetAddress');
return iaClass.getByName(address).getHostName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment