]> Nutra Git (v2) - nutratech/cli.git/commitdiff
wip cache mirrors
authorShane Jaroch <chown_tee@proton.me>
Thu, 15 Feb 2024 00:30:09 +0000 (19:30 -0500)
committerShane Jaroch <chown_tee@proton.me>
Thu, 15 Feb 2024 00:30:09 +0000 (19:30 -0500)
ntclient/argparser/__init__.py
ntclient/argparser/funcs.py
ntclient/persistence/__init__.py
ntclient/services/api/__init__.py
ntclient/services/api/mirrorcache.py [new file with mode: 0644]
ntclient/services/bugs.py

index b4800d71a353f81e83aafc3a4c46e3b19ec72d90..92060413c560b0da57b548127750bfaaa4107b63 100644 (file)
@@ -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)
 
index d63a2f1deb9283350056d7bf831a1d623405720d..fd945ce1cd5ef8a877e77a29277827a1589be856 100644 (file)
@@ -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
 
 
index 717bd0e36e4cbc60e3a2dc41d7dfa5a5f02f7c1f..8422701f49e545270a111c069b65300601aa5a3b 100644 (file)
@@ -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
index 0b1e89a191d398b075031ef7cb311a3a28b8e222..5571e405ca142f71a853cfd36db156fbf937f1eb 100644 (file)
@@ -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 (file)
index 0000000..560484e
--- /dev/null
@@ -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
index 28d1e84fb64eed0b1cbb94a0984cd9e7967cd8b3..4266518b4907fadc98f3a6c154533ca41846d0c2 100644 (file)
@@ -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()