From b735a0841c385a1fb97f04e30c4039d33d6652ba Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 2 Feb 2025 23:56:14 -0800 Subject: [PATCH] Just use mtime and keep it fresh on every train --- src/documents/classifier.py | 5 +++++ src/documents/views.py | 23 +---------------------- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/src/documents/classifier.py b/src/documents/classifier.py index 72bf1f16c..008a4ce10 100644 --- a/src/documents/classifier.py +++ b/src/documents/classifier.py @@ -1,6 +1,8 @@ import logging +import os import pickle import re +import time import warnings from collections.abc import Iterator from hashlib import sha256 @@ -229,6 +231,9 @@ class DocumentClassifier: and self.last_doc_change_time >= latest_doc_change ) and self.last_auto_type_hash == hasher.digest(): logger.info("No updates since last training") + # Update the modification time of the file to mark it as fresh + new_mtime = time.time() + os.utime(settings.MODEL_FILE, (new_mtime, new_mtime)) # Set the classifier information into the cache # Caching for 50 minutes, so slightly less than the normal retrain time cache.set( diff --git a/src/documents/views.py b/src/documents/views.py index 92bb53a1b..410bb9e17 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -15,7 +15,6 @@ from urllib.parse import quote from urllib.parse import urlparse import pathvalidate -from django.apps import apps from django.conf import settings from django.contrib.auth.models import Group from django.contrib.auth.models import User @@ -2174,33 +2173,13 @@ class SystemStatusView(PassUserMixin): classifier_status = "WARNING" raise FileNotFoundError(classifier_error) classifier_status = "OK" - task_result_model = apps.get_model("django_celery_results", "taskresult") - result = ( - task_result_model.objects.filter( - task_name="documents.tasks.train_classifier", - status="SUCCESS", - ) - .order_by( - "-date_done", - ) - .first() - ) - classifier_last_auto_trained = result.date_done if result else None - classifier_last_modified = ( + classifier_last_trained = ( make_aware( datetime.fromtimestamp(settings.MODEL_FILE.stat().st_mtime), ) if settings.MODEL_FILE.exists() else None ) - classifier_last_trained = ( - max( - classifier_last_auto_trained, - classifier_last_modified, - ) - if classifier_last_auto_trained and classifier_last_modified - else classifier_last_auto_trained or classifier_last_modified - ) except Exception as e: if classifier_status is None: classifier_status = "ERROR"