reverse pedigree

This commit is contained in:
2026-03-12 07:27:41 -05:00
parent 5ca594fdc7
commit 42bab14ac3
2 changed files with 143 additions and 67 deletions

View File

@@ -180,4 +180,48 @@ export const getPedigreeCompleteness = (treeData, targetGenerations = 5) => {
const expectedTotal = Math.pow(2, targetGenerations) - 1
const actualCount = countAncestors(treeData)
return Math.min(100, Math.round((actualCount / expectedTotal) * 100))
}
/**
* Transform API descendant data to react-d3-tree format
* @param {Object} dog - Dog object from API with nested offspring array
* @param {number} maxGenerations - Maximum generations to display (default 3)
* @returns {Object} Tree data in react-d3-tree format
*/
export const transformDescendantData = (dog, maxGenerations = 3) => {
if (!dog) return null
const buildTree = (dogData, generation = 0) => {
if (!dogData || generation >= maxGenerations) {
return null
}
const node = {
name: dogData.name || 'Unknown',
attributes: {
id: dogData.id,
sex: dogData.sex,
registration: dogData.registration_number || '',
birth_year: dogData.birth_date ? new Date(dogData.birth_date).getFullYear() : ''
},
children: []
}
if (dogData.offspring && dogData.offspring.length > 0) {
dogData.offspring.forEach(child => {
const childNode = buildTree(child, generation + 1)
if (childNode) {
node.children.push(childNode)
}
})
}
if (node.children.length === 0) {
delete node.children
}
return node
}
return buildTree(dog)
}