| 1 | import type { SessionData } from "../dataTypes"; |
| 2 | import Frame from "./Frame"; |
| 3 | |
| 4 | export default class Session { |
| 5 | startTime: number; |
| 6 | duration: number; |
| 7 | minInterval: number; |
| 8 | maxInterval: number; |
| 9 | precision: number; |
| 10 | sampleCount: number; |
| 11 | target_description: string; |
| 12 | cpuTime: number; |
| 13 | rootFrame: Frame|null; |
| 14 | sysPath: string; |
| 15 | sysPrefixes: string[]; |
| 16 | |
| 17 | constructor(data: SessionData) { |
| 18 | this.startTime = data.session.start_time; |
| 19 | this.duration = data.session.duration; |
| 20 | this.minInterval = data.session.min_interval; |
| 21 | this.maxInterval = data.session.max_interval; |
| 22 | this.sampleCount = data.session.sample_count; |
| 23 | this.target_description = data.session.target_description; |
| 24 | this.cpuTime = data.session.cpu_time; |
| 25 | this.sysPath = data.session.sys_path; |
| 26 | this.sysPrefixes = data.session.sys_prefixes |
| 27 | this.precision = Math.ceil(-Math.log10(Math.min(Math.max(1e-9, this.maxInterval), 1))) |
| 28 | this.rootFrame = data.frame_tree ? new Frame(data.frame_tree, this) : null |
| 29 | } |
| 30 | |
| 31 | _shortenPathCache: {[path: string]: string} = {} |
| 32 | shortenPath(path: string): string { |
| 33 | if (this._shortenPathCache[path]) { |
| 34 | return this._shortenPathCache[path] |
| 35 | } |
| 36 | |
| 37 | let result = path |
| 38 | const pathParts = pathSplit(path) |
| 39 | |
| 40 | if (pathParts.length > 1) { |
| 41 | for (const sysPathEntry of this.sysPath) { |
| 42 | const candidate = getRelPath(path, sysPathEntry) |
| 43 | if (pathSplit(candidate).length < pathSplit(result).length) { |
| 44 | result = candidate |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | this._shortenPathCache[path] = result |
| 50 | return result |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | function pathSplit(path: string): string[] { |
| 55 | return path.split(/[/\\]/) |
| 56 | } |
| 57 | |
| 58 | function getPathDrive(path: string): string | null { |
| 59 | const parts = pathSplit(path) |
| 60 | if (parts.length > 0 && parts[0].endsWith(":")) { |
| 61 | return parts[0] |
| 62 | } else { |
| 63 | return null |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | function getRelPath(path: string, start: string): string { |
| 68 | // returns the relative path from start to path |
| 69 | // e.g. getRelPath("/a/b/c", "/a") -> "b/c" |
| 70 | // e.g. getRelPath("/a/b/c", "/a/d/e") -> "../../b/c" |
| 71 | |
| 72 | if (getPathDrive(path) != getPathDrive(start)) { |
| 73 | // different drives, can't make a relative path |
| 74 | return path |
| 75 | } |
| 76 | |
| 77 | const parts = pathSplit(path) |
| 78 | const startParts = pathSplit(start) |
| 79 | let i = 0 |
| 80 | while (i < parts.length && i < startParts.length && parts[i] == startParts[i]) { |
| 81 | i++ |
| 82 | } |
| 83 | const relParts = startParts.slice(i).map(_ => "..") |
| 84 | |
| 85 | return relParts.concat(parts.slice(i)).join("/") |
| 86 | } |