snproject/bitter

Bitter 是一个简单但功能强大的分析库

1.2.0 2014-12-01 13:19 UTC

This package is not auto-updated.

Last update: 2024-09-24 08:40:34 UTC


README

https://secure.travis-ci.org/jeremyFreeAgent/Bitter.png?branch=master

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 使用 Redis 版本 >=2.6

注意:在 Redis 中创建的每个键都将以前缀 bitter: 开头,临时键以前缀 bitter_temp: 开头。

Bitter 使用 Bitset PECL 扩展 版本 =1.0.1 用于 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.1

  • 修复了 bitDateRange。

1.1.0

  • 添加了使用 bitDateRange 方法的日期周期统计。

待办事项

感谢

此库是 bitmapist(Python)的移植,由 Amir Salihefendic 提供。