-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathModuleFlamegraph.vue
More file actions
138 lines (126 loc) · 3.06 KB
/
Copy pathModuleFlamegraph.vue
File metadata and controls
138 lines (126 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<script setup lang="ts">
import type { TreeNodeInput } from 'nanovis'
import type { ModuleInfo, SessionContext } from '~~/shared/types'
import { Flamegraph, normalizeTreeNode } from 'nanovis'
import { computed, nextTick, onMounted, onUnmounted, ref, shallowRef, useTemplateRef, watch } from 'vue'
const props = defineProps<{
info: ModuleInfo
session: SessionContext
flowNodeSelected: boolean
}>()
const n = (node: TreeNodeInput<any>) => normalizeTreeNode(node, undefined, false)
const tree = computed(() => {
const resolveIds = props.info.resolve_ids.map((id, idx) => n({
id: `resolveId-${idx}`,
text: id.plugin_name,
size: id.duration,
}))
const loads = props.info.loads.map((load, idx) => n({
id: `load-${idx}`,
text: load.plugin_name,
size: load.duration,
}))
const transforms = props.info.transforms.map((transform, idx) => n({
id: `transform-${idx}`,
text: transform.plugin_name,
size: transform.duration,
}))
const children = [
n({
id: '~resolves',
text: 'resolve',
children: resolveIds,
}),
n({
id: '~loads',
text: 'load',
children: loads,
}),
n({
id: '~transforms',
text: 'transform',
children: transforms,
}),
]
return n({
id: '~root',
text: 'Module Flamegraph',
children,
})
})
const hoverNode = ref<{
plugin_name: string
duration: number
} | null>(null)
const hoverX = ref<number>(0)
const hoverY = ref<number>(0)
const el = useTemplateRef<HTMLDivElement>('el')
const flamegraph = shallowRef<Flamegraph | null>(null)
function buildFlamegraph() {
flamegraph.value = new Flamegraph(tree.value, {
animate: true,
palette: {
fg: '#888',
},
getSubtext: (node) => {
const p = node.size / tree.value.size * 100
if (p > 15 && p !== 100) {
return `${p.toFixed(1)}%`
}
return undefined
},
onHover(node, e) {
if (!node) {
hoverNode.value = null
return
}
if (e) {
hoverX.value = e.clientX
hoverY.value = e.clientY
}
hoverNode.value = {
plugin_name: node.text!,
duration: node.size,
}
},
})
el.value!.appendChild(flamegraph.value!.el)
}
function disposeFlamegraph() {
flamegraph.value?.dispose()
}
onMounted(() => {
buildFlamegraph()
})
onUnmounted(() => {
disposeFlamegraph()
})
watch(tree, async () => {
disposeFlamegraph()
buildFlamegraph()
}, {
deep: true,
})
watch(() => props.flowNodeSelected, async () => {
await nextTick()
flamegraph.value?.resize()
})
</script>
<template>
<div relative border="t base" pb10 py1 mt4>
<Teleport to="body">
<div
v-if="hoverNode"
border="~ base" rounded shadow px2 py1 fixed
z-panel-content bg-glass pointer-events-none text-sm
:style="{ left: `${hoverX}px`, top: `${hoverY}px` }"
>
<div font-bold font-mono>
{{ hoverNode.plugin_name }}
</div>
<DisplayDuration :duration="hoverNode.duration" />
</div>
</Teleport>
<div ref="el" min-h-30 />
</div>
</template>