main.py 3.2 KB

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