From c117811a70ae78565f9ec7d6a368fd27ff1cad6d Mon Sep 17 00:00:00 2001 From: gecBurton Date: Wed, 13 Nov 2024 14:40:41 +0000 Subject: [PATCH 1/2] display error message --- django_app/redbox_app/redbox_core/models.py | 18 +++++++++++++---- .../redbox_core/views/document_views.py | 7 +------ redbox-core/redbox/loader/ingester.py | 20 ++++++++++--------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/django_app/redbox_app/redbox_core/models.py b/django_app/redbox_app/redbox_core/models.py index 3edfc82ce..1a9d7c3ed 100644 --- a/django_app/redbox_app/redbox_core/models.py +++ b/django_app/redbox_app/redbox_core/models.py @@ -635,10 +635,20 @@ def unique_name(self) -> str: return self.original_file.name def get_status_text(self) -> str: - return next( - (status[1] for status in File.Status.choices if self.status == status[0]), - "Unknown", - ) + permanent_error = f"Failed to extract text, pls contact {settings.CONTACT_EMAIL}" + temporary_error = "Failed to extract text, please try later" + if self.ingest_error: + temporary_error_substrings = [ + "ConnectionError", + "RateLimitError", + "ConnectTimeout", + "openai.InternalServerError", + ] + for substring in temporary_error_substrings: + if substring in self.ingest_error: + return temporary_error + return permanent_error + return dict(File.Status.choices).get(self.status, permanent_error) @property def expires_at(self) -> datetime: diff --git a/django_app/redbox_app/redbox_core/views/document_views.py b/django_app/redbox_app/redbox_core/views/document_views.py index c9e08e8ea..08541112c 100644 --- a/django_app/redbox_app/redbox_core/views/document_views.py +++ b/django_app/redbox_app/redbox_core/views/document_views.py @@ -76,7 +76,6 @@ def get(self, request: HttpRequest) -> HttpResponse: @method_decorator(login_required) def post(self, request: HttpRequest) -> HttpResponse: errors: MutableSequence[str] = [] - ingest_errors: MutableSequence[str] = [] uploaded_files: MutableSequence[UploadedFile] = request.FILES.getlist("uploadDocs") @@ -89,11 +88,7 @@ def post(self, request: HttpRequest) -> HttpResponse: if not errors: for uploaded_file in uploaded_files: # ingest errors are handled differently, as the other documents have started uploading by this point - ingest_error = self.ingest_file(uploaded_file, request.user) - if ingest_error: - ingest_errors.append(f"{uploaded_file.file_name}: {ingest_error[0]}") - - request.session["ingest_errors"] = ingest_errors + request.session["ingest_errors"] = self.ingest_file(uploaded_file, request.user) return redirect(reverse("documents")) return self.build_response(request, errors) diff --git a/redbox-core/redbox/loader/ingester.py b/redbox-core/redbox/loader/ingester.py index bd8372e8e..d80a73870 100644 --- a/redbox-core/redbox/loader/ingester.py +++ b/redbox-core/redbox/loader/ingester.py @@ -50,7 +50,7 @@ def create_alias(alias: str): es.indices.put_alias(index=chunk_index_name, name=alias) -def ingest_file(file_name: str, es_index_name: str = alias) -> str | None: +def _ingest_file(file_name: str, es_index_name: str = alias): logging.info("Ingesting file: %s", file_name) es = env.elasticsearch_client() @@ -96,15 +96,17 @@ def ingest_file(file_name: str, es_index_name: str = alias) -> str | None: env=env, ) + new_ids = RunnableParallel({"normal": chunk_ingest_chain, "largest": large_chunk_ingest_chain}).invoke(file_name) + logging.info( + "File: %s %s chunks ingested", + file_name, + {k: len(v) for k, v in new_ids.items()}, + ) + + +def ingest_file(file_name: str, es_index_name: str = alias) -> str | None: try: - new_ids = RunnableParallel({"normal": chunk_ingest_chain, "largest": large_chunk_ingest_chain}).invoke( - file_name - ) - logging.info( - "File: %s %s chunks ingested", - file_name, - {k: len(v) for k, v in new_ids.items()}, - ) + _ingest_file(file_name, es_index_name) except Exception as e: logging.exception("Error while processing file [%s]", file_name) return f"{type(e)}: {e.args[0]}" From 0d3c85045ef9f3fed3b141a47ad5d01f97e24d44 Mon Sep 17 00:00:00 2001 From: Kevin Etchells Date: Wed, 13 Nov 2024 15:40:46 +0000 Subject: [PATCH 2/2] Add support message for errored docs --- .../js/web-components/documents/file-status.js | 1 + django_app/frontend/src/styles.scss | 16 ++++++++++++++-- django_app/redbox_app/redbox_core/models.py | 4 ++-- django_app/redbox_app/templates/documents.html | 6 ++++++ .../templates/macros/iai-doc-list.html | 2 +- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/django_app/frontend/src/js/web-components/documents/file-status.js b/django_app/frontend/src/js/web-components/documents/file-status.js index ff68d35a3..c9bdb13c6 100644 --- a/django_app/frontend/src/js/web-components/documents/file-status.js +++ b/django_app/frontend/src/js/web-components/documents/file-status.js @@ -12,6 +12,7 @@ class FileStatus extends HTMLElement { ); const responseObj = await response.json(); this.textContent = responseObj.status; + this.dataset.status = responseObj.status.toLowerCase(); if (responseObj.status.toLowerCase() === "complete") { const evt = new CustomEvent("doc-complete", { diff --git a/django_app/frontend/src/styles.scss b/django_app/frontend/src/styles.scss index 6c7b99250..9afd96e4f 100644 --- a/django_app/frontend/src/styles.scss +++ b/django_app/frontend/src/styles.scss @@ -120,14 +120,26 @@ upload-button { .rb-docs:has(.iai-doc-list__item) .rb-docs__show-if-no-docs { display: none; } +.rb-docs__upload-error { + align-items: center; + display: none; + font-size: 0.875rem; + gap: 0.75rem; + line-height: 1.4; +} +.rb-docs__upload-error svg { + color: var(--iai-product-colour); +} +.rb-docs:has(file-status[data-status*="error"]) .rb-docs__upload-error { + display: flex; +} /* Chats Page - to come after i.AI Design System */ @import "./chat-styles.scss"; @import "./profile-overlay-styles.scss"; - /* To move to i.AI design system */ .iai-chat-bubbles__sources-link:visited { color: var(--iai-product-colour, var(--iai-colour-brand)); -} \ No newline at end of file +} diff --git a/django_app/redbox_app/redbox_core/models.py b/django_app/redbox_app/redbox_core/models.py index 1a9d7c3ed..8207cef78 100644 --- a/django_app/redbox_app/redbox_core/models.py +++ b/django_app/redbox_app/redbox_core/models.py @@ -635,8 +635,8 @@ def unique_name(self) -> str: return self.original_file.name def get_status_text(self) -> str: - permanent_error = f"Failed to extract text, pls contact {settings.CONTACT_EMAIL}" - temporary_error = "Failed to extract text, please try later" + permanent_error = "Error" + temporary_error = "Error, please try again" if self.ingest_error: temporary_error_substrings = [ "ConnectionError", diff --git a/django_app/redbox_app/templates/documents.html b/django_app/redbox_app/templates/documents.html index 96206f676..8e8b603f4 100644 --- a/django_app/redbox_app/templates/documents.html +++ b/django_app/redbox_app/templates/documents.html @@ -69,6 +69,12 @@

Processing

docs = processing_files, type = "processing" ) }} +
+ +

You have documents that failed to process. Please try again, and if the problem persists contact redbox-support@cabinetoffice.gov.uk with details.

+

General actions

diff --git a/django_app/redbox_app/templates/macros/iai-doc-list.html b/django_app/redbox_app/templates/macros/iai-doc-list.html index 237a018a8..7f96a02fa 100644 --- a/django_app/redbox_app/templates/macros/iai-doc-list.html +++ b/django_app/redbox_app/templates/macros/iai-doc-list.html @@ -21,7 +21,7 @@ {% if type == "complete" %} {{ doc.get_status_text() }} {% else %} - {{ doc.get_status_text() }} + {{ doc.get_status_text() }} {% endif %}