cli: integrate with stats reported by the modules

This commit is contained in:
Dima Gerasimov 2020-05-25 11:27:54 +01:00
parent d890599c7c
commit 7bd7cc9228
7 changed files with 49 additions and 4 deletions

View file

@ -100,20 +100,34 @@ def config_check(args):
def modules_check(args): def modules_check(args):
verbose = args.verbose verbose = args.verbose
vw = '' if verbose else '; pass --verbose to print more information'
from .util import get_modules from .util import get_modules
for m in get_modules(): for m in get_modules():
try: try:
importlib.import_module(m) mod = importlib.import_module(m)
except Exception as e: except Exception as e:
# todo more specific command? # todo more specific command?
vw = '' if verbose else '; pass --verbose to print more information'
warning(f'{color.RED}FAIL{color.RESET}: {m:<30} loading failed{vw}') warning(f'{color.RED}FAIL{color.RESET}: {m:<30} loading failed{vw}')
if verbose: if verbose:
tb(e) tb(e)
continue
info(f'{color.GREEN}OK{color.RESET} : {m:<30}')
stats = getattr(mod, 'stats', None)
if stats is None:
continue
try:
res = stats()
except Exception as ee:
warning(f' - {color.RED}stats:{color.RESET} computing failed{vw}')
if verbose:
tb(ee)
else: else:
info(f'{color.GREEN}OK{color.RESET} : {m:<30}') info(f' - stats: {res}')
def list_modules(args): def list_modules(args):

View file

@ -322,6 +322,12 @@ def by_night() -> Dict[date, Emfit]:
return res return res
def stats():
return {
'nights': len(by_night()),
}
def main(): def main():
for k, v in by_night().items(): for k, v in by_night().items():
print(k, v.start, v.end) print(k, v.start, v.end)

View file

@ -90,3 +90,10 @@ def get_cid_map(bfile: str):
def print_checkins(): def print_checkins():
print(get_checkins()) print(get_checkins())
def stats():
from more_itertools import ilen
return {
'checkins': ilen(get_checkins()),
}

View file

@ -69,6 +69,16 @@ def pages() -> List[Res[Page]]:
return sort_res_by(_dal().pages(), key=lambda h: h.created) return sort_res_by(_dal().pages(), key=lambda h: h.created)
# todo not public api yet
def stats():
# todo add 'last date' checks et
return {
# todo ilen
'highlights': len(highlights()),
'pages' : len(pages()),
}
def _main(): def _main():
for page in get_pages(): for page in get_pages():
print(page) print(page)

View file

@ -40,3 +40,10 @@ def states() -> Iterable[SubscriptionState]:
dt = isoparse(dts) dt = isoparse(dts)
subs = parse_file(f) subs = parse_file(f)
yield dt, subs yield dt, subs
def stats():
from more_itertools import ilen, last
return {
'subscriptions': ilen(last(states())[1])
}

View file

@ -1,6 +1,7 @@
from my.foursquare import get_checkins from my.foursquare import get_checkins
def test_checkins(): def test_checkins():
# todo reuse stats?
checkins = get_checkins() checkins = get_checkins()
assert len(checkins) > 100 assert len(checkins) > 100
assert any('Victoria Park' in c.summary for c in checkins) assert any('Victoria Park' in c.summary for c in checkins)

View file

@ -17,7 +17,7 @@ commands =
# TODO add; once I figure out porg depdencency?? tests/config.py # TODO add; once I figure out porg depdencency?? tests/config.py
# TODO run demo.py? just make sure with_my is a bit cleverer? # TODO run demo.py? just make sure with_my is a bit cleverer?
# TODO e.g. under CI, rely on installing # TODO e.g. under CI, rely on installing
hpi hello hpi modules
[testenv:demo] [testenv:demo]