| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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]
- npc_drop = False
- total_stocks = data_latest.total_shares
- previous_available_shares = data_previous.available_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)
- 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()
|