Service Definition
The Statistics service is defined inkyber_api.proto:
User Search
Search User
Searches for users by name to retrieve their statistics. Endpoint:Statistics.SearchUser
Request:
Search query (username or partial username)
Array of matching users
API/internal/rpc/statistics.go:80
Search Behavior:
- If Elasticsearch is configured, uses fuzzy prefix matching with Elasticsearch
- Otherwise, falls back to MongoDB text search
- Returns empty array if query is less than 1 character
- Maximum 50 expansions for prefix matching
User’s EA/Kyber ID
EA persona ID
User’s display name
User’s region
Whether the user has played on Kyber
Statistics Retrieval
Get Stats
Retrieves player statistics from a specified source. Endpoint:Statistics.GetStats
Request:
Source to retrieve stats from (see StatsSource enum below)
User ID (required for KYBER and PERSONAL sources)
EA persona ID (required for EA_PC source)
Dictionary of stat names to values
API/internal/rpc/statistics.go:194
Stats Sources:
EA_PC (0)
Retrieves statistics from EA’s official servers for PC.- Caching: Results are cached in Redis to reduce EA API calls
- Requirements:
personaIdmust be provided - Service: Requires EA Bridge gRPC service (
KYBER_EA_BRIDGE) - Response Time: Fast if cached; slower on first request
KYBER (3)
Retrieves statistics accumulated from Kyber servers.- Caching: No caching; reads directly from database
- Requirements:
user(Kyber user ID) must be provided - Returns: Empty stats object if user has no Kyber stats
PERSONAL (4)
Retrieves personal statistics for the authenticated user.- Authentication: Required (reads stats for authenticated user only)
- Use Case: Private stat tracking per user
- Returns: Error if stats don’t exist
Statistics Management
Update Stats
Updates or creates statistics for a user. Endpoint:Statistics.UpdateStats
Request:
Source to update (currently only KYBER is supported)
User ID to update stats for
Complete stats dictionary with all stat keys and values
EntitlementOfficialStats for KYBER source)
Implementation: API/internal/rpc/statistics.go:279
Validation:
- Only
KYBERsource can be updated via this endpoint - User must have
EntitlementOfficialStatsentitlement - Stats must include all existing keys (cannot provide partial updates)
- Missing stat keys will result in
InvalidArgumenterror
Data Models
StatsSource Enum
Stats Dictionary
Stats are returned as a key-value map where:- Key: Stat identifier (e.g., “kills”, “deaths”, “wins”)
- Value: Numeric stat value (float)
EAUser
Caching Strategy
EA Stats Caching
EA statistics are cached using Redis to minimize external API calls:- Cache Key:
stats:{personaId}:{source} - TTL: Configurable (typically 15-30 minutes)
- Cache Miss: Fetches from EA Bridge service
- Cache Hit: Returns immediately from Redis
API/internal/cache/redis_stats.go
Cache Methods
Elasticsearch Integration
When configured, Elasticsearch provides fast user search: Index:users-index
Query Type: MatchPhrasePrefix with:
- Slop: 1 (allows one word gap)
- Max Expansions: 50 (limits prefix matching)
ELASTICSEARCH_URL environment variable
Fallback: If Elasticsearch is unavailable, falls back to MongoDB search
Error Codes
NOT_FOUND(5) - User or stats not foundINVALID_ARGUMENT(3) - Invalid source or insufficient stats providedPERMISSION_DENIED(7) - Missing required entitlementsINTERNAL(13) - Database, cache, or service communication errorUNAVAILABLE(14) - EA Bridge service not available
Service Dependencies
Required Services
- Database - MongoDB for storing Kyber/Personal stats
- Cache - Redis for EA stats caching
Optional Services
- EA Bridge (
KYBER_EA_BRIDGE) - Required for EA_PC stats - Elasticsearch (
ELASTICSEARCH_URL) - Enhances search performance
Implementation Notes
Stats Storage
Stats are stored in thestats collection with:
- UserID - Kyber user ID
- Source - StatsSource identifier
- Stats - Map of stat keys to values
- UpdatedBy - ID of user/service that updated stats
- UpdatedAt - Timestamp of last update
Stats Update Rules
-
Kyber Stats:
- Can only be updated by users with
EntitlementOfficialStats - Must provide complete stat set (all existing keys)
- Partial updates are rejected to prevent data corruption
- Can only be updated by users with
-
Personal Stats:
- Can be updated by the owner
- No key validation
-
EA Stats:
- Read-only via API
- Fetched from EA servers
- Cached for performance