def pop_lowest_sell_order(r: redis.client.Redis) -> Tuple[Order, bool]: lowest_sell = r.zrange(redis_sell_prices_name, 0, 0, desc=False, withscores=True) if len(lowest_sell) == 0: return (None, False) else: (sell_order_id, sell_price) = lowest_sell[0] sell_order_size = int( r.hget(redis_order_sizes_name, sell_order_id).decode('utf-8')) sell_order_account_name = r.hget(redis_account_names_name, sell_order_id) successful_remove = remove_price_and_size(r, redis_sell_prices_name, redis_order_sizes_name, redis_account_names_name, sell_order_id) if successful_remove: sell_order_id = sell_order_id.decode('utf-8') sell_order_account_name = sell_order_account_name.decode('utf-8') order = Order(sell_order_account_name, sell_order_id, "Sell", sell_order_size, sell_price) return (order, True) else: return (None, False)
def pop_highest_buy_order(r: redis.client.Redis) -> Tuple[Order, bool]: highest_buy = r.zrange(redis_buy_prices_name, 0, 0, desc=True, withscores=True) if len(highest_buy) == 0: return (None, False) else: (buy_order_id, buy_price) = highest_buy[0] buy_order_size = int( r.hget(redis_order_sizes_name, buy_order_id).decode('utf-8')) buy_order_account_name = r.hget(redis_account_names_name, buy_order_id) successful_remove = remove_price_and_size(r, redis_buy_prices_name, redis_order_sizes_name, redis_account_names_name, buy_order_id) if successful_remove: buy_order_id = buy_order_id.decode('utf-8') buy_order_account_name = buy_order_account_name.decode('utf-8') order = Order(buy_order_account_name, buy_order_id, "Buy", buy_order_size, buy_price) return (order, True) else: return (None, False)
def get_whole_db(redis_instance: redis.client.Redis, hashset_name: str) -> Json: db_set = dict() for key in get_db_keys(redis_instance, hashset_name): key = key.decode("utf-8") value = redis_instance.hget(hashset_name, key) value = value.decode("utf-8") db_set[key] = value return json.dumps(db_set)
def vote_for(voted_letter: str, redis_instance: redis.client.Redis, hashset_name: str) -> Json: if redis_instance.hget(hashset_name, voted_letter) == None: abort(400, 'This value does not exist') for key in get_db_keys(redis_instance, hashset_name): key = key.decode("utf-8") if key == voted_letter: redis_instance.hincrby(hashset_name, voted_letter, 1) return get_whole_db(redis_instance, hashset_name)