-
Notifications
You must be signed in to change notification settings - Fork 59
feat: implemented datasources for google sheet #829
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
Open
cmd-err
wants to merge
2
commits into
juspay:release
Choose a base branch
from
cmd-err:feat/data-sources-integration
base: release
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
app/ai/voice/agents/breeze_buddy/managers/data_source_prefetch.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """ | ||
| Data Source Prefetch Manager | ||
|
|
||
| Pre-warms Redis with Google Sheets content for all DataSourceRefs attached to a | ||
| template at dispatch time. Runs concurrently with greeting TTS synthesis. | ||
|
|
||
| Cache key is scoped to the data_source_id (not lead_id) so that concurrent | ||
| calls referencing the same sheet share a single cached copy. | ||
|
|
||
| Cache key : ``datasource:content:{data_source_id}`` | ||
| TTL : 60 s (short: keeps data fresh, covers burst window) | ||
| """ | ||
|
|
||
| import asyncio | ||
| from typing import Optional | ||
|
|
||
| from app.ai.voice.agents.breeze_buddy.template.types import DataSourceRef, TemplateModel | ||
| from app.core.logger import logger | ||
| from app.database.accessor.breeze_buddy.data_source import get_data_source_by_id | ||
| from app.services.data_sources import data_source_in_template_scope | ||
| from app.services.google.sheets import fetch_formatted | ||
| from app.services.redis import get_redis_service | ||
|
|
||
| _CACHE_TTL = 60 # seconds — shared across leads; short to keep data fresh | ||
| _FETCH_TIMEOUT = 5.0 # generous timeout for background prefetch | ||
|
|
||
|
|
||
| async def _prefetch_one( | ||
| lead_id: str, template: TemplateModel, ref: DataSourceRef | ||
| ) -> None: | ||
| """Fetch and cache content for a single DataSourceRef.""" | ||
| cache_key = f"datasource:content:{ref.data_source_id}" | ||
| try: | ||
| ds = await get_data_source_by_id(ref.data_source_id) | ||
| if not ds: | ||
| logger.warning( | ||
| "Prefetch: data source %s not found in DB (ref name=%s)", | ||
| ref.data_source_id, | ||
| ref.name, | ||
| ) | ||
| return | ||
| if not data_source_in_template_scope( | ||
| ds, template.reseller_id, template.merchant_id | ||
| ): | ||
| logger.warning( | ||
| "Prefetch: data source %s is inactive or outside template scope " | ||
| "(template=%s, ref name=%s)", | ||
| ref.data_source_id, | ||
| template.id, | ||
| ref.name, | ||
| ) | ||
| return | ||
|
|
||
| content = await asyncio.wait_for( | ||
| fetch_formatted( | ||
| spreadsheet_id=ds.spreadsheet_id, | ||
| sheet_name=ds.sheet_name, | ||
| columns=ds.columns, | ||
| format=ds.format, | ||
| ), | ||
| timeout=_FETCH_TIMEOUT, | ||
| ) | ||
|
|
||
| redis = await get_redis_service() | ||
| await redis.setex(cache_key, content, ttl_seconds=_CACHE_TTL) | ||
| logger.info( | ||
| "Prefetched data source '%s' (ds=%s, %d chars, TTL=%ds)", | ||
| ref.name, | ||
| ref.data_source_id, | ||
| len(content), | ||
| _CACHE_TTL, | ||
| ) | ||
| except asyncio.TimeoutError: | ||
| logger.warning( | ||
| "Prefetch timeout for data source '%s' (ds=%s)", | ||
| ref.name, | ||
| ref.data_source_id, | ||
| ) | ||
| except Exception as exc: | ||
| logger.error( | ||
| "Prefetch error for data source '%s' (ds=%s): %s", | ||
| ref.name, | ||
| ref.data_source_id, | ||
| exc, | ||
| exc_info=True, | ||
| ) | ||
|
|
||
|
|
||
| async def prefetch_data_sources( | ||
| lead_id: str, | ||
| template: Optional[TemplateModel], | ||
| ) -> None: | ||
| """ | ||
| Pre-warm Redis with sheet content for every DataSourceRef on *template*. | ||
|
|
||
| Safe to call even when template is None or has no data_sources — it | ||
| silently returns without doing any work. | ||
| """ | ||
| if not template or not template.data_sources: | ||
| return | ||
|
|
||
| await asyncio.gather( | ||
| *[_prefetch_one(lead_id, template, ref) for ref in template.data_sources], | ||
| return_exceptions=True, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check Redis
setexoutcome before logging prefetch success.RedisService.setex()returnsFalseon Redis write failures; the current path logs success even when the cache write did not persist. This can mask prefetch misses and mislead operational debugging.💡 Suggested patch
🤖 Prompt for AI Agents