]> git.mxchange.org Git - fba.git/blobdiff - fba/models/instances.py
Continued:
[fba.git] / fba / models / instances.py
index fe72a66f1a59b6e840a5cb2b52c55a7440c81724..e86128181f8dd487a299d40412f6a4502e9c039b 100644 (file)
@@ -62,13 +62,17 @@ _pending = {
     "last_blocked"       : {},
     # Last nodeinfo (fetched)
     "last_nodeinfo"      : {},
+    # Last response time
+    "last_response_time" : {},
     # Last status code
     "last_status_code"   : {},
     # Last error details
     "last_error_details" : {},
     # Wether obfuscation has been used
     "has_obfuscation"    : {},
-    # Determined software
+    # Original software
+    "original_software"  : {},
+    # Aliased software
     "software"           : {},
 }
 
@@ -82,6 +86,8 @@ def _set_data(key: str, domain: str, value: any):
         raise ValueError("Parameter 'key' is empty")
     elif not key in _pending:
         raise ValueError(f"key='{key}' not found in _pending")
+    elif blacklist.is_blacklisted(domain):
+        raise Exception(f"domain='{domain}' is blacklisted but function has been invoked")
     elif not utils.is_primitive(value):
         raise ValueError(f"value[]='{type(value)}' is not a primitive type")
 
@@ -94,10 +100,17 @@ def has_pending(domain: str) -> bool:
     logger.debug("domain='%s' - CALLED!", domain)
     domain_helper.raise_on(domain)
 
+    if not is_registered(domain):
+        raise ValueError(f"domain='{domain}' is not registered but function was invoked.")
+    elif blacklist.is_blacklisted(domain):
+        raise Exception(f"domain='{domain}' is blacklisted but function has been invoked")
+
     has = False
+    logger.debug("Checking %d _pending array elements ...", len(_pending))
     for key in _pending:
-        logger.debug("key='%s',domain='%s',_pending[key]()=%d", key, domain, len(_pending[key]))
+        logger.debug("domain='%s',_pending[%s]()=%d", domain, key, len(_pending[key]))
         if domain in _pending[key]:
+            logger.debug("domain='%s' at key='%s' has pending data ...", domain, key)
             has = True
             break
 
@@ -107,14 +120,18 @@ def has_pending(domain: str) -> bool:
 def update(domain: str):
     logger.debug("domain='%s' - CALLED!", domain)
     domain_helper.raise_on(domain)
-    if not has_pending(domain):
-        raise Exception(f"domain='{domain}' has no pending instance data, but function invoked")
-    elif not is_registered(domain):
+
+    if not is_registered(domain):
         raise Exception(f"domain='{domain}' cannot be updated while not being registered")
+    elif not has_pending(domain):
+        raise Exception(f"domain='{domain}' has no pending instance data, but function invoked")
+    elif blacklist.is_blacklisted(domain):
+        raise Exception(f"domain='{domain}' is blacklisted but function has been invoked")
 
-    logger.debug("Updating instance data for domain='%s' ...", domain)
     sql_string = ""
     fields = list()
+
+    logger.debug("Checking %d _pending array elements ...", len(_pending))
     for key in _pending:
         logger.debug("Checking key='%s',domain='%s'", key, domain)
         if domain in _pending[key]:
