main.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import model
  2. import sys
  3. import stocks
  4. import view
  5. import database
  6. """
  7. This is the controller file
  8. """
  9. def process_drop(this_stock):
  10. """
  11. Processes call when the action is "DROP"
  12. :param this_stock:
  13. :return:
  14. """
  15. global db
  16. global timestamp_previous
  17. global timestamp_latest
  18. stock_id = this_stock[1]
  19. data_previous = model.get_data(stock_id, timestamp_previous, db)
  20. data_latest = model.get_data(stock_id, timestamp_latest, db)
  21. threshold = this_stock[3]
  22. max_price = this_stock[2]
  23. is_drop = model.process_data(data_previous, data_latest, max_price, threshold)
  24. current_price = float(data_latest.current_price)
  25. quantity = data_latest.available_shares - data_previous.available_shares
  26. stock_name = this_stock[0]
  27. message = view.create_message(
  28. stock_name, timestamp_latest, current_price, quantity)
  29. print(message)
  30. if is_drop:
  31. view.broadcast(message)
  32. if __name__ == '__main__':
  33. db = database.db
  34. timestamp_latest = model.get_timestamp_latest(db) # global
  35. timestamp_stored = model.get_timestamp_stored() # global
  36. if timestamp_latest == timestamp_stored:
  37. """Nothing to do"""
  38. sys.exit()
  39. # Update timestamp_stored
  40. model.put_timestamp_stored(timestamp_latest)
  41. timestamp_previous = model.get_timestamp_previous(db) # global
  42. for stock in stocks.stocks:
  43. action = stock[-1]
  44. if action == "DROP":
  45. process_drop(stock)
  46. db.close()