Skip to content

Commit

Permalink
Feature/ingest 2016 nfl season (winsleague#193)
Browse files Browse the repository at this point in the history
* added game hook for when games are removed

* changed to look up team by mascot name because in nfl.com changed JAC to JAX and rams are now in LA.

* variable rename for clarity

* clearer logging

* more efficient updating because we're isolating to league teams within the season

* migration to add 2016 games. next time, include this in the original migration
  • Loading branch information
noahsw authored Aug 14, 2016
1 parent a573ce5 commit d25dbb0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 15 deletions.
5 changes: 5 additions & 0 deletions app/imports/api/games/server/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ Games.after.update((userId, doc, fieldNames, modifier, options) => {
SeasonLeagueTeamUpdater.updateTeamStats(doc.leagueId, doc.seasonId, doc.homeTeamId);
SeasonLeagueTeamUpdater.updateTeamStats(doc.leagueId, doc.seasonId, doc.awayTeamId);
});

Games.after.remove((userId, doc) => {
SeasonLeagueTeamUpdater.updateTeamStats(doc.leagueId, doc.seasonId, doc.homeTeamId);
SeasonLeagueTeamUpdater.updateTeamStats(doc.leagueId, doc.seasonId, doc.awayTeamId);
});
34 changes: 30 additions & 4 deletions app/imports/api/games/server/nfl_game_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ export default {
},

ingestSeasonData(season) {
if (! season) throw new Error(`Season is null!`);
if (! season) {
throw new Error('Season is null!');
}

const league = LeagueFinder.getByName('NFL');
if (! league) throw new Error(`League is not found!`);
if (! league) {
throw new Error('League is not found!');
}

Games.remove({ leagueId: league._id, seasonId: season._id });

Expand Down Expand Up @@ -83,15 +87,37 @@ export default {
log.info(`season: ${season.year}, week: ${week}, game: ${game.eid}`);
const leagueId = LeagueFinder.getIdByName('NFL');
const gameDate = new Date(`${game.eid.substr(0, 4)}-${game.eid.substr(4, 2)}-${game.eid.substr(6, 2)}`); // 20151224

const homeLeagueTeam = LeagueTeams.findOne({
leagueId,
mascotName: {
$regex: new RegExp(game.hnn, 'i'),
},
});
if (! homeLeagueTeam) {
throw new Error(`Cannot find LeagueTeam in leagueId ${leagueId} and mascot ${game.hnn}`);
}

const awayLeagueTeam = LeagueTeams.findOne({
leagueId,
mascotName: {
$regex: `^${game.vnn}$`,
$options: 'i',
},
});
if (! awayLeagueTeam) {
throw new Error(`Cannot find LeagueTeam in leagueId ${leagueId} and mascot ${game.vnn}`);
}

Games.insert({
leagueId,
seasonId: season._id,
gameId: game.gsis,
gameDate,
week,
homeTeamId: LeagueTeams.findOne({ leagueId, abbreviation: game.h })._id,
homeTeamId: homeLeagueTeam._id,
homeScore: game.hs,
awayTeamId: LeagueTeams.findOne({ leagueId, abbreviation: game.v })._id,
awayTeamId: awayLeagueTeam._id,
awayScore: game.vs,
period: cleanPeriod(game.q),
status: cleanStatus(game.q),
Expand Down
8 changes: 4 additions & 4 deletions app/imports/api/pool_teams/server/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import { SeasonLeagueTeams } from '../../season_league_teams/season_league_teams
import PoolTeamPickUpdater from '../../pool_team_picks/server/updater';

export default {
updateWhoPickedLeagueTeam(leagueTeamId) {
log.info('Finding PoolTeams who picked leagueTeamId:', leagueTeamId);
updateWhoPickedLeagueTeam(seasonId, leagueTeamId) {
log.info(`Finding PoolTeams who picked leagueTeamId ${leagueTeamId} for seasonId ${seasonId}`);

const poolTeamPicks = PoolTeamPicks.find({ leagueTeamId });
const poolTeamPicks = PoolTeamPicks.find({ seasonId, leagueTeamId });
poolTeamPicks.forEach(poolTeamPick => {
this.updatePoolTeamRecord(poolTeamPick.poolTeamId);
this.updatePoolTeamPickQuality(poolTeamPick.poolTeamId);
});

log.debug('Done finding PoolTeams who picked leagueTeamId:', leagueTeamId);
log.debug(`Done finding PoolTeams who picked leagueTeamId ${leagueTeamId} for seasonId ${seasonId}`);
},

updatePoolTeamRecord(poolTeamId) {
Expand Down
4 changes: 2 additions & 2 deletions app/imports/api/season_league_teams/server/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import PoolTeamUpdater from '../../pool_teams/server/updater';
SeasonLeagueTeams.hookOptions.after.update = { fetchPrevious: false };

SeasonLeagueTeams.after.insert(function (userId, doc) {
PoolTeamUpdater.updateWhoPickedLeagueTeam(doc.leagueTeamId);
PoolTeamUpdater.updateWhoPickedLeagueTeam(doc.seasonId, doc.leagueTeamId);
});

SeasonLeagueTeams.after.update(function (userId, doc, fieldNames, modifier, options) {
PoolTeamUpdater.updateWhoPickedLeagueTeam(doc.leagueTeamId);
PoolTeamUpdater.updateWhoPickedLeagueTeam(doc.seasonId, doc.leagueTeamId);
});
16 changes: 11 additions & 5 deletions app/imports/api/season_league_teams/server/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import '../../season_league_teams/server/hooks';

export default {
updateTeamStats(leagueId, seasonId, leagueTeamId) {
if (!leagueId) { throw new Error('Undefined leagueId!'); }
if (!seasonId) { throw new Error('Undefined seasonId!'); }
if (!leagueTeamId) { throw new Error('Undefined leagueTeamId!'); }
if (!leagueId) {
throw new Error('Undefined leagueId!');
}
if (!seasonId) {
throw new Error('Undefined seasonId!');
}
if (!leagueTeamId) {
throw new Error('Undefined leagueTeamId!');
}

log.info('Updating stats for seasonLeagueTeam: ', leagueTeamId);
log.info(`Updating stats for seasonId ${seasonId} and leagueTeam ${leagueTeamId}`);

const games = Games.find({ leagueId, seasonId, status: 'completed',
$or: [{ homeTeamId: leagueTeamId }, { awayTeamId: leagueTeamId }] });
Expand Down Expand Up @@ -55,6 +61,6 @@ export default {
awayWins, awayLosses, awayTies,
pointsFor, pointsAgainst,
} });
log.debug(`seasonLeagueTeams.upsert numberAffected: ${result.numberAffected}`);
log.debug(`SeasonLeagueTeams.upsert: ${result.numberAffected} rows affected`);
},
};
22 changes: 22 additions & 0 deletions app/server/migrations/v6_add_2016_nfl_games.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Migrations } from 'meteor/percolate:migrations';
import log from '../../imports/utils/log';
import NflGameData from '../../imports/api/games/server/nfl_game_data';
import LeagueFinder from '../../imports/api/leagues/finder';
import SeasonFinder from '../../imports/api/seasons/finder';
import { Games } from '../../imports/api/games/games';

Migrations.add({
version: 6,
name: 'Adds 2016 games to NFL',
up: () => {
const season = SeasonFinder.getByYear('NFL', 2016);

NflGameData.ingestSeasonData(season);
},
down: () => {
const league = LeagueFinder.getByName('NFL');
const season = SeasonFinder.getByYear('NFL', 2016);

Games.remove({ leagueId: league._id, seasonId: season._id });
},
});

0 comments on commit d25dbb0

Please sign in to comment.