Skip to content

Commit

Permalink
Test getAllMedia
Browse files Browse the repository at this point in the history
  • Loading branch information
Natsume-197 committed Sep 15, 2024
1 parent f78b97e commit 0c30929
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 88 deletions.
65 changes: 44 additions & 21 deletions backend/controllers/mediaController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const requestIp = require('request-ip');

import {querySegments, querySurroundingSegments, queryWordsMatched} from "../external/elasticsearch";
import {queryMediaInfo} from "../external/database_queries";
import {QueryMediaInfoResponse} from "../models/external/queryMediaInfoResponse";
import {MediaInfoStats} from "../models/external/queryMediaInfoResponse";
import {GetAllAnimesResponse} from "../models/controller/GetAllAnimesResponse";
import {SearchAnimeSentencesRequest} from "../models/controller/SearchAnimeSentencesRequest";
import {ControllerRequest, ControllerResponse, getBaseUrlMedia, getBaseUrlTmp} from "../utils/utils";
Expand All @@ -28,7 +28,8 @@ import {GetContextAnimeResponse} from "../models/controller/GetContextAnimeRespo
import { SaveUserSearchHistory } from "./databaseController";
import { EventTypeHistory } from "../models/miscellaneous/userSearchHistory"
import { Segment } from "../models/media/segment";
import { CategoryType } from "../models/media/media"
import { CategoryType,Media } from "../models/media/media"
import { Op } from 'sequelize';

const tmpDirectory: string = process.env.TMP_DIRECTORY!;

Expand Down Expand Up @@ -203,41 +204,63 @@ export const getAllMedia = async (
const pageSize = parseInt(req.query.size as string) || 20;
const searchQuery = typeof req.query.query === 'string' ? req.query.query : '';
const cursor = req.query.cursor ? parseInt(req.query.cursor as string) : 0;
const type = typeof req.query.type === 'string' ? req.query.type.toLowerCase() : '';
const type = typeof req.query.type === 'string' ? req.query.type.toLowerCase() : '';

const response: QueryMediaInfoResponse = await queryMediaInfo();

let results = Object.values(response.results);
const page = Math.floor(cursor / pageSize) + 1;

const categoryMap: Record<string, CategoryType> = {
anime: CategoryType.ANIME,
liveaction: CategoryType.JDRAMA,
};

const whereClause: any = {};
if (type && categoryMap[type]) {
const selectedCategory = categoryMap[type];
results = results.filter((media) => media.category === selectedCategory);
whereClause.category = categoryMap[type];
}

if (searchQuery) {
results = results.filter(
(media) =>
media.english_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
media.japanese_name.includes(searchQuery)
);
whereClause[Op.or] = [
{ english_name: { [Op.iLike]: `%${searchQuery}%` } },
{ japanese_name: { [Op.iLike]: `%${searchQuery}%` } },
{ romaji_name: { [Op.iLike]: `%${searchQuery}%` } }
];
}

const paginatedResults = results.slice(cursor, cursor + pageSize);
const nextCursor = cursor + pageSize;
const hasMoreResults = nextCursor < results.length;
const { count, rows } = await Media.findAndCountAll({
where: whereClause,
order: [['created_at', 'DESC']],
limit: pageSize,
offset: cursor
});

const mediaInfo = await queryMediaInfo(page, pageSize);

return res.status(StatusCodes.OK).json({
stats: response.stats,
const paginatedResults = rows.map(media => {
const mediaData = media.toJSON();
const location_media = mediaData.category === CategoryType.ANIME ? 'anime' : 'jdrama';
mediaData.cover = [getBaseUrlMedia(), location_media, mediaData.cover].join("/");
mediaData.banner = [getBaseUrlMedia(), location_media, mediaData.banner].join("/");
return mediaData;
});

const nextCursor = cursor + paginatedResults.length;
const hasMoreResults = nextCursor < count;

const stats: MediaInfoStats = {
total_animes: count,
total_segments: paginatedResults.reduce((sum, media) => sum + media.num_segments, 0),
full_total_animes: mediaInfo.stats.full_total_animes,
full_total_segments: mediaInfo.stats.full_total_segments,
};

const response: GetAllAnimesResponse = {
stats,
results: paginatedResults,
cursor: hasMoreResults ? nextCursor : null,
hasMoreResults: hasMoreResults
});

hasMoreResults
};

return res.status(StatusCodes.OK).json(response);
} catch (error) {
next(error);
}
Expand Down
116 changes: 49 additions & 67 deletions backend/external/database_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
QueryMediaInfoResponse,
} from "../models/external/queryMediaInfoResponse";
import { getBaseUrlMedia } from "../utils/utils";
import { Media } from '../models/media/media'

let MEDIA_TABLE_CACHE: QueryMediaInfoResponse | undefined = undefined;

