Port monitor to normalized streams

its_happening
Tyler Goodlet 2020-08-10 15:48:57 -04:00
parent 37607d61ca
commit 23dcc45b63
2 changed files with 33 additions and 28 deletions

View File

@ -69,7 +69,6 @@ async def update_quotes(
chngcell.color = color chngcell.color = color
hdrcell.color = color hdrcell.color = color
# briefly highlight bg of certain cells on each trade execution # briefly highlight bg of certain cells on each trade execution
unflash = set() unflash = set()
tick_color = None tick_color = None
@ -105,39 +104,37 @@ async def update_quotes(
# initial coloring # initial coloring
to_sort = set() to_sort = set()
for sym, quote in first_quotes.items(): for quote in first_quotes:
row = table.get_row(sym) row = table.get_row(quote['symbol'])
record, displayable = formatter( row.update(quote)
quote, symbol_data=symbol_data) color_row(row, quote, {})
row.update(record, displayable)
color_row(row, record, {})
to_sort.add(row.widget) to_sort.add(row.widget)
table.render_rows(to_sort) table.render_rows(to_sort)
log.debug("Finished initializing update loop") log.debug("Finished initializing update loop")
task_status.started() task_status.started()
# real-time cell update loop # real-time cell update loop
async for quotes in agen: # new quotes data only async for quotes in agen: # new quotes data only
to_sort = set() to_sort = set()
for symbol, quote in quotes.items(): for symbol, quote in quotes.items():
row = table.get_row(symbol) row = table.get_row(symbol)
record, displayable = formatter(
quote, symbol_data=symbol_data)
# don't red/green the header cell in ``row.update()`` # don't red/green the header cell in ``row.update()``
record.pop('symbol') quote.pop('symbol')
quote.pop('key')
# determine if sorting should happen # determine if sorting should happen
sort_key = table.sort_key sort_key = table.sort_key
last = row.get_field(sort_key) last = row.get_field(sort_key)
new = record.get(sort_key, last) new = quote.get(sort_key, last)
if new != last: if new != last:
to_sort.add(row.widget) to_sort.add(row.widget)
# update and color # update and color
cells = row.update(record, displayable) cells = row.update(quote)
color_row(row, record, cells) color_row(row, quote, cells)
if to_sort: if to_sort:
table.render_rows(to_sort) table.render_rows(to_sort)
@ -179,18 +176,14 @@ async def _async_main(
This is started with cli cmd `piker monitor`. This is started with cli cmd `piker monitor`.
''' '''
feed = DataFeed(portal, brokermod) feed = DataFeed(portal, brokermod)
quote_gen, quotes = await feed.open_stream( quote_gen, first_quotes = await feed.open_stream(
symbols, symbols,
'stock', 'stock',
rate=rate, rate=rate,
test=test, test=test,
) )
first_quotes_list = list(first_quotes.copy().values())
first_quotes, _ = feed.format_quotes(quotes) quotes = list(first_quotes.copy().values())
if first_quotes[0].get('last') is None:
log.error("Broker API is down temporarily")
return
# build out UI # build out UI
Window.set_title(f"monitor: {name}\t(press ? for help)") Window.set_title(f"monitor: {name}\t(press ? for help)")
@ -202,7 +195,9 @@ async def _async_main(
bidasks = brokermod._stock_bidasks bidasks = brokermod._stock_bidasks
# add header row # add header row
headers = first_quotes[0].keys() headers = list(first_quotes_list[0].keys())
headers.remove('displayable')
header = Row( header = Row(
{key: key for key in headers}, {key: key for key in headers},
headers=headers, headers=headers,
@ -217,11 +212,17 @@ async def _async_main(
cols=1, cols=1,
size_hint=(1, None), size_hint=(1, None),
) )
for ticker_record in first_quotes: for ticker_record in first_quotes_list:
symbol = ticker_record['symbol']
table.append_row( table.append_row(
ticker_record['symbol'], symbol,
Row(ticker_record, headers=('symbol',), Row(
bidasks=bidasks, table=table) ticker_record,
headers=('symbol',),
bidasks=bidasks,
no_cell=('displayable',),
table=table
)
) )
table.last_clicked_row = next(iter(table.symbols2rows.values())) table.last_clicked_row = next(iter(table.symbols2rows.values()))

View File

@ -300,10 +300,10 @@ class Row(HoverBehavior, GridLayout):
# handle bidask cells # handle bidask cells
if key in layouts: if key in layouts:
self.add_widget(layouts[key]) self.add_widget(layouts[key])
elif key in children_flat: elif key in children_flat or key in no_cell:
# these cells have already been added to the `BidAskLayout` # these cells have already been added to the `BidAskLayout`
continue continue
elif key not in no_cell: else:
cell = self._append_cell(val, key, header=header) cell = self._append_cell(val, key, header=header)
cell.key = key cell.key = key
self._cell_widgets[key] = cell self._cell_widgets[key] = cell
@ -329,7 +329,7 @@ class Row(HoverBehavior, GridLayout):
self.add_widget(cell) self.add_widget(cell)
return cell return cell
def update(self, record, displayable): def update(self, record):
"""Update this row's cells with new values from a quote """Update this row's cells with new values from a quote
``record``. ``record``.
@ -341,7 +341,11 @@ class Row(HoverBehavior, GridLayout):
fgreen = colorcode('forestgreen') fgreen = colorcode('forestgreen')
red = colorcode('red2') red = colorcode('red2')
displayable = record['displayable']
for key, val in record.items(): for key, val in record.items():
if key not in displayable:
continue
last = self.get_field(key) last = self.get_field(key)
color = gray color = gray
try: try: