From 9f21696dcfa45c2587802b57733272aa670ad4b9 Mon Sep 17 00:00:00 2001 From: Sainan Date: Sat, 18 Jan 2025 16:35:52 +0100 Subject: [PATCH] chore: optimise DB query in stats/view.php ~30ms to ~20ms --- src/controllers/stats/viewController.ts | 4 ++-- src/services/inventoryService.ts | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/controllers/stats/viewController.ts b/src/controllers/stats/viewController.ts index e89759df..f7f6457d 100644 --- a/src/controllers/stats/viewController.ts +++ b/src/controllers/stats/viewController.ts @@ -1,14 +1,14 @@ import { RequestHandler } from "express"; import { getAccountIdForRequest } from "@/src/services/loginService"; -import { Inventory } from "@/src/models/inventoryModels/inventoryModel"; import { IStatsView } from "@/src/types/statTypes"; import { config } from "@/src/services/configService"; import allScans from "@/static/fixed_responses/allScans.json"; import { ExportEnemies } from "warframe-public-export-plus"; +import { getInventory } from "@/src/services/inventoryService"; const viewController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const inventory = await Inventory.findOne({ accountOwnerId: accountId }); + const inventory = await getInventory(accountId, "XPInfo"); if (!inventory) { res.status(400).json({ error: "inventory was undefined" }); return; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index e7567772..c58a17df 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -96,8 +96,11 @@ export const combineInventoryChanges = (InventoryChanges: IInventoryChanges, del } }; -export const getInventory = async (accountOwnerId: string): Promise => { - const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId }); +export const getInventory = async ( + accountOwnerId: string, + projection: string | undefined = undefined +): Promise => { + const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId }, projection); if (!inventory) { throw new Error(`Didn't find an inventory for ${accountOwnerId}`);