Skip to content

Commit

Permalink
Merge pull request #133 from game-node-app/dev
Browse files Browse the repository at this point in the history
Greatly improves trending/games performance
  • Loading branch information
Lamarcke authored Dec 11, 2024
2 parents 4d2da42 + 497aafb commit 1ee5c4c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/statistics/dto/find-statistics-trending-games.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ import { GameRepositoryFilterDto } from "../../game/game-repository/dto/game-rep
import { IsEnum, IsNotEmpty, IsOptional } from "class-validator";
import { StatisticsPeriod } from "../statistics.constants";
import { OmitType } from "@nestjs/swagger";
import { Transform } from "class-transformer";

export class FindStatisticsTrendingGamesDto extends OmitType(
BaseFindDto<Statistics>,
["orderBy", "search"],
) {
@IsOptional()
@Transform(({ value }) => {
const parsedCriteria: GameRepositoryFilterDto = {
...value,
limit: undefined,
offset: undefined,
};

return parsedCriteria;
})
criteria?: GameRepositoryFilterDto;
@IsNotEmpty()
@IsEnum(StatisticsPeriod)
Expand Down
12 changes: 11 additions & 1 deletion src/statistics/game-statistics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { hours } from "@nestjs/throttler";
import { GameRepositoryService } from "../game/game-repository/game-repository.service";
import { GameFilterService } from "../game/game-filter/game-filter.service";
import { MATURE_THEME_ID } from "../game/game-filter/game-filter.constants";
import isEmptyObject from "../utils/isEmptyObject";

@Injectable()
export class GameStatisticsService implements StatisticsService {
Expand Down Expand Up @@ -176,7 +177,7 @@ export class GameStatisticsService implements StatisticsService {
.where(`(uv.createdAt >= :uvDate OR s.viewsCount = 0)`, {
uvDate: viewsStartDate,
})
// Excludes games with specific themes
// Excludes games with mature theme
.andWhere(
`NOT EXISTS (SELECT 1 FROM game_themes_game_theme AS gtgt WHERE gtgt.gameId = s.gameId
AND gtgt.gameThemeId = :excludedThemeId)`,
Expand All @@ -191,6 +192,15 @@ export class GameStatisticsService implements StatisticsService {

const statistics = await query.getMany();

// This greatly improves performance when no filtering is actually being done.
if (criteria == undefined || isEmptyObject(criteria)) {
const slicedStatistics = statistics.slice(
offsetToUse,
offsetToUse + limitToUse,
);
return [slicedStatistics, fixedStatisticsLimit];
}

const gameIds = statistics.map((s) => s.gameId);
const games = await this.gameRepositoryService.findAllIdsWithFilters({
...criteria,
Expand Down
13 changes: 12 additions & 1 deletion src/utils/isEmptyObject.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* Checks if a given object is empty ({})
* Note: this returns FALSE if 'obj' is null.
* @param obj
* @example
* const obj1 = {}
* isEmptyObject(obj1) // true
*
* const obj2 = null
* isEmptyObject(obj2) // false
*/
export default function isEmptyObject(obj: any) {
return JSON.stringify(obj) === JSON.stringify({});
return JSON.stringify(obj) === "{}";
}

0 comments on commit 1ee5c4c

Please sign in to comment.