-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cache): cache game thumbnails (#5430)
* feat(cache): cache game thumbnails * update
- Loading branch information
Showing
10 changed files
with
157 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/database/migration/mysql/16.0.x/1661348421631-cacheTitleChangeToIdAddThumbnail.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class cacheTitleChangeToIdAddThumbnail1661348421631 implements MigrationInterface { | ||
name = 'cacheTitleChangeToIdAddThumbnail1661348421631'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE \`cache_games\` ADD \`thumbnail\` varchar(255) NULL`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
return; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/database/migration/postgres/16.0.x/1661348421631-cacheTitleChangeToIdAddThumbnail.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class cacheTitleChangeToIdAddThumbnail1661348421631 implements MigrationInterface { | ||
name = 'cacheTitleChangeToIdAddThumbnail1661348421631'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "cache_games" ADD "thumbnail" character varying`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
return; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/database/migration/sqlite/16.0.x/1661348421631-cacheTitleChangeToIdAddThumbnail.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
import { insertItemIntoTable } from '~/database/insertItemIntoTable'; | ||
|
||
export class cacheTitleChangeToIdAddThumbnail1661348421631 implements MigrationInterface { | ||
name = 'cacheTitleChangeToIdAddThumbnail1661348421631'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
const items = await queryRunner.query(`SELECT * from "cache_games"`); | ||
const items2 = await queryRunner.query(`SELECT * from "cache_titles"`); | ||
await queryRunner.query(`DELETE from "cache_titles" WHERE 1=1`); | ||
await queryRunner.query(`DROP TABLE "cache_games"`); | ||
await queryRunner.query(`CREATE TABLE "cache_games" ("id" integer PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "thumbnail" varchar)`); | ||
await queryRunner.query(`CREATE INDEX "IDX_f37be3c66dbd449a8cb4fe7d59" ON "cache_games" ("name") `); | ||
|
||
for (const title of items2) { | ||
const game = items.find((o: any) => o.name === title.game); | ||
if (game) { | ||
title.game = game.name; | ||
await insertItemIntoTable('cache_titles', { | ||
...title, | ||
}, queryRunner); | ||
} | ||
} | ||
|
||
for (const item of items) { | ||
await insertItemIntoTable('cache_games', { | ||
...item, | ||
}, queryRunner); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
return; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { CacheGames } from '@entity/cacheGames'; | ||
import { getRepository } from 'typeorm'; | ||
|
||
import client from '../api/client'; | ||
|
||
import { debug, isDebugEnabled, warning } from '~/helpers/log'; | ||
|
||
async function getGameThumbnailFromName (name: string): Promise<string | undefined> { | ||
if (isDebugEnabled('api.calls')) { | ||
debug('api.calls', new Error().stack); | ||
} | ||
const gameFromDb = await getRepository(CacheGames).findOne({ name }); | ||
// check if name is cached | ||
if (gameFromDb && gameFromDb.thumbnail) { | ||
return String(gameFromDb.thumbnail); | ||
} | ||
|
||
try { | ||
const clientBot = await client('bot'); | ||
const getGameByName = await clientBot.games.getGameByName(name); | ||
if (!getGameByName) { | ||
return undefined; | ||
} | ||
// add id->game to cache | ||
const id = Number(getGameByName.id); | ||
await getRepository(CacheGames).save({ id, name, thumbnail: getGameByName.boxArtUrl }); | ||
return String(id); | ||
} catch (e: unknown) { | ||
if (e instanceof Error) { | ||
warning(`getGameThumbnailFromName => ${e.stack ?? e.message}`); | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
export { getGameThumbnailFromName }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import client from '../api/client'; | ||
import { refresh } from '../token/refresh.js'; | ||
|
||
import { getFunctionName } from '~/helpers/getFunctionName'; | ||
import { debug, error, isDebugEnabled, warning } from '~/helpers/log'; | ||
|
||
async function searchCategoriesPaginated (game: string) { | ||
if (isDebugEnabled('api.calls')) { | ||
debug('api.calls', new Error().stack); | ||
} | ||
try { | ||
const clientBot = await client('bot'); | ||
return await clientBot.search.searchCategoriesPaginated(game).getAll(); | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
if (e.message.includes('Invalid OAuth token')) { | ||
warning(`${getFunctionName()} => Invalid OAuth token - attempting to refresh token`); | ||
await refresh('bot'); | ||
} else { | ||
error(`${getFunctionName()} => ${e.stack ?? e.message}`); | ||
} | ||
} | ||
return; | ||
} | ||
} | ||
|
||
export { searchCategoriesPaginated }; |