free-agent / bitter
此包已被废弃,不再维护。未建议替代包。
Bitter 是一个简单但强大的分析库
1.1.1
2013-09-19 14:41 UTC
Requires
- php: >=5.3.3
Requires (Dev)
- atoum/atoum: dev-master
- predis/predis: 0.8.*
Suggests
- predis/predis: 0.8.*
This package is auto-updated.
Last update: 2020-06-13 14:08:25 UTC
README
1.2.0 进行中
Bitter 是一个简单但强大的分析库
"使用 Bitter,你有时间喝一杯苦啤酒!"
-- Jérémy Romey
Bitter 可以回答以下问题
- 用户 X 今天/这周/这个月是否在线?
- 用户 X 是否执行了动作 "Y"?
- 这个月/这个小时有多少用户活跃?
- 这周有多少唯一用户执行了动作 "Y"?
- 上周活跃的用户中还有多少%仍然活跃?
- 上个月活跃的用户中还有多少%在这个月仍然活跃?
Bitter 非常容易使用,并允许你轻松创建自己的报告 - 请参阅Bitter 库网站获取更多信息和此项目的文档。
安装
使用 Composer 安装:free-agent/bitter
。
在你的 composer.json 中应有
{ "require": { "free-agent/bitter": "1.1.*" } }
要求
Bitter 使用版本 >=2.6 的 Redis。
注意:Redis 中创建的每个键都将使用前缀 bitter:
,临时键使用 bitter_temp:
。
Bitter 使用版本 =1.0.1 的 Bitset PECL 扩展,用于 getIds
方法。
基本用法
使用 Redis 客户端创建 Bitter(以 Predis 为例)
$redisClient = new \Predis\Client(); $bitter = new \FreeAgent\Bitter\Bitter($redisClient);
标记用户 123 为活跃且已播放歌曲
$bitter ->mark('active', 123) ->mark('song:played', 123) ;
注意:请勿使用巨大的 ID(例如,2^32 或更大),因为这需要大量的内存。
传递 DateTime 作为第三个参数
$bitter->mark('song:played', 123, new \DateTime('yesterday'));
测试用户 123 这周是否播放了歌曲
$currentWeek = new FreeAgent\Bitter\UnitOfTime\Week('song:played'); if ($bitter->in(123, $currentWeek) { echo 'User with id 123 has played a song this week.'; } else { echo 'User with id 123 has not played a song this week.'; }
昨天有多少用户活跃
$yesterday = new \FreeAgent\Bitter\UnitOfTime\Day('active', new \DateTime('yesterday')); echo $bitter->count($yesterday) . ' users were active yesterday.';
使用 BitOp
昨天和今天都活跃的用户数量
$today = new \FreeAgent\Bitter\UnitOfTime\Day('active'); $yesterday = new \FreeAgent\Bitter\UnitOfTime\Day('active', new \DateTime('yesterday')); $count = $bitter ->bitOpAnd('bit_op_example', $today, $yesterday) ->count('bit_op_example') ; echo $count . ' users were active yesterday and today.';
注意:bit_op_example 键将在 60 秒后过期。
测试用户 123 昨天是否活跃且今天是否活跃
$today = new \FreeAgent\Bitter\UnitOfTime\Day('active'); $yesterday = new \FreeAgent\Bitter\UnitOfTime\Day('active', new \DateTime('yesterday')); $active = $bitter ->bitOpAnd('bit_op_example', $today, $yesterday) ->in(123, 'bit_op_example') ; if ($active) { echo 'User with id 123 was active yesterday and today.'; } else { echo 'User with id 123 was not active yesterday and today.'; }
注意:请参阅 Redis BITOP 命令 以了解性能考虑。
自定义日期周期统计
在指定日期周期内有多少用户活跃
$from = new \DateTime('2010-14-02 20:15:30'); $to = new \DateTime('2012-21-12 13:30:45'); $count = $bitter ->bitDateRange('active', 'active_period_example', $from, $to) ->count('active_period_example') ; echo $count . ' users were active from "2010-14-02 20:15:30" to "2012-21-12 13:30:45".';
获取指定键的 ID
获取指定日期周期的 ID
$from = new \DateTime('2010-14-02 20:15:30'); $to = new \DateTime('2012-21-12 13:30:45'); $ids = $bitter ->bitDateRange('active', 'active_period_example', $from, $to) ->getIds('active_period_example') ; echo 'Ids of users that were active from "2010-14-02 20:15:30" to "2012-21-12 13:30:45":'; echo '<br />'; echo implode(', ', $ids);
单元测试
您可以使用以下命令运行测试
bin/atoum -d tests/units
发行说明
1.2.0
- 添加了删除特定临时键的方法。
- 添加了删除事件所有数据的方法。
- 将 Event 重命名为 UnitOfTime 以使其更具说明性。
1.1.0
- 使用bitDateRange方法添加了日期时间段统计数据。
待办事项
感谢
此库是bitmapist(Python)的移植,由Amir Salihefendic编写。