基于窗口的计数器

sifr的Python项目详细描述


… and with the sign 0 … any number may be written

—Fibionacci

travis-cicoverallslandscapepypilicense

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直接存储

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

使用sifr

sifr.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日

  • 初始版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
哦,我需要在这里重建Java吗?   java在按键后做一些事情(但需要等待一点)   java迭代ArrayList的泛型类型<User>   Android Studio和WebStorm之间的java语言级别不一致   java如何在复杂模型中使用@valid注释   Java字符串初始化为基元类型   java外键级联删除在OpenJPA/Postgresql中未按预期工作   java运行时堆分析;匹配模式的字符串何时初始化   java Selenium webdriver我无法将webdriver设置为Firefox I   java服务器发送事件(SSE)和Jetty(编程设置)   linux java。lang.NullPointerException和返回代码   OpenGL VBO多维数据集未正确渲染(Java、LWJGL)   java JavaFX如何将图形“裁剪”到按钮   java数据库插入未向表中添加数据   java在字符串数组中添加数字   在java中识别一串字符串中的编号系统/序列/模式