main.py 2.7 KB

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