main.py 2.8 KB

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