| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import model
- import sys
- import stocks
- import view
- import database
- """
- This is the controller file
- """
- def process_drop(this_stock):
- """
- Processes call when the action is "DROP"
- :param this_stock:
- :return:
- """
- global db
- global timestamp_previous
- global timestamp_latest
- stock_id = this_stock[1]
- data_previous = model.get_data(stock_id, timestamp_previous, db)
- data_latest = model.get_data(stock_id, timestamp_latest, db)
- threshold = this_stock[3]
- max_price = this_stock[2]
- is_drop = model.process_data(data_previous, data_latest, max_price, threshold)
- current_price = float(data_latest.current_price)
- quantity = data_latest.available_shares - data_previous.available_shares
- stock_name = this_stock[0]
- message = view.create_message(
- stock_name, timestamp_latest, current_price, quantity)
- print(message)
- if is_drop:
- view.broadcast(message)
- if __name__ == '__main__':
- db = database.db
- timestamp_latest = model.get_timestamp_latest(db) # global
- timestamp_stored = model.get_timestamp_stored() # global
- if timestamp_latest == timestamp_stored:
- """Nothing to do"""
- sys.exit()
- # Update timestamp_stored
- model.put_timestamp_stored(timestamp_latest)
- timestamp_previous = model.get_timestamp_previous(db) # global
- for stock in stocks.stocks:
- action = stock[-1]
- if action == "DROP":
- process_drop(stock)
- db.close()
|