|
1 | 1 | import type { FlameChartNode } from "flame-chart-js/dist/types"; |
2 | | -import type { ProfilerCost, ProfilerEdge, ProfilerEdges } from "~/src/entities/profiler/types"; |
| 2 | +import type { ProfilerCost, ProfilerEdges } from "~/src/entities/profiler/types"; |
3 | 3 | import { GraphTypes } from "~/src/shared/types"; |
4 | 4 |
|
5 | 5 | type FlameChartData = FlameChartNode & { |
6 | 6 | cost: ProfilerCost |
7 | 7 | } |
8 | 8 | export const build = (edges: ProfilerEdges, field: GraphTypes): FlameChartData => { |
9 | | - let walked = [] as ProfilerEdge['callee'][] |
| 9 | + return buildWaterfall(edges)[0] |
| 10 | +} |
10 | 11 |
|
11 | | - const datum: Record<string, FlameChartData> = {} |
| 12 | +// TODO: add types |
| 13 | +function buildWaterfall(events) { |
| 14 | + const waterfall = []; |
| 15 | + const eventCache = {}; |
12 | 16 |
|
13 | | - Object.values(edges).forEach((edge) => { |
14 | | - const parent = edge.caller |
15 | | - const target = edge.callee |
| 17 | + // First pass to create each event and find its parent. |
| 18 | + for (const key of Object.keys(events)) { |
| 19 | + const event = events[key]; |
| 20 | + const duration = event.cost.wt || 0; |
| 21 | + const eventData = { |
| 22 | + name: event.callee, |
| 23 | + cost: event.cost, |
| 24 | + start: 0, // Temporarily zero, will adjust based on the parent later |
| 25 | + duration: duration, |
| 26 | + type: 'task', |
| 27 | + children: [], |
| 28 | + color: getColorForPercentCount(event.cost.p_wt), |
| 29 | + }; |
16 | 30 |
|
17 | | - const duration = (edge.cost[String(field)] || 0) > 0 ? edge.cost[String(field)] / 1_000 : 0 |
18 | | - const start = 0 |
| 31 | + eventCache[event.id] = eventData; |
19 | 32 |
|
20 | | - if (target && !datum[target]) { |
21 | | - datum[target] = { |
22 | | - name: target, |
23 | | - start, |
24 | | - duration, |
25 | | - cost: edge.cost, |
26 | | - children: [] |
| 33 | + if (event.parent) { |
| 34 | + // If there's a parent, add to its children list. |
| 35 | + const parentEventData = eventCache[event.parent]; |
| 36 | + if (parentEventData) { |
| 37 | + parentEventData.children.push(eventData); |
27 | 38 | } |
| 39 | + } else { |
| 40 | + // No parent implies it is a top-level event. |
| 41 | + waterfall.push(eventData); |
28 | 42 | } |
| 43 | + } |
29 | 44 |
|
30 | | - if (parent && !datum[parent]) { |
31 | | - datum[parent] = { |
32 | | - name: parent, |
33 | | - start, |
34 | | - duration, |
35 | | - cost: edge.cost, |
36 | | - children: [] |
37 | | - } |
38 | | - } |
| 45 | + // Second pass to adjust start times based on the order in the children array. |
| 46 | + function adjustStartTimes(eventList, startTime) { |
| 47 | + for (let i = 0; i < eventList.length; i++) { |
| 48 | + const event = eventList[i]; |
| 49 | + event.start = startTime; |
| 50 | + startTime += event.duration; // Next event starts after the current event ends. |
39 | 51 |
|
40 | | - // NOTE: walked skips several targettions (recursion detected), should be fixed |
41 | | - if (!parent || walked.includes(target)) { |
42 | | - // console.log(node, target) |
43 | | - return |
| 52 | + // Recursively adjust times for children. |
| 53 | + adjustStartTimes(event.children, event.start); |
44 | 54 | } |
| 55 | + } |
45 | 56 |
|
46 | | - if (datum[parent] && datum[parent].children) { |
47 | | - const parentChildren = datum[parent].children || [] |
| 57 | + // Start the adjustment from the root events. |
| 58 | + adjustStartTimes(waterfall, 0); |
48 | 59 |
|
49 | | - const lastChild = parentChildren ? parentChildren[parentChildren.length - 1]: null |
50 | | - datum[target].start = lastChild ? lastChild.start + lastChild.duration : datum[target].start |
51 | | - } else { |
52 | | - datum[target].start += datum[parent].start |
53 | | - } |
54 | | - |
55 | | - datum[parent].children?.push(datum[target]) |
56 | | - walked.push(target) |
57 | | - }) |
| 60 | + return waterfall; |
| 61 | +} |
58 | 62 |
|
59 | | - walked = [] |
| 63 | +const getColorForPercentCount = (percent: number): string => { |
| 64 | + if (percent <= 10) { |
| 65 | + return '#B3E5FC'; // Light Blue |
| 66 | + } |
| 67 | + if (percent <= 20) { |
| 68 | + return '#81D4FA'; // Light Sky Blue |
| 69 | + } |
| 70 | + if (percent <= 30) { |
| 71 | + return '#4FC3F7'; // Vivid Light Blue |
| 72 | + } |
| 73 | + if (percent <= 40) { |
| 74 | + return '#29B6F6'; // Bright Light Blue |
| 75 | + } |
| 76 | + if (percent <= 50) { |
| 77 | + return '#FFCDD2'; // Pink (Light Red) |
| 78 | + } |
| 79 | + if (percent <= 60) { |
| 80 | + return '#FFB2B2'; // Lighter Red |
| 81 | + } |
| 82 | + if (percent <= 70) { |
| 83 | + return '#FF9E9E'; // Soft Red |
| 84 | + } |
| 85 | + if (percent <= 80) { |
| 86 | + return '#FF8989'; // Soft Coral |
| 87 | + } |
| 88 | + if (percent <= 90) { |
| 89 | + return '#FF7474'; // Soft Tomato |
| 90 | + } |
60 | 91 |
|
61 | | - return datum['main()'] |
62 | | -} |
| 92 | + return '#FF5F5F'; // Light Coral |
| 93 | +}; |
0 commit comments