mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-18 11:51:41 +03:00
3400b8633f
This pull request refactors the get_brains_usages method in the
Analytics class. The changes include improving code readability,
optimizing database queries, and fixing potential bugs.
<!--
ELLIPSIS_HIDDEN
-->
----
| <a href="https://ellipsis.dev" target="_blank"><img
src="https://avatars.githubusercontent.com/u/80834858?s=400&u=31e596315b0d8f7465b3ee670f25cea677299c96&v=4"
alt="Ellipsis" width="30px" height="30px"/></a> | 🚀 This PR
description was created by [Ellipsis](https://www.ellipsis.dev) for
commit f0ad664673
. |
|--------|--------|
### Summary:
This PR optimizes the `get_brains_usages` method in the `Analytics`
class by reducing database calls and improving code readability.
**Key points**:
- Refactored `get_brains_usages` method in `Analytics` class.
- Optimized database query to fetch chat history in one go.
- Improved code readability and formatting.
----
Generated with ❤️ by [ellipsis.dev](https://www.ellipsis.dev)
<!--
ELLIPSIS_HIDDEN
-->
57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
from collections import defaultdict
|
|
from datetime import datetime, timedelta
|
|
from typing import Optional
|
|
from uuid import UUID
|
|
|
|
from models.settings import get_supabase_client
|
|
from modules.analytics.entity.analytics import BrainsUsages, Range, Usage
|
|
from modules.brain.service.brain_user_service import BrainUserService
|
|
|
|
brain_user_service = BrainUserService()
|
|
|
|
|
|
class Analytics:
|
|
def __init__(self):
|
|
supabase_client = get_supabase_client()
|
|
self.db = supabase_client
|
|
|
|
def get_brains_usages(
|
|
self, user_id: UUID, graph_range: Range, brain_id: Optional[UUID] = None
|
|
) -> BrainsUsages:
|
|
user_brains = brain_user_service.get_user_brains(user_id)
|
|
if brain_id is not None:
|
|
user_brains = [brain for brain in user_brains if brain.id == brain_id]
|
|
|
|
usage_per_day = defaultdict(int)
|
|
|
|
brain_ids = [brain.id for brain in user_brains]
|
|
chat_history = (
|
|
self.db.from_("chat_history")
|
|
.select("*")
|
|
.in_("brain_id", brain_ids)
|
|
.execute()
|
|
).data
|
|
|
|
for chat in chat_history:
|
|
message_time = datetime.strptime(
|
|
chat["message_time"], "%Y-%m-%dT%H:%M:%S.%f"
|
|
)
|
|
usage_per_day[message_time.date()] += 1
|
|
|
|
start_date = datetime.now().date() - timedelta(days=graph_range)
|
|
all_dates = [start_date + timedelta(days=i) for i in range(graph_range)]
|
|
|
|
for date in all_dates:
|
|
usage_per_day[date] += 0
|
|
|
|
usages = sorted(
|
|
[
|
|
Usage(date=date, usage_count=count)
|
|
for date, count in usage_per_day.items()
|
|
if start_date <= date <= datetime.now().date()
|
|
],
|
|
key=lambda usage: usage.date,
|
|
)
|
|
|
|
return BrainsUsages(usages=usages)
|