Skip to content

Add game stat metrics to index endpoint #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
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
143 changes: 143 additions & 0 deletions src/entities/game-stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ import { EntityManager, Entity, ManyToOne, PrimaryKey, Property, Collection, One
import { Request, Required, ValidationCondition } from 'koa-clay'
import Game from './game'
import PlayerGameStat from './player-game-stat'
import { ClickHouseClient } from '@clickhouse/client'
import Player from './player'
import { formatDateForClickHouse } from '../lib/clickhouse/formatDateTime'
import { endOfDay } from 'date-fns'

type GlobalValueMetrics = {
minValue: number
maxValue: number
medianValue: number
averageValue: number
averageChange: number
}

type PlayerValueMetrics = {
minValue: number
maxValue: number
medianValue: number
averageValue: number
}

@Entity()
export default class GameStat {
Expand Down Expand Up @@ -93,6 +112,8 @@ export default class GameStat {
@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date()

metrics?: { globalCount: number, globalValue: GlobalValueMetrics, playerValue: PlayerValueMetrics }

constructor(game: Game) {
this.game = game
}
Expand All @@ -109,13 +130,135 @@ export default class GameStat {
.reduce((acc, curr) => acc += curr.value, 0)
}

async buildMetricsWhereConditions(startDate?: string, endDate?: string, player?: Player): Promise<string> {
let whereConditions = `WHERE game_stat_id = ${this.id}`

if (startDate) {
whereConditions += ` AND created_at >= '${formatDateForClickHouse(new Date(startDate))}'`
}
if (endDate) {
// when using YYYY-MM-DD, use the end of the day
const end = endDate.length === 10 ? endOfDay(new Date(endDate)) : new Date(endDate)
whereConditions += ` AND created_at <= '${formatDateForClickHouse(end)}'`
}
if (player) {
await player.aliases.loadItems()
const aliasIds = player.aliases.getIdentifiers()
whereConditions += ` AND player_alias_id IN (${aliasIds.join(', ')})`
}

return whereConditions
}

async loadMetrics(clickhouse: ClickHouseClient, metricsStartDate?: string, metricsEndDate?: string): Promise<void> {
const whereConditions = await this.buildMetricsWhereConditions(metricsStartDate, metricsEndDate)

const [globalCount, globalValue] = await this.getGlobalValueMetrics(clickhouse, whereConditions)
const playerValue = await this.getPlayerValueMetrics(clickhouse, whereConditions)

this.metrics = {
globalCount,
globalValue,
playerValue
}
}

async getGlobalValueMetrics(
clickhouse: ClickHouseClient,
whereConditions: string
): Promise<[number, GlobalValueMetrics]> {
const query = `
SELECT
count() as rawCount,
min(global_value) as minValue,
max(global_value) as maxValue,
median(global_value) as medianValue,
avg(global_value) as averageValue,
avg(change) as averageChange
FROM player_game_stat_snapshots
${whereConditions}
`

const res = await clickhouse.query({
query: query,
format: 'JSONEachRow'
}).then((res) => res.json<{
rawCount: string | number
minValue: number
maxValue: number
medianValue: number | null
averageValue: number | null
averageChange: number | null
}>())

const {
rawCount,
minValue,
maxValue,
medianValue,
averageValue,
averageChange
} = res[0]

return [
Number(rawCount),
{
minValue: minValue || this.defaultValue,
maxValue: maxValue || this.defaultValue,
medianValue: medianValue ?? this.defaultValue,
averageValue: averageValue ?? this.defaultValue,
averageChange: averageChange ?? 0
}
]
}

async getPlayerValueMetrics(
clickhouse: ClickHouseClient,
whereConditions: string
): Promise<PlayerValueMetrics> {
const query = `
SELECT
min(value) as minValue,
max(value) as maxValue,
median(value) as medianValue,
avg(value) as averageValue
FROM player_game_stat_snapshots
${whereConditions}
`

const res = await clickhouse.query({
query: query,
format: 'JSONEachRow'
}).then((res) => res.json<{
minValue: number
maxValue: number
medianValue: number | null
averageValue: number | null
}>())

const {
minValue,
maxValue,
medianValue,
averageValue
} = res[0]

return {
minValue: minValue || this.defaultValue,
maxValue: maxValue || this.defaultValue,
medianValue: medianValue ?? this.defaultValue,
averageValue: averageValue ?? this.defaultValue
}
}

toJSON() {
return {
id: this.id,
internalName: this.internalName,
name: this.name,
global: this.global,
globalValue: this.hydratedGlobalValue ?? this.globalValue,
metrics: this.metrics,
defaultValue: this.defaultValue,
maxChange: this.maxChange,
minValue: this.minValue,
Expand Down
136 changes: 6 additions & 130 deletions src/services/api/game-stat-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EntityManager } from '@mikro-orm/mysql'
import { differenceInSeconds, endOfDay } from 'date-fns'
import { differenceInSeconds } from 'date-fns'
import { HasPermission, Request, Response, Route, Validate } from 'koa-clay'
import GameStatAPIDocs from '../../docs/game-stat-api.docs'
import GameStat from '../../entities/game-stat'
Expand All @@ -11,115 +11,9 @@ import { ClickHouseClient } from '@clickhouse/client'
import PlayerGameStatSnapshot, { ClickHousePlayerGameStatSnapshot } from '../../entities/player-game-stat-snapshot'
import Player from '../../entities/player'
import { buildDateValidationSchema } from '../../lib/dates/dateValidationSchema'
import { formatDateForClickHouse } from '../../lib/clickhouse/formatDateTime'
import PlayerAlias from '../../entities/player-alias'
import { TraceService } from '../../lib/tracing/trace-service'

type GlobalValueMetrics = {
minValue: number
maxValue: number
medianValue: number
averageValue: number
averageChange: number
}

type PlayerValueMetrics = {
minValue: number
maxValue: number
medianValue: number
averageValue: number
}

async function getGlobalValueMetrics(
clickhouse: ClickHouseClient,
stat: GameStat,
whereConditions: string
): Promise<[number, GlobalValueMetrics]> {
const query = `
SELECT
count() as rawCount,
min(global_value) as minValue,
max(global_value) as maxValue,
median(global_value) as medianValue,
avg(global_value) as averageValue,
avg(change) as averageChange
FROM player_game_stat_snapshots
${whereConditions}
`

const res = await clickhouse.query({
query: query,
format: 'JSONEachRow'
}).then((res) => res.json<{
rawCount: string | number
minValue: number
maxValue: number
medianValue: number | null
averageValue: number | null
averageChange: number | null
}>())

const {
rawCount,
minValue,
maxValue,
medianValue,
averageValue,
averageChange
} = res[0]

return [
Number(rawCount),
{
minValue: minValue || stat.defaultValue,
maxValue: maxValue || stat.defaultValue,
medianValue: medianValue ?? stat.defaultValue,
averageValue: averageValue ?? stat.defaultValue,
averageChange: averageChange ?? 0
}
]
}

async function getPlayerValueMetrics(
clickhouse: ClickHouseClient,
stat: GameStat,
whereConditions: string
): Promise<PlayerValueMetrics> {
const query = `
SELECT
min(value) as minValue,
max(value) as maxValue,
median(value) as medianValue,
avg(value) as averageValue
FROM player_game_stat_snapshots
${whereConditions}
`

const res = await clickhouse.query({
query: query,
format: 'JSONEachRow'
}).then((res) => res.json<{
minValue: number
maxValue: number
medianValue: number | null
averageValue: number | null
}>())

const {
minValue,
maxValue,
medianValue,
averageValue
} = res[0]

return {
minValue: minValue || stat.defaultValue,
maxValue: maxValue || stat.defaultValue,
medianValue: medianValue ?? stat.defaultValue,
averageValue: averageValue ?? stat.defaultValue
}
}

@TraceService()
export default class GameStatAPIService extends APIService {
@Route({
Expand Down Expand Up @@ -261,18 +155,7 @@ export default class GameStatAPIService extends APIService {
const stat: GameStat = req.ctx.state.stat
const player: Player = req.ctx.state.player

await player.aliases.loadItems()
const aliasIds = player.aliases.getIdentifiers()

let whereConditions = `WHERE game_stat_id = ${stat.id} AND player_alias_id IN (${aliasIds.join(', ')})`
if (startDate) {
whereConditions += ` AND created_at >= '${formatDateForClickHouse(new Date(startDate))}'`
}
if (endDate) {
// when using YYYY-MM-DD, use the end of the day
const end = endDate.length === 10 ? endOfDay(new Date(endDate)) : new Date(endDate)
whereConditions += ` AND created_at <= '${formatDateForClickHouse(end)}'`
}
const whereConditions = await stat.buildMetricsWhereConditions(startDate, endDate, player)

const query = `
WITH (SELECT count() FROM player_game_stat_snapshots ${whereConditions}) AS count
Expand Down Expand Up @@ -328,15 +211,8 @@ export default class GameStatAPIService extends APIService {
req.ctx.throw(400, 'This stat is not globally available')
}

let whereConditions = `WHERE game_stat_id = ${stat.id}`
if (startDate) {
whereConditions += ` AND created_at >= '${formatDateForClickHouse(new Date(startDate))}'`
}
if (endDate) {
// when using YYYY-MM-DD, use the end of the day
const end = endDate.length === 10 ? endOfDay(new Date(endDate)) : new Date(endDate)
whereConditions += ` AND created_at <= '${formatDateForClickHouse(end)}'`
}
let whereConditions = await stat.buildMetricsWhereConditions(startDate, endDate)

if (playerId) {
try {
const player = await em.repo(Player).findOneOrFail({
Expand All @@ -363,8 +239,8 @@ export default class GameStatAPIService extends APIService {
}).then((res) => res.json<ClickHousePlayerGameStatSnapshot>())

const history = await Promise.all(snapshots.map((snapshot) => new PlayerGameStatSnapshot().hydrate(em, snapshot)))
const [count, globalValue] = await getGlobalValueMetrics(clickhouse, stat, whereConditions)
const playerValue = await getPlayerValueMetrics(clickhouse, stat, whereConditions)
const [count, globalValue] = await stat.getGlobalValueMetrics(clickhouse, whereConditions)
const playerValue = await stat.getPlayerValueMetrics(clickhouse, whereConditions)

return {
status: 200,
Expand Down
7 changes: 7 additions & 0 deletions src/services/game-stat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ import PlayerGameStat from '../entities/player-game-stat'
import triggerIntegrations from '../lib/integrations/triggerIntegrations'
import updateAllowedKeys from '../lib/entities/updateAllowedKeys'
import { TraceService } from '../lib/tracing/trace-service'
import { buildDateValidationSchema } from '../lib/dates/dateValidationSchema'

@TraceService()
export default class GameStatService extends Service {
@Route({
method: 'GET'
})
@Validate({
query: buildDateValidationSchema(false, false)
})
@HasPermission(GameStatPolicy, 'index')
async index(req: Request): Promise<Response> {
const { metricsStartDate, metricsEndDate } = req.query

const em: EntityManager = req.ctx.em
const stats = await em.getRepository(GameStat).find({ game: req.ctx.state.game })

for (const stat of stats) {
if (stat.global) {
await stat.recalculateGlobalValue(req.ctx.state.includeDevData)
await stat.loadMetrics(req.ctx.clickhouse, metricsStartDate, metricsEndDate)
}
}

Expand Down
Loading