-
Notifications
You must be signed in to change notification settings - Fork 14
Add traceAPIDependency debug utility for inspecting transitive API de… #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -87,6 +87,69 @@ const getRootUnreadyAPI = (host: AppHost) => { | |
| } | ||
| } | ||
|
|
||
| export type DependencyTree = { | ||
| entryPoint: string | ||
| deps: Array<{ api: string; subtree: DependencyTree | null }> | ||
| } | ||
|
|
||
| const traceAPIDependency = (entryPointName: string, apiName: string, entryPoints: EntryPoint[]): DependencyTree | null => { | ||
| const apiToDeclarer = mapApiToEntryPoint(entryPoints) | ||
| const entryPointByName = new Map(entryPoints.map(ep => [ep.name, ep])) | ||
| if (!entryPointByName.has(entryPointName)) { | ||
| return null | ||
| } | ||
|
|
||
| const onPath = new Set<string>() | ||
|
|
||
| const build = (epName: string): DependencyTree | null => { | ||
| if (onPath.has(epName)) { | ||
| return null | ||
| } | ||
| onPath.add(epName) | ||
|
|
||
| const deps: DependencyTree['deps'] = [] | ||
| const epDeps = entryPointByName.get(epName)?.getDependencyAPIs?.() ?? [] | ||
| for (const dep of epDeps) { | ||
| if (dep.name === apiName) { | ||
| deps.push({ api: dep.name, subtree: null }) | ||
| } else { | ||
| const declarer = apiToDeclarer.get(dep.name) | ||
| const subtree = declarer ? build(declarer.name) : null | ||
| if (subtree) { | ||
| deps.push({ api: dep.name, subtree }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| onPath.delete(epName) | ||
| return deps.length > 0 ? { entryPoint: epName, deps } : null | ||
| } | ||
|
|
||
| return build(entryPointName) | ||
| } | ||
|
|
||
| const visualizeDependencyTree = (tree: DependencyTree | null): string => { | ||
| if (!tree) { | ||
| return '(no dependency paths)' | ||
| } | ||
|
|
||
| const lines: string[] = [tree.entryPoint] | ||
| const render = (node: DependencyTree, prefix: string): void => { | ||
| node.deps.forEach((edge, i) => { | ||
| const isLast = i === node.deps.length - 1 | ||
| const connector = isLast ? '└─ ' : '├─ ' | ||
| const nextPrefix = prefix + (isLast ? ' ' : '│ ') | ||
| const declaredBy = edge.subtree ? ` (declared by ${edge.subtree.entryPoint})` : '' | ||
| lines.push(`${prefix}${connector}${edge.api}${declaredBy}`) | ||
| if (edge.subtree) { | ||
| render(edge.subtree, nextPrefix) | ||
| } | ||
| }) | ||
| } | ||
| render(tree, '') | ||
| return lines.join('\n') | ||
| } | ||
|
|
||
| const getAPIOrEntryPointsDependencies = ( | ||
| apisOrEntryPointsNames: string[], | ||
| entryPoints: EntryPoint[] | ||
|
|
@@ -167,6 +230,9 @@ export function setupDebugInfo({ | |
| apisOrEntryPointsNames: string[], | ||
| entryPoints = [...addedShells.values()].map(x => x.entryPoint) | ||
| ) => getAPIOrEntryPointsDependencies(apisOrEntryPointsNames, entryPoints), | ||
| traceAPIDependency: (entryPointName: string, apiName: string): DependencyTree | null => | ||
| traceAPIDependency(entryPointName, apiName, [...getUnreadyEntryPoints(), ...[...addedShells.values()].map(x => x.entryPoint)]), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not reuse
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It throws "not implemented" error in runtime |
||
| visualizeDependencyTree, | ||
| performance: getPerformanceDebug(options, trace, memoizedArr) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These public signatures are inconsistent with the rest of the API and also with themselves.
Did you expose (
getAPIOrEntryPointsDependencies) in public signature on purpose (+ isn't it weird that someone would need to provide an entrypoints array for the debug tool, or is there a use-case you know of)?Why do you need
entryPointNameinstead of deriving it by searching the declarer of the API?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added types. The implementation itself is already there, and it accepts a list of entryPoints. Not sure if there are real use cases for that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EntryPointName is the starting point.
The goal is to visualize subtree of dependencies starting from this entry point to the specific API.
Use case: we have one entry point that we don't want to be dependant on the DocumentServicesAPI, because this entry point should be executed as early as possible (to show welcome screen). So adding documentServices even transitively will make it appear much later.
So this utility should make it easy to understand if provided entry point is dependant on the provided api, and if yes - visualise why exactly