def quickBackfill(channel): sendDebugMessage('quickBackfill start', '@' + channel) start_time = time.time() post_id = 1 while True: posts = webgram.getPosts(channel, post_id) for post in posts[1:]: dbase.update(post) if post_id == posts[-1].post_id + 1: break post_id = posts[-1].post_id + 1 if time.time() - start_time > time_limit: break sendDebugMessage('quickBackfill end', '@' + channel, post_id)
def indexingImp(): for channel, score in channels.items(): if score < 0 or random.random() > 1.0 / (score * score + 1): continue if 'test' in sys.argv and random.random() > 0.1: continue # better testing posts = webgram.getPosts(channel, 1) # force cache for post in posts: dbase.update(post) if len(posts) > 1: # save http call for post in webgram.getPosts(channel): dbase.update(post) sendDebugMessage(*(['indexingImpDuration'] + dbase.resetStatus()), persistent=True)
def slowBackfill(channel): post_id = _findLastMessage(channel) sendDebugMessage('slowBackfill', '@' + channel, post_id) start_time = time.time() while post_id > 1: post_id -= 1 key = channel + '/' + str(post_id) if index.get(key): post_id -= int(random.random() * 100) continue post = webgram.getPost(channel, post_id) if post.getIndex(): dbase.update(post) if time.time() - start_time > time_limit: break print('slowBackfill end', '@' + channel, post_id)
def vrf_code(usr): """verify that the code received is same as server will clear the password on one wrong try """ email = usr.get("email") user = get_user(email) if user is None: raise EXCEPTION_USER_NOT_EXIST diff = int(time.time()) - user.get("tstamp", 0) if diff > TIMEOUT_USER_VERIFICATION: raise EXCEPTION_UNAUTHORIZED code = usr.get("code") check = user.get("code") if not code or not check: raise EXCEPTION_MALFORMED if not dbase.update("users", "where email=?", [email], { "code": "", "tstamp": 0 }): raise EXCEPTION_DATABASE return code == check
def indexingImp(): sendDebugMessage(*(['indexingImpStart'] + dbase.resetStatus())) for channel, score in channels.items(): if score < 0 or random.random() > 1.0 / min( score ** 3 + 1, score ** 2.5 * 2 + 1): continue if 'test' in sys.argv and random.random() > 0.1: continue # better testing if channel in dbase.delay._db.items and random.random() > 0.01: continue posts = webgram.getPosts(channel, 1) # force cache for post in posts: dbase.update(post) if len(posts) <= 1: # save http call continue dbase.updateAll(webgram.getPosts(channel)) dbase.updateDelayStatus(channel) sendDebugMessage(*(['indexingImpDuration'] + dbase.resetStatus()), persistent=True)
def chg_name(usr): """change the name of the user""" email = usr.get("email") if not is_user_exist(email): raise EXCEPTION_USER_NOT_EXIST name = usr.get("name", email) return dbase.update("users", "where email=?", [email], {"name": name})
def slowBackfill(channel): post_id = _findLastMessage(channel) findNew = False for _ in range(getMaxIteration(channel)): post_id -= 1 if post_id <= 1: break key = channel + '/' + str(post_id) if index.get(key): post_id -= int(random.random() * 100) continue post = webgram.getPost(channel, post_id) if post.getIndex(): findNew = True dbase.update(post) elif findNew: dbase.removeKey(key) if postTooOld(post): break
def _findLastMessage(channel): left = getMaxInIndex(channel) right = 10000 + left while left < right - 50: hit = False for _ in range(5): post_id = int(left + (random.random() * 0.75 + 0.25) * (right - left)) post = webgram.getPost(channel, post_id) if post.getIndex(): dbase.update(post) hit = True break mid = int((left + right) / 2) if hit: if post_id > mid: right = post_id * 2 - left left = post_id else: right = mid return left
def update_room(obj): """update room details""" rm_id = obj.get("rm_id") name = obj.get("name") vacy = obj.get("vacy") aval = obj.get("aval") if not rm_id: raise EXCEPTION_MALFORMED res = {} if name: res["name"] = name if vacy: res["numAvailable"] = vacy if aval: res["weekRange"] = aval return dbase.update("rooms", "where _id=?", [rm_id], res)
def gen_code(usr): """generate code to be emailed to user""" email = usr.get("email") if not is_user_exist(email): raise EXCEPTION_USER_NOT_EXIST part1, part2 = rand_code(), rand_code() res = dbase.update("users", "where email=?", [email], { "code": part1 + "-" + part2, "tstamp": int(time.time()) }) if not res: raise EXCEPTION_DATABASE sendmail({ "to": email, "subj": "logging in", "text": get_template("email_verification") % (part1 + "-" + part2) }) return part1