Enhance getGifts method in FalukantService with detailed performance metrics and optimized data retrieval

- Implemented timing metrics to track performance across various steps of the getGifts method, improving traceability.
- Refactored data loading to optimize user, character, and relationship queries, ensuring only necessary fields are fetched.
- Enhanced error handling and logging for better debugging and performance insights during execution.
This commit is contained in:
Torsten Schulz (local)
2026-01-12 11:49:49 +01:00
parent d2ac2bfdd8
commit ec113058d0

View File

@@ -3566,7 +3566,12 @@ class FalukantService extends BaseService {
}
async getGifts(hashedUserId) {
const startTime = Date.now();
const timings = {};
try {
// 1) User & Character optimiert laden (nur benötigte Felder)
const step1Start = Date.now();
const user = await FalukantUser.findOne({
include: [
{ model: User, as: 'user', attributes: ['hashedId'], where: { hashedId: hashedUserId } },
@@ -3581,8 +3586,10 @@ class FalukantService extends BaseService {
if (!user) throw new Error('User not found');
const myChar = user.character;
if (!myChar) throw new Error('Character not found');
timings.step1_user_character = Date.now() - step1Start;
// 2) Beziehung finden und „anderen" Character bestimmen (optimiert: nur benötigte Felder)
const step2Start = Date.now();
const rel = await Relationship.findOne({
where: {
[Op.or]: [
@@ -3619,8 +3626,10 @@ class FalukantService extends BaseService {
}
]
});
timings.step2_relationship = Date.now() - step2Start;
// 3) Wenn keine Beziehung gefunden, alle Gifts ohne Filter zurückgeben
const step3Start = Date.now();
let relatedTraitIds = [];
let relatedMoodId = null;
@@ -3630,8 +3639,10 @@ class FalukantService extends BaseService {
relatedTraitIds = relatedChar.traits ? relatedChar.traits.map(t => t.id) : [];
relatedMoodId = relatedChar.moodId;
}
timings.step3_process_relationship = Date.now() - step3Start;
// 4) Gifts laden mit Mood/Trait-Filter nur wenn Beziehung existiert
const step4Start = Date.now();
const giftIncludes = [
{
model: PromotionalGiftMood,
@@ -3654,17 +3665,21 @@ class FalukantService extends BaseService {
if (rel && relatedTraitIds.length > 0) {
giftIncludes[1].where = { trait_id: { [Op.in]: relatedTraitIds } };
}
timings.step4_prepare_gift_includes = Date.now() - step4Start;
// 5) Parallel: Gifts und lowestTitleOfNobility laden
const step5Start = Date.now();
const [gifts, lowestTitleOfNobility] = await Promise.all([
PromotionalGift.findAll({
include: giftIncludes
}),
TitleOfNobility.findOne({ order: [['id', 'ASC']] })
]);
timings.step5_load_gifts_and_title = Date.now() - step5Start;
// 6) Kosten berechnen (getGiftCost ist synchron, kein await nötig)
return gifts.map(gift => ({
const step6Start = Date.now();
const result = gifts.map(gift => ({
id: gift.id,
name: gift.name,
cost: this.getGiftCost(
@@ -3675,6 +3690,16 @@ class FalukantService extends BaseService {
moodsAffects: gift.promotionalgiftmoods, // nur Einträge mit relatedMoodId
charactersAffects: gift.characterTraits // nur Einträge mit relatedTraitIds
}));
timings.step6_calculate_costs = Date.now() - step6Start;
const totalTime = Date.now() - startTime;
console.log(`[getGifts] Performance: ${totalTime}ms total`, timings);
return result;
} catch (error) {
console.error('[getGifts] Error:', error);
throw error;
}
}
async getChildren(hashedUserId) {