main.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. npc_drop = False
  28. total_stocks = data_latest.total_shares
  29. if model.is_npc_drop(data_previous, data_latest):
  30. npc_drop = True
  31. message = view.create_message(
  32. stock_name, timestamp_latest, current_price, quantity, current_quantity=total_stocks, npc_drop=npc_drop)
  33. print(message)
  34. if is_drop:
  35. view.broadcast(message)
  36. def process_forecast(this_stock):
  37. """
  38. Processes FORECAST requests
  39. :param this_stock:
  40. :return:
  41. """
  42. global db
  43. global timestamp_previous
  44. global timestamp_latest
  45. stock_id = this_stock[1]
  46. data_previous = model.get_data(stock_id, timestamp_previous, db)
  47. data_latest = model.get_data(stock_id, timestamp_latest, db)
  48. available = this_stock[3]
  49. max_price = this_stock[2]
  50. forecast_previous = data_previous.forecast
  51. forecast_latest = data_latest.forecast
  52. is_forecast_changed = model.process_forecast(data_previous, data_latest, max_price, available)
  53. current_price = float(data_latest.current_price)
  54. quantity = data_latest.available_shares - data_previous.available_shares
  55. stock_name = this_stock[0]
  56. current_quantity = data_latest.available_shares
  57. message = view.create_message(
  58. stock_name, timestamp_latest, current_price, quantity, current_quantity,
  59. forecast_previous, forecast_latest, False)
  60. print(message)
  61. if is_forecast_changed:
  62. view.broadcast(message)
  63. if __name__ == '__main__':
  64. db = database.db
  65. timestamp_latest = model.get_timestamp_latest(db) # global
  66. timestamp_stored = model.get_timestamp_stored() # global
  67. if timestamp_latest == timestamp_stored:
  68. """Nothing to do"""
  69. sys.exit()
  70. # Update timestamp_stored
  71. model.put_timestamp_stored(timestamp_latest)
  72. timestamp_previous = model.get_timestamp_previous(db) # global
  73. for stock in stocks.stocks:
  74. action = stock[-1]
  75. if action == "DROP":
  76. process_drop(stock)
  77. if action == "FORECAST":
  78. process_forecast(stock)
  79. db.close()