Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/entities/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,58 @@ export default class Event extends ClickHouseEntity<ClickHouseEvent, [string, Ga
createdAt!: Date
updatedAt: Date = new Date()

static async massHydrate(em: EntityManager, data: ClickHouseEvent[], clickhouse: ClickHouseClient, loadProps: boolean = false): Promise<Event[]> {
const playerAliasIds = Array.from(new Set(data.map((event) => event.player_alias_id)))

const playerAliases = await em.getRepository(PlayerAlias).find({
id: {
$in: playerAliasIds
}
}, { populate: ['player'] })

const playerAliasesMap = new Map<number, PlayerAlias>()
playerAliases.forEach((alias) => playerAliasesMap.set(alias.id, alias))

const propsMap = new Map<string, Prop[]>()
if (loadProps) {
const eventIds = data.map((event) => event.id)
if (eventIds.length > 0) {
const props = await clickhouse.query({
query: 'SELECT * FROM event_props WHERE event_id IN ({eventIds:Array(String)})',
query_params: { eventIds },
format: 'JSONEachRow'
}).then((res) => res.json<ClickHouseEventProp>())

props.forEach((prop) => {
if (!propsMap.has(prop.event_id)) {
propsMap.set(prop.event_id, [])
}
propsMap.get(prop.event_id)!.push(new Prop(prop.prop_key, prop.prop_value))
})
}
}

return data.map((eventData) => {
const playerAlias = playerAliasesMap.get(eventData.player_alias_id)
if (!playerAlias) {
return null
}

const event = new Event()
event.construct(eventData.name, playerAlias.player.game)
event.id = eventData.id
event.playerAlias = playerAlias
event.createdAt = new Date(eventData.created_at)
event.updatedAt = new Date(eventData.updated_at)

if (loadProps) {
event.props = propsMap.get(eventData.id) || []
}

return event
}).filter((event) => !!event)
}

construct(name: string, game: Game): this {
this.name = name
this.game = game
Expand Down Expand Up @@ -91,7 +143,8 @@ export default class Event extends ClickHouseEntity<ClickHouseEvent, [string, Ga

if (loadProps) {
const props = await clickhouse.query({
query: `SELECT * FROM event_props WHERE event_id = '${data.id}'`,
query: 'SELECT * FROM event_props WHERE event_id = {eventId:String}',
query_params: { eventId: data.id },
format: 'JSONEachRow'
}).then((res) => res.json<ClickHouseEventProp>())

Expand Down
2 changes: 1 addition & 1 deletion src/services/player.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export default class PlayerService extends Service {
query_params: queryParams,
format: 'JSONEachRow'
}).then((res) => res.json<ClickHouseEvent>())
const events = await Promise.all(items.map((data) => new Event().hydrate(em, data, clickhouse, true)))
const events = await Event.massHydrate(em, items, clickhouse, true)

const countQuery = `
SELECT count(DISTINCT events.id) AS count
Expand Down
5 changes: 5 additions & 0 deletions tests/services/player/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ describe('Player service - get events', () => {
values: events.map((event) => event.toInsertable()),
format: 'JSONEachRow'
})
await clickhouse.insert({
table: 'event_props',
values: events.flatMap((event) => event.getInsertableProps()),
format: 'JSONEachRow'
})

const res = await request(app)
.get(`/games/${game.id}/players/${player.id}/events`)
Expand Down