Add complete mockupAWS platform for AWS cost estimation: - FastAPI backend with scenario management - PostgreSQL database schema for scenarios, metrics, logs - AWS pricing table with real pricing data - React frontend dashboard (planned) - PII detection and token counting - Report generation (PDF/CSV) - Complete test suite with pytest - Docker Compose setup - Documentation: README, PRD, Architecture - OpenCode configuration (.opencode/)
27 lines
880 B
Python
27 lines
880 B
Python
import sys
|
|
import tiktoken
|
|
|
|
# Inizializziamo l'encoder globalmente.
|
|
# cl100k_base è lo standard di fatto per le misurazioni token moderne.
|
|
_encoder = tiktoken.get_encoding("cl100k_base")
|
|
|
|
def count_tokens(text: str) -> int:
|
|
"""
|
|
Calcola il numero esatto di token in ingresso per una data stringa.
|
|
Fondamentale per il calcolo accurato dei costi LLM.
|
|
"""
|
|
if not text:
|
|
return 0
|
|
return len(_encoder.encode(text))
|
|
|
|
def calculate_sqs_blocks(payload_json: str) -> int:
|
|
"""
|
|
Calcola i blocchi fatturabili per Amazon SQS.
|
|
AWS addebita 1 richiesta per ogni payload (o frammento) fino a 64 KB (65536 bytes).
|
|
"""
|
|
# sys.getsizeof restituisce la dimensione in byte della stringa in memoria
|
|
payload_size_bytes = sys.getsizeof(payload_json)
|
|
|
|
# Calcolo dei blocchi (divisione intera + 1)
|
|
return (payload_size_bytes // 65536) + 1
|