Created
April 29, 2016 20:01
-
-
Save sameg14/916f2e939221ed28dd0300f4eee918d0 to your computer and use it in GitHub Desktop.
An example of a worker written in PHP that collects user statistics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tib\CoreBundle\Job\Worker; | |
use Tib\CoreBundle\Exception\APIException; | |
use Tib\CoreBundle\Exception\InvalidDataException; | |
use Tib\CoreBundle\Util\JsonResponseTrait; | |
/** | |
* Class CollectUserStatsWorker collects user information and records it to a table | |
* @package Tib\CoreBundle\Job\Worker | |
*/ | |
class CollectUserStatsWorker extends AbstractWorker | |
{ | |
use JsonResponseTrait; | |
/** | |
* Collect user stats and store in a table | |
* @throws InvalidDataException | |
* @throws APIException | |
* @return bool | |
*/ | |
public function perform() | |
{ | |
$args = $this->getArgs(); | |
if (empty($args)) { | |
throw new InvalidDataException('No client stats were recorded as no arguments were found'); | |
} | |
// Ignore all symfony profiler requests | |
if (preg_match('/_wdt/', $args['uri'])) { | |
return false; | |
} | |
// Ignore all requests that log the position of the currently playing track | |
if (preg_match('/log_position/', $args['uri'])) { | |
return false; | |
} | |
$db = $this->getContainer()->get('db2');; | |
$userAgent = isset($args['user_agent']) ? $args['user_agent'] : null; | |
$cookie = isset($args['cookie']) ? $args['cookie'] : null; | |
$requestTime = isset($args['request_time']) ? $args['request_time'] : strtotime('now'); | |
$requestTime = date('Y-m-d h:i:s', $requestTime); | |
$clientIp = isset($args['client_ip']) ? $args['client_ip'] : null; | |
$queryString = isset($args['query_string']) ? $args['query_string'] : null; | |
$uri = isset($args['uri']) ? $args['uri'] : null; | |
$sessionId = isset($args['session_id']) ? $args['session_id'] : null; | |
// Attempt to locate an existing record | |
$query = ' | |
select | |
id | |
from | |
ill_stats | |
where | |
uri = :uri | |
and ip = :ip | |
and session_id = :sessionId'; | |
$row = $db->fetchRow($query, array( | |
'uri' => $uri, | |
'ip' => $clientIp, | |
'sessionId' => $sessionId | |
)); | |
$id = !empty($row) && isset($row['id']) ? $row['id'] : null; | |
if (empty($id)) { // Create a new record | |
return $db->insert('ill_stats', array( | |
'user_agent' => $userAgent, | |
'cookie' => $cookie, | |
'request_time' => $requestTime, | |
'ip' => $clientIp, | |
'query_string' => $queryString, | |
'uri' => $uri, | |
'session_id' => $sessionId, | |
'num_hits' => 1 | |
)); | |
} else { // Don't count the same page view twice, assume the user is refreshing the page, just increment a counter | |
$query = 'select num_hits from ill_stats where id = :id'; | |
$row = $db->fetchRow($query, array('id' => $id)); | |
$numHits = $row['num_hits']; | |
return $db->update('ill_stats', | |
array('id' => $id, 'request_time' => $requestTime), | |
array('num_hits' => ++$numHits) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment