Refactor relationship retrieval in FalukantService for improved performance
- Optimized the process of finding relationships by using two separate queries for better index utilization, reducing data overhead. - Enhanced loading of related characters and traits by implementing parallel data fetching, improving efficiency in data retrieval. - Updated timing metrics to reflect changes in the relationship loading process, ensuring accurate performance tracking.
This commit is contained in:
@@ -3588,58 +3588,47 @@ class FalukantService extends BaseService {
|
|||||||
if (!myChar) throw new Error('Character not found');
|
if (!myChar) throw new Error('Character not found');
|
||||||
timings.step1_user_character = Date.now() - step1Start;
|
timings.step1_user_character = Date.now() - step1Start;
|
||||||
|
|
||||||
// 2) Beziehung finden und „anderen" Character bestimmen (optimiert: nur benötigte Felder)
|
// 2) Beziehung finden (zwei separate Queries für bessere Index-Nutzung)
|
||||||
const step2Start = Date.now();
|
const step2Start = Date.now();
|
||||||
const rel = await Relationship.findOne({
|
const [relAsChar1, relAsChar2] = await Promise.all([
|
||||||
where: {
|
Relationship.findOne({
|
||||||
[Op.or]: [
|
where: { character1Id: myChar.id },
|
||||||
{ character1Id: myChar.id },
|
attributes: ['character1Id', 'character2Id']
|
||||||
{ character2Id: myChar.id }
|
}),
|
||||||
]
|
Relationship.findOne({
|
||||||
},
|
where: { character2Id: myChar.id },
|
||||||
include: [
|
attributes: ['character1Id', 'character2Id']
|
||||||
{
|
})
|
||||||
model: FalukantCharacter,
|
]);
|
||||||
as: 'character1',
|
const rel = relAsChar1 || relAsChar2;
|
||||||
attributes: ['id', 'moodId'],
|
|
||||||
include: [{
|
|
||||||
model: CharacterTrait,
|
|
||||||
as: 'traits',
|
|
||||||
attributes: ['id'],
|
|
||||||
through: { attributes: [] },
|
|
||||||
required: false
|
|
||||||
}],
|
|
||||||
required: false
|
|
||||||
},
|
|
||||||
{
|
|
||||||
model: FalukantCharacter,
|
|
||||||
as: 'character2',
|
|
||||||
attributes: ['id', 'moodId'],
|
|
||||||
include: [{
|
|
||||||
model: CharacterTrait,
|
|
||||||
as: 'traits',
|
|
||||||
attributes: ['id'],
|
|
||||||
through: { attributes: [] },
|
|
||||||
required: false
|
|
||||||
}],
|
|
||||||
required: false
|
|
||||||
}
|
|
||||||
]
|
|
||||||
});
|
|
||||||
timings.step2_relationship = Date.now() - step2Start;
|
timings.step2_relationship = Date.now() - step2Start;
|
||||||
|
|
||||||
// 3) Wenn keine Beziehung gefunden, alle Gifts ohne Filter zurückgeben
|
// 3) Related Character und Traits laden (nur wenn Relationship existiert)
|
||||||
const step3Start = Date.now();
|
const step3Start = Date.now();
|
||||||
let relatedTraitIds = [];
|
let relatedTraitIds = [];
|
||||||
let relatedMoodId = null;
|
let relatedMoodId = null;
|
||||||
|
|
||||||
if (rel) {
|
if (rel) {
|
||||||
const relatedChar = rel.character1.id === myChar.id ? rel.character2 : rel.character1;
|
const relatedCharId = rel.character1Id === myChar.id ? rel.character2Id : rel.character1Id;
|
||||||
// Trait-IDs und Mood des relatedChar
|
|
||||||
relatedTraitIds = relatedChar.traits ? relatedChar.traits.map(t => t.id) : [];
|
// Parallel: Character (moodId) und Traits laden
|
||||||
|
const [relatedChar, traitRows] = await Promise.all([
|
||||||
|
FalukantCharacter.findOne({
|
||||||
|
where: { id: relatedCharId },
|
||||||
|
attributes: ['id', 'moodId']
|
||||||
|
}),
|
||||||
|
FalukantCharacterTrait.findAll({
|
||||||
|
where: { characterId: relatedCharId },
|
||||||
|
attributes: ['traitId']
|
||||||
|
})
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (relatedChar) {
|
||||||
relatedMoodId = relatedChar.moodId;
|
relatedMoodId = relatedChar.moodId;
|
||||||
|
relatedTraitIds = traitRows.map(t => t.traitId);
|
||||||
}
|
}
|
||||||
timings.step3_process_relationship = Date.now() - step3Start;
|
}
|
||||||
|
timings.step3_load_character_and_traits = Date.now() - step3Start;
|
||||||
|
|
||||||
// 4) Gifts laden – mit Mood/Trait-Filter nur wenn Beziehung existiert
|
// 4) Gifts laden – mit Mood/Trait-Filter nur wenn Beziehung existiert
|
||||||
const step4Start = Date.now();
|
const step4Start = Date.now();
|
||||||
|
|||||||
Reference in New Issue
Block a user