From 515b0c174b84c7a392d49e60928bd4d7bd190dea Mon Sep 17 00:00:00 2001 From: Geoff Dutton Date: Thu, 12 Mar 2015 17:27:06 -0500 Subject: [PATCH 1/4] allow array of events and pipeline them --- src/FreeAgent/Bitter/Bitter.php | 44 ++++++++++------- tests/units/Bitter.php | 84 +++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 16 deletions(-) diff --git a/src/FreeAgent/Bitter/Bitter.php b/src/FreeAgent/Bitter/Bitter.php index 3ec82a2..e3845fd 100644 --- a/src/FreeAgent/Bitter/Bitter.php +++ b/src/FreeAgent/Bitter/Bitter.php @@ -55,27 +55,39 @@ public function setRedisClient($redisClient) /** * Marks an event for hours, days, weeks and months * - * @param string $eventName The name of the event, could be "active" or "new_signups" + * @param string|array $eventName The name of the event, could be "active" or "new_signups" * @param integer $id An unique id, typically user id. The id should not be huge, read Redis documentation why (bitmaps) * @param DateTime $dateTime Which date should be used as a reference point, default is now */ - public function mark($eventName, $id, DateTime $dateTime = null) + public function mark($eventNames, $id, DateTime $dateTime = null) { + $eventNames = is_array($eventNames) ? $eventNames : [$eventNames]; $dateTime = is_null($dateTime) ? new DateTime : $dateTime; - - $eventData = array( - new Year($eventName, $dateTime), - new Month($eventName, $dateTime), - new Week($eventName, $dateTime), - new Day($eventName, $dateTime), - new Hour($eventName, $dateTime), - ); - - foreach ($eventData as $event) { - $key = $this->prefixKey . $event->getKey(); - $this->getRedisClient()->setbit($key, $id, 1); - $this->getRedisClient()->sadd($this->prefixKey . 'keys', $key); - } + $prefix = $this->prefixKey; + + $this->getRedisClient() + ->pipeline(function($pipe) use ( + $eventNames, + $prefix, + $dateTime, + $id) { + + foreach ($eventNames as $key => $eventName) { + $eventData = array( + new Year($eventName, $dateTime), + new Month($eventName, $dateTime), + new Week($eventName, $dateTime), + new Day($eventName, $dateTime), + new Hour($eventName, $dateTime), + ); + + foreach ($eventData as $event) { + $key = $prefix . $event->getKey(); + $pipe->setbit($key, $id, 1); + $pipe->sadd($prefix . 'keys', $key); + } + } + }); return $this; } diff --git a/tests/units/Bitter.php b/tests/units/Bitter.php index baa0a60..1ff636f 100644 --- a/tests/units/Bitter.php +++ b/tests/units/Bitter.php @@ -133,6 +133,90 @@ public function testMarkUnitOfTime($redisClient) $this->removeAll($redisClient); } + /** + * @dataProvider dataProviderTestedClients + */ + public function testMultipleMarks($redisClient) + { + $bitter = new TestedBitter($redisClient, $this->getPrefixKey(), $this->getPrefixTempKey()); + + $this->removeAll($redisClient); + + $dateTime = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-06 15:30:45'); + + $events = [ + 'drink_a_bitter_beer', + 'drink_another_bitter_beer', + 'pass_out', + ]; + + foreach ($events as $event) { + $day = new Day($event, $dateTime); + + $this + ->integer($bitter->count($day)) + ->isIdenticalTo(0) + ; + $this + ->boolean($bitter->in(404, $day)) + ->isFalse() + ; + } + + + $this + ->object($bitter->mark($events, 404, $dateTime)) + ->isIdenticalTo($bitter) + ; + + foreach ($events as $event) { + $day = new Day($event, $dateTime); + + $this + ->integer($bitter->count($day)) + ->isIdenticalTo(1) + ; + $this + ->boolean($bitter->in(404, $day)) + ->isTrue() + ; + } + + + // Adding it a second time with the same dateTime ! + $this + ->object($bitter->mark($events, 404, $dateTime)) + ->isIdenticalTo($bitter) + ; + + foreach ($events as $event) { + $day = new Day($event, $dateTime); + $this + ->integer($bitter->count($day)) + ->isIdenticalTo(1) + ; + $this + ->boolean($bitter->in(404, $day)) + ->isTrue() + ; + } + + $this->removeAll($redisClient); + + $day = new Day('drink_a_bitter_beer', new DateTime()); + $this + ->boolean($bitter->in(13, $day)) + ->isFalse() + ; + $bitter->mark('drink_a_bitter_beer', 13); + $this + ->boolean($bitter->in(13, $day)) + ->isTrue() + ; + + $this->removeAll($redisClient); + } + /** * @dataProvider dataProviderTestedClients */ From bc99d1d495a8ed951ed8b03b78ea9619dc238a43 Mon Sep 17 00:00:00 2001 From: Geoff Dutton Date: Thu, 12 Mar 2015 17:49:09 -0500 Subject: [PATCH 2/4] php 5.3 derp --- src/FreeAgent/Bitter/Bitter.php | 45 +++++++++++++++------------------ tests/units/Bitter.php | 4 +-- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/FreeAgent/Bitter/Bitter.php b/src/FreeAgent/Bitter/Bitter.php index e3845fd..6051796 100644 --- a/src/FreeAgent/Bitter/Bitter.php +++ b/src/FreeAgent/Bitter/Bitter.php @@ -61,33 +61,28 @@ public function setRedisClient($redisClient) */ public function mark($eventNames, $id, DateTime $dateTime = null) { - $eventNames = is_array($eventNames) ? $eventNames : [$eventNames]; + $eventNames = is_array($eventNames) ? $eventNames : array($eventNames); $dateTime = is_null($dateTime) ? new DateTime : $dateTime; - $prefix = $this->prefixKey; - - $this->getRedisClient() - ->pipeline(function($pipe) use ( - $eventNames, - $prefix, - $dateTime, - $id) { - - foreach ($eventNames as $key => $eventName) { - $eventData = array( - new Year($eventName, $dateTime), - new Month($eventName, $dateTime), - new Week($eventName, $dateTime), - new Day($eventName, $dateTime), - new Hour($eventName, $dateTime), - ); - - foreach ($eventData as $event) { - $key = $prefix . $event->getKey(); - $pipe->setbit($key, $id, 1); - $pipe->sadd($prefix . 'keys', $key); - } + + $pipe = $this->getRedisClient()->pipeline(); + + foreach ($eventNames as $key => $eventName) { + $eventData = array( + new Year($eventName, $dateTime), + new Month($eventName, $dateTime), + new Week($eventName, $dateTime), + new Day($eventName, $dateTime), + new Hour($eventName, $dateTime), + ); + + foreach ($eventData as $event) { + $key = $this->prefixKey . $event->getKey(); + $pipe->setbit($key, $id, 1); + $pipe->sadd($this->prefixKey . 'keys', $key); } - }); + } + + $pipe->execute(); return $this; } diff --git a/tests/units/Bitter.php b/tests/units/Bitter.php index 1ff636f..85a3f21 100644 --- a/tests/units/Bitter.php +++ b/tests/units/Bitter.php @@ -144,11 +144,11 @@ public function testMultipleMarks($redisClient) $dateTime = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-06 15:30:45'); - $events = [ + $events = array( 'drink_a_bitter_beer', 'drink_another_bitter_beer', 'pass_out', - ]; + ); foreach ($events as $event) { $day = new Day($event, $dateTime); From 78301196176688a7e7b5a232fdc8c2c6ed653db6 Mon Sep 17 00:00:00 2001 From: geoffdutton Date: Thu, 12 Mar 2015 17:58:13 -0500 Subject: [PATCH 3/4] added array example --- README.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.rst b/README.rst index d58a05b..b1a1ff3 100644 --- a/README.rst +++ b/README.rst @@ -62,6 +62,12 @@ Mark user 123 as active and has played a song: ->mark('active', 123) ->mark('song:played', 123) ; + +.. code-block:: php + + $bitter + ->mark(['active', 'song:played'], 123) + ; **Note**: Please don't use huge ids (e.g. 2^32 or bigger) cause this will require large amounts of memory. From fb4e2a3ed3f84aba173eb17c0c5c19f942148b13 Mon Sep 17 00:00:00 2001 From: geoffdutton Date: Thu, 12 Mar 2015 17:58:43 -0500 Subject: [PATCH 4/4] Update README.rst --- README.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.rst b/README.rst index b1a1ff3..f8fed2b 100644 --- a/README.rst +++ b/README.rst @@ -63,6 +63,8 @@ Mark user 123 as active and has played a song: ->mark('song:played', 123) ; +Or... + .. code-block:: php $bitter