def main(argv=sys.argv): config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) storeData = StoreOrderBookData(settings['kafka.url'])
def __init__(self, kafka_url: str): self.log.info("Initialized with kafka_url=" + kafka_url) self.consumer = KafkaConsumer( bootstrap_servers=kafka_url, value_deserializer=lambda v: json.loads(v)) self.consumer.subscribe(['orderbook']) self.log.debug("Init OK") while (1): with transaction.manager: self.log.debug("Polling records") records = self.consumer.poll(1000) orderbook = None for key in records: self.log.debug("Got " + str(len(records[key])) + " records") for record in records[key]: orderbook = OrderBook(json.loads(record.value)) if orderbook != None: self.log.debug("Got latest orderbook=" + repr(orderbook)) storage: OrderBookStorage = DBSession.query( OrderBookStorage).filter_by(id=1).one() storage.asks = orderbook.asks storage.bids = orderbook.bids self.log.debug("Stored latest orderbook status")
def put_place_limit_order(request: Request): user = map_user(request) if user == None: return {"error": "no auth"} else: user_id = user.id amount = float(request.matchdict['amount']) price = float(request.matchdict['price']) type = request.matchdict['type'] log.debug("New order for data:" + json.dumps({"amount":amount, "price":price, "type":type, "owner_id":user_id})) with transaction.manager: user = DBSession.query(User).filter_by(id=user_id).one() if type == "ASK": if user.stuff >= amount: user.stuff = float(user.stuff) - amount else: log.debug("User does not have enough stuff") return None elif type == "BID": if user.balance >= price*amount: user.balance = float(user.balance) - price*amount else: log.debug("User does not have enough funds") return None order = {"id": str(uuid4()), "amount": amount, "price": price, "operation": "ADD", "type": type, "owner_id": user_id} producer: KafkaProducer = request.registry.kafka_producer log.debug("Placing order into queue" + json.dumps(order)) producer.send(topic='limit_order', value=order) log.debug("Order successfully placed in the queue") return order
def get_user_details(request): user = map_user(request) if user == None: return {"error": "no auth"} else: user_id = user.id user = DBSession.query(User).filter_by(id=user_id).one() return user.toDict()
def map_user(request): if (request.authorization is not None): if (JWSig().unpack(request.authorization.params).valid()): username = JWT().unpack(request.authorization.params).payload()['preferred_username'] #map username to userid user = DBSession.query(User).filter_by(name=username).first() return user return None
def get_user_orders(request): user = map_user(request) if user == None: return {"error": "no auth"} else: user_id = user.id orders = DBSession.query(Order).filter_by(user_id=user_id).all() orders_array = [] for order in orders: orders_array.append(order.toDict()) return orders_array
def get_orderbook(request): log.debug("Requested BID orderbooks") return DBSession.query(OrderBookStorage).one().bids