from django.db import models from inventory.models import Dealer from django.contrib.auth.models import User from django.conf import settings from django.utils import timezone class ChatLog(models.Model): """ Handles storage and representation of chat logs between users and chatbot. This class is designed to store chat logs, which include user messages, chatbot responses, their associated dealer, and timestamps of when the chat entries are created. It supports linking chat logs to a specific dealer using a foreign key relationship. The class provides an easy and structured way to manage and retrieve historical chat data. :ivar dealer: The dealer associated with this chat log. :type dealer: Dealer :ivar user_message: The message sent by the user. :type user_message: str :ivar chatbot_response: The response generated by the chatbot. :type chatbot_response: str :ivar timestamp: The date and time when the chat log entry was created. :type timestamp: datetime """ dealer = models.ForeignKey(Dealer, on_delete=models.CASCADE, related_name='chatlogs', db_index=True) user_message = models.TextField() chatbot_response = models.TextField() timestamp = models.DateTimeField(auto_now_add=True, db_index=True) class Meta: ordering = ['-timestamp'] indexes = [ models.Index(fields=['dealer', 'timestamp']), ] def __str__(self): return f"{self.dealer.name}: {self.user_message[:50]}..." class AnalysisCache(models.Model): """ Model to cache analysis results for performance optimization. This model stores cached results of model analysis operations to improve performance for repeated queries. It includes a hash of the prompt, user information, dealer ID, timestamps, and the cached result in JSON format. :ivar prompt_hash: MD5 hash of the prompt + dealer_id + language :type prompt_hash: str :ivar user: The user who made the request (optional) :type user: User :ivar dealer_id: ID of the dealer associated with this cache entry :type dealer_id: int :ivar created_at: When the cache entry was created :type created_at: datetime :ivar updated_at: When the cache entry was last updated :type updated_at: datetime :ivar expires_at: When the cache entry expires :type expires_at: datetime :ivar result: The cached analysis result :type result: dict """ prompt_hash = models.CharField(max_length=64, db_index=True) user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True) dealer_id = models.IntegerField(null=True, blank=True, db_index=True) created_at = models.DateTimeField(default=timezone.now) updated_at = models.DateTimeField(auto_now=True) expires_at = models.DateTimeField(db_index=True) result = models.JSONField() class Meta: indexes = [ models.Index(fields=['prompt_hash', 'dealer_id']), models.Index(fields=['expires_at']), ] verbose_name_plural = "Analysis caches" def is_expired(self): """ Check if the cache entry has expired. :return: True if the cache entry has expired, False otherwise :rtype: bool """ return timezone.now() > self.expires_at def __str__(self): return f"Cache: {self.prompt_hash[:10]}... (Dealer: {self.dealer_id})"