diff options
Diffstat (limited to 'store.py')
-rw-r--r-- | store.py | 35 |
1 files changed, 28 insertions, 7 deletions
@@ -499,13 +499,32 @@ class Portfolio: | |||
499 | worker_started = False | 499 | worker_started = False |
500 | worker_notify = None | 500 | worker_notify = None |
501 | callback = None | 501 | callback = None |
502 | poll_started_at = None | ||
502 | 503 | ||
503 | @classmethod | 504 | @classmethod |
504 | def start_worker(cls, poll=30): | 505 | def next_wait_time(cls): |
506 | now = datetime.datetime.now() | ||
507 | if cls.poll_started_at is None: | ||
508 | cls.poll_started_at = now | ||
509 | delta = now - cls.poll_started_at | ||
510 | |||
511 | if delta < datetime.timedelta(minutes=30): | ||
512 | return 30 | ||
513 | elif delta < datetime.timedelta(hours=1): | ||
514 | return 60 | ||
515 | elif delta < datetime.timedelta(hours=4): | ||
516 | return 5*60 | ||
517 | elif delta < datetime.timedelta(days=1): | ||
518 | return 60*60 | ||
519 | else: | ||
520 | raise "Too long waiting" | ||
521 | |||
522 | @classmethod | ||
523 | def start_worker(cls): | ||
505 | import threading | 524 | import threading |
506 | 525 | ||
507 | cls.worker = threading.Thread(name="portfolio", daemon=True, | 526 | cls.worker = threading.Thread(name="portfolio", daemon=True, |
508 | target=cls.wait_for_notification, kwargs={"poll": poll}) | 527 | target=cls.wait_for_notification) |
509 | cls.worker_notify = threading.Event() | 528 | cls.worker_notify = threading.Event() |
510 | cls.callback = threading.Event() | 529 | cls.callback = threading.Event() |
511 | 530 | ||
@@ -526,7 +545,7 @@ class Portfolio: | |||
526 | return cls.worker == threading.current_thread() | 545 | return cls.worker == threading.current_thread() |
527 | 546 | ||
528 | @classmethod | 547 | @classmethod |
529 | def wait_for_notification(cls, poll=30): | 548 | def wait_for_notification(cls): |
530 | if not cls.is_worker_thread(): | 549 | if not cls.is_worker_thread(): |
531 | raise RuntimeError("This method needs to be ran with the worker") | 550 | raise RuntimeError("This method needs to be ran with the worker") |
532 | while cls.worker_started: | 551 | while cls.worker_started: |
@@ -536,7 +555,7 @@ class Portfolio: | |||
536 | cls.report.print_log("[Worker] Fetching cryptoportfolio") | 555 | cls.report.print_log("[Worker] Fetching cryptoportfolio") |
537 | cls.get_cryptoportfolio(refetch=True) | 556 | cls.get_cryptoportfolio(refetch=True) |
538 | cls.callback.set() | 557 | cls.callback.set() |
539 | time.sleep(poll) | 558 | time.sleep(cls.next_wait_time()) |
540 | 559 | ||
541 | @classmethod | 560 | @classmethod |
542 | def stop_worker(cls): | 561 | def stop_worker(cls): |
@@ -550,11 +569,11 @@ class Portfolio: | |||
550 | cls.callback.wait() | 569 | cls.callback.wait() |
551 | 570 | ||
552 | @classmethod | 571 | @classmethod |
553 | def wait_for_recent(cls, delta=4, poll=30): | 572 | def wait_for_recent(cls, delta=4): |
554 | cls.get_cryptoportfolio() | 573 | cls.get_cryptoportfolio() |
555 | while cls.last_date.get() is None or datetime.datetime.now() - cls.last_date.get() > datetime.timedelta(delta): | 574 | while cls.last_date.get() is None or datetime.datetime.now() - cls.last_date.get() > datetime.timedelta(delta): |
556 | if cls.worker is None: | 575 | if cls.worker is None: |
557 | time.sleep(poll) | 576 | time.sleep(cls.next_wait_time()) |
558 | cls.report.print_log("Attempt to fetch up-to-date cryptoportfolio") | 577 | cls.report.print_log("Attempt to fetch up-to-date cryptoportfolio") |
559 | cls.get_cryptoportfolio(refetch=True) | 578 | cls.get_cryptoportfolio(refetch=True) |
560 | 579 | ||
@@ -585,7 +604,7 @@ class Portfolio: | |||
585 | cls.data.set(r.json(parse_int=D, parse_float=D)) | 604 | cls.data.set(r.json(parse_int=D, parse_float=D)) |
586 | cls.parse_cryptoportfolio() | 605 | cls.parse_cryptoportfolio() |
587 | cls.store_cryptoportfolio() | 606 | cls.store_cryptoportfolio() |
588 | except (JSONDecodeError, SimpleJSONDecodeError): | 607 | except (AssertionError, JSONDecodeError, SimpleJSONDecodeError): |
589 | cls.data.set(None) | 608 | cls.data.set(None) |
590 | cls.last_date.set(None) | 609 | cls.last_date.set(None) |
591 | cls.liquidities.set({}) | 610 | cls.liquidities.set({}) |
@@ -648,6 +667,8 @@ class Portfolio: | |||
648 | high_liquidity = parse_weights(cls.data.get("portfolio_1")) | 667 | high_liquidity = parse_weights(cls.data.get("portfolio_1")) |
649 | medium_liquidity = parse_weights(cls.data.get("portfolio_2")) | 668 | medium_liquidity = parse_weights(cls.data.get("portfolio_2")) |
650 | 669 | ||
670 | assert len(high_liquidity) > 0 | ||
671 | assert len(medium_liquidity) > 0 | ||
651 | cls.liquidities.set({ | 672 | cls.liquidities.set({ |
652 | "medium": medium_liquidity, | 673 | "medium": medium_liquidity, |
653 | "high": high_liquidity, | 674 | "high": high_liquidity, |