From: Shane Jaroch Date: Thu, 15 Feb 2024 00:30:09 +0000 (-0500) Subject: wip cache mirrors X-Git-Tag: v0.2.8.dev0~39 X-Git-Url: https://git.nutra.tk/v1?a=commitdiff_plain;h=4e367976ea0e1054f274bd2421074a9fb91fbdb5;p=nutratech%2Fcli.git wip cache mirrors --- diff --git a/ntclient/argparser/__init__.py b/ntclient/argparser/__init__.py index b4800d7..9206041 100644 --- a/ntclient/argparser/__init__.py +++ b/ntclient/argparser/__init__.py @@ -328,7 +328,7 @@ def build_subcommand_bug(subparsers: argparse._SubParsersAction) -> None: bug_parser = subparsers.add_parser("bug", help="report bugs") bug_subparser = bug_parser.add_subparsers(title="bug subcommands") bug_parser.add_argument( - "--all", action="store_true", help="include already submitted bugs, too" + "--show", action="store_true", help="show list of unsubmitted bugs" ) bug_parser.set_defaults(func=parser_funcs.bugs_list) diff --git a/ntclient/argparser/funcs.py b/ntclient/argparser/funcs.py index d63a2f1..fd945ce 100644 --- a/ntclient/argparser/funcs.py +++ b/ntclient/argparser/funcs.py @@ -341,21 +341,25 @@ def calc_lbm_limits(args: argparse.Namespace) -> tuple: def bugs_list(args: argparse.Namespace) -> tuple: """List bug reports that have een saved""" _bugs_list = ntclient.services.bugs.list_bugs() + n_bugs_total = len(_bugs_list) + n_bugs_unsubmitted = len([x for x in _bugs_list if not bool(x[-1])]) - print(f"You have: {len(_bugs_list)} total bugs amassed in your journey.") - print( - f"Of these, {len([x for x in _bugs_list if not bool(x[-1])])} " - f"require submission/reporting." - ) + print(f"You have: {n_bugs_total} total bugs amassed in your journey.") + print(f"Of these, {n_bugs_unsubmitted} require submission/reporting.") print() for bug in _bugs_list: + if not args.show: + continue # Skip submitted bugs by default - if bool(bug[-1]) and not args.all: + if bool(bug[-1]) and not args.debug: continue - # Print all (except noisy stacktrace) + # Print all bug properties (except noisy stacktrace) print(", ".join(str(x) for x in bug if "\n" not in str(x))) + if n_bugs_unsubmitted > 0: + print("NOTE: You have bugs awaiting submission. Please run the report command") + return 0, _bugs_list diff --git a/ntclient/persistence/__init__.py b/ntclient/persistence/__init__.py index 717bd0e..8422701 100644 --- a/ntclient/persistence/__init__.py +++ b/ntclient/persistence/__init__.py @@ -8,3 +8,12 @@ Created on Sat Mar 23 13:09:07 2019 @author: shane """ +import os + +from ntclient import NUTRA_HOME + +# TODO: create and maintain prefs.json file? See if there's a library for that, lol + +PREFS_JSON = os.path.join(NUTRA_HOME, "prefs.json") + +# if diff --git a/ntclient/services/api/__init__.py b/ntclient/services/api/__init__.py index 0b1e89a..5571e40 100644 --- a/ntclient/services/api/__init__.py +++ b/ntclient/services/api/__init__.py @@ -9,15 +9,14 @@ import sqlite3 import requests -URL_API = "https://api.nutra.tk" +REQUEST_READ_TIMEOUT = 18 +REQUEST_CONNECT_TIMEOUT = 5 + # TODO: try all of these; cache (save in prefs.json) the one which works first URLS_API = ( "https://api.nutra.tk", - "https://216.218.216.163/api", # prod - "https://216.218.228.93/api", # dev + "http://216.218.216.163", # prod ) -REQUEST_READ_TIMEOUT = 18 -REQUEST_CONNECT_TIMEOUT = 5 class ApiClient: @@ -25,7 +24,7 @@ class ApiClient: def __init__( self, - host: str = URL_API, + host: str = URLS_API[0], ): self.host = host diff --git a/ntclient/services/api/mirrorcache.py b/ntclient/services/api/mirrorcache.py new file mode 100644 index 0000000..560484e --- /dev/null +++ b/ntclient/services/api/mirrorcache.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Wed Feb 14 18:58:12 2024 + +@author: shane +""" + +import requests + +from ntclient.services.api import ( + REQUEST_CONNECT_TIMEOUT, + REQUEST_READ_TIMEOUT, + URLS_API, +) + + +def cache_mirrors() -> bool: + """Cache mirrors""" + for mirror in URLS_API: + try: + _res = requests.get( + mirror, + timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT), + verify=mirror.startswith("https://"), + ) + + _res.raise_for_status() + print(f"INFO: mirror '{mirror}' SUCCEEDED! Saving it.") + return True + except requests.exceptions.ConnectionError: + print(f"INFO: mirror '{mirror}' failed") + + return False diff --git a/ntclient/services/bugs.py b/ntclient/services/bugs.py index 28d1e84..4266518 100644 --- a/ntclient/services/bugs.py +++ b/ntclient/services/bugs.py @@ -11,6 +11,7 @@ import traceback import ntclient.services.api from ntclient.persistence.sql.nt import sql as sql_nt +from ntclient.services.api.mirrorcache import cache_mirrors def insert(args: list, exception: Exception) -> None: @@ -52,6 +53,13 @@ def list_bugs() -> list: def submit_bugs() -> int: """Submit bug reports to developer, return n_submitted.""" + # Probe mirrors, cache best working one + is_mirror_alive = cache_mirrors() + if not is_mirror_alive: + print("ERROR: we couldn't find an active mirror, can't submit bugs.") + return -1 + + # Gather bugs for submission sql_bugs = sql_nt("SELECT * FROM bug WHERE submitted = 0") api_client = ntclient.services.api.ApiClient()