Browse Source

fix circular import and show log of plugins loading on startup

Nick Sweeting 1 year ago
parent
commit
518c46b4ab

+ 4 - 3
archivebox/abid_utils/admin.py

@@ -10,12 +10,11 @@ from django.shortcuts import redirect
 
 from django_object_actions import DjangoObjectActions, action
 
-
-from api.auth import get_or_create_api_token
-
 from archivebox.misc.util import parse_date
+
 from .abid import ABID
 
+
 def highlight_diff(display_val: Any, compare_val: Any, invert: bool=False, color_same: str | None=None, color_diff: str | None=None):
     """highlight each character in red that differs with the char at the same index in compare_val"""
 
@@ -37,6 +36,8 @@ def highlight_diff(display_val: Any, compare_val: Any, invert: bool=False, color
     ))
 
 def get_abid_info(self, obj, request=None):
+    from archivebox.api.auth import get_or_create_api_token
+    
     try:
         #abid_diff = f' != obj.ABID: {highlight_diff(obj.ABID, obj.abid)} ❌' if str(obj.ABID) != str(obj.abid) else ' == .ABID ✅'
 

+ 38 - 0
archivebox/abid_utils/models.py

@@ -321,6 +321,44 @@ class ABIDModel(models.Model):
     def get_absolute_url(self):
         return self.api_docs_url
 
+
+
+class ModelWithHealthStats(models.Model):
+    num_uses_failed = models.PositiveIntegerField(default=0)
+    num_uses_succeeded = models.PositiveIntegerField(default=0)
+    
+    class Meta:
+        abstract = True
+    
+    def record_health_failure(self) -> None:
+        self.num_uses_failed += 1
+        self.save()
+
+    def record_health_success(self) -> None:
+        self.num_uses_succeeded += 1
+        self.save()
+        
+    def reset_health(self) -> None:
+        # move all the failures to successes when resetting so we dont lose track of the total count
+        self.num_uses_succeeded = self.num_uses_failed + self.num_uses_succeeded
+        self.num_uses_failed = 0
+        self.save()
+        
+    @property
+    def health(self) -> int:
+        total_uses = max((self.num_uses_failed + self.num_uses_succeeded, 1))
+        success_pct = (self.num_uses_succeeded / total_uses) * 100
+        return round(success_pct)
+
+
+
+
+
+
+
+
+
+
 ####################################################
 
 # Django helpers

+ 6 - 2
archivebox/abx/archivebox/__init__.py

@@ -32,9 +32,13 @@ def load_archivebox_plugins(pm, plugins_dict: Dict[str, Path]):
         for ab_plugin in archivebox_plugins_found:
             pm.register(ab_plugin)
             for hook in ab_plugin.hooks:
-                hook.__signature__ = hook.__class__.__signature__              # fix to make pydantic model usable as Pluggy plugin
+                try:
+                    # if hook is a pydantic class, fix its __signature__ to make it usable as a Pluggy plugin
+                    hook.__signature__ = hook.__class__.__signature__              # fix to make pydantic model usable as Pluggy plugin
+                except Exception:
+                    pass
                 pm.register(hook)
             LOADED_PLUGINS[plugin_module] = ab_plugin
             
-        # print(f'    √ Loaded plugin: {LOADED_PLUGINS}')
+        print(f'    √ Loaded plugin: {plugin_module} {len(archivebox_plugins_found) * "🧩"}')
     return LOADED_PLUGINS

+ 0 - 1
archivebox/api/v1_core.py

@@ -6,7 +6,6 @@ from typing import List, Optional, Union, Any
 from datetime import datetime
 
 from django.db.models import Q
-from django.shortcuts import get_object_or_404
 from django.core.exceptions import ValidationError
 from django.contrib.auth import get_user_model