From 4898459bcdb3cfaecd209c2bc0a550b53386ee09 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 16 Apr 2018 02:16:26 -0400 Subject: [PATCH] Make watchlist app retrieve quotes from the broker daemon --- piker/ui/watchlist.py | 132 +++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 67 deletions(-) diff --git a/piker/ui/watchlist.py b/piker/ui/watchlist.py index 9f57d708..4f6f4d25 100644 --- a/piker/ui/watchlist.py +++ b/piker/ui/watchlist.py @@ -7,7 +7,6 @@ Launch with ``piker watch ``. """ from itertools import chain from types import ModuleType -from functools import partial import trio from kivy.uix.boxlayout import BoxLayout @@ -21,7 +20,6 @@ from kivy.core.window import Window from ..log import get_logger from .pager import PagerView -from ..brokers.core import poll_tickers log = get_logger('watchlist') @@ -49,7 +47,7 @@ _kv = (f''' #:kivy 1.10.0 - font_size: 18 + font_size: 20 # text_size: self.size size: self.texture_size color: {colorcode('gray')} @@ -386,77 +384,77 @@ async def _async_main(name, tickers, brokermod, rate): This is started with cli command `piker watch`. ''' - queue = trio.Queue(1000) + # setup ticker stream + from ..brokers.core import StreamQueue + queue = StreamQueue(await trio.open_tcp_stream('127.0.0.1', 1616)) + await queue.put(tickers) # initial request for symbols price streams + + # get initial symbol data async with brokermod.get_client() as client: - async with trio.open_nursery() as nursery: - # get long term data including last days close price - sd = await client.symbol_data(tickers) + # get long term data including last days close price + sd = await client.symbol_data(tickers) - nursery.start_soon( - partial(poll_tickers, client, brokermod.quoter, tickers, queue, - rate=rate) - ) + async with trio.open_nursery() as nursery: + # get first quotes response + quotes = await queue.get() + first_quotes = [ + brokermod.format_quote(quote, symbol_data=sd)[0] + for quote in quotes.values()] - # get first quotes response - quotes = await queue.get() - first_quotes = [ - brokermod.format_quote(quote, symbol_data=sd)[0] - for quote in quotes.values()] + if first_quotes[0].get('last') is None: + log.error("Broker API is down temporarily") + nursery.cancel_scope.cancel() + return - if first_quotes[0].get('last') is None: - log.error("Broker API is down temporarily") - nursery.cancel_scope.cancel() - return + # build out UI + Window.set_title(f"watchlist: {name}\t(press ? for help)") + Builder.load_string(_kv) + box = BoxLayout(orientation='vertical', padding=5, spacing=5) - # build out UI - Window.set_title(f"watchlist: {name}\t(press ? for help)") - Builder.load_string(_kv) - box = BoxLayout(orientation='vertical', padding=5, spacing=5) + # define bid-ask "stacked" cells + # (TODO: needs some rethinking and renaming for sure) + bidasks = brokermod._bidasks - # define bid-ask "stacked" cells - # (TODO: needs some rethinking and renaming for sure) - bidasks = brokermod._bidasks + # add header row + headers = first_quotes[0].keys() + header = Row( + {key: key for key in headers}, + headers=headers, + bidasks=bidasks, + is_header_row=True, + size_hint=(1, None), + ) + box.add_widget(header) - # add header row - headers = first_quotes[0].keys() - header = Row( - {key: key for key in headers}, - headers=headers, - bidasks=bidasks, - is_header_row=True, - size_hint=(1, None), - ) - box.add_widget(header) + # build grid + grid = TickerTable( + cols=1, + size_hint=(1, None), + ) + for ticker_record in first_quotes: + grid.append_row(ticker_record, bidasks=bidasks) + # associate the col headers row with the ticker table even though + # they're technically wrapped separately in containing BoxLayout + header.table = grid - # build grid - grid = TickerTable( - cols=1, - size_hint=(1, None), - ) - for ticker_record in first_quotes: - grid.append_row(ticker_record, bidasks=bidasks) - # associate the col headers row with the ticker table even though - # they're technically wrapped separately in containing BoxLayout - header.table = grid + # mark the initial sorted column header as bold and underlined + sort_cell = header.get_cell(grid.sort_key) + sort_cell.bold = sort_cell.underline = True + grid.last_clicked_col_cell = sort_cell - # mark the initial sorted column header as bold and underlined - sort_cell = header.get_cell(grid.sort_key) - sort_cell.bold = sort_cell.underline = True - grid.last_clicked_col_cell = sort_cell + # set up a pager view for large ticker lists + grid.bind(minimum_height=grid.setter('height')) + pager = PagerView(box, grid, nursery) + box.add_widget(pager) - # set up a pager view for large ticker lists - grid.bind(minimum_height=grid.setter('height')) - pager = PagerView(box, grid, nursery) - box.add_widget(pager) - - widgets = { - # 'anchor': anchor, - 'root': box, - 'grid': grid, - 'box': box, - 'header': header, - 'pager': pager, - } - nursery.start_soon(run_kivy, widgets['root'], nursery) - nursery.start_soon( - update_quotes, brokermod, widgets, queue, sd, quotes) + widgets = { + # 'anchor': anchor, + 'root': box, + 'grid': grid, + 'box': box, + 'header': header, + 'pager': pager, + } + nursery.start_soon(run_kivy, widgets['root'], nursery) + nursery.start_soon( + update_quotes, brokermod, widgets, queue, sd, quotes)