feat(api): add community statistics aggregation
This commit is contained in:
1 parent
71394b4a47
commit
98eee9a0c4
5 files changed
+1453
No files matched your search
@@ -0,0 +1,59 @@
|
|||||||
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
import { GarnetService } from '../global/garnet/garnet.service';
|
||||||
|
|
||||||
|
const COMMUNITY_STATS_TTL_SECONDS = 60 * 60;
|
||||||
|
const COMMUNITY_STATS_DETAIL_TTL_SECONDS = 24 * 60 * 60;
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CommunityStatsCacheService {
|
||||||
|
private readonly logger = new Logger(CommunityStatsCacheService.name);
|
||||||
|
|
||||||
|
constructor(private readonly garnetService: GarnetService) {}
|
||||||
|
|
||||||
|
public get<T>(key: string): Promise<T | null> {
|
||||||
|
return this.garnetService.getJson<T>(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public set(
|
||||||
|
key: string,
|
||||||
|
value: unknown,
|
||||||
|
ttlSeconds = COMMUNITY_STATS_TTL_SECONDS,
|
||||||
|
): Promise<void> {
|
||||||
|
return this.garnetService.setJson(key, value, ttlSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public setDetail(key: string, value: unknown): Promise<void> {
|
||||||
|
return this.set(key, value, COMMUNITY_STATS_DETAIL_TTL_SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async invalidateCommunity(communityId: string): Promise<number> {
|
||||||
|
const prefix = `riot:communitystats:`;
|
||||||
|
const match = `${prefix}*${communityId}*`;
|
||||||
|
const client = this.garnetService.raw();
|
||||||
|
let cursor = '0';
|
||||||
|
let deleted = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
const reply = (await client.sendCommand([
|
||||||
|
'SCAN',
|
||||||
|
cursor,
|
||||||
|
'MATCH',
|
||||||
|
match,
|
||||||
|
'COUNT',
|
||||||
|
'100',
|
||||||
|
])) as [string, string[]];
|
||||||
|
cursor = reply[0];
|
||||||
|
const keys = reply[1] ?? [];
|
||||||
|
if (keys.length > 0) {
|
||||||
|
deleted += await client.del(keys);
|
||||||
|
}
|
||||||
|
} while (cursor !== '0');
|
||||||
|
|
||||||
|
if (deleted > 0) {
|
||||||
|
this.logger.debug(
|
||||||
|
`Invalidated ${deleted} community stats cache keys for ${communityId}.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return deleted;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
import { Controller, Get, Param, Query } from '@nestjs/common';
|
||||||
|
import mongoose from 'mongoose';
|
||||||
|
import { AllowPublicAccess } from '../auth/allow-public-access.decorator';
|
||||||
|
import { ParseObjectIdPipe } from '../common/parse-object-id.pipe';
|
||||||
|
import { CommunityStatsService } from './community-stats.service';
|
||||||
|
import type { CommunityStatsFilters } from './community-stats.types';
|
||||||
|
|
||||||
|
@Controller('communities/:communityId')
|
||||||
|
export class CommunityStatsController {
|
||||||
|
constructor(private readonly communityStatsService: CommunityStatsService) {}
|
||||||
|
|
||||||
|
@Get('internal-stats/matches')
|
||||||
|
@AllowPublicAccess()
|
||||||
|
public getMatches(
|
||||||
|
@Param('communityId', ParseObjectIdPipe)
|
||||||
|
communityId: mongoose.Types.ObjectId,
|
||||||
|
@Query('seasonId') seasonId?: string,
|
||||||
|
@Query('from') from?: string,
|
||||||
|
@Query('to') to?: string,
|
||||||
|
@Query('championId') championId?: string,
|
||||||
|
@Query('cursor') cursor?: string,
|
||||||
|
@Query('limit') limit?: string,
|
||||||
|
) {
|
||||||
|
return this.communityStatsService.getMatches({
|
||||||
|
communityId,
|
||||||
|
filters: this.toFilters({ seasonId, from, to, championId, cursor, limit }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('internal-stats/leaderboard')
|
||||||
|
@AllowPublicAccess()
|
||||||
|
public getLeaderboard(
|
||||||
|
@Param('communityId', ParseObjectIdPipe)
|
||||||
|
communityId: mongoose.Types.ObjectId,
|
||||||
|
@Query('seasonId') seasonId?: string,
|
||||||
|
@Query('from') from?: string,
|
||||||
|
@Query('to') to?: string,
|
||||||
|
@Query('championId') championId?: string,
|
||||||
|
) {
|
||||||
|
return this.communityStatsService.getLeaderboard({
|
||||||
|
communityId,
|
||||||
|
filters: this.toFilters({ seasonId, from, to, championId }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('internal-stats/team-insights')
|
||||||
|
@AllowPublicAccess()
|
||||||
|
public getTeamInsights(
|
||||||
|
@Param('communityId', ParseObjectIdPipe)
|
||||||
|
communityId: mongoose.Types.ObjectId,
|
||||||
|
@Query('seasonId') seasonId?: string,
|
||||||
|
@Query('from') from?: string,
|
||||||
|
@Query('to') to?: string,
|
||||||
|
@Query('championId') championId?: string,
|
||||||
|
) {
|
||||||
|
return this.communityStatsService.getTeamInsights({
|
||||||
|
communityId,
|
||||||
|
filters: this.toFilters({ seasonId, from, to, championId }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('internal-stats/match/:matchId')
|
||||||
|
@AllowPublicAccess()
|
||||||
|
public getMatchDetail(
|
||||||
|
@Param('communityId', ParseObjectIdPipe)
|
||||||
|
communityId: mongoose.Types.ObjectId,
|
||||||
|
@Param('matchId', ParseObjectIdPipe) matchId: mongoose.Types.ObjectId,
|
||||||
|
) {
|
||||||
|
return this.communityStatsService.getMatchDetail({ communityId, matchId });
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('users/:userId/internal-history')
|
||||||
|
@AllowPublicAccess()
|
||||||
|
public getUserInternalHistory(
|
||||||
|
@Param('communityId', ParseObjectIdPipe)
|
||||||
|
communityId: mongoose.Types.ObjectId,
|
||||||
|
@Param('userId', ParseObjectIdPipe) userId: mongoose.Types.ObjectId,
|
||||||
|
@Query('seasonId') seasonId?: string,
|
||||||
|
@Query('from') from?: string,
|
||||||
|
@Query('to') to?: string,
|
||||||
|
@Query('championId') championId?: string,
|
||||||
|
@Query('cursor') cursor?: string,
|
||||||
|
@Query('limit') limit?: string,
|
||||||
|
) {
|
||||||
|
return this.communityStatsService.getUserInternalHistory({
|
||||||
|
communityId,
|
||||||
|
userId,
|
||||||
|
filters: this.toFilters({ seasonId, from, to, championId, cursor, limit }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private toFilters(params: {
|
||||||
|
seasonId?: string;
|
||||||
|
from?: string;
|
||||||
|
to?: string;
|
||||||
|
championId?: string;
|
||||||
|
cursor?: string;
|
||||||
|
limit?: string;
|
||||||
|
}): CommunityStatsFilters {
|
||||||
|
return {
|
||||||
|
seasonId: params.seasonId,
|
||||||
|
from: params.from,
|
||||||
|
to: params.to,
|
||||||
|
championId: this.toNumber(params.championId),
|
||||||
|
cursor: this.toNumber(params.cursor),
|
||||||
|
limit: this.toNumber(params.limit),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private toNumber(value: string | undefined): number | undefined {
|
||||||
|
if (!value) return undefined;
|
||||||
|
const parsed = Number(value);
|
||||||
|
return Number.isFinite(parsed) ? parsed : undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { CommunityStatsCacheService } from './community-stats-cache.service';
|
||||||
|
import { CommunityStatsController } from './community-stats.controller';
|
||||||
|
import { CommunityStatsService } from './community-stats.service';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
controllers: [CommunityStatsController],
|
||||||
|
providers: [CommunityStatsService, CommunityStatsCacheService],
|
||||||
|
exports: [CommunityStatsService],
|
||||||
|
})
|
||||||
|
export class CommunityStatsModule {}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,196 @@
|
|||||||
|
import type { RiotLane, TeamSide } from '../global/database/schema';
|
||||||
|
import type { RecentMatchView, HistorySeasonDto } from '../match-history/match-history.types';
|
||||||
|
|
||||||
|
export interface CommunityStatsFilters {
|
||||||
|
seasonId?: string;
|
||||||
|
from?: string;
|
||||||
|
to?: string;
|
||||||
|
championId?: number;
|
||||||
|
cursor?: number;
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface InternalMatchListResponse {
|
||||||
|
matches: InternalMatchCardView[];
|
||||||
|
nextCursor: number | null;
|
||||||
|
season: HistorySeasonDto;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface InternalMatchCardView {
|
||||||
|
matchId: string;
|
||||||
|
title: string;
|
||||||
|
scheduledAt: string;
|
||||||
|
status: 'finished' | 'in-progress';
|
||||||
|
gameCount: number;
|
||||||
|
blueScore: number;
|
||||||
|
redScore: number;
|
||||||
|
winnerSide?: TeamSide;
|
||||||
|
games: Array<{
|
||||||
|
gameNumber: number;
|
||||||
|
riotMatchId: string | null;
|
||||||
|
winnerSide: TeamSide | null;
|
||||||
|
gameDuration: number;
|
||||||
|
}>;
|
||||||
|
teams: Array<{
|
||||||
|
side: TeamSide;
|
||||||
|
name: string;
|
||||||
|
members: Array<{
|
||||||
|
userId: string;
|
||||||
|
summonerName: string;
|
||||||
|
profileIconId: number;
|
||||||
|
}>;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LeaderboardResponse {
|
||||||
|
rows: LeaderboardRow[];
|
||||||
|
season: HistorySeasonDto;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LeaderboardRow {
|
||||||
|
userId: string;
|
||||||
|
summonerName: string;
|
||||||
|
profileIconId: number;
|
||||||
|
games: number;
|
||||||
|
wins: number;
|
||||||
|
losses: number;
|
||||||
|
winRate: number;
|
||||||
|
totalKills: number;
|
||||||
|
totalDeaths: number;
|
||||||
|
totalAssists: number;
|
||||||
|
kda: number;
|
||||||
|
avgGoldPerMin: number;
|
||||||
|
avgKillParticipation: number;
|
||||||
|
avgCsPerMin: number;
|
||||||
|
avgDamageShare: number;
|
||||||
|
avgVisionScore: number;
|
||||||
|
byLane: Partial<Record<RiotLane, { games: number; wins: number; kda: number }>>;
|
||||||
|
topChampions: Array<{ championId: number; games: number; winRate: number }>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LeaderHighlight {
|
||||||
|
metric: number;
|
||||||
|
userId: string;
|
||||||
|
summonerName: string;
|
||||||
|
championId?: number;
|
||||||
|
matchId?: string;
|
||||||
|
isSingleGame: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TeamInsightsResponse {
|
||||||
|
season: HistorySeasonDto;
|
||||||
|
sideWinRate: {
|
||||||
|
blue: number;
|
||||||
|
red: number;
|
||||||
|
bluePicks: number;
|
||||||
|
redPicks: number;
|
||||||
|
};
|
||||||
|
topKda: LeaderHighlight | null;
|
||||||
|
mostKills: LeaderHighlight | null;
|
||||||
|
topTeamGpm: LeaderHighlight | null;
|
||||||
|
topKillParticipation: LeaderHighlight | null;
|
||||||
|
bestFarm: LeaderHighlight | null;
|
||||||
|
minGamesThreshold: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface InternalMatchDetailResponse {
|
||||||
|
match: InternalMatchCardView;
|
||||||
|
games: Array<{
|
||||||
|
gameNumber: number;
|
||||||
|
riotMatchId: string;
|
||||||
|
gameCreation: number;
|
||||||
|
gameDuration: number;
|
||||||
|
teams: Array<{
|
||||||
|
side: TeamSide;
|
||||||
|
win: boolean;
|
||||||
|
objectives: { baron: number; dragon: number; tower: number };
|
||||||
|
participants: Array<{
|
||||||
|
userId: string | null;
|
||||||
|
summonerName: string;
|
||||||
|
side: TeamSide;
|
||||||
|
teamPosition: string;
|
||||||
|
championId: number;
|
||||||
|
kills: number;
|
||||||
|
deaths: number;
|
||||||
|
assists: number;
|
||||||
|
cs: number;
|
||||||
|
csPerMin: number;
|
||||||
|
gold: number;
|
||||||
|
gpm: number;
|
||||||
|
damageDealtToChampions: number;
|
||||||
|
damageShare: number;
|
||||||
|
killParticipation: number;
|
||||||
|
visionScore: number;
|
||||||
|
items: number[];
|
||||||
|
trinket: number;
|
||||||
|
summonerSpells: [number, number];
|
||||||
|
perks: { primaryStyle?: number; subStyle?: number; keystone?: number };
|
||||||
|
win: boolean;
|
||||||
|
}>;
|
||||||
|
}>;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserInternalHistoryResponse {
|
||||||
|
season: HistorySeasonDto;
|
||||||
|
championStats: LeaderboardChampionStats;
|
||||||
|
summary: {
|
||||||
|
windowSize: number;
|
||||||
|
winRate: number;
|
||||||
|
wins: number;
|
||||||
|
losses: number;
|
||||||
|
streak: { type: 'win' | 'loss'; count: number } | null;
|
||||||
|
kda: number;
|
||||||
|
avgKills: number;
|
||||||
|
avgDeaths: number;
|
||||||
|
avgAssists: number;
|
||||||
|
bestRating: {
|
||||||
|
rating: number | 'Perfect';
|
||||||
|
kills: number;
|
||||||
|
deaths: number;
|
||||||
|
assists: number;
|
||||||
|
matchId: string;
|
||||||
|
} | null;
|
||||||
|
positionRates: Partial<Record<RiotLane, number>>;
|
||||||
|
topChampions: Array<{
|
||||||
|
championId: number;
|
||||||
|
games: number;
|
||||||
|
winRate: number;
|
||||||
|
pickRate: number;
|
||||||
|
avgRating: number;
|
||||||
|
}>;
|
||||||
|
lastFetchedAt: string | null;
|
||||||
|
};
|
||||||
|
matches: RecentMatchView[];
|
||||||
|
nextCursor: number | null;
|
||||||
|
lastFetchedAt: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LeaderboardChampionStats {
|
||||||
|
queueGroup: 'internal';
|
||||||
|
rank: 'all';
|
||||||
|
queueIds: number[];
|
||||||
|
season: HistorySeasonDto;
|
||||||
|
lastFetchedAt: string | null;
|
||||||
|
champions: Array<{
|
||||||
|
championId: number;
|
||||||
|
games: number;
|
||||||
|
wins: number;
|
||||||
|
losses: number;
|
||||||
|
winRate: number;
|
||||||
|
avgKills: number;
|
||||||
|
avgDeaths: number;
|
||||||
|
avgAssists: number;
|
||||||
|
kda: number;
|
||||||
|
totalKills: number;
|
||||||
|
totalDeaths: number;
|
||||||
|
totalAssists: number;
|
||||||
|
doubleKills: number;
|
||||||
|
tripleKills: number;
|
||||||
|
quadraKills: number;
|
||||||
|
pentaKills: number;
|
||||||
|
avgCs: number;
|
||||||
|
avgCsPerMin: number;
|
||||||
|
}>;
|
||||||
|
totals: LeaderboardChampionStats['champions'][number];
|
||||||
|
}
|
||||||
Reference in new issue
Block a user