-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace archy with a specific implementation, refactor timetree (#211)
* fix timetree typo * integrate pretty printing into avvio * move time-tree into lib folder * add test as issue #205 is also solved. * add jsdoc, remove unreachable code * use -1 as diff default value
- Loading branch information
Showing
7 changed files
with
596 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
'use strict' | ||
|
||
const kUntrackNode = Symbol('avvio.TimeTree.untrackNode') | ||
const kTrackNode = Symbol('avvio.TimeTree.trackNode') | ||
const kGetParent = Symbol('avvio.TimeTree.getParent') | ||
const kGetNode = Symbol('avvio.TimeTree.getNode') | ||
const kAddNode = Symbol('avvio.TimeTree.addNode') | ||
|
||
/** | ||
* Node of the TimeTree | ||
* @typedef {object} TimeTreeNode | ||
* @property {string} id | ||
* @property {string|null} parent | ||
* @property {string} label | ||
* @property {Array<TimeTreeNode>} nodes | ||
* @property {number} start | ||
* @property {number|undefined} stop | ||
* @property {number|undefined} diff | ||
*/ | ||
|
||
class TimeTree { | ||
constructor () { | ||
/** | ||
* @type {TimeTreeNode|null} root | ||
* @public | ||
*/ | ||
this.root = null | ||
|
||
/** | ||
* @type {Map<string, TimeTreeNode>} tableId | ||
* @public | ||
*/ | ||
this.tableId = new Map() | ||
|
||
/** | ||
* @type {Map<string, Array<TimeTreeNode>>} tableLabel | ||
* @public | ||
*/ | ||
this.tableLabel = new Map() | ||
} | ||
|
||
/** | ||
* @param {TimeTreeNode} node | ||
*/ | ||
[kTrackNode] (node) { | ||
this.tableId.set(node.id, node) | ||
if (this.tableLabel.has(node.label)) { | ||
this.tableLabel.get(node.label).push(node) | ||
} else { | ||
this.tableLabel.set(node.label, [node]) | ||
} | ||
} | ||
|
||
/** | ||
* @param {TimeTreeNode} node | ||
*/ | ||
[kUntrackNode] (node) { | ||
this.tableId.delete(node.id) | ||
|
||
const labelNode = this.tableLabel.get(node.label) | ||
labelNode.pop() | ||
|
||
if (labelNode.length === 0) { | ||
this.tableLabel.delete(node.label) | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} parent | ||
* @returns {TimeTreeNode} | ||
*/ | ||
[kGetParent] (parent) { | ||
const parentNode = this.tableLabel.get(parent) | ||
return parentNode[parentNode.length - 1] | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} nodeId | ||
* @returns {TimeTreeNode} | ||
*/ | ||
[kGetNode] (nodeId) { | ||
return this.tableId.get(nodeId) | ||
} | ||
|
||
/** | ||
* @param {string} parent | ||
* @param {string} label | ||
* @param {number} start | ||
* @returns {TimeTreeNode["id"]} | ||
*/ | ||
[kAddNode] (parent, label, start) { | ||
const isRoot = parent === null | ||
if (isRoot) { | ||
this.root = { | ||
parent: null, | ||
id: 'root', | ||
label, | ||
nodes: [], | ||
start, | ||
stop: undefined, | ||
diff: -1 | ||
} | ||
this[kTrackNode](this.root) | ||
return this.root.id | ||
} | ||
|
||
const parentNode = this[kGetParent](parent) | ||
const nodeId = `${label}-${Math.random()}` | ||
/** | ||
* @type {TimeTreeNode} | ||
*/ | ||
const childNode = { | ||
parent, | ||
id: nodeId, | ||
label, | ||
nodes: [], | ||
start, | ||
stop: undefined, | ||
diff: -1 | ||
} | ||
parentNode.nodes.push(childNode) | ||
this[kTrackNode](childNode) | ||
return nodeId | ||
} | ||
|
||
/** | ||
* @param {string} parent | ||
* @param {string} label | ||
* @param {number|undefined} start | ||
* @returns {TimeTreeNode["id"]} | ||
*/ | ||
start (parent, label, start = Date.now()) { | ||
return this[kAddNode](parent, label, start) | ||
} | ||
|
||
/** | ||
* @param {string} nodeId | ||
* @param {number|undefined} stop | ||
*/ | ||
stop (nodeId, stop = Date.now()) { | ||
const node = this[kGetNode](nodeId) | ||
if (node) { | ||
node.stop = stop | ||
node.diff = (node.stop - node.start) || 0 | ||
this[kUntrackNode](node) | ||
} | ||
} | ||
|
||
/** | ||
* @returns {TimeTreeNode} | ||
*/ | ||
toJSON () { | ||
return Object.assign({}, this.root) | ||
} | ||
|
||
/** | ||
* @returns {string} | ||
*/ | ||
prettyPrint () { | ||
return prettyPrintTimeTree(this.toJSON()) | ||
} | ||
} | ||
|
||
/** | ||
* @param {TimeTreeNode} obj | ||
* @param {string|undefined} prefix | ||
* @returns {string} | ||
*/ | ||
function prettyPrintTimeTree (obj, prefix = '') { | ||
let result = prefix | ||
|
||
const nodesCount = obj.nodes.length | ||
const lastIndex = nodesCount - 1 | ||
result += `${obj.label} ${obj.diff} ms\n` | ||
|
||
for (let i = 0; i < nodesCount; ++i) { | ||
const node = obj.nodes[i] | ||
const prefix_ = prefix + (i === lastIndex ? ' ' : '│ ') | ||
|
||
result += prefix | ||
result += (i === lastIndex ? '└─' : '├─') | ||
result += (node.nodes.length === 0 ? '─ ' : '┬ ') | ||
result += prettyPrintTimeTree(node, prefix_).slice(prefix.length + 2) | ||
} | ||
return result | ||
} | ||
|
||
module.exports = TimeTree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,6 @@ | |
"typescript": "^5.0.2" | ||
}, | ||
"dependencies": { | ||
"archy": "^1.0.0", | ||
"fastq": "^1.6.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const boot = require('../..') | ||
|
||
test('should print the time tree', (t) => { | ||
t.plan(2) | ||
const app = boot() | ||
|
||
app.use(function first (instance, opts, cb) { | ||
const out = instance.prettyPrint().split('\n') | ||
t.equal(out[0], 'bound root -1 ms') | ||
t.equal(out[1], '└── first -1 ms') | ||
cb() | ||
}) | ||
}) |
Oops, something went wrong.