Skip to content

Commit

Permalink
Improved logging and Pulsar client settings
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaam committed May 4, 2024
1 parent 7759306 commit b82ce6d
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 71 deletions.
32 changes: 0 additions & 32 deletions babblebox/babblebox/api/clients/consumer.py

This file was deleted.

18 changes: 10 additions & 8 deletions babblebox/babblebox/api/clients/pulsar_client_avro.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import logging
import pulsar
from avro.io import DatumWriter, BinaryEncoder
import io
from django.conf import settings

from pulsar.schema import BytesSchema

topic = settings.NEW_MESSAGE_TOPIC
client = None
producer = None
if settings.DISABLE_PULSAR_CLIENT is True:
print("Pulsar is enabled")
if settings.DISABLE_PULSAR_CLIENT is False:
logging.info("Pulsar is enabled")
try:
client = pulsar.Client(settings.PULSAR_URL)
print("No client created")
producer = client.create_producer(topic, schema=BytesSchema())
except Exception as e:
print("Exception raised" + str(e))
logging.error("Exception raised" + str(e))
else:
logging.info("Pulsar is disabled")

class PulsarClient:
@classmethod
# Function to serialize Django model to Avro
def serialize_to_avro(cls, data, avro_schema):
writer = DatumWriter(avro_schema)
bytes_writer = io.BytesIO()
Expand All @@ -33,6 +33,8 @@ def send_message(cls, data, schema):
try:
serialized_data = PulsarClient.serialize_to_avro(data, schema)
producer.send(serialized_data)
print("Message sent to pulsar for new transcription")
logging.info(f"Message sent to pulsar for new transcription: {data}")
except Exception as e:
print("Exception raised" + str(e))
# convert below line to better string formatting
logging.error("Failed sending pulsar message with data: {}".format(data))
logging.error("Exception raised: " + str(e))
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
{
"name": "timestamp",
"type": "string"
},
{
"name": "owner",
"type": "int"
},
{
"name": "owner_username",
"type": "string"
}
]
}
}
9 changes: 3 additions & 6 deletions babblebox/babblebox/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.conf import settings

from config.settings.base import BASE_DIR
from babblebox.api.whisper.whisper import get_transcription


class AudioFile(models.Model):
Expand All @@ -24,7 +23,7 @@ def save(self, *args, **kwargs):
# Generate a unique ID
self.id = str(uuid.uuid4())
super(AudioFile, self).save(*args, **kwargs)
print(self.audio)
logging.info(f"Uploading audio file: ${self.audio}")
# Generate transcription after the file is saved
# Update the transcription fields
self.file_location = settings.MEDIA_ROOT + "/" + str(self.audio)
Expand Down Expand Up @@ -121,12 +120,10 @@ def get_messages_for_chat(cls, chat_id):

@classmethod
def get_avro_schema(cls):
schema_file = os.path.join(BASE_DIR, 'babblebox/api/schemas/chat_message_schema.avsc')
print(BASE_DIR)
print(schema_file)
schema_file = os.path.join(BASE_DIR, 'babblebox/api/clients/schemas/chat_message_schema.avsc')
logging.info(f"Schema file path: {schema_file}")
with open(schema_file, 'r') as file:
schema = parse(file.read())
print(schema)
return schema


Expand Down
3 changes: 1 addition & 2 deletions babblebox/babblebox/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,14 @@ class ChatViewSet(viewsets.ModelViewSet):

def perform_create(self, serializer):
with transaction.atomic():
print("Creating chat")
# Save the Chat instance created by the serializer
# Assuming the request includes the owner information.
# You may need to adjust this based on how your owner is determined (e.g., from the request user)
owner = self.request.user
chat = serializer.save(owner=owner)

# Create a Participant instance for the owner with the necessary flags
ChatParticipant.objects.create(chat=chat, user=owner, has_read_access=True, has_write_access=True)
logger.info(f"Chat created with id: {chat.id}")


def get_permissions(self):
Expand Down
Empty file.
18 changes: 0 additions & 18 deletions babblebox/babblebox/api/whisper/whisper.py

This file was deleted.

7 changes: 4 additions & 3 deletions babblebox/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
import os
import environ
import logging

BASE_DIR = Path(__file__).resolve(strict=True).parent.parent.parent

Expand All @@ -13,17 +14,17 @@
env = environ.Env()

if env.ENVIRON.get("DJANGO_READ_LOCAL_ENV_FILE", default=False):
print(f"Using local env file. BASE_DIR: {BASE_DIR}")
logging.info(f"Using local env file. BASE_DIR: {BASE_DIR}")
environ.Env.read_env(os.path.join(BASE_DIR, ".envs/.local/.django"))
environ.Env.read_env(os.path.join(BASE_DIR, ".envs/.local/.postgres"))
print(f"Using Database: {env.db('DATABASE_URL')['HOST']}")
logging.info(f"Using Database: {env.db('DATABASE_URL')['HOST']}")


# GENERAL
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = True
DISABLE_PULSAR_CLIENT = env("DISABLE_PULSAR_CLIENT", default=False)
DISABLE_PULSAR_CLIENT = env("DISABLE_PULSAR_CLIENT", default=True)

# Local time zone. Choices are
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
Expand Down
3 changes: 2 additions & 1 deletion babblebox/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import os
import sys
from pathlib import Path
import logging

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
print(f"Starting local server from path: {os.getcwd()}")
logging.info(f"Starting local server from path: {os.getcwd()}")
try:
from django.core.management import execute_from_command_line
except ImportError:
Expand Down

0 comments on commit b82ce6d

Please sign in to comment.