main.py 2.9 KB

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