| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import model
- import sys
- import stocks
- import view
- import database
- """
- This is the controller file
- """
- def process_drop(this_stock):
- """
- Processes call when the action is "DROP"
- :param this_stock:
- :return:
- """
- global db
- global timestamp_previous
- global timestamp_latest
- stock_id = this_stock[1]
- data_previous = model.get_data(stock_id, timestamp_previous, db)
- data_latest = model.get_data(stock_id, timestamp_latest, db)
- threshold = this_stock[3]
- max_price = this_stock[2]
- is_drop = model.process_data(data_previous, data_latest, max_price, threshold)
- current_price = float(data_latest.current_price)
- quantity = data_latest.available_shares - data_previous.available_shares
- stock_name = this_stock[0]
- stock_id = this_stock[5]
- npc_drop = False
- total_stocks = data_latest.total_shares
- previous_available_shares = '{:,} / {:,}'.format(data_latest.available_shares,
- data_latest.total_shares)
- if model.is_npc_drop(data_previous, data_latest):
- npc_drop = True
- message = view.create_message(
- stock_name,
- timestamp_latest,
- current_price,
- quantity,
- current_quantity=previous_available_shares,
- npc_drop=npc_drop,
- stock_id=stock_id)
- print(message)
- if is_drop:
- view.broadcast(message)
- def process_forecast(this_stock):
- """
- Processes FORECAST requests
- :param this_stock:
- :return:
- """
- global db
- global timestamp_previous
- global timestamp_latest
- stock_id = this_stock[1]
- data_previous = model.get_data(stock_id, timestamp_previous, db)
- data_latest = model.get_data(stock_id, timestamp_latest, db)
- available = this_stock[3]
- max_price = this_stock[2]
- forecast_previous = data_previous.forecast
- forecast_latest = data_latest.forecast
- is_forecast_changed = model.process_forecast(data_previous, data_latest, max_price, available)
- current_price = float(data_latest.current_price)
- quantity = data_latest.available_shares - data_previous.available_shares
- stock_name = this_stock[0]
- current_quantity = data_latest.available_shares
- message = view.create_message(
- stock_name, timestamp_latest, current_price, quantity, current_quantity,
- forecast_previous, forecast_latest, False)
- print(message)
- if is_forecast_changed:
- view.broadcast(message)
- if __name__ == '__main__':
- db = database.db
- timestamp_latest = model.get_timestamp_latest(db) # global
- timestamp_stored = model.get_timestamp_stored() # global
- if timestamp_latest == timestamp_stored:
- """Nothing to do"""
- sys.exit()
- # Update timestamp_stored
- model.put_timestamp_stored(timestamp_latest)
- timestamp_previous = model.get_timestamp_previous(db) # global
- for stock in stocks.stocks:
- action = stock[-1]
- if action == "DROP":
- process_drop(stock)
- if action == "FORECAST":
- process_forecast(stock)
- db.close()
|