Expand All @@ -13,92 +14,73 @@ export const queryMediaInfo = async (
page: number = 1,
pageSize: number = 10
): Promise<QueryMediaInfoResponse> => {
// TODO: Fix cache
//if(MEDIA_TABLE_CACHE === undefined) {
//}
await refreshMediaInfoCache(page, pageSize);

if (MEDIA_TABLE_CACHE === undefined) {
await refreshMediaInfoCache(page, pageSize);
}
return MEDIA_TABLE_CACHE!;
};

export const refreshMediaInfoCache = async (page: number, pageSize: number) => {
let size_positino_filter = ''
if(page == 0){
size_positino_filter = `LIMIT ${pageSize}`
}else{
const offset = (page - 1) * pageSize;
size_positino_filter = `LIMIT ${pageSize} OFFSET ${offset}`
}
const sql = `SELECT
json_build_object(
'media_id', me.id,
'category', me.category,
'created_at', me.created_at,
'updated_at', me.updated_at,
'romaji_name', me.romaji_name,
'english_name', me.english_name,
'japanese_name', me.japanese_name,
'airing_format', me.airing_format,
'airing_status', me.airing_status,
'folder_media_name', me.folder_media_name,
'genres', me.genres,
'cover', me.cover,
'banner', me.banner,
'release_date', me.release_date,
'version', me.version,
'num_segments', me.num_segments,
'num_seasons', me.num_seasons,
'num_episodes', me.num_episodes
) AS media_info
FROM
nadedb.public."Media" me
GROUP BY
me.id, me.romaji_name, me.english_name, me.japanese_name
ORDER BY me.created_at DESC
${size_positino_filter}`;
const offset = (page - 1) * pageSize;

const sql_full = 'SELECT ( SELECT COUNT(*) FROM nadedb.public."Media" ) AS count1, ( SELECT COUNT(*) FROM nadedb.public."Segment" ) AS count2'

const queryResponse = await connection.query(sql);
const queryResponseFull = await connection.query(sql_full);
//@ts-ignore
const full_total_animes = parseInt(queryResponseFull[0][0].count1, 10);
//@ts-ignore
const full_total_segments = parseInt(queryResponseFull[0][0].count2, 10);
const [mediaResults, counts] = await Promise.all([
Media.findAll({
attributes: [
'id', 'category', 'created_at', 'updated_at', 'romaji_name', 'english_name',
'japanese_name', 'airing_format', 'airing_status', 'folder_media_name',
'genres', 'cover', 'banner', 'release_date', 'version',
'num_segments', 'num_seasons', 'num_episodes'
],
order: [['created_at', 'DESC']],
limit: pageSize,
offset: offset
}),
Media.count(),
Media.sum('num_segments')
]);

const results: { [key: number]: MediaInfoData } = {};
let total_animes = 0;
let total_segments = 0;

queryResponse[0].forEach((result: any) => {
if (!("media_id" in result.media_info)) {
console.log("WARN: Invalid query, media_id not found");
return;
}

let location_media = result.media_info.category == 1 ? 'anime' : 'jdrama'
result.media_info.cover = [getBaseUrlMedia(), location_media ,result.media_info.cover].join(
"/"
);
result.media_info.banner = [
getBaseUrlMedia(),
location_media,
result.media_info.banner,
].join("/");
mediaResults.forEach((media: any) => {
const mediaInfo: MediaInfoData = {
media_id: media.id,
category: media.category,
created_at: media.created_at.toISOString(),
updated_at: media.updated_at ? Math.floor(media.updated_at.getTime() / 1000) : undefined,
romaji_name: media.romaji_name,
english_name: media.english_name,
japanese_name: media.japanese_name,
airing_format: media.airing_format,
airing_status: media.airing_status,
folder_media_name: media.folder_media_name,
genres: media.genres,
cover: media.cover,
banner: media.banner,
release_date: media.release_date,
version: media.version,
num_segments: media.num_segments,
num_seasons: media.num_seasons,
num_episodes: media.num_episodes
};

results[Number(result.media_info["media_id"])] = result.media_info;
let location_media = mediaInfo.category == 1 ? 'anime' : 'jdrama';
mediaInfo.cover = [getBaseUrlMedia(), location_media, mediaInfo.cover].join("/");
mediaInfo.banner = [getBaseUrlMedia(), location_media, mediaInfo.banner].join("/");

total_segments += result.media_info.num_segments;
results[mediaInfo.media_id] = mediaInfo;
total_segments += mediaInfo.num_segments;
total_animes += 1;
});

MEDIA_TABLE_CACHE = {
stats: {
total_animes,
full_total_animes,
full_total_segments,
full_total_animes: counts,
full_total_segments: counts[1] || 0,
total_segments,
},
results,
};
};
};

0 comments on commit 0c30929

Please sign in to comment.