|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { TreeNodeInput } from 'nanovis' |
| 3 | +import type { ModuleInfo, SessionContext } from '~~/shared/types' |
| 4 | +import { Flamegraph, normalizeTreeNode } from 'nanovis' |
| 5 | +import { computed, defineProps, onMounted, ref, useTemplateRef } from 'vue' |
| 6 | +
|
| 7 | +const props = defineProps<{ |
| 8 | + info: ModuleInfo |
| 9 | + session: SessionContext |
| 10 | +}>() |
| 11 | +
|
| 12 | +const n = (node: TreeNodeInput<any>) => normalizeTreeNode(node, undefined, false) |
| 13 | +
|
| 14 | +const tree = computed(() => { |
| 15 | + const resolveIds = props.info.resolve_ids.map((id, idx) => n({ |
| 16 | + id: `resolveId-${idx}`, |
| 17 | + text: id.plugin_name, |
| 18 | + size: id.duration, |
| 19 | + })) |
| 20 | + const loads = props.info.loads.map((load, idx) => n({ |
| 21 | + id: `load-${idx}`, |
| 22 | + text: load.plugin_name, |
| 23 | + size: load.duration, |
| 24 | + })) |
| 25 | + const transforms = props.info.transforms.map((transform, idx) => n({ |
| 26 | + id: `transform-${idx}`, |
| 27 | + text: transform.plugin_name, |
| 28 | + size: transform.duration, |
| 29 | + })) |
| 30 | + const children = [ |
| 31 | + n({ |
| 32 | + id: '~resolves', |
| 33 | + text: 'resolve', |
| 34 | + children: resolveIds, |
| 35 | + }), |
| 36 | + n({ |
| 37 | + id: '~loads', |
| 38 | + text: 'load', |
| 39 | + children: loads, |
| 40 | + }), |
| 41 | + n({ |
| 42 | + id: '~transforms', |
| 43 | + text: 'transform', |
| 44 | + children: transforms, |
| 45 | + }), |
| 46 | + ] |
| 47 | +
|
| 48 | + return n({ |
| 49 | + id: '~root', |
| 50 | + text: 'Module Flamegraph', |
| 51 | + children, |
| 52 | + }) |
| 53 | +}) |
| 54 | +
|
| 55 | +const hoverNode = ref<{ |
| 56 | + plugin_name: string |
| 57 | + duration: number |
| 58 | +} | null>(null) |
| 59 | +const hoverX = ref<number>(0) |
| 60 | +const hoverY = ref<number>(0) |
| 61 | +const el = useTemplateRef<HTMLDivElement>('el') |
| 62 | +
|
| 63 | +onMounted(() => { |
| 64 | + const flamegraph = new Flamegraph(tree.value, { |
| 65 | + animate: true, |
| 66 | + palette: { |
| 67 | + fg: '#888', |
| 68 | + }, |
| 69 | + getSubtext: (node) => { |
| 70 | + const p = node.size / tree.value.size * 100 |
| 71 | + if (p > 15 && p !== 100) { |
| 72 | + return `${p.toFixed(1)}%` |
| 73 | + } |
| 74 | + return undefined |
| 75 | + }, |
| 76 | + onHover(node, e) { |
| 77 | + if (!node) { |
| 78 | + hoverNode.value = null |
| 79 | + return |
| 80 | + } |
| 81 | + if (e) { |
| 82 | + hoverX.value = e.clientX |
| 83 | + hoverY.value = e.clientY |
| 84 | + } |
| 85 | + hoverNode.value = { |
| 86 | + plugin_name: node.text!, |
| 87 | + duration: node.size, |
| 88 | + } |
| 89 | + }, |
| 90 | + }) |
| 91 | +
|
| 92 | + el.value!.appendChild(flamegraph.el) |
| 93 | +
|
| 94 | + return () => { |
| 95 | + flamegraph.dispose() |
| 96 | + } |
| 97 | +}) |
| 98 | +</script> |
| 99 | + |
| 100 | +<template> |
| 101 | + <div relative border="t base" pb10 py1> |
| 102 | + <Teleport to="body"> |
| 103 | + <div |
| 104 | + v-if="hoverNode" |
| 105 | + border="~ base" rounded shadow px2 py1 fixed |
| 106 | + z-panel-content bg-glass pointer-events-none text-sm |
| 107 | + :style="{ left: `${hoverX}px`, top: `${hoverY}px` }" |
| 108 | + > |
| 109 | + <div font-bold font-mono> |
| 110 | + {{ hoverNode.plugin_name }} |
| 111 | + </div> |
| 112 | + <DisplayDuration :duration="hoverNode.duration" /> |
| 113 | + </div> |
| 114 | + </Teleport> |
| 115 | + <div ref="el" min-h-30 /> |
| 116 | + </div> |
| 117 | +</template> |
0 commit comments