@@ -171,6 +188,8 @@ def add(domain: str, origin: str, command: str, path: str = None, software: str
         raise ValueError(f"path[]='{type(path)}' is not of type 'str'")
     elif path == "":
         raise ValueError("Parameter 'path' is empty")
+    elif path is not None and not path.startswith("/"):
+        raise ValueError(f"path='{path}' does not start with / but should")
     elif not isinstance(software, str) and software is not None:
         raise ValueError(f"software[]='{type(software)}' is not of type 'str'")
     elif software == "":
@@ -195,19 +214,22 @@ def add(domain: str, origin: str, command: str, path: str = None, software: str
     logger.debug("Determined software='%s'", software)
     if software == "lemmy" and domain.find("/c/") > 0:
         domain = domain.split("/c/")[0]
+
+        logger.debug("domain='%s' - LEMMY /c/ !", domain)
         if is_registered(domain):
             logger.warning("domain='%s' already registered after cutting off user part. - EXIT!", domain)
             return
 
-    logger.info("Adding instance domain='%s',origin='%s',software='%s',command='%s'", domain, origin, software, command)
+    logger.info("Adding instance domain='%s',origin='%s',software='%s',command='%s' ...", domain, origin, software, command)
     database.cursor.execute(
-        "INSERT INTO instances (domain, origin, command, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)",
+        "INSERT INTO instances (domain, origin, command, hash, software, original_software, first_seen) VALUES (?, ?, ?, ?, ?, ?, ?)",
         (
            domain,
            origin,
            command,
            utils.get_hash(domain),
            software,
+           software,
            time.time()
         ),
     )
@@ -254,6 +276,14 @@ def set_last_error(domain: str, error: dict):
         logger.debug("Setting last_error_details='%s' (error_message)", error['error_message'])
         _set_data("last_status_code"  , domain, error["status_code"])
         _set_data("last_error_details", domain, error["error_message"] if error["error_message"] != "" else None)
+    elif "json" in error and "error" in error["json"] and "msg" in error["json"]:
+        logger.debug("Setting last_error_details='%s' (json,error)", error["json"]["msg"])
+        _set_data("last_status_code"  , domain, error["status_code"])
+        _set_data("last_error_details", domain, error["json"]["msg"] if error["json"]["msg"] != "" else None)
+    elif "json" in error and "error" in error["json"] and "message" in error["json"]["error"]:
+        logger.debug("Setting last_error_details='%s' (json,error)", error["json"]["error"]["message"])
+        _set_data("last_status_code"  , domain, error["status_code"])
+        _set_data("last_error_details", domain, error["json"]["error"]["message"] if error["json"]["error"]["message"] != "" else None)
     elif "json" in error and "error" in error["json"]:
         logger.debug("Setting last_error_details='%s' (json,error)", error["json"]["error"])
         _set_data("last_status_code"  , domain, error["status_code"])
@@ -276,7 +306,11 @@ def set_success(domain: str):
 
 def is_registered(domain: str, skip_raise = False) -> bool:
     logger.debug("domain='%s',skip_raise='%s' - CALLED!", domain, skip_raise)
-    if not isinstance(skip_raise, bool):
+    domain_helper.raise_on(domain)
+
+    if blacklist.is_blacklisted(domain):
+        raise Exception(f"domain='{domain}' is blacklisted but function has been invoked")
+    elif not isinstance(skip_raise, bool):
         raise ValueError(f"skip_raise[]='{type(skip_raise)}' is not type of 'bool'")
 
     if not skip_raise:
@@ -339,8 +373,6 @@ def deobfuscate(char: str, domain: str, blocked_hash: str = None) -> tuple:
         raise ValueError(f"char='{char}' not found in domain='{domain}' but function invoked")
     elif not isinstance(domain, str):
         raise ValueError(f"Parameter domain[]='{type(domain)}'")
-    elif domain == "":
-        raise ValueError("Parameter 'domain' is empty")
     elif not isinstance(blocked_hash, str) and blocked_hash is not None:
         raise ValueError(f"Parameter blocked_hash[]='{type(blocked_hash)}' is not of type 'str'")
 
@@ -398,6 +430,19 @@ def set_last_instance_fetch(domain: str):
     _set_data("last_instance_fetch", domain, time.time())
     logger.debug("EXIT!")
 
+def set_last_response_time(domain: str, response_time: float):
+    logger.debug("domain='%s',response_time=%d - CALLED!", domain, response_time)
+    domain_helper.raise_on(domain)
+
+    if not isinstance(response_time, float):
+        raise ValueError(f"response_time[]='{type(response_time)}' is not of type 'float'")
+    elif response_time < 0:
+        raise ValueError(f"response_time={response_time} is below zero")
+
+    # Set timestamp
+    _set_data("last_response_time", domain, response_time)
+    logger.debug("EXIT!")
+
 def set_total_peers(domain: str, peers: list):
     logger.debug("domain='%s',peers()=%d - CALLED!", domain, len(peers))
     domain_helper.raise_on(domain)
@@ -441,6 +486,8 @@ def set_nodeinfo_url(domain: str, url: str):
         raise ValueError(f"Parameter url[]='{type(url)}' is not of type 'str'")
     elif url == "":
         raise ValueError("Parameter 'url' is empty")
+    elif url is not None and not validators.url(url):
+        raise ValueError(f"Parameter url='{url}' is not a valid URL")
 
     # Set timestamp
     _set_data("nodeinfo_url", domain, url)
@@ -470,6 +517,20 @@ def set_has_obfuscation(domain: str, status: bool):
     _set_data("has_obfuscation", domain, status)
     logger.debug("EXIT!")
 
+def set_original_software(domain: str, software: str):
+    logger.debug("domain='%s',software='%s' - CALLED!", domain, software)
+    domain_helper.raise_on(domain)
+
+    if not isinstance(software, str) and software is not None:
+        raise ValueError(f"Parameter software[]='{type(software)}' is not of type 'str'")
+    elif software == "":
+        raise ValueError("Parameter 'software' is empty")
+
+    # Set original software
+    _set_data("original_software", domain, software)
+    logger.debug("EXIT!")
+
+
 def set_software(domain: str, software: str):
     logger.debug("domain='%s',software='%s' - CALLED!", domain, software)
     domain_helper.raise_on(domain)
@@ -479,7 +540,7 @@ def set_software(domain: str, software: str):
     elif software == "":
         raise ValueError("Parameter 'software' is empty")
 
-    # Set timestamp
+    # Set software (maybe aliased to generic name)
     _set_data("software", domain, software)
     logger.debug("EXIT!")
 
@@ -531,6 +592,7 @@ def translate_idnas(rows: list, column: str):
                 logger.warning("Deleting row[%s]='%s' as translated='%s' already exist", column, row[column], translated)
                 database.cursor.execute(f"DELETE FROM instances WHERE {column} = ? LIMIT 1", [row[column]])
             else:
+                logger.debug("Updating row[%s]='%s' to translated='%s' ...", column, row[column], translated)
                 database.cursor.execute(f"UPDATE instances SET {column} = ? WHERE {column} = ? LIMIT 1", [translated, row[column]])
 
             logger.debug("Invoking commit() ...")