Skip to content

Commit

Permalink
refactor: adjust dump CLI to emit a tournament
Browse files Browse the repository at this point in the history
  • Loading branch information
alee committed Dec 6, 2023
1 parent 38b2cfd commit ee8b9b9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 31 deletions.
9 changes: 7 additions & 2 deletions dump.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env bash

#######
# usage
# ./dump.sh (prod|dev) --tournamentId <tournamentId>


set -o nounset
set -o pipefail
set -o errexit
Expand All @@ -8,8 +13,8 @@ ENV=$1
shift

case "$ENV" in
dev) docker compose exec server yarn cli dump "$@";;
*) docker compose exec server yarn cli:prod dump "$@";;
prod) docker compose exec server yarn cli:prod dump tournament "$@";;
*) docker compose exec server yarn cli dump tournament "$@";;
esac

docker run -it --rm -v $PWD/analytics:/home/analytics -v $PWD/docker/dump:/dump -w /home/analytics rocker/tidyverse:3 Rscript R/export.R
87 changes: 60 additions & 27 deletions server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,31 @@ async function withConnection<T>(f: (em: EntityManager) => Promise<T>): Promise<
}
}

async function exportData(
async function exportTournament(em: EntityManager, tournamentId: number): Promise<void> {
logger.debug("=====EXPORT TOURNAMENT [%d] DATA START=====", tournamentId);
const s = getServices(em);
const tournament = await s.tournament.getTournament(tournamentId, true);
const rounds = tournament.rounds;
for (const round of rounds) {
await exportTournamentRound(em, round.id, round.roundNumber);
}
}

async function exportTournamentRound(
em: EntityManager,
tournamentRoundId: number,
gameIds: Array<number>
tournamentRoundNumber: number,
gameIds?: Array<number>
): Promise<void> {
logger.debug("=====EXPORT DATA START=====");
logger.debug("=====EXPORT TOURNAMENT ROUND [%d] START=====", tournamentRoundId);
let eventQuery = await em
.getRepository(GameEvent)
.createQueryBuilder("ge")
.leftJoinAndSelect("ge.game", "g")
.innerJoin(TournamentRound, "tournamentRound", "tournamentRound.id = g.tournamentRoundId")
.where("tournamentRound.id = :tournamentRoundId", { tournamentRoundId })
.orderBy("ge.id", "ASC");
if (gameIds && gameIds.length > 0) {
if (typeof gameIds !== "undefined" && gameIds.length > 0) {
eventQuery = eventQuery.andWhere('"gameId" in (:...gameIds)', { gameIds });
}
const playerQuery = em
Expand All @@ -90,24 +101,27 @@ async function exportData(
.where("game.tournamentRound.id = :tournamentRoundId", { tournamentRoundId });
const games = await gameQuery.getMany();

await mkdir("/dump/processed", { recursive: true });
await mkdir("/dump/raw", { recursive: true });
const rawDir = `/dump/${tournamentRoundNumber}/raw`;
const processedDir = `/dump/${tournamentRoundNumber}/processed`;

const gameSummarizer = new GameSummarizer(games, "/dump/raw/games.csv");
const marsLogSummarizer = new MarsLogSummarizer(events, "/dump/processed/marsLog.csv");
const playerSummarizer = new PlayerSummarizer(playerRaw, "/dump/processed/player.csv");
const gameEventSummarizer = new GameEventSummarizer(events, "/dump/processed/gameEvent.csv");
const victoryPointSummarizer = new VictoryPointSummarizer(
await mkdir(rawDir, { recursive: true });
await mkdir(processedDir, { recursive: true });

const gameSummarizer = new GameSummarizer(games, `${rawDir}/games.csv`);
const playerInvestmentSummarizer = new PlayerInvestmentSummarizer(
events,
"/dump/processed/victoryPoint.csv"
`${rawDir}/playerInvestment.csv`
);
const playerInvestmentSummarizer = new PlayerInvestmentSummarizer(
const marsLogSummarizer = new MarsLogSummarizer(events, `${processedDir}/marsLog.csv`);
const playerSummarizer = new PlayerSummarizer(playerRaw, `${processedDir}/player.csv`);
const gameEventSummarizer = new GameEventSummarizer(events, `${processedDir}/gameEvent.csv`);
const victoryPointSummarizer = new VictoryPointSummarizer(
events,
"/dump/raw/playerInvestment.csv"
`${processedDir}/victoryPoint.csv`
);
const marsEventSummarizer = new MarsEventSummarizer(events, "/dump/processed/marsEvent.csv");
const marsEventSummarizer = new MarsEventSummarizer(events, `${processedDir}/marsEvent.csv`);
const accomplishmentSummarizer = new AccomplishmentSummarizer(
"/dump/processed/accomplishment.csv"
`${processedDir}/accomplishment.csv`
);
await Promise.all(
[
Expand All @@ -121,7 +135,7 @@ async function exportData(
accomplishmentSummarizer,
].map(s => s.save())
);
logger.debug("=====EXPORTING DATA END=====");
logger.debug("=====EXPORTING TOURNAMENT ROUND [%d] END=====", tournamentRoundId);
}

async function validate(em: EntityManager, gameId: number): Promise<void> {
Expand Down Expand Up @@ -513,7 +527,7 @@ program
.addCommand(
program
.createCommand("create")
.option("-o, --open", "create an open beta tournament round")
.option("-o, --open", "create a tournament round")
.option(
"--tournamentId <tournamentId>",
"id of an existing tournament",
Expand Down Expand Up @@ -625,16 +639,35 @@ program
.addCommand(
program
.createCommand("dump")
.description("dump game data for a given tournament round id to a pile of CSV files")
.requiredOption(
"--tournamentRoundId <tournamentRoundId>",
"tournament round id",
customParseInt
.description("subcommands to dump game data for a tournament or tournament round")
.addCommand(
program
.createCommand("tournament")
.description("dump game data for a given tournament round id to a pile of CSV files")
.requiredOption("--tournamentId <tournamentId>", "tournament id", customParseInt)
.action(async cmd => {
await withConnection(em => exportTournament(em, cmd.tournamentId));
})
)
.addCommand(
program
.createCommand("round")
.description("dump game data for a given tournament round id")
.requiredOption(
"--tournamentRoundId <tournamentRoundId>",
"tournament round id",
customParseInt
)
.option(
"--gids <game_ids>",
"specific game ids to export",
toIntArray,
[] as Array<number>
)
.action(async cmd => {
await withConnection(em => exportTournamentRound(em, cmd.tournamentId, cmd.gids));
})
)
.option("--gids <game_ids>", "specific game ids to export", toIntArray, [] as Array<number>)
.action(async cmd => {
await withConnection(em => exportData(em, cmd.tournamentRoundId, cmd.gids));
})
)
.addCommand(
program
Expand Down
11 changes: 9 additions & 2 deletions server/src/services/tournament.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ export class TournamentService extends BaseService {
});
}

async getTournament(id?: number): Promise<Tournament> {
async getTournament(id?: number, includeRounds = false): Promise<Tournament> {
if (id) {
if (includeRounds) {
return this.em.getRepository(Tournament).findOneOrFail({
where: {
id: id,
},
relations: ["rounds"],
});
}
return this.em.getRepository(Tournament).findOneOrFail({
where: {
id: id,
name: Not("freeplay"),
},
});
} else {
Expand Down

0 comments on commit ee8b9b9

Please sign in to comment.