Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/document status check #376

Merged
merged 19 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions django_app/frontend/js/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Alpine.data('file-status', () => ({
const checkStatus = async () => {

// UPDATE THESE AS REQUIRED
const FILE_STATUS_ENDPOINT = '/api/get-status';
const FILE_STATUS_ENDPOINT = '/file-status';
const CHECK_INTERVAL_MS = 5000;

const response = await fetch(`${FILE_STATUS_ENDPOINT}?id=${this.$el.dataset.id}`);
const responseText = await response.text();
const responseText = await response.json();
if (response.ok) {
this.status = responseText;
this.status = responseText["status"];
}
if (responseText !== 'complete') {
if (this.status !== 'complete') {
window.setTimeout(checkStatus, CHECK_INTERVAL_MS);
}

Expand Down
7 changes: 7 additions & 0 deletions django_app/redbox_app/redbox_core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ def rag_chat(self, message_history: list[dict[str, str]], token: str) -> SimpleN
logger.debug("response_data: %s", response_data)

return response_data

def get_file_status(self, file_id: uuid, token: str) -> SimpleNamespace:
252afh marked this conversation as resolved.
Show resolved Hide resolved
url = f"{self.url}/file/{file_id}/status"
252afh marked this conversation as resolved.
Show resolved Hide resolved
response = requests.get(url, headers={"Authorization": token}, timeout=60)
response.raise_for_status()
response_data = response.json(object_hook=lambda d: SimpleNamespace(**d))
return response_data
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-05-15 15:20

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('redbox_core', '0006_file_core_file_uuid'),
]

operations = [
migrations.AlterField(
model_name='file',
name='processing_status',
field=models.CharField(choices=[('uploaded', 'Uploaded'), ('parsing', 'Parsing'), ('chunking', 'Chunking'), ('embedding', 'Embedding'), ('indexing', 'Indexing'), ('complete', 'Complete'), ('unknown', 'Unknown')]),
),
]
1 change: 1 addition & 0 deletions django_app/redbox_app/redbox_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ProcessingStatusEnum(models.TextChoices):
embedding = "embedding"
indexing = "indexing"
complete = "complete"
unknown = "unknown"


class File(UUIDPrimaryKeyBase, TimeStampedModel):
Expand Down
50 changes: 43 additions & 7 deletions django_app/redbox_app/redbox_core/views.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import logging
import os
import uuid
from urllib.error import HTTPError

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.exceptions import FieldError, ValidationError
from django.core.exceptions import FieldError, ObjectDoesNotExist, ValidationError
from django.core.files.uploadedfile import UploadedFile
from django.http import HttpRequest, HttpResponse
from django.http import HttpRequest, HttpResponse, JsonResponse
from django.shortcuts import redirect, render
from django.urls import reverse
from django.views.decorators.http import require_http_methods
from redbox_app.redbox_core.client import CoreApiClient
from redbox_app.redbox_core.models import ChatHistory, ChatMessage, ChatRoleEnum, File, ProcessingStatusEnum, User
from requests.exceptions import HTTPError
from yarl import URL

from redbox_app.redbox_core.client import CoreApiClient
from redbox_app.redbox_core.models import (
ChatHistory,
ChatMessage,
ChatRoleEnum,
File,
ProcessingStatusEnum,
User,
)

logger = logging.getLogger(__name__)

CHUNK_SIZE = 1024
Expand Down Expand Up @@ -92,7 +100,7 @@ def upload_view(request):
errors.append(f"File type {file_extension} not supported")

if not errors:
errors += injest_file(uploaded_file, request.user)
errors += ingest_file(uploaded_file, request.user)

if not errors:
return redirect(documents_view)
Expand All @@ -104,7 +112,7 @@ def upload_view(request):
)


def injest_file(uploaded_file: UploadedFile, user: User) -> list[str]:
def ingest_file(uploaded_file: UploadedFile, user: User) -> list[str]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh! Sorry about that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this was my fault

errors: list[str] = []
api = CoreApiClient(host=settings.CORE_API_HOST, port=settings.CORE_API_PORT)
try:
Expand All @@ -130,6 +138,7 @@ def injest_file(uploaded_file: UploadedFile, user: User) -> list[str]:
file.save()
return errors


@login_required
def remove_doc_view(request, doc_id: uuid):
file = File.objects.get(pk=doc_id)
Expand Down Expand Up @@ -200,6 +209,33 @@ def post_message(request: HttpRequest) -> HttpResponse:
return redirect(reverse(sessions_view, args=(session.id,)))


@require_http_methods(["GET"])
@login_required
def file_status_api_view(request: HttpRequest) -> JsonResponse:
file_id = request.GET.get("id", None)
252afh marked this conversation as resolved.
Show resolved Hide resolved
if not file_id:
logger.error("Error getting file object information - no file ID provided %s.")
return JsonResponse({"status": ProcessingStatusEnum.unknown.label})
try:
file = File.objects.get(pk=file_id)
except ObjectDoesNotExist as ex:
252afh marked this conversation as resolved.
Show resolved Hide resolved
logger.error("File object information not found in django - file does not exist %s.", file_id, exc_info=ex)
return JsonResponse({"status": ProcessingStatusEnum.unknown.label})
core_api = CoreApiClient(host=settings.CORE_API_HOST, port=settings.CORE_API_PORT)
token = request.user.get_bearer_token()
try:
core_file_status_response = core_api.get_file_status(file_id=file.core_file_uuid, token=token)
except HTTPError as ex:
logger.error("File object information from core not found - file does not exist %s.", file_id, exc_info=ex)
if not file.processing_status:
file.processing_status = ProcessingStatusEnum.unknown.label
file.save()
return JsonResponse({"status": file.processing_status})
file.processing_status = core_file_status_response.processing_status
file.save()
return JsonResponse({"status": file.get_processing_status_text()})


@require_http_methods(["GET"])
def health(_request: HttpRequest) -> HttpResponse:
"""this required by ECS Fargate"""
Expand Down
1 change: 1 addition & 0 deletions django_app/redbox_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
other_urlpatterns = [
path("", views.homepage_view, name="homepage"),
path("health/", views.health, name="health"),
path("file-status/", views.file_status_api_view, name="file-status"),
]

urlpatterns = (
Expand Down
Loading