Created
September 1, 2012 16:24
-
-
Save Gargron/3579337 to your computer and use it in GitHub Desktop.
Metrics with Mongo
This file contains hidden or 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 | |
/** | |
* Example usage: | |
* | |
* Metric::record(1, 'slot', 12, 'impression'); | |
* | |
*/ | |
class Metric | |
{ | |
/** | |
* @var MongoDB | |
*/ | |
protected static $mongo; | |
/** | |
* @var array | |
*/ | |
public static $timeframes = array( | |
'day', | |
'hour', | |
); | |
/** | |
* @see Queue | |
* @param string | |
* @param integer | |
* @param string | |
* @param integer | |
* @param string | |
* @return void | |
*/ | |
public static function record($user_id, $property, $id, $type) | |
{ | |
foreach(self::$timeframes as $timeframe) | |
{ | |
#self::record_timeframe($timeframe, $user_id, $property, $id, $type); | |
Queue::add('Metric', 'record_timeframe', array($timeframe, $user_id, $property, $id, $type)); | |
} | |
} | |
/** | |
* @param string | |
* @param integer | |
* @param string | |
* @param integer | |
* @param string | |
* @return void | |
*/ | |
public static function record_timeframe($timeframe, $user_id, $property, $id, $type) | |
{ | |
$collection_name = 'user_' . $user_id . '_' . Str::plural($property) . '_by_' . $timeframe; | |
$collection = self::mongo()->$collection_name; | |
$collection->ensureIndex(array($timeframe => 1, $property => 1)); | |
$collection->update(array( | |
$timeframe => new MongoDate(self::timeframe($timeframe)), | |
$property => $id, | |
), array( | |
'$inc' => array( | |
$type => 1 | |
), | |
), array('upsert' => true)); | |
} | |
/** | |
* @see self::$timeframes | |
* @param string | |
* @return integer | |
*/ | |
protected static function timeframe($timeframe) | |
{ | |
switch($timeframe) | |
{ | |
case 'hour': | |
$date = date('Y-m-d H:00:00'); | |
break; | |
case 'day': | |
$date = date('Y-m-d 00:00:00'); | |
break; | |
} | |
return strtotime($date); | |
} | |
/** | |
* @see self::$mongo | |
* @return MongoDB | |
*/ | |
protected static function mongo() | |
{ | |
if(is_null(self::$mongo)) | |
{ | |
$mongodb = new Mongo('mongodb://' . Config::get('database.mongodb.host') . ':' . Config::get('database.mongodb.port')); | |
self::$mongo = $mongodb->metrics; | |
} | |
return self::$mongo; | |
} | |
/** | |
* @param string | |
* @param integer | |
* @param string | |
* @param string | |
* @return array | |
*/ | |
public static function get_sum($timeframe, $user_id, $property, $type) | |
{ | |
$collection_name = 'user_' . $user_id . '_' . Str::plural($property) . '_by_' . $timeframe; | |
$collection = self::mongo()->$collection_name; | |
$items = $collection->group( | |
new MongoCode('function(obj) { return {' . $timeframe . ': obj.' . $timeframe . '}; }'), | |
array($type . '_sum' => 0), | |
new MongoCode('function(obj, prev) { prev.' . $type . '_sum += obj.' . $type . '; }') | |
); | |
return $items; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment