mlb-api 1.0.3

Endpoints for MLB's public Statcast API.
Documentation
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
//! Attendance for games and seasons.
//!
//! Typically only seasonal [`AttendanceRecord`]s are accessible so some extra work is needed to get a specific game's attendance.
//!
//! Within regards to attendance, the term frequently used is "Opening" over "Game";
//! this is for reasons including but not limited to: single ticket double headers,
//! and rescheduled games.
//!
//! Averages are calculated with respect to the # of openings on the sample, not the number of games the team played as either "home" or "away".
//!
//! Since the 2020 season had 0 total attendance, the 'peak attendance game' has its default value of [`NaiveDate::MIN`]

use crate::league::LeagueId;
use crate::season::SeasonId;
use crate::team::TeamId;
use crate::{Copyright, HomeAway, MLB_API_DATE_FORMAT};
use bon::Builder;
use chrono::{DateTime, Datelike, Local, NaiveDate, Utc};
use either::Either;
use serde::de::Error;
use serde::{Deserialize, Deserializer};
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Formatter};
use std::iter::Sum;
use std::num::NonZeroU32;
use std::ops::Add;
use crate::game::GameId;
use crate::meta::GameType;
use crate::request::RequestURL;

/// Response from the `attendance` endpoint.
/// Returns a [`Vec`] of [`AttendanceRecord`].
///
/// Example: <http://statsapi.mlb.com/api/v1/attendance?teamId=141>
#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
#[serde(from = "AttendanceResponseStruct")]
pub struct AttendanceResponse {
	pub copyright: Copyright,
	#[serde(rename = "records")]
	pub annual_records: Vec<AttendanceRecord>,
}

impl AttendanceResponse {
	/// Combines all the attendance records into one for all the recorded openings.
	#[must_use]
	pub fn into_aggregate(self) -> AttendanceRecord {
		self.annual_records.into_iter().sum()
	}
}

#[derive(Deserialize)]
struct AttendanceResponseStruct {
	copyright: Copyright,
	records: Vec<AttendanceRecord>,
}

impl From<AttendanceResponseStruct> for AttendanceResponse {
	fn from(value: AttendanceResponseStruct) -> Self {
		let AttendanceResponseStruct { copyright, records } = value;
		Self { copyright, annual_records: records }
	}
}

/// A record of attendance.
///
/// Does not represent a single opening, those opening-by-opening requests require a little more MacGyver-ing with the date.
///
/// Represents a full season of attendance data (segmented by [`GameType`]).
#[derive(Debug, Deserialize, PartialEq, Eq, Clone)]
#[serde(from = "AnnualRecordStruct")]
pub struct AttendanceRecord {
	pub total_openings: HomeAway<u32>,
	pub total_openings_lost: u32,
	pub total_games: HomeAway<u32>,
	pub season: SeasonWithMinorId,
	pub attendance_totals: HomeAway<u32>,
	/// Minimum at an opening, then maximum at an opening
	pub single_opening_min_max: Option<(DatedAttendance, DatedAttendance)>,
	pub game_type: GameType,
}

impl Add for AttendanceRecord {
	type Output = Self;

	/// Since the [`AttendanceRecord::default()`] value has some "worse"-er defaults (high and low attendance records have the epoch start time as their dates), we always take the later values in case of ties.
	fn add(self, rhs: Self) -> Self::Output {
		Self {
			total_openings: HomeAway {
				home: self.total_openings.home + rhs.total_openings.home,
				away: self.total_openings.away + rhs.total_openings.away,
			},
			total_openings_lost: self.total_openings_lost + rhs.total_openings_lost,
			total_games: HomeAway {
				home: self.total_games.home + rhs.total_games.home,
				away: self.total_games.away + rhs.total_games.away,
			},
			season: SeasonWithMinorId::max(self.season, rhs.season),
			attendance_totals: HomeAway {
				home: self.attendance_totals.home + rhs.attendance_totals.home,
				away: self.attendance_totals.away + rhs.attendance_totals.away,
			},
			single_opening_min_max: match (self.single_opening_min_max, rhs.single_opening_min_max) {
				(None, None) => None,
				(Some(min_max), None) | (None, Some(min_max)) => Some(min_max),
				// ties go to rhs in `min` and `max` calls
				(Some((a_min, a_max)), Some((b_min, b_max))) => Some((b_min.min(a_min), a_max.max(b_max))),
			},
			game_type: rhs.game_type,
		}
	}
}

impl Default for AttendanceRecord {
	#[allow(clippy::cast_sign_loss, reason = "jesus is not alive")]
	fn default() -> Self {
		Self {
			total_openings: HomeAway::new(0, 0),
			total_openings_lost: 0,
			total_games: HomeAway::new(0, 0),
			season: (Local::now().year() as u32).into(),
			attendance_totals: HomeAway::new(0, 0),
			single_opening_min_max: None,
			game_type: GameType::default(),
		}
	}
}

impl Sum for AttendanceRecord {
	fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
		iter.fold(Self::default(), |acc, x| acc + x)
	}
}

impl AttendanceRecord {
	/// Calculates the average attendance.
	///
	/// # Examples
	/// ```
	/// assert_eq!(AttendanceRecord {
	///     total_openings: (2, 2).into(),
	///     attendance_totals: (200, 200).into(),
	///     ..Default::default(),
	/// }.average_attendance(), (100, 100).into());
	/// ```
	#[must_use]
	pub const fn average_attendance(&self) -> HomeAway<u32> {
		let HomeAway { home, away } = self.attendance_totals;
		let HomeAway { home: num_at_home, away: num_at_away } = self.total_openings;
		HomeAway::new((home + num_at_home / 2) / num_at_home, (away + num_at_away / 2) / num_at_away)
	}
}

/// Season with an optional minor part
///
/// Some seasons are duplicated since there might be multiple in the same year, because of that we get stuff like `2018.2`.
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Hash)]
pub struct SeasonWithMinorId {
	season: SeasonId,
	minor: Option<NonZeroU32>,
}

impl From<SeasonId> for SeasonWithMinorId {
	fn from(value: SeasonId) -> Self {
		Self { season: value, minor: None }
	}
}

impl From<u32> for SeasonWithMinorId {
	fn from(value: u32) -> Self {
		Self { season: value.into(), minor: None }
	}
}

impl<'de> Deserialize<'de> for SeasonWithMinorId {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: Deserializer<'de>
	{
		struct Visitor;

		impl serde::de::Visitor<'_> for Visitor {
			type Value = SeasonWithMinorId;

			fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
				formatter.write_str("a season id, or a string with a . denoting the minor")
			}

			fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
			where
				E: Error
			{
				Ok(SeasonWithMinorId { season: SeasonId::from(value), minor: None })
			}

			fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
			where
				E: Error,
			{
				if let Some((season, minor)) = v.split_once('.') {
					let season = season.parse::<u32>().map_err(Error::custom)?;
					let minor = minor.parse::<u32>().map_err(Error::custom)?;
					let minor = NonZeroU32::try_from(minor).map_err(Error::custom)?;
					Ok(SeasonWithMinorId { season: SeasonId::from(season), minor: Some(minor) })
				} else {
					Ok(v.parse::<u32>().map(|season| SeasonWithMinorId { season: SeasonId::from(season), minor: None }).map_err(Error::custom)?)
				}
			}
		}

		deserializer.deserialize_any(Visitor)
	}
}

impl Display for SeasonWithMinorId {
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}", self.season)?;
		if let Some(minor) = self.minor {
			write!(f, ".{minor}")?;
		}
		Ok(())
	}
}

#[derive(Debug, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
struct AnnualRecordStruct {
	// openings_total: u32,
	openings_total_away: u32,
	openings_total_home: u32,
	openings_total_lost: u32,
	// games_total: u32,
	games_away_total: u32,
	games_home_total: u32,
	year: SeasonWithMinorId,
	// attendance_average_away: u32,
	// attendance_average_home: u32,
	// attendance_average_ytd: u32,
	attendance_high: Option<u32>,
	attendance_high_date: Option<DateTime<Utc>>,
	attendance_high_game: Option<GameId>,
	attendance_low: Option<u32>,
	attendance_low_date: Option<DateTime<Utc>>,
	attendance_low_game: Option<GameId>,
	// attendance_opening_average: u32,
	// attendance_total: u32,
	attendance_total_away: Option<u32>,
	attendance_total_home: Option<u32>,
	game_type: GameType,
	// team: Team,
}

impl From<AnnualRecordStruct> for AttendanceRecord {
	#[allow(clippy::too_many_lines, reason = "low cognitive complexity")]
	fn from(value: AnnualRecordStruct) -> Self {
		let AnnualRecordStruct {
			// openings_total,
			openings_total_away,
			openings_total_home,
			openings_total_lost,
			// games_total,
			games_away_total,
			games_home_total,
			year,
			// attendance_average_away,
			// attendance_average_home,
			// attendance_average_ytd,
			attendance_high,
			attendance_high_date,
			attendance_high_game,
			attendance_low,
			attendance_low_date,
			attendance_low_game,
			// attendance_opening_average,
			// attendance_total,
			attendance_total_away,
			attendance_total_home,
			game_type,
			// team,
		} = value;

		let single_opening_min_max = if let Some(((attendance_high, attendance_high_date), attendance_high_game)) = attendance_high
			.zip(attendance_high_date).zip(attendance_high_game) {
			let max = DatedAttendance {
				value: attendance_high,
				date: attendance_high_date.date_naive(),
				game: attendance_high_game,
			};

			let min = {
				if let Some(((attendance_low, attendance_low_date), attendance_low_game)) = attendance_low
					.zip(attendance_low_date).zip(attendance_low_game) {
					DatedAttendance {
						value: attendance_low,
						date: attendance_low_date.date_naive(),
						game: attendance_low_game,
					}
				} else {
					max.clone()
				}
			};

			Some((min, max))
		} else {
			None
		};
		
		Self {
			total_openings: HomeAway {
				home: openings_total_home,
				away: openings_total_away,
			},
			total_openings_lost: openings_total_lost,
			total_games: HomeAway {
				home: games_home_total,
				away: games_away_total,
			},
			season: year,
			attendance_totals: HomeAway {
				home: attendance_total_home.unwrap_or(0),
				away: attendance_total_away.unwrap_or(0),
			},
			single_opening_min_max,
			game_type,
		}
	}
}

/// An attendance record of a single game.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DatedAttendance {
	/// Attendance quantity
	pub value: u32,
	/// Date of attendance
	pub date: NaiveDate,
	/// Game in which people attended
	pub game: GameId,
}

impl PartialOrd<Self> for DatedAttendance {
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
		Some(self.cmp(other))
	}
}

impl Ord for DatedAttendance {
	fn cmp(&self, other: &Self) -> Ordering {
		self.value.cmp(&other.value)
	}
}

/// Returns [`AttendanceResponse`]
#[derive(Builder)]
#[builder(derive(Into))]
pub struct AttendanceRequest {
	#[doc(hidden)]
	#[builder(setters(vis = "", name = __id_internal))]
	id: Either<TeamId, LeagueId>,
	#[builder(into)]
	season: Option<SeasonWithMinorId>,
	#[builder(into)]
	date: Option<NaiveDate>,
	#[builder(default)]
	game_type: GameType,
}

impl<S: attendance_request_builder::State + attendance_request_builder::IsComplete> crate::request::RequestURLBuilderExt for AttendanceRequestBuilder<S> {
    type Built = AttendanceRequest;
}

#[allow(dead_code, reason = "optionally used by the end user")]
impl<S: attendance_request_builder::State> AttendanceRequestBuilder<S> {
	#[doc = "_**Required.**_\n\n"]
	pub fn team_id(self, id: impl Into<TeamId>) -> AttendanceRequestBuilder<attendance_request_builder::SetId<S>>
	where
		S::Id: attendance_request_builder::IsUnset,
	{
		self.__id_internal(Either::Left(id.into()))
	}

	#[doc = "_**Required.**_\n\n"]
	pub fn league_id(self, id: impl Into<LeagueId>) -> AttendanceRequestBuilder<attendance_request_builder::SetId<S>>
	where
		S::Id: attendance_request_builder::IsUnset,
	{
		self.__id_internal(Either::Right(id.into()))
	}
}

impl Display for AttendanceRequest {
	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
		write!(
			f,
			"http://statsapi.mlb.com/api/v1/attendance{}",
			gen_params! { "teamId"?: self.id.left(), "leagueId"?: self.id.right(), "season"?: self.season, "date"?: self.date.as_ref().map(|date| date.format(MLB_API_DATE_FORMAT)), "gameType": format!("{:?}", self.game_type) }
		)
	}
}

impl RequestURL for AttendanceRequest {
	type Response = AttendanceResponse;
}

#[cfg(test)]
mod tests {
	use crate::attendance::AttendanceRequest;
	use crate::request::{RequestURL, RequestURLBuilderExt};
	use crate::team::TeamsRequest;
	use crate::TEST_YEAR;

	#[tokio::test]
	#[cfg_attr(not(feature = "_heavy_tests"), ignore)]
	async fn parse_all_teams_test_year() {
		let mlb_teams = TeamsRequest::all_sports()
			.season(TEST_YEAR)
			.build_and_get()
		.await
		.unwrap()
		.teams;
		for team in mlb_teams {
			let request = AttendanceRequest::builder()
				.team_id(team.id)
				.build();
			let _response = request.get()
				.await
				.unwrap();
		}
	}
}