class GetLatestPriceController(object): """Gets the latest known price for a given symbol""" def __init__(self): self.log = Log(file_handler, self.__class__.__name__) def call(self, symbol): if symbol not in allowed_symbols or not buffer_dict: self.log.debug("Requested symbol %s is not allowed or has no data." % symbol) return json.dumps({"result": None}) last_trade_time = max(buffer_dict) data = buffer_dict.get(last_trade_time) result = {"symbol": data.get("symbol"), "price": data.get("price"), "time": last_trade_time.strftime("%Y-%m-%d %H:%M:%S")} return json.dumps({"result": result})
class GetChartDataController(object): """Gets chart data for a given symbol""" def __init__(self): self.log = Log(file_handler, self.__class__.__name__) def call(self, symbol): if symbol not in allowed_symbols or not buffer_dict: self.log.debug("Requested symbol %s is not allowed or has no data." % symbol) return json.dumps({"result": None}) labels = [k.strftime("%H:%M:%S") for k in buffer_dict.keys()] data = [p.get("price") for p in buffer_dict.values()] result = {"symbol": symbol, "labels": labels, "data": data, "min": float(min(data)) - 10, "max": float(max(data)) + 10} return json.dumps({"result": result})