Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nboyse committed Feb 7, 2025
1 parent 30e91bf commit 5b11001
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

env = get_settings()

es_client = env.elasticsearch_client()
es_client = env.opensearch_client()


class Command(BaseCommand):
help = """This is a one-off command to add an ElasticSearch alias to the existing chunks index."""
help = """This is a one-off command to add an OpenSearch alias to the existing chunks index."""

def handle(self, *args, **kwargs): # noqa:ARG002
existing_index = f"{env.elastic_root_index}-chunk"
existing_index = f"{env.opensearch_root_index}-chunk"
self.stdout.write(self.style.NOTICE(f"Creating the alias {existing_index}-current"))

if not es_client.indices.exists_alias(name=f"{existing_index}-current"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class Command(BaseCommand):
help = """This is a one-off command to back populate elastic logs."""
help = """This is a one-off command to back populate opensearch logs."""

def handle(self, *args, **kwargs): # noqa:ARG002
for chat_message in ChatMessage.objects.all():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

env = get_settings()

es_client = env.elasticsearch_client()
es_client = env.opensearch_client()


class Command(BaseCommand):
help = """
This is a command to change the aliased elasticsearch index after a reingestion.
This is a command to change the aliased opensearch index after a reingestion.
Eventually, this may be able to be combined into reingest_files,
but this allows for a manual check of reingestion before moving the alias.
The new_index should be available as an arg on the recent redbox_app.worker.ingest tasks in Django Admin.
"""

def add_arguments(self, parser):
default_alias = env.elastic_chunk_alias
default_alias = env.opensearch_chunk_alias

parser.add_argument("new_index", nargs="?", type=str)
parser.add_argument("alias", nargs="?", type=str, default=default_alias)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

env = get_settings()

es_client = env.elasticsearch_client()
es_client = env.opensearch_client()


class Command(BaseCommand):
help = """
This is a command to remove old ElasticSearch indexes after reingestion and realiasing.
This is a command to remove old OpenSearch indexes after reingestion and realiasing.
Eventually, this may be combined into reingest_files,
but this allows for a manual check of data before deletion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def handle(self, *_args, **_kwargs):
)

try:
file.delete_from_elastic()
file.delete_from_opensearch()
file.delete_from_s3()

except BotoCoreError as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

env = get_settings()

es_client = env.elasticsearch_client()
es_client = env.opensearch_client()


def switch_aliases(alias, new_index):
Expand Down Expand Up @@ -40,7 +40,7 @@ def add_arguments(self, parser):
def handle(self, *_args, **kwargs):
self.stdout.write(self.style.NOTICE("Reingesting active files from Django"))

new_index = f"{env.elastic_root_index}-chunk-{int(time.time())}"
new_index = f"{env.opensearch_root_index}-chunk-{int(time.time())}"

for file in File.objects.exclude(status__in=File.INACTIVE_STATUSES):
logger.debug("Reingesting file object %s", file)
Expand All @@ -52,4 +52,4 @@ def handle(self, *_args, **kwargs):
group="re-ingest",
sync=kwargs["sync"],
)
async_task(switch_aliases, env.elastic_chunk_alias, new_index, task_name="switch_aliases")
async_task(switch_aliases, env.opensearch_chunk_alias, new_index, task_name="switch_aliases")
12 changes: 6 additions & 6 deletions django_app/redbox_app/redbox_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

env = get_settings()

es_client = env.elasticsearch_client()
es_client = env.opensearch_client()


class UUIDPrimaryKeyBase(models.Model):
Expand Down Expand Up @@ -547,8 +547,8 @@ def delete_from_s3(self):
"""Manually deletes the file from S3 storage."""
self.original_file.delete(save=False)

def delete_from_elastic(self):
index = env.elastic_chunk_alias
def delete_from_opensearch(self):
index = env.opensearch_chunk_alias
if es_client.indices.exists(index=index):
es_client.delete_by_query(
index=index,
Expand Down Expand Up @@ -814,7 +814,7 @@ def get_messages_ordered_by_citation_priority(cls, chat_id: uuid.UUID) -> Sequen

def log(self):
token_sum = sum(token_use.token_count for token_use in self.chatmessagetokenuse_set.all())
elastic_log_msg = {
opensearch_log_msg = {
"@timestamp": self.created_at.isoformat(),
"id": str(self.id),
"chat_id": str(self.chat.id),
Expand All @@ -828,9 +828,9 @@ def log(self):
"rating_chips": list(map(str, self.rating_chips)) if self.rating_chips else None,
}
es_client.create(
index=env.elastic_chat_mesage_index,
index=env.opensearch_chat_mesage_index,
id=uuid.uuid4(),
body=elastic_log_msg,
body=opensearch_log_msg,
)

def unique_citation_uris(self) -> list[tuple[str, str]]:
Expand Down
2 changes: 1 addition & 1 deletion django_app/redbox_app/redbox_core/views/document_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def remove_doc_view(request, doc_id: uuid):

if request.method == "POST":
try:
file.delete_from_elastic()
file.delete_from_opensearch()
except Exception as e:
logger.exception("Error deleting file object %s.", file, exc_info=e)
errors.append("There was an error deleting this file")
Expand Down
2 changes: 1 addition & 1 deletion django_app/redbox_app/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def ingest(file_id: UUID, es_index: str | None = None) -> None:
from redbox_app.redbox_core.models import File

if not es_index:
es_index = env.elastic_chunk_alias
es_index = env.opensearch_chunk_alias

file = File.objects.get(id=file_id)

Expand Down
8 changes: 4 additions & 4 deletions django_app/tests/management/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path
from unittest.mock import MagicMock, patch

import elasticsearch
from opensearchpy import OpenSearch
import pytest
from botocore.exceptions import UnknownClientMethodError
from django.conf import settings
Expand Down Expand Up @@ -106,10 +106,10 @@ def test_delete_expired_files(uploaded_file: File, last_referenced: datetime, sh
assert is_deleted == should_delete


@patch("redbox_app.redbox_core.models.File.delete_from_elastic")
@patch("redbox_app.redbox_core.models.File.delete_from_opensearch")
@pytest.mark.django_db()
def test_delete_expired_files_with_elastic_error(deletion_mock: MagicMock, uploaded_file: File):
deletion_mock.side_effect = elasticsearch.BadRequestError(message="i am am error", meta=None, body=None)
def test_delete_expired_files_with_opensearch_error(deletion_mock: MagicMock, uploaded_file: File):
deletion_mock.side_effect = OpenSearch.BadRequestError(message="i am am error", meta=None, body=None)

# Given
mock_file = uploaded_file
Expand Down

0 comments on commit 5b11001

Please sign in to comment.