|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { TreeNodeInput } from 'nanovis' |
| 3 | +import type { ViteModuleListItem } from '~/types/modules' |
| 4 | +import DisplayDuration from '@vitejs/devtools-ui/components/DisplayDuration.vue' |
| 5 | +import { Flamegraph, normalizeTreeNode } from 'nanovis' |
| 6 | +import { computed, nextTick, onMounted, onUnmounted, ref, shallowRef, useTemplateRef, watch } from 'vue' |
| 7 | +import { normalizeTimestamp } from '~/utils/format' |
| 8 | +
|
| 9 | +interface FlowTransform { |
| 10 | + name: string |
| 11 | + result?: string | null |
| 12 | + start: number |
| 13 | + end: number |
| 14 | +} |
| 15 | +
|
| 16 | +interface ViteModuleFlamegraphItem { |
| 17 | + plugin_name: string |
| 18 | + duration: number |
| 19 | + timestamp_start: number |
| 20 | + timestamp_end: number |
| 21 | +} |
| 22 | +
|
| 23 | +interface ViteModuleFlamegraphInfo { |
| 24 | + resolve_ids: ViteModuleFlamegraphItem[] |
| 25 | + loads: ViteModuleFlamegraphItem[] |
| 26 | + transforms: ViteModuleFlamegraphItem[] |
| 27 | +} |
| 28 | +
|
| 29 | +const props = withDefaults(defineProps<{ |
| 30 | + module: ViteModuleListItem |
| 31 | + transforms: FlowTransform[] |
| 32 | + flowNodeSelected?: boolean |
| 33 | +}>(), { |
| 34 | + flowNodeSelected: false, |
| 35 | +}) |
| 36 | +
|
| 37 | +const n = (node: TreeNodeInput<any>) => normalizeTreeNode(node, undefined, false) |
| 38 | +
|
| 39 | +const info = computed<ViteModuleFlamegraphInfo>(() => { |
| 40 | + const first = props.transforms[0] |
| 41 | + const load = first && first.name !== '__load__' |
| 42 | + ? [{ |
| 43 | + plugin_name: first.name, |
| 44 | + duration: Math.max(0, first.end - first.start), |
| 45 | + timestamp_start: first.start, |
| 46 | + timestamp_end: first.end, |
| 47 | + }] |
| 48 | + : [] |
| 49 | +
|
| 50 | + return { |
| 51 | + resolve_ids: props.module.plugins |
| 52 | + .filter(plugin => plugin.resolveId != null) |
| 53 | + .map((plugin, index) => ({ |
| 54 | + plugin_name: plugin.name, |
| 55 | + duration: plugin.resolveId ?? 0, |
| 56 | + timestamp_start: index, |
| 57 | + timestamp_end: index + (plugin.resolveId ?? 0), |
| 58 | + })), |
| 59 | + loads: load, |
| 60 | + transforms: props.transforms |
| 61 | + .slice(1) |
| 62 | + .filter(transform => transform.name !== '__load__') |
| 63 | + .map(transform => ({ |
| 64 | + plugin_name: transform.name, |
| 65 | + duration: Math.max(0, transform.end - transform.start), |
| 66 | + timestamp_start: transform.start, |
| 67 | + timestamp_end: transform.end, |
| 68 | + })), |
| 69 | + } |
| 70 | +}) |
| 71 | +
|
| 72 | +const tree = computed(() => { |
| 73 | + const resolveIds = info.value.resolve_ids.map((id, idx) => n({ |
| 74 | + id: `resolveId-${idx}`, |
| 75 | + text: id.plugin_name, |
| 76 | + size: id.duration, |
| 77 | + meta: id, |
| 78 | + })) |
| 79 | + const loads = info.value.loads.map((load, idx) => n({ |
| 80 | + id: `load-${idx}`, |
| 81 | + text: load.plugin_name, |
| 82 | + size: load.duration, |
| 83 | + meta: load, |
| 84 | + })) |
| 85 | + const transforms = info.value.transforms.map((transform, idx) => n({ |
| 86 | + id: `transform-${idx}`, |
| 87 | + text: transform.plugin_name, |
| 88 | + size: transform.duration, |
| 89 | + meta: transform, |
| 90 | + })) |
| 91 | + const children = [ |
| 92 | + n({ |
| 93 | + id: '~resolves', |
| 94 | + text: 'Resolve Id', |
| 95 | + children: resolveIds, |
| 96 | + }), |
| 97 | + n({ |
| 98 | + id: '~loads', |
| 99 | + text: 'Load', |
| 100 | + children: loads, |
| 101 | + }), |
| 102 | + n({ |
| 103 | + id: '~transforms', |
| 104 | + text: 'Transform', |
| 105 | + children: transforms, |
| 106 | + }), |
| 107 | + ] |
| 108 | +
|
| 109 | + return n({ |
| 110 | + id: '~root', |
| 111 | + text: 'Module Flamegraph', |
| 112 | + children, |
| 113 | + }) |
| 114 | +}) |
| 115 | +
|
| 116 | +const hoverNode = ref<{ |
| 117 | + plugin_name: string |
| 118 | + duration: number |
| 119 | + meta: ViteModuleFlamegraphItem | undefined |
| 120 | +} | null>(null) |
| 121 | +const hoverX = ref<number>(0) |
| 122 | +const hoverY = ref<number>(0) |
| 123 | +const el = useTemplateRef<HTMLDivElement>('el') |
| 124 | +const flamegraph = shallowRef<Flamegraph | null>(null) |
| 125 | +
|
| 126 | +function buildFlamegraph() { |
| 127 | + flamegraph.value = new Flamegraph(tree.value, { |
| 128 | + animate: true, |
| 129 | + palette: { |
| 130 | + fg: '#888', |
| 131 | + }, |
| 132 | + getSubtext: (node) => { |
| 133 | + const p = node.size / tree.value.size * 100 |
| 134 | + if (p > 15 && p !== 100) { |
| 135 | + return `${p.toFixed(1)}%` |
| 136 | + } |
| 137 | + return undefined |
| 138 | + }, |
| 139 | + onHover(node, e) { |
| 140 | + if (!node) { |
| 141 | + hoverNode.value = null |
| 142 | + return |
| 143 | + } |
| 144 | + if (e) { |
| 145 | + hoverX.value = e.clientX |
| 146 | + hoverY.value = e.clientY |
| 147 | + } |
| 148 | + hoverNode.value = { |
| 149 | + plugin_name: node.text!, |
| 150 | + duration: node.size, |
| 151 | + meta: node.meta, |
| 152 | + } |
| 153 | + }, |
| 154 | + onLeave() { |
| 155 | + hoverNode.value = null |
| 156 | + }, |
| 157 | + }) |
| 158 | + el.value!.appendChild(flamegraph.value!.el) |
| 159 | +} |
| 160 | +
|
| 161 | +function disposeFlamegraph() { |
| 162 | + flamegraph.value?.dispose() |
| 163 | +} |
| 164 | +
|
| 165 | +onMounted(() => { |
| 166 | + buildFlamegraph() |
| 167 | +}) |
| 168 | +
|
| 169 | +onUnmounted(() => { |
| 170 | + disposeFlamegraph() |
| 171 | +}) |
| 172 | +
|
| 173 | +watch(tree, async () => { |
| 174 | + disposeFlamegraph() |
| 175 | + buildFlamegraph() |
| 176 | +}, { |
| 177 | + deep: true, |
| 178 | +}) |
| 179 | +
|
| 180 | +watch(() => props.flowNodeSelected, async () => { |
| 181 | + await nextTick() |
| 182 | + flamegraph.value?.resize() |
| 183 | +}) |
| 184 | +</script> |
| 185 | + |
| 186 | +<template> |
| 187 | + <div relative border="t base" pb10 py1 mt4> |
| 188 | + <DisplayGraphHoverView :hover-x="hoverX" :hover-y="hoverY"> |
| 189 | + <div |
| 190 | + v-if="hoverNode" |
| 191 | + border="~ base" rounded-lg shadow-lg px3 py2 |
| 192 | + bg-glass pointer-events-none text-sm max-w-80 |
| 193 | + > |
| 194 | + <div font-semibold font-mono text-base mb2> |
| 195 | + {{ hoverNode.plugin_name }} |
| 196 | + </div> |
| 197 | + <div v-if="hoverNode.meta" border="t base" pt2 flex="~ col gap-1.5" min-w-48> |
| 198 | + <div flex="~ justify-between items-center" py1> |
| 199 | + <label text-xs opacity-70>Start Time</label> |
| 200 | + <time |
| 201 | + :datetime="new Date(hoverNode.meta.timestamp_start).toISOString()" |
| 202 | + font-mono text="xs" |
| 203 | + bg="base/10" |
| 204 | + px1.5 py0.5 rounded |
| 205 | + > |
| 206 | + {{ normalizeTimestamp(hoverNode.meta.timestamp_start) }} |
| 207 | + </time> |
| 208 | + </div> |
| 209 | + <div flex="~ justify-between items-center" py1> |
| 210 | + <label text-xs opacity-70>End Time</label> |
| 211 | + <time |
| 212 | + :datetime="new Date(hoverNode.meta.timestamp_end).toISOString()" |
| 213 | + font-mono text="xs" |
| 214 | + bg="base/10" |
| 215 | + px1.5 py0.5 rounded |
| 216 | + > |
| 217 | + {{ normalizeTimestamp(hoverNode.meta.timestamp_end) }} |
| 218 | + </time> |
| 219 | + </div> |
| 220 | + <div flex="~ justify-between items-center" py1 border="t base dashed" pt2> |
| 221 | + <label text="xs" op70>Duration</label> |
| 222 | + <DisplayDuration :duration="hoverNode.duration" /> |
| 223 | + </div> |
| 224 | + </div> |
| 225 | + <div v-else> |
| 226 | + <DisplayDuration :duration="hoverNode.duration" /> |
| 227 | + </div> |
| 228 | + </div> |
| 229 | + </DisplayGraphHoverView> |
| 230 | + <div ref="el" min-h-30 /> |
| 231 | + </div> |
| 232 | +</template> |
0 commit comments