Skip to content

Commit ed2f94d

Browse files
committed
optimise getting events + headlines
1 parent a13d317 commit ed2f94d

File tree

5 files changed

+37
-34
lines changed

5 files changed

+37
-34
lines changed

src/services/api/leaderboard-api.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ export default class LeaderboardAPIService extends APIService {
8383
return integration.handleLeaderboardEntryCreated(em, entry)
8484
})
8585

86-
let query = em.createQueryBuilder(LeaderboardEntry, 'le')
86+
const query = em.createQueryBuilder(LeaderboardEntry, 'le')
8787
.select('le.*', true)
8888
.where({ leaderboard: entry.leaderboard })
8989
.orderBy({ score: entry.leaderboard.sortMode })
9090

9191
if (!req.ctx.state.includeDevData) {
92-
query = query.andWhere({
92+
query.andWhere({
9393
playerAlias: {
9494
player: devDataPlayerFilter(em)
9595
}

src/services/event.service.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ export default class EventService extends Service {
2020
})
2121
@HasPermission(EventPolicy, 'index')
2222
async index(req: Request): Promise<Response> {
23-
const { startDate, endDate } = req.query
23+
const { startDate: startDateQuery, endDate: endDateQuery } = req.query
2424
const em: EntityManager = req.ctx.em
2525

26+
const startDate = new Date(startDateQuery)
27+
const endDate = new Date(endDateQuery)
28+
2629
const where: FilterQuery<Event> = {
2730
game: req.ctx.state.game,
2831
createdAt: {
29-
$gte: new Date(startDate),
30-
$lte: endOfDay(new Date(endDate))
32+
$gte: startDate,
33+
$lte: endOfDay(endDate)
3134
}
3235
}
3336

@@ -51,16 +54,19 @@ export default class EventService extends Service {
5154
// }
5255

5356
const data = groupBy(events, 'name')
57+
5458
for (const name in data) {
5559
const processed: EventData[] = []
5660

57-
for (let date = new Date(startDate).getTime(); date <= new Date(endDate).getTime(); date += 86400000 /* 24 hours in ms */) {
58-
const count = data[name].filter((event: Event) => isSameDay(new Date(date), new Date(event.createdAt))).length
61+
for (let time = startDate.getTime(); time <= endDate.getTime(); time += 86400000 /* 24 hours in ms */) {
62+
const dateFromTime = new Date(time)
63+
64+
const count = data[name].filter((event: Event) => isSameDay(dateFromTime, event.createdAt)).length
5965
const change = processed.length > 0 ? this.calculateChange(count, processed[processed.length - 1]) : 0
6066

6167
processed.push({
6268
name,
63-
date,
69+
date: time,
6470
count,
6571
change
6672
})

src/services/game-feedback.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ export default class GameFeedbackService extends Service {
4040
const { feedbackCategoryInternalName, search, page } = req.query
4141
const em: EntityManager = req.ctx.em
4242

43-
let query = em.qb(GameFeedback, 'gf')
43+
const query = em.qb(GameFeedback, 'gf')
4444
.select('gf.*')
4545
.orderBy({ createdAt: QueryOrder.DESC })
4646
.limit(itemsPerPage)
4747
.offset(Number(page) * itemsPerPage)
4848

4949
if (feedbackCategoryInternalName) {
50-
query = query
50+
query
5151
.andWhere({
5252
category: {
5353
internalName: feedbackCategoryInternalName
@@ -56,7 +56,7 @@ export default class GameFeedbackService extends Service {
5656
}
5757

5858
if (search) {
59-
query = query.andWhere({
59+
query.andWhere({
6060
$or: [
6161
{ comment: { $like: `%${search}%` } },
6262
{

src/services/headline.service.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { FilterQuery, EntityManager } from '@mikro-orm/mysql'
22
import { endOfDay, isSameDay } from 'date-fns'
33
import { Service, Request, Response, Validate, HasPermission, Routes } from 'koa-clay'
4-
import groupBy from 'lodash.groupby'
54
import Event from '../entities/event'
65
import Player from '../entities/player'
76
import HeadlinePolicy from '../policies/headline.policy'
@@ -127,30 +126,28 @@ export default class HeadlineService extends Service {
127126
const { startDate, endDate } = req.query
128127
const em: EntityManager = req.ctx.em
129128

130-
const where: FilterQuery<Event> = {
131-
game: req.ctx.state.game,
132-
createdAt: {
133-
$gte: new Date(startDate),
134-
$lte: endOfDay(new Date(endDate))
135-
}
136-
}
129+
const query = em.qb(Event, 'e')
130+
.join('e.playerAlias', 'pa')
131+
.count('pa.player_id', true)
132+
.where({
133+
game: req.ctx.state.game.id,
134+
createdAt: { $gte: new Date(startDate), $lte: endOfDay(new Date(endDate)) }
135+
})
137136

138137
if (!req.ctx.state.includeDevData) {
139-
where.playerAlias = {
140-
player: devDataPlayerFilter(em)
141-
}
138+
query.andWhere({
139+
playerAlias: {
140+
player: devDataPlayerFilter(em)
141+
}
142+
})
142143
}
143144

144-
const events = await em.getRepository(Event).find(where, {
145-
populate: ['playerAlias.player']
146-
})
147-
148-
const count = Object.keys(groupBy(events, 'playerAlias.player.id')).length
145+
const result = await query.execute('get')
149146

150147
return {
151148
status: 200,
152149
body: {
153-
count
150+
count: result.count
154151
}
155152
}
156153
}

src/services/player.service.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ export default class PlayerService extends Service {
127127
const { search, page } = req.query
128128
const em: EntityManager = req.ctx.em
129129

130-
let query = em.qb(Player, 'p')
130+
const query = em.qb(Player, 'p')
131131
.select('p.*')
132132
.orderBy({ lastSeenAt: QueryOrder.DESC })
133133
.limit(itemsPerPage)
134134
.offset(Number(page) * itemsPerPage)
135135

136136
if (search) {
137-
query = query
137+
query
138138
.where({
139139
props: {
140140
$in: em.qb(PlayerProp).select('id').where({
@@ -167,7 +167,7 @@ export default class PlayerService extends Service {
167167
}
168168

169169
if (groups.length > 0) {
170-
query = query
170+
query
171171
.orWhere({
172172
groups: {
173173
$in: groups
@@ -177,7 +177,7 @@ export default class PlayerService extends Service {
177177
}
178178

179179
if (!req.ctx.state.includeDevData) {
180-
query = query.andWhere(devDataPlayerFilter(em))
180+
query.andWhere(devDataPlayerFilter(em))
181181
}
182182

183183
const [players, count] = await query
@@ -257,14 +257,14 @@ export default class PlayerService extends Service {
257257
const em: EntityManager = req.ctx.em
258258
const player: Player = req.ctx.state.player // set in the policy
259259

260-
let query = em.createQueryBuilder(Event, 'e')
260+
const query = em.createQueryBuilder(Event, 'e')
261261
.select('e.*')
262262
.orderBy({ createdAt: QueryOrder.DESC })
263263
.limit(itemsPerPage)
264264
.offset(Number(page) * itemsPerPage)
265265

266266
if (search) {
267-
query = query
267+
query
268268
.where('json_extract(props, \'$[*].value\') like ?', [`%${search}%`])
269269
.orWhere({
270270
name: {

0 commit comments

Comments
 (0)