Debian Codegraph @

html_renderer/src/lib/model/Frame.ts

raw symbols SCIP
1 import type FrameGroup from './FrameGroup';
2 import { randomId } from '../utils';
3 // import type { FrameData } from '../dataTypes';
4
5 export interface FrameData {
6 identifier: string,
7 time?: number // duration in seconds
8 startTime?: number
9 attributes?: {[name: string]: number},
10 children?: readonly FrameData[],
11 }
12
13
14 const IDENTIFIER_SEP = "\x00"
15 const ATTRIBUTES_SEP = "\x01"
16
17 export const AWAIT_FRAME_IDENTIFIER = "[await]"
18 export const SELF_TIME_FRAME_IDENTIFIER = "[self]"
19 export const OUT_OF_CONTEXT_FRAME_IDENTIFIER = "[out-of-context]"
20 export const DUMMY_ROOT_FRAME_IDENTIFIER = "[root]"
21
22 export const SYNTHETIC_FRAME_IDENTIFIERS = [
23 AWAIT_FRAME_IDENTIFIER,
24 SELF_TIME_FRAME_IDENTIFIER,
25 OUT_OF_CONTEXT_FRAME_IDENTIFIER,
26 DUMMY_ROOT_FRAME_IDENTIFIER,
27 ]
28
29 export const SYNTHETIC_LEAF_IDENTIFIERS = [
30 AWAIT_FRAME_IDENTIFIER,
31 SELF_TIME_FRAME_IDENTIFIER,
32 OUT_OF_CONTEXT_FRAME_IDENTIFIER,
33 ]
34
35 const ATTRIBUTE_MARKER_CLASS_NAME = "c"
36 const ATTRIBUTE_MARKER_LINE_NUMBER = "l"
37 const ATTRIBUTE_MARKER_TRACEBACKHIDE = "h"
38
39 export default class Frame {
40 uuid: string = randomId()
41 identifier: string
42 _identifierParts: string[]
43 startTime: number
44 time: number = 0
45 absorbedTime: number = 0
46 group: FrameGroup|null = null
47 attributes: {[name: string]: number}
48 _children: Frame[] = []
49 parent: Frame | null = null
50
51 context: FrameContext
52
53 constructor(
54 data: FrameData,
55 context: FrameContext,
56 ) {
57 this.identifier = data.identifier
58 this._identifierParts = this.identifier.split(IDENTIFIER_SEP)
59 this.startTime = data.startTime ?? 0
60 this.time = data.time ?? 0
61 this.attributes = data.attributes ?? {}
62 this.context = context
63
64 let childStartTime = this.startTime
65 const children = data.children?.map(f => {
66 if (f.startTime === undefined) {
67 f = {...f, startTime: childStartTime}
68 childStartTime += f.time ?? 0
69 }
70 childStartTime = f.startTime! + (f.time ?? 0)
71 return new Frame(f, context)
72 });
73 if (children) {
74 this.addChildren(children)
75 }
76 }
77
78 cloneDeep(): Frame {
79 return new Frame(this, this.context)
80 }
81
82 get children(): readonly Frame[] {
83 return this._children
84 }
85
86 addChild(frame: Frame, options: {after?: Frame} = {}) {
87 frame.removeFromParent()
88 frame.parent = this
89 if (options.after) {
90 const index = this._children.indexOf(options.after)
91 if (index == -1) {
92 throw new Error("After frame not found")
93 }
94 this._children.splice(index+1, 0, frame)
95 } else {
96 this._children.push(frame)
97 }
98 }
99
100 addChildren(frames: readonly Frame[], options: {after?: Frame} = {}) {
101 frames = frames.slice()
102 if (options.after) {
103 const reversed = frames.slice()
104 reversed.reverse()
105 frames.forEach(f => this.addChild(f, options))
106 } else {
107 frames.forEach(f => this.addChild(f, options))
108 }
109 }
110
111 removeFromParent() {
112 if (this.parent) {
113 const idx = this.parent._children.indexOf(this)
114 this.parent._children.splice(idx, 1)
115 this.parent = null
116 }
117 }
118
119 getAttributes(marker: string): {data: string, time: number}[] {
120 const keys = Object.keys(this.attributes).filter(k => k.startsWith(marker))
121 return keys.map(k => (
122 {data: k.slice(1), time: this.attributes[k]}
123 ))
124 }
125
126 getAttributeValue(marker: string) {
127 const attributes = this.getAttributes(marker)
128 if (!attributes) return null
129 if (attributes.length == 0) return null
130
131 let maxIdx = 0
132 for (let i = 0; i < attributes.length; i++) {
133 if (attributes[i].time > attributes[maxIdx].time) {
134 maxIdx = i
135 }
136 }
137
138 return attributes[maxIdx].data
139 }
140
141 get hasTracebackHide(): boolean {
142 return this.getAttributeValue(ATTRIBUTE_MARKER_TRACEBACKHIDE) == '1'
143 }
144
145 get function(): string {
146 return this._identifierParts[0]
147 }
148
149 get filePath(): string | null {
150 return this._identifierParts[1] ?? null
151 }
152
153 get lineNo(): number | null {
154 const lineNo = this._identifierParts[2]
155 return lineNo ? parseInt(lineNo) : null
156 }
157
158 get isSynthetic(): boolean {
159 return SYNTHETIC_FRAME_IDENTIFIERS.includes(this.identifier)
160 }
161
162 get filePathShort(): string | null {
163 if (this.isSynthetic && this.parent) {
164 return this.parent.filePathShort
165 }
166
167 if (!this.filePath) return null
168
169 return this.context.shortenPath(this.filePath)
170 }
171
172 get isApplicationCode(): boolean {
173 if (this.isSynthetic) {
174 return false;
175 }
176
177 const filePath = this.filePath;
178
179 if (!filePath) {
180 return false;
181 }
182
183 const prefixes = this.context.sysPrefixes
184 if (prefixes.some(path => filePath.startsWith(path))) {
185 // this code lives in the Python installation dir or a virtualenv
186 return false;
187 }
188
189 if (filePath.startsWith("<")) {
190 if (filePath.startsWith("<ipython-input-")) {
191 // Lines typed at a console or in a notebook are considered application code
192 return true;
193 } else if (filePath == "<string>" || filePath == "<stdin>") {
194 // eval/exec is app code if started by a parent frame that is app code
195 if (this.parent) {
196 return this.parent.isApplicationCode
197 } else {
198 // if this is the root frame, it must have been started
199 // with -c, so it's app code
200 return true
201 }
202 } else {
203 // Otherwise, this is likely some form of library-internal code generation
204 return false;
205 }
206 }
207
208 return true;
209 }
210
211 get proportionOfParent(): number {
212 if (!this.parent) {
213 return 1;
214 }
215
216 return this.time / this.parent.time;
217 }
218
219 get className(): string {
220 return this.getAttributeValue(ATTRIBUTE_MARKER_CLASS_NAME) ?? ""
221 }
222
223 get library(): string|null {
224 const filePathShort = this.filePathShort;
225 if (!filePathShort) {
226 return null;
227 }
228 // return the first part of the path that isn't slashes or dots
229 return /^[\\/.]*[^\\/.]*/.exec(filePathShort)![0] ?? ''
230 }
231 }
232
233 interface FrameContext {
234 shortenPath(path: string): string
235 sysPrefixes: string[]
236 precision: number
237 }