jithujose/bitter

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

1.1.1 2013-09-19 14:41 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:14:13 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 提供。