def report_today_users_activity(action): """return how many users have done 'action' today""" date = datetime.datetime.now().strftime("%Y-%m-%d") c = redis_connection.get_connection() count = c.get("traking:%s:%s" % (action, date)) return count
def track_action(action, username, date_format): """track a user action""" date = datetime.datetime.now().strftime(date_format) c = redis_connection.get_connection() user_id = get_user_id(username) c.setbit("traking:%s:%s" % (action, date), user_id, 1)
def like(username, id): c = redis_connection.get_connection() expires = c.ttl("timeline:%s:text" % id) #if the key is not found or has not an associated ttl -1 is returned if expires < 0: return c.multi() c.sadd("timeline:%s:likes" % id, username) c.expire("timeline:%s:likes" % id, expires)#message likes expires with the message c.execute()
def last_messages(count): """ return the last "count" messages if there are some """ c = redis_connection.get_connection() last_id = int(c.get("timeline.last.id")) oldest_id = last_id - count if oldest_id<0: oldest_id=0 return get_messages(oldest_id, last_id)
def last_messages_by_last_id(oldest_id): """ return all messages starting from oldest_id to "timeline.last.id" key value """ c = redis_connection.get_connection() last_id = int(c.get("timeline.last.id")) if oldest_id<0: oldest_id=0 if oldest_id == last_id: return [] return get_messages(oldest_id, last_id)
def write_to_timeline(username, text): """ write a message to the timeline a message has a default ttl of 24 hours """ c = redis_connection.get_connection() id = c.incr("timeline.last.id") expires = 60*60*24 #timeline messages lasts one day c.multi() c.setex("timeline:%s:text" % id, expires, text) c.setex("timeline:%s:username" % id, expires, username) c.setex("timeline:%s:datetime" % id, expires, datetime.datetime.now().isoformat()) #c.expire("timeline:%s:*" % id, ) c.execute() return id
def get_messages(oldest_id, last_id): """ return a list of messages whose ids are between oldset_id and last_id extremes included """ c = redis_connection.get_connection() messages = [] for id in range(oldest_id, last_id+1): msg = { 'id': id, 'username':c.get("timeline:%s:username"%id), 'text':c.get("timeline:%s:text"%id), 'datetime':c.get("timeline:%s:datetime"%id), 'likes':c.smembers("timeline:%s:likes"%id) } messages.append(msg) return messages
def get_user_id(username): """ returns the id associated with the username if it is not already tracked it will be added to the users collection """ c = redis_connection.get_connection() id = c.get("users:%s:id" % username) if id is None: print "untracked user %s" % username id = c.incr("users.last.id") if c.setnx("users:%s:id" % username, id) == 0: id = c.get("users:%s:id" % username) print "%s has id %s" % (username, id) return id
c.setex("timeline:%s:datetime" % id, expires, datetime.datetime.now().isoformat()) #c.expire("timeline:%s:*" % id, ) c.execute() return id def like(username, id): c = redis_connection.get_connection() expires = c.ttl("timeline:%s:text" % id) #if the key is not found or has not an associated ttl -1 is returned if expires < 0: return c.multi() c.sadd("timeline:%s:likes" % id, username) c.expire("timeline:%s:likes" % id, expires)#message likes expires with the message c.execute() if __name__ == "__main__": like('fra', 20) print last_messages(4) c = redis_connection.get_connection() expires = c.ttl("timeline:%s:text" % 60) print expires