Files
stonks-oracle/services/shared/redis_keys.py
T
Celes Renata bc077bfcc8
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build-2 Pipeline was successful
ci/woodpecker/push/build-3 Pipeline was successful
ci/woodpecker/push/build-1 Pipeline was successful
ci/woodpecker/push/finalize Pipeline was successful
Build and Push / lint-and-test (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.adapters.broker_adapter name:broker-adapter]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.aggregation.worker name:aggregation]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.extractor.worker name:extractor]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.ingestion.worker name:ingestion]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.lake_publisher.worker name:lake-publisher]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.parser.worker name:parser]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.recommendation.worker name:recommendation]) (push) Has been cancelled
Build and Push / build-services (map[cmd:python -m services.scheduler.app name:scheduler]) (push) Has been cancelled
Build and Push / build-services (map[cmd:uvicorn services.api.app:app --host 0.0.0.0 --port 8000 name:query-api]) (push) Has been cancelled
Build and Push / build-services (map[cmd:uvicorn services.risk.app:app --host 0.0.0.0 --port 8000 name:risk]) (push) Has been cancelled
Build and Push / build-services (map[cmd:uvicorn services.symbol_registry.app:app --host 0.0.0.0 --port 8000 name:symbol-registry]) (push) Has been cancelled
Build and Push / build-services (map[cmd:uvicorn services.trading.app:app --host 0.0.0.0 --port 8000 name:trading-engine]) (push) Has been cancelled
Build and Push / build-dashboard (push) Has been cancelled
Build and Push / build-superset (push) Has been cancelled
Build and Push / integration-test (push) Has been cancelled
Build and Push / beta-gate (push) Has been cancelled
feat: trading feedback engine — periodic performance reports with AI summarization
- Migration 038: trading_reports table + report-summarizer agent seed
- 6 reporting modules: models, collector, sections, validator, summarizer, generator
- API endpoints: GET /api/reports (paginated, filterable), GET /api/reports/{id}
- Frontend hooks: useReports, useReport with TanStack Query
- Scheduler: daily (after 16:30 ET) and weekly (Saturday) report triggers
- Redis queue consumer for async report generation with retry/dedup
- 5 property-based tests (chunking, serialization, validation, accuracy, deltas)
- 109 unit/integration tests across all modules
- 6 frontend hook tests with MSW mocks
2026-05-01 22:13:09 +00:00

109 lines
2.9 KiB
Python

"""Redis key conventions and queue abstractions."""
import os
# --- Key prefixes ---
_STAGE = os.getenv("DEPLOY_STAGE", "")
PREFIX = f"stonks:{_STAGE}" if _STAGE else "stonks"
# Distributed locks
LOCK_PREFIX = f"{PREFIX}:lock"
# Rate limit counters
RATE_LIMIT_PREFIX = f"{PREFIX}:ratelimit"
# Job queues
QUEUE_PREFIX = f"{PREFIX}:queue"
# Dedupe markers
DEDUPE_PREFIX = f"{PREFIX}:dedupe"
# Cache
CACHE_PREFIX = f"{PREFIX}:cache"
# Retry backoff state
RETRY_PREFIX = f"{PREFIX}:retry"
def lock_key(resource: str) -> str:
return f"{LOCK_PREFIX}:{resource}"
def rate_limit_key(source: str, window: str) -> str:
return f"{RATE_LIMIT_PREFIX}:{source}:{window}"
def queue_key(queue_name: str) -> str:
return f"{QUEUE_PREFIX}:{queue_name}"
def dedupe_key(content_hash: str) -> str:
return f"{DEDUPE_PREFIX}:{content_hash}"
def cache_key(namespace: str, key: str) -> str:
return f"{CACHE_PREFIX}:{namespace}:{key}"
def retry_key(job_id: str) -> str:
return f"{RETRY_PREFIX}:{job_id}"
# Dead-letter queues
DLQ_PREFIX = f"{PREFIX}:dlq"
def dlq_key(queue_name: str) -> str:
"""Return the dead-letter queue key for a given source queue."""
return f"{DLQ_PREFIX}:{queue_name}"
# --- Queue names ---
QUEUE_INGESTION = "ingestion"
QUEUE_PARSING = "parsing"
QUEUE_EXTRACTION = "extraction"
QUEUE_AGGREGATION = "aggregation"
QUEUE_RECOMMENDATION = "recommendation"
QUEUE_LAKE_PUBLISH = "lake_publish"
QUEUE_TRADE = "trade"
QUEUE_BROKER = "broker_orders"
QUEUE_MACRO_CLASSIFICATION = "macro_classification"
QUEUE_REPORT_GENERATION = "report_generation"
QUEUE_REPORT_GENERATION = "report_generation"
# --- Trading engine ---
QUEUE_TRADING_DECISIONS = "trading_decisions"
TRADING_DEDUPE_PREFIX = f"{PREFIX}:dedupe:trading"
TRADING_CB_PREFIX = f"{PREFIX}:trading:circuit_breaker"
TRADING_NOTIFICATION_RATE = f"{PREFIX}:trading:notification_rate"
def trading_dedupe_key(recommendation_id: str) -> str:
"""Return the deduplication key for a trading recommendation."""
return f"{TRADING_DEDUPE_PREFIX}:{recommendation_id}"
def trading_cb_key(trigger_type: str) -> str:
"""Return the circuit breaker state key for a given trigger type."""
return f"{TRADING_CB_PREFIX}:{trigger_type}"
def trading_notification_rate_key(channel: str) -> str:
"""Return the notification rate-limit key for a given channel."""
return f"{TRADING_NOTIFICATION_RATE}:{channel}"
# --- Pipeline toggle ---
PIPELINE_ENABLED_KEY = f"{PREFIX}:pipeline:enabled"
async def is_pipeline_enabled(rds: "redis.asyncio.Redis") -> bool: # type: ignore[name-defined] # noqa: F821
"""Check whether the pipeline is enabled via the Redis toggle.
Returns True (enabled) when the key is absent or set to anything
other than ``"0"``. Workers should call this at the top of each
loop iteration and sleep when it returns False.
"""
val = await rds.get(PIPELINE_ENABLED_KEY)
return val != "0"