Skip to main content

基于窗口的计数器

项目描述

... 并带有符号 0 ... 可以写任何数字

——斐波那契

travis-ci 连体工作服 景观 皮皮 执照

http://i.imgur.com/luJUJ31.png

使用内存、redis 或 riak 存储计算各种基于时间的窗口中的事物。

安装

安装基本包:

pip install sifr

使用 redis 依赖安装sifr :

pip install 'sifr[redis]'

使用riak依赖项安装sifr :

pip install 'sifr[riak]'

使用sifrd服务依赖项安装sifr:

pip install 'sifr[daemon]'

例子

sifr与直接存储一起使用

import datetime
import redis, riak

from sifr.span import Year, Month, Day, Hour, Minute, get_time_spans
from sifr.storage import MemoryStorage, RedisStorage, RiakStorage

redis_client = redis.Redis()
redis_store = RedisStorage(redis_client)

riak_client = riak.RiakClient()
riak_store = RiakStorage(riak_client)

memory_store = MemoryStorage()

stores = [memory_store, redis_store, riak_store]

now = datetime.datetime.now()
user_id = 1
page = "index.html"

# construct the windows. These are the resolutions that will be tracked.
spans = [
    span(now, ["views", "user", user_id])
    for span in [Year, Month, Day, Hour, Minute]
]
# incr a counter for all resolutions
[store.incr_multi(spans) for store in stores]

# incr a unique counter
[store.incr_unique_multi(spans, page) for store in stores]
[store.incr_unique_multi(spans, page) for store in stores]

# track the page view
[store.track_multi(spans, page) for store in stores]
[store.track_multi(spans, page) for store in stores]

# get the counts/uniques for a single year window
for store in stores:
  assert 1 == store.count(Year(now, ["views", "user", 1]))
  assert 1 == store.cardinality(Year(now, ["views", "user", 1]))
  assert set(["index.html"]) == store.uniques(Year(now, ["views", "user", 1]))


# get the counts/uniques for a range
start = now - datetime.timedelta(minutes=1)
end = now + datetime.timedelta(minutes=1)

span_range = get_time_spans(start, end, ["views", "user", 1], [Minute])
for store in stores:
  assert [1] == [store.count(span) for span in span_range]
  assert [1] == [store.cardinality(span) for span in span_range]
  assert [set(["index.html"])] == [store.uniques(span) for span in span_range]

通过 rpc使用sifr

sifr.yml(使用 redis 后端)

storage: redis
redis_url: redis://localhost:6379/1
host: localhost
port: 6000

sifr.yml(使用 riak 后端)

storage: riak
riak_nodes:
    - host: localhost
      pb_port: 8087
host: localhost
port: 6000

运行服务器

sifrd msgpack_server --config=sifr.yml

与服务器交互

from sifr import RPCClient
client = RPCCient(host='localhost', port=6000, resolutions=["year", "month", "day"])
client.incr("views:user:1")
client.incr_unique("views:user:1", "index.html")
client.incr_unique("views:user:1", "index.html")
client.track("views:user:1", "index.html")
client.track("views:user:1", "index.html")

assert 1 == client.count("views:user:1", datetime.datetime.now(), "day")
assert 1 == client.cardinality("views:user:1", datetime.datetime.now(), "day")
assert set(["index.html"]) == client.uniques("views:user:1", datetime.datetime.now(), "day")

参考

变更日志

0.0.4 2015-06-16

  • 从 redis 存储中删除了事务管道。

0.0.3 2015-06-10

  • 初始发行

下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

sifr-0.0.4.tar.gz (224.3 kB 查看哈希

已上传 source

内置分布

sifr-0.0.4-py2.7.egg (38.0 kB 查看哈希

已上传 2 7