Skip to content

Commit ee2a634

Browse files
authored
Merge pull request #1462 from getappmap/feat/recognize-more-roots
feat: Recognize certain labeled functions as roots
2 parents 4b57eb3 + 77de691 commit ee2a634

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

packages/models/src/appMapFilter.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ class DeclutterProperty {
1414
}
1515

1616
class DeclutterTimeProperty extends DeclutterProperty {
17-
time = 100;
17+
DEFAULT_TIME = 100;
1818

19-
constructor(on = true, defaultValue = true, time = 100) {
19+
time = this.DEFAULT_TIME;
20+
21+
constructor(on = true, defaultValue = true, time = DeclutterTimeProperty.DEFAULT_TIME) {
2022
super(on, defaultValue);
2123

2224
this.time = time;
@@ -46,6 +48,8 @@ class DeclutterExternalPathsProperty extends DeclutterProperty {
4648
}
4749
}
4850

51+
const ROOT_EVENT_LABELS = ['cli.command', 'job.perform', 'message.handle'];
52+
4953
class Declutter {
5054
limitRootEvents = new DeclutterProperty();
5155
hideMediaRequests = new DeclutterProperty();
@@ -130,9 +134,24 @@ export default class AppMapFilter {
130134
}, new Set());
131135
}
132136

133-
// Include only subtrees of an HTTP server request, unless there are no HTTP server requests.
137+
// Include only subtrees of "command"-type events, unless there are no commands.
134138
if (this.declutter.limitRootEvents.on) {
135-
events = includeSubtrees(events, (e) => e.httpServerRequest, false);
139+
// Return true if the event is a "command". Types of commands recognized by this test include:
140+
// - HTTP server request - the event has http_server_request data
141+
// - cli.command - command of a CLI application
142+
// - job.perform - a background job
143+
// - message.handle - a handler for a message queue
144+
//
145+
// @param {Event} e
146+
const isCommand = (e) => {
147+
if (e.httpServerRequest) return true;
148+
149+
const { labels } = e.codeObject;
150+
151+
return ROOT_EVENT_LABELS.find((label) => labels.has(label));
152+
};
153+
154+
events = includeSubtrees(events, isCommand, false);
136155
}
137156

138157
// Include only subtrees of a specified root object. This could also be stored and managed

packages/models/tests/unit/appMapFilter.spec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,23 @@ describe('appMapFilter', () => {
136136
}
137137
});
138138

139-
it('is a nop if there are no HTTP server requests in the AppMap', () => {
139+
it('recognizes ROOT_EVENT_LABELS', () => {
140+
const augmentedModelData = { ...modelData };
141+
const validateFunction = augmentedModelData.classMap[0].children[0].children[0].children[1];
142+
expect(validateFunction.name).toEqual('validate');
143+
validateFunction.labels = ['job.perform'];
144+
const augmentedModelAppMap = buildAppMap().source(augmentedModelData).build();
145+
filter.declutter.limitRootEvents.on = true;
146+
const filteredAppMap = filter.filter(augmentedModelAppMap);
147+
expect(filteredAppMap.events.length).toEqual(8);
148+
});
149+
150+
it('is a nop if there are no "commands" in the AppMap', () => {
140151
const eventCount = modelAppMap.events.length;
152+
expect(eventCount).toEqual(10);
141153
filter.declutter.limitRootEvents.on = true;
142154
const filteredAppMap = filter.filter(modelAppMap);
143-
expect(filteredAppMap.events.length).toEqual(eventCount);
155+
expect(filteredAppMap.events.length).toEqual(10);
144156
});
145157
});
146158
});

0 commit comments

Comments
 (0)