-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnfl_data_models.py
More file actions
563 lines (471 loc) · 18.5 KB
/
nfl_data_models.py
File metadata and controls
563 lines (471 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
"""
NFL Data Pipeline - Core Data Models and Configuration
Defines all data structures, enums, and configuration for the real-time NFL pipeline
"""
from dataclasses import dataclass, field
from datetime import datetime
from typing import Dict, List, Optional, Any, Union
from enum import Enum
import hashlib
import json
# ===== CONFIGURATION CLASS =====
class Config:
"""
Central configuration for the NFL data pipeline system
Contains all performance targets, API endpoints, and operational parameters
"""
# Database connection strings
REDIS_URL = "redis://localhost:6379"
POSTGRES_URL = "postgresql://user:pass@localhost:5432/nfl_data"
# External API endpoints for data sources
ESPN_API_BASE = "https://site.api.espn.com/apis/site/v2/sports/football/nfl"
NFL_COM_BASE = "https://api.nfl.com/v1"
PFR_BASE = "https://www.pro-football-reference.com"
# Performance and reliability targets
MAX_LATENCY_MS = 200 # Target latency from source to processing
DAILY_UPDATE_VOLUME = 10000 # Expected player updates per game day
TARGET_UPTIME = 99.9 # Uptime percentage during NFL Sundays
RATE_LIMIT_PER_MINUTE = 300 # API calls per minute limit
# Processing configuration
MAX_RETRIES = 3 # Maximum retry attempts for failed requests
BATCH_SIZE = 1000 # Batch size for bulk operations
UPDATE_THRESHOLD_SECONDS = 30 # Real-time update frequency
HISTORICAL_SEASONS = 3 # Seasons of historical data to maintain
# Data validation thresholds
CONFIDENCE_THRESHOLD = 0.85 # Minimum confidence for stat validation
DEVIATION_THRESHOLD = 0.10 # Maximum allowed variance between sources
# Queue configuration
HIGH_PRIORITY_QUEUE = "nfl:queue:high"
NORMAL_PRIORITY_QUEUE = "nfl:queue:normal"
LOW_PRIORITY_QUEUE = "nfl:queue:low"
# ===== ENUMS =====
class DataSource(Enum):
"""
Enumeration of all supported data sources with reliability weights
Used for data validation and source prioritization
"""
ESPN = "espn"
NFL_COM = "nfl_com"
PRO_FOOTBALL_REFERENCE = "pfr"
PRO_FOOTBALL_FOCUS = "pff"
FANTASY_PROS = "fantasy_pros"
class StatCategory(Enum):
"""
Categories of NFL statistics for proper routing and processing
"""
PASSING = "passing"
RUSHING = "rushing"
RECEIVING = "receiving"
DEFENSE = "defense"
KICKING = "kicking"
SPECIAL_TEAMS = "special_teams"
class GameStatus(Enum):
"""
Possible game states for real-time processing prioritization
"""
SCHEDULED = "scheduled"
IN_PROGRESS = "in_progress"
HALFTIME = "halftime"
FINAL = "final"
POSTPONED = "postponed"
CANCELLED = "cancelled"
class InjuryStatus(Enum):
"""
Standard NFL injury report designations
"""
HEALTHY = "healthy"
QUESTIONABLE = "questionable"
DOUBTFUL = "doubtful"
OUT = "out"
INJURED_RESERVE = "ir"
PHYSICALLY_UNABLE = "pup"
class TrendDirection(Enum):
"""
Statistical trend analysis directions
"""
INCREASING = "increasing"
DECREASING = "decreasing"
STABLE = "stable"
VOLATILE = "volatile"
# ===== CORE DATA MODELS =====
@dataclass
class PlayerUpdate:
"""
Represents a single player statistic update from any data source
Contains all relevant information for processing and validation
"""
# Player identification
player_id: str
name: str
team: str
position: str
# Game context
game_id: Optional[str] = None
week: Optional[int] = None
season: Optional[int] = None
# Core statistics (flexible dict for different stat types)
stats: Dict[str, Any] = field(default_factory=dict)
# Advanced metrics
snap_count: Optional[int] = None
team_snaps: Optional[int] = None
targets: Optional[int] = None
air_yards: Optional[int] = None
# Injury and status information
injury_status: Optional[str] = None
active_status: bool = True
# Data provenance and quality
timestamp: datetime = field(default_factory=datetime.now)
source: str = DataSource.ESPN.value
confidence_score: float = 1.0
def to_hash(self) -> str:
"""
Generate unique hash for deduplication
Used to prevent duplicate processing of identical updates
"""
# Create content string for hashing (exclude timestamp for consistency)
content = f"{self.player_id}_{self.game_id}_{json.dumps(self.stats, sort_keys=True)}"
return hashlib.md5(content.encode()).hexdigest()
def get_fantasy_points(self, scoring_system: str = "ppr") -> float:
"""
Calculate fantasy points based on stats and scoring system
Supports PPR, Half-PPR, and Standard scoring
"""
points = 0.0
# Passing points
if "passing_yards" in self.stats:
points += self.stats["passing_yards"] * 0.04 # 1 point per 25 yards
if "passing_tds" in self.stats:
points += self.stats["passing_tds"] * 4
if "interceptions" in self.stats:
points -= self.stats["interceptions"] * 2
# Rushing points
if "rushing_yards" in self.stats:
points += self.stats["rushing_yards"] * 0.1 # 1 point per 10 yards
if "rushing_tds" in self.stats:
points += self.stats["rushing_tds"] * 6
# Receiving points
if "receiving_yards" in self.stats:
points += self.stats["receiving_yards"] * 0.1
if "receiving_tds" in self.stats:
points += self.stats["receiving_tds"] * 6
if "receptions" in self.stats and scoring_system == "ppr":
points += self.stats["receptions"] # PPR bonus
elif "receptions" in self.stats and scoring_system == "half_ppr":
points += self.stats["receptions"] * 0.5
return round(points, 2)
@dataclass
class GameData:
"""
Real-time game information and context
Essential for understanding player performance context
"""
# Game identification
game_id: str
week: int
season: int
# Team information
home_team: str
away_team: str
home_score: int = 0
away_score: int = 0
# Game state
status: str = GameStatus.SCHEDULED.value
quarter: Optional[int] = None
time_remaining: Optional[str] = None
down: Optional[int] = None
distance: Optional[int] = None
field_position: Optional[str] = None
# Environmental factors
weather: Optional[Dict[str, Any]] = None
temperature: Optional[int] = None
wind_speed: Optional[int] = None
precipitation: Optional[str] = None
# Data tracking
last_updated: datetime = field(default_factory=datetime.now)
data_sources: List[str] = field(default_factory=list)
def is_active(self) -> bool:
"""Check if game is currently in progress and needs real-time updates"""
return self.status in [GameStatus.IN_PROGRESS.value, GameStatus.HALFTIME.value]
def get_game_context(self) -> Dict[str, Any]:
"""Get contextual information for player performance analysis"""
return {
"is_divisional": self._is_divisional_matchup(),
"is_primetime": self._is_primetime_game(),
"weather_impact": self._assess_weather_impact(),
"pace_factor": self._calculate_pace_factor()
}
def _is_divisional_matchup(self) -> bool:
"""Determine if this is a divisional game (affects player performance)"""
divisions = {
"AFC East": ["NE", "BUF", "MIA", "NYJ"],
"AFC North": ["PIT", "BAL", "CLE", "CIN"],
"AFC South": ["IND", "TEN", "HOU", "JAX"],
"AFC West": ["KC", "LV", "LAC", "DEN"],
"NFC East": ["DAL", "PHI", "NYG", "WAS"],
"NFC North": ["GB", "MIN", "CHI", "DET"],
"NFC South": ["NO", "TB", "ATL", "CAR"],
"NFC West": ["SF", "SEA", "LAR", "ARI"]
}
for division_teams in divisions.values():
if self.home_team in division_teams and self.away_team in division_teams:
return True
return False
def _is_primetime_game(self) -> bool:
"""Check if this is a primetime game (SNF, MNF, TNF)"""
# This would be determined by game time/day
# Simplified implementation
return self.game_id.endswith("_prime")
def _assess_weather_impact(self) -> str:
"""Assess weather impact on game (affects passing/kicking)"""
if not self.weather:
return "none"
if self.wind_speed and self.wind_speed > 15:
return "high"
elif self.precipitation and self.precipitation != "none":
return "medium"
elif self.temperature and self.temperature < 32:
return "medium"
else:
return "low"
def _calculate_pace_factor(self) -> float:
"""Calculate game pace factor based on score differential and time"""
# Simplified pace calculation
score_diff = abs(self.home_score - self.away_score)
if score_diff > 14:
return 1.2 # Fast pace due to comeback situation
elif score_diff < 3:
return 0.9 # Slower pace in close game
else:
return 1.0 # Normal pace
@dataclass
class InjuryReport:
"""
Player injury status and related information
Critical for lineup decisions and performance expectations
"""
player_id: str
player_name: str
team: str
# Injury details
status: str # Using InjuryStatus enum values
injury_type: str
body_part: str
severity: Optional[str] = None
# Timeline information
injury_date: Optional[datetime] = None
expected_return: Optional[datetime] = None
weeks_out: Optional[int] = None
# Practice participation
wednesday_practice: Optional[str] = None # DNP, Limited, Full
thursday_practice: Optional[str] = None
friday_practice: Optional[str] = None
# Data tracking
updated_at: datetime = field(default_factory=datetime.now)
source: str = DataSource.NFL_COM.value
reliability_score: float = 1.0
def get_game_probability(self) -> float:
"""
Estimate probability of player participating in upcoming game
Based on injury status and practice participation
"""
if self.status == InjuryStatus.OUT.value:
return 0.0
elif self.status == InjuryStatus.DOUBTFUL.value:
return 0.25
elif self.status == InjuryStatus.QUESTIONABLE.value:
# Adjust based on practice participation
if self.friday_practice == "Full":
return 0.75
elif self.friday_practice == "Limited":
return 0.50
elif self.friday_practice == "DNP":
return 0.25
else:
return 0.50 # Default for questionable
else:
return 1.0 # Healthy or probable
@dataclass
class PlayerSeasonStats:
"""
Comprehensive season-long statistics and analytics for a player
Includes advanced metrics, trends, and fantasy relevance
"""
# Player identification
player_id: str
name: str
team: str
position: str
season: int
# Game participation
games_played: int = 0
games_started: int = 0
total_snaps: int = 0
snap_percentage: float = 0.0
# Core statistics (accumulated over season)
total_stats: Dict[str, Any] = field(default_factory=dict)
# Fantasy metrics
total_fantasy_points: float = 0.0
fantasy_ppg: float = 0.0
fantasy_rank_position: Optional[int] = None
fantasy_rank_overall: Optional[int] = None
# Advanced receiving metrics (for skill position players)
target_share: float = 0.0
air_yards_share: float = 0.0
red_zone_targets: int = 0
red_zone_target_share: float = 0.0
# Efficiency metrics
yards_per_target: float = 0.0
yards_after_contact: float = 0.0
drop_rate: float = 0.0
# Trend analysis
last_4_weeks_avg: float = 0.0
last_8_weeks_avg: float = 0.0
trend_direction: str = TrendDirection.STABLE.value
consistency_score: float = 0.0 # Lower variance = higher consistency
# Weekly performance history
weekly_scores: List[float] = field(default_factory=list)
weekly_snaps: List[int] = field(default_factory=list)
weekly_targets: List[int] = field(default_factory=list)
# Data quality and validation
last_updated: datetime = field(default_factory=datetime.now)
data_sources: List[str] = field(default_factory=list)
confidence_score: float = 1.0
def calculate_consistency(self) -> float:
"""
Calculate player consistency score based on weekly performance variance
Higher score = more consistent performance
"""
if len(self.weekly_scores) < 4:
return 0.0
scores_array = np.array(self.weekly_scores)
mean_score = np.mean(scores_array)
std_score = np.std(scores_array)
# Coefficient of variation (lower is more consistent)
if mean_score > 0:
cv = std_score / mean_score
# Convert to 0-1 scale where 1 is most consistent
consistency = max(0.0, 1.0 - (cv / 2.0))
else:
consistency = 0.0
return round(consistency, 3)
def get_trend_analysis(self) -> Dict[str, Any]:
"""
Analyze recent performance trends
Returns trend direction and statistical significance
"""
if len(self.weekly_scores) < 6:
return {"trend": "insufficient_data", "confidence": 0.0}
recent_scores = self.weekly_scores[-6:] # Last 6 weeks
weeks = list(range(len(recent_scores)))
# Linear regression for trend
correlation = np.corrcoef(weeks, recent_scores)[0, 1]
if correlation > 0.3:
trend = TrendDirection.INCREASING.value
elif correlation < -0.3:
trend = TrendDirection.DECREASING.value
else:
# Check for volatility
cv = np.std(recent_scores) / (np.mean(recent_scores) + 0.01)
if cv > 0.5:
trend = TrendDirection.VOLATILE.value
else:
trend = TrendDirection.STABLE.value
return {
"trend": trend,
"correlation": correlation,
"confidence": abs(correlation),
"recent_avg": np.mean(recent_scores),
"season_avg": self.fantasy_ppg
}
@dataclass
class ValidationResult:
"""
Result of cross-source data validation
Used to ensure data accuracy and reliability
"""
stat_name: str
player_id: str
# Validation outcome
is_valid: bool
confidence_score: float
consensus_value: float
# Source comparison
source_values: Dict[str, float]
outlier_sources: List[str]
deviation_percentage: float
# Statistical analysis
mean_value: float
median_value: float
standard_deviation: float
# Decision rationale
validation_method: str
timestamp: datetime = field(default_factory=datetime.now)
def get_recommended_value(self) -> float:
"""
Get the recommended value to use based on validation results
Prioritizes consensus while accounting for source reliability
"""
if self.is_valid and self.confidence_score >= Config.CONFIDENCE_THRESHOLD:
return self.consensus_value
elif self.median_value:
return self.median_value # Fallback to median for outlier resistance
else:
return self.mean_value # Final fallback
# ===== MESSAGE QUEUE MODELS =====
@dataclass
class QueueMessage:
"""
Standardized message format for internal queue system
Ensures consistent processing across all pipeline components
"""
message_id: str = field(default_factory=lambda: str(uuid.uuid4()))
message_type: str = "player_update"
priority: int = 1 # 1=high, 2=normal, 3=low
# Message content
data: Dict[str, Any] = field(default_factory=dict)
# Processing metadata
created_at: datetime = field(default_factory=datetime.now)
retry_count: int = 0
max_retries: int = Config.MAX_RETRIES
# Routing information
source_component: str = ""
target_component: str = ""
def to_json(self) -> str:
"""Serialize message for queue storage"""
message_dict = asdict(self)
# Convert datetime to ISO string
message_dict["created_at"] = self.created_at.isoformat()
return json.dumps(message_dict)
@classmethod
def from_json(cls, json_str: str) -> "QueueMessage":
"""Deserialize message from queue storage"""
data = json.loads(json_str)
# Convert ISO string back to datetime
data["created_at"] = datetime.fromisoformat(data["created_at"])
return cls(**data)
def can_retry(self) -> bool:
"""Check if message can be retried on failure"""
return self.retry_count < self.max_retries
def increment_retry(self) -> None:
"""Increment retry counter"""
self.retry_count += 1
"""
SUMMARY:
This module defines the core data models and configuration for the NFL real-time data pipeline.
Key Components:
1. Config class - Central configuration with performance targets (<200ms latency, 10K+ updates/day, 99.9% uptime)
2. Enums - Type-safe definitions for data sources, stat categories, game statuses, etc.
3. PlayerUpdate - Individual stat updates with deduplication hashing and fantasy point calculation
4. GameData - Real-time game context with environmental factors and pace analysis
5. InjuryReport - Injury status tracking with game participation probability
6. PlayerSeasonStats - Comprehensive season analytics with trend analysis and consistency scoring
7. ValidationResult - Cross-source data validation with confidence scoring
8. QueueMessage - Standardized internal messaging format
The models support the target requirements:
- Real-time updates with <30 second freshness
- Multi-source data validation for accuracy
- 3+ seasons of historical depth for trend analysis
- Fantasy-relevant metrics (PPG, target share, snap %, red zone usage)
- Data quality tracking and confidence scoring
All models include proper typing, default values, and utility methods for processing efficiency.
"""