基于窗口的计数器
sifr的Python项目详细描述
… and with the sign 0 … any number may be written
—Fibionacci
使用内存、redis或riak计算各种基于时间的窗口中的内容 储藏室。
安装
安装基本软件包:
pip install sifr
使用redis依赖项安装sifr:
pip install 'sifr[redis]'
使用riak依赖项安装sifr:
pip install 'sifr[riak]'
使用sifrd服务依赖项安装sifr
pip install 'sifr[daemon]'
示例
使用sifr直接存储
importdatetimeimportredis,riakfromsifr.spanimportYear,Month,Day,Hour,Minute,get_time_spansfromsifr.storageimportMemoryStorage,RedisStorage,RiakStorageredis_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=1page="index.html"# construct the windows. These are the resolutions that will be tracked.spans=[span(now,["views","user",user_id])forspanin[Year,Month,Day,Hour,Minute]]# incr a counter for all resolutions[store.incr_multi(spans)forstoreinstores]# incr a unique counter[store.incr_unique_multi(spans,page)forstoreinstores][store.incr_unique_multi(spans,page)forstoreinstores]# track the page view[store.track_multi(spans,page)forstoreinstores][store.track_multi(spans,page)forstoreinstores]# get the counts/uniques for a single year windowforstoreinstores:assert1==store.count(Year(now,["views","user",1]))assert1==store.cardinality(Year(now,["views","user",1]))assertset(["index.html"])==store.uniques(Year(now,["views","user",1]))# get the counts/uniques for a rangestart=now-datetime.timedelta(minutes=1)end=now+datetime.timedelta(minutes=1)span_range=get_time_spans(start,end,["views","user",1],[Minute])forstoreinstores:assert[1]==[store.count(span)forspaninspan_range]assert[1]==[store.cardinality(span)forspaninspan_range]assert[set(["index.html"])]==[store.uniques(span)forspaninspan_range]
通过rpc
使用sifrsifr.yml(使用redis后端)
storage:redisredis_url:redis://localhost:6379/1host:localhostport:6000
sifr.yml(使用RIAK后端)
storage:riakriak_nodes:-host:localhostpb_port:8087host:localhostport:6000
运行服务器
sifrd msgpack_server --config=sifr.yml
与服务器交互
fromsifrimportRPCClientclient=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")assert1==client.count("views:user:1",datetime.datetime.now(),"day")assert1==client.cardinality("views:user:1",datetime.datetime.now(),"day")assertset(["index.html"])==client.uniques("views:user:1",datetime.datetime.now(),"day")
0.0.4 2015年6月16日
- 已从redis存储中删除事务管道
0.0.3 2015年6月10日
- 初始版本