From e5f7c8b846dc7ef2fea6b132ce1a96cf5686284c Mon Sep 17 00:00:00 2001 From: Giulio De Donato Date: Fri, 8 Nov 2013 16:34:35 +0100 Subject: [PATCH] added daily monthly and yearly stat to the repo and to total --- .../StatsBundle/Service/RedisPersister.php | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/src/PUGX/StatsBundle/Service/RedisPersister.php b/src/PUGX/StatsBundle/Service/RedisPersister.php index 21281a32..76e36eda 100644 --- a/src/PUGX/StatsBundle/Service/RedisPersister.php +++ b/src/PUGX/StatsBundle/Service/RedisPersister.php @@ -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; } @@ -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; } @@ -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); + } } \ No newline at end of file