tuxone/bitter

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

1.1.1 2013-09-19 14:41 UTC

This package is auto-updated.

Last update: 2024-09-04 23:05:05 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 使用版本 >=2.6Redis

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

Bitter 使用版本 =1.0.1Bitset 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".';

为给定键获取 Ids

为给定日期周期获取 Ids

$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 提供。