Skip to content

Commit

Permalink
Merge pull request #89 from liuggio/stats
Browse files Browse the repository at this point in the history
added daily monthly and yearly stat to the repo and to total
  • Loading branch information
liuggio committed Nov 15, 2013
2 parents ff27dfb + e5f7c8b commit f33eb84
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/PUGX/StatsBundle/Service/RedisPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ private function concatenateKeys($prefix, $keyName)
public function incrementTotalAccess()
{
$this->redis->incr($this->keyTotal);

$key = $this->createDailyKey($this->keyTotal);
$this->redis->incr($key);

$key = $this->createMonthlyKey($this->keyTotal);
$this->redis->incr($key);

$key = $this->createYearlyKey($this->keyTotal);
$this->redis->incr($key);

return $this;
}

Expand All @@ -74,6 +84,15 @@ public function incrementRepositoryAccess($repository)
{
$this->redis->hincrby($this->concatenateKeys($this->keyHash, $repository), self::KEY_TOTAL, 1);

$key = $this->createDailyKey(self::KEY_TOTAL);
$this->redis->hincrby($this->concatenateKeys($this->keyHash, $repository), $key, 1);

$key = $this->createMonthlyKey(self::KEY_TOTAL);
$this->redis->hincrby($this->concatenateKeys($this->keyHash, $repository), $key, 1);

$key = $this->createYearlyKey(self::KEY_TOTAL);
$this->redis->hincrby($this->concatenateKeys($this->keyHash, $repository), $key, 1);

return $this;
}

Expand Down Expand Up @@ -121,4 +140,62 @@ public function addReferer($url)

return $this;
}



/**
* Create the yearly key with prefix eg. 'total_2003'
*
* @param string $prefix
* @param \DateTime $datetime
*
* @return string
*/
private function createYearlyKey($prefix, \DateTime $datetime = null)
{
return sprintf("%s_%s", $prefix, $this->formatDate($datetime, 'Y'));
}

/**
* Create the monthly key with prefix eg. 'total_2003_11'
*
* @param string $prefix
* @param \DateTime $datetime
*
* @return string
*/
private function createMonthlyKey($prefix, \DateTime $datetime = null)
{
return sprintf("%s_%s", $prefix, $this->formatDate($datetime, 'Y_m'));
}

/**
* Create the daily key with prefix eg. 'total_2003_11_29'
*
* @param string $prefix
* @param \DateTime $datetime
*
* @return string
*/
private function createDailyKey($prefix, \DateTime $datetime = null)
{
return sprintf("%s_%s", $prefix, $this->formatDate($datetime, 'Y_m_d'));
}

/**
* format a date.
*
* @param \DateTime $datetime
* @param string $format
*
* @return string
*/
private function formatDate(\DateTime $datetime = null, $format = 'Y_m_d')
{
if (null == $datetime) {
$datetime = new \DateTime('now');
}

return $datetime->format($format);
}
}

0 comments on commit f33eb84

Please sign in to comment.