Skip to content

Commit

Permalink
Fixes (some) of #212 by adding regular expression filtering to the lo…
Browse files Browse the repository at this point in the history
…gEntryEventStream component. Did some cleanup, as well
  • Loading branch information
jamessimone committed Nov 10, 2021
1 parent 6599810 commit 9ddcbce
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('LogEntryEventStream tests', () => {
});
document.body.appendChild(element);
await Promise.resolve();
const loggingLevelFilterDropdown = element.shadowRoot.querySelector('lightning-combobox[data-id="logging-level-filter"]');
const loggingLevelFilterDropdown = element.shadowRoot.querySelector('lightning-combobox[data-id="loggingLevelFilter"]');
loggingLevelFilterDropdown.value = loggingLevels.DEBUG;
loggingLevelFilterDropdown.dispatchEvent(new CustomEvent('change'));

Expand All @@ -136,7 +136,7 @@ describe('LogEntryEventStream tests', () => {
});
document.body.appendChild(element);
await Promise.resolve();
const loggingLevelFilterDropdown = element.shadowRoot.querySelector('lightning-combobox[data-id="logging-level-filter"]');
const loggingLevelFilterDropdown = element.shadowRoot.querySelector('lightning-combobox[data-id="loggingLevelFilter"]');
loggingLevelFilterDropdown.value = loggingLevels.DEBUG;
loggingLevelFilterDropdown.dispatchEvent(new CustomEvent('change'));

Expand All @@ -159,7 +159,7 @@ describe('LogEntryEventStream tests', () => {
});
document.body.appendChild(element);
await Promise.resolve();
const originTypeFilterDropdown = element.shadowRoot.querySelector('lightning-combobox[data-id="origin-type-filter"]');
const originTypeFilterDropdown = element.shadowRoot.querySelector('lightning-combobox[data-id="originTypeFilter"]');
originTypeFilterDropdown.value = 'Flow';
originTypeFilterDropdown.dispatchEvent(new CustomEvent('change'));

Expand All @@ -182,7 +182,7 @@ describe('LogEntryEventStream tests', () => {
});
document.body.appendChild(element);
await Promise.resolve();
const originTypeFilterDropdown = element.shadowRoot.querySelector('lightning-combobox[data-id="origin-type-filter"]');
const originTypeFilterDropdown = element.shadowRoot.querySelector('lightning-combobox[data-id="originTypeFilter"]');
originTypeFilterDropdown.value = 'Flow';
originTypeFilterDropdown.dispatchEvent(new CustomEvent('change'));

Expand All @@ -204,7 +204,7 @@ describe('LogEntryEventStream tests', () => {
});
document.body.appendChild(element);
await Promise.resolve();
const originLocationFilterDropdown = element.shadowRoot.querySelector('lightning-input[data-id="origin-location-filter"]');
const originLocationFilterDropdown = element.shadowRoot.querySelector('lightning-input[data-id="originLocationFilter"]');
originLocationFilterDropdown.value = 'SomeClass.someMethod';
originLocationFilterDropdown.dispatchEvent(new CustomEvent('change'));

Expand All @@ -227,7 +227,7 @@ describe('LogEntryEventStream tests', () => {
});
document.body.appendChild(element);
await Promise.resolve();
const originLocationFilterDropdown = element.shadowRoot.querySelector('lightning-input[data-id="origin-location-filter"]');
const originLocationFilterDropdown = element.shadowRoot.querySelector('lightning-input[data-id="originLocationFilter"]');
originLocationFilterDropdown.value = 'SomeClass.someMethod';
originLocationFilterDropdown.dispatchEvent(new CustomEvent('change'));

Expand All @@ -249,7 +249,7 @@ describe('LogEntryEventStream tests', () => {
});
document.body.appendChild(element);
await Promise.resolve();
const originLocationFilterDropdown = element.shadowRoot.querySelector('lightning-input[data-id="logged-by-filter"]');
const originLocationFilterDropdown = element.shadowRoot.querySelector('lightning-input[data-id="loggedByFilter"]');
originLocationFilterDropdown.value = '[email protected]';
originLocationFilterDropdown.dispatchEvent(new CustomEvent('change'));

Expand All @@ -272,7 +272,7 @@ describe('LogEntryEventStream tests', () => {
});
document.body.appendChild(element);
await Promise.resolve();
const originLocationFilterDropdown = element.shadowRoot.querySelector('lightning-input[data-id="logged-by-filter"]');
const originLocationFilterDropdown = element.shadowRoot.querySelector('lightning-input[data-id="loggedByFilter"]');
originLocationFilterDropdown.value = '[email protected]';
originLocationFilterDropdown.dispatchEvent(new CustomEvent('change'));

Expand All @@ -288,13 +288,13 @@ describe('LogEntryEventStream tests', () => {
const eventStreamDiv = element.shadowRoot.querySelector('.event-stream');
expect(eventStreamDiv.textContent).toBeFalsy();
});
it('includes matching log entry event for message filter', async () => {
it('includes matching log entry event using string for message filter', async () => {
const element = createElement('log-entry-event-stream', {
is: LogEntryEventStream
});
document.body.appendChild(element);
await Promise.resolve();
const messageFilterTextarea = element.shadowRoot.querySelector('lightning-textarea[data-id="message-filter"]');
const messageFilterTextarea = element.shadowRoot.querySelector('lightning-textarea[data-id="messageFilter"]');
messageFilterTextarea.value = 'matching text';
messageFilterTextarea.dispatchEvent(new CustomEvent('change'));

Expand All @@ -317,7 +317,7 @@ describe('LogEntryEventStream tests', () => {
});
document.body.appendChild(element);
await Promise.resolve();
const messageFilterTextarea = element.shadowRoot.querySelector('lightning-textarea[data-id="message-filter"]');
const messageFilterTextarea = element.shadowRoot.querySelector('lightning-textarea[data-id="messageFilter"]');
messageFilterTextarea.value = 'non-matching text';
messageFilterTextarea.dispatchEvent(new CustomEvent('change'));

Expand All @@ -333,4 +333,28 @@ describe('LogEntryEventStream tests', () => {
const eventStreamDiv = element.shadowRoot.querySelector('.event-stream');
expect(eventStreamDiv.textContent).toBeFalsy();
});

it('includes matching log entry event using regex for message filter', async () => {
const element = createElement('log-entry-event-stream', {
is: LogEntryEventStream
});
document.body.appendChild(element);
await Promise.resolve();

const messageFilterTextarea = element.shadowRoot.querySelector('lightning-textarea[data-id="messageFilter"]');
messageFilterTextarea.value = 'Something.+? blah$';
messageFilterTextarea.dispatchEvent(new CustomEvent('change'));

const matchingLogEntryEvent = { ...mockLogEntryEventTemplate };
matchingLogEntryEvent.Message__c = 'Something, something, something, beep boop beep!!!!!!!%@#$!%, blah, blah, blah';
await jestMockPublish('/event/LogEntryEvent__e', {
data: {
payload: matchingLogEntryEvent
}
});

const expectedStreamText = getPlatformEventText(matchingLogEntryEvent);
const eventStreamDiv = element.shadowRoot.querySelector('.event-stream');
expect(eventStreamDiv.textContent).toBe(expectedStreamText);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,46 @@
value={maxEvents}
></lightning-input>
<lightning-combobox
data-id="logging-level-filter"
data-id="loggingLevelFilter"
label="Minimum Logging Level"
onchange={handleLoggingLevelFilterChange}
onchange={handleFilterChange}
options={loggingLevelOptions}
placeholder="Select Logging Level"
value={loggingLevelFilter}
></lightning-combobox>
<lightning-combobox
data-id="origin-type-filter"
data-id="originTypeFilter"
label="Origin Type"
onchange={handleOriginTypeFilterChange}
onchange={handleFilterChange}
options={originTypeOptions}
placeholder="Select Origin Type"
value={originTypeFilter}
></lightning-combobox>
<lightning-input
data-id="origin-location-filter"
data-id="originLocationFilter"
label="Origin Location"
onchange={handleOriginLocationFilterChange}
onchange={handleFilterChange}
placeholder="Specify Origin Location"
value={originLocationFilter}
></lightning-input>
<lightning-input
data-id="logged-by-filter"
data-id="scenarioFilter"
label="Scenario"
onchange={handleFilterChange}
placeholder="Specify Scenario"
value={scenarioFilter}
></lightning-input>
<lightning-input
data-id="loggedByFilter"
label="Logged By"
onchange={handleLoggedByFilterChange}
onchange={handleFilterChange}
placeholder="Specify Username"
value={loggedByFilter}
></lightning-input>
<lightning-textarea
data-id="message-filter"
data-id="messageFilter"
label="Message"
onchange={handleMessageFilterChange}
onchange={handleFilterChange}
placeholder="Message Contains"
value={messageFilter}
></lightning-textarea>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LightningElement } from 'lwc';
import { isEmpEnabled, subscribe, unsubscribe } from 'lightning/empApi';

export default class LogEntryEventStream extends LightningElement {
unfilteredEvents = [];
logEntryEvents = [];
isExpanded = false;
isStreamEnabled = true;
Expand All @@ -12,13 +13,19 @@ export default class LogEntryEventStream extends LightningElement {
messageFilter;
originTypeFilter;
originLocationFilter;
scenarioFilter;
maxEvents = 50;

_channel = '/event/LogEntryEvent__e'; // TODO need to support namespace in managed package
_subscription = {};

get title() {
return this.logEntryEvents.length + ' Log Entry Events';
let logEntryString = ' Log Entry Events';
let startingTitle = this.logEntryEvents.length + logEntryString;
if (this.unfilteredEvents.length !== this.logEntryEvents.length) {
startingTitle = this.logEntryEvents.length + ' matching results out of ' + this.unfilteredEvents.length + logEntryString;
}
return startingTitle;
}

get streamButtonVariant() {
Expand Down Expand Up @@ -59,7 +66,7 @@ export default class LogEntryEventStream extends LightningElement {
}

createSubscription() {
subscribe(this._channel, -1, this.subscriptionCallback.bind(this)).then(response => {
subscribe(this._channel, -1, this.subscriptionCallback).then(response => {
this._subscription = response;
});
}
Expand All @@ -68,24 +75,9 @@ export default class LogEntryEventStream extends LightningElement {
unsubscribe(this._subscription);
}

handleLoggingLevelFilterChange(event) {
this.loggingLevelFilter = event.target.value;
}

handleOriginTypeFilterChange(event) {
this.originTypeFilter = event.target.value;
}

handleOriginLocationFilterChange(event) {
this.originLocationFilter = event.target.value;
}

handleLoggedByFilterChange(event) {
this.loggedByFilter = event.target.value;
}

handleMessageFilterChange(event) {
this.messageFilter = event.target.value;
handleFilterChange(event) {
this[event.target.dataset.id] = event.target.value;
this._filterEvents();
}

handleMaxEventsChange(event) {
Expand All @@ -94,6 +86,7 @@ export default class LogEntryEventStream extends LightningElement {

onClear() {
this.logEntryEvents = [];
this.unfilteredEvents = [];
}

// onToggleExpand() {
Expand All @@ -104,44 +97,38 @@ export default class LogEntryEventStream extends LightningElement {

onToggleStream() {
this.isStreamEnabled = !this.isStreamEnabled;
if (this.isStreamEnabled) {
this.createSubscription();
} else {
this.cancelSubscription();
}
// eslint-disable-next-line
this.isStreamEnabled ? this.createSubscription() : this.cancelSubscription();
}

subscriptionCallback(response) {
subscriptionCallback = response => {
const logEntryEvent = response.data.payload;
// As of API v52.0 (Summer '21), platform events have a unique field, EventUUID
// but it doesn't seem to be populated via empApi, so use a synthetic key instead
logEntryEvent.key = logEntryEvent.TransactionId__c + '__' + logEntryEvent.TransactionEntryNumber__c;
this.unfilteredEvents.unshift(logEntryEvent);
this._filterEvents();
};

const updatedLogEntryEvents = [...this.logEntryEvents];

if (
this._meetsLoggedByFilter(logEntryEvent) &&
this._meetsLoggingLevelFilter(logEntryEvent) &&
this._meetsMessageFilter(logEntryEvent) &&
this._meetsOriginLocationFilter(logEntryEvent) &&
this._meetsOriginTypeFilter(logEntryEvent)
) {
updatedLogEntryEvents.unshift(logEntryEvent);
// Private functions
_filterEvents() {
while (this.unfilteredEvents.length > this.maxEvents) {
this.unfilteredEvents.pop();
}

while (updatedLogEntryEvents.length > this.maxEvents) {
updatedLogEntryEvents.pop();
}
this.logEntryEvents = updatedLogEntryEvents;
this.logEntryEvents = this.unfilteredEvents.filter(
logEntryEvent =>
this._meetsLoggedByFilter(logEntryEvent) &&
this._meetsLoggingLevelFilter(logEntryEvent) &&
this._meetsMessageFilter(logEntryEvent) &&
this._meetsOriginLocationFilter(logEntryEvent) &&
this._meetsOriginTypeFilter(logEntryEvent) &&
this._meetsScenarioFilter(logEntryEvent)
);
}

// Private functions
_meetsLoggedByFilter(logEntryEvent) {
let matches = false;
if (!this.loggedByFilter || logEntryEvent.LoggedByUsername__c.includes(this.loggedByFilter)) {
matches = true;
}
return matches;
return this._matchesTextFilter(this.loggedByFilter, logEntryEvent.LoggedByUsername__c);
}

_meetsLoggingLevelFilter(logEntryEvent) {
Expand All @@ -153,27 +140,24 @@ export default class LogEntryEventStream extends LightningElement {
}

_meetsMessageFilter(logEntryEvent) {
// TODO support for regex searches in Message__c
let matches = false;
if (!this.messageFilter || logEntryEvent.Message__c.includes(this.messageFilter)) {
matches = true;
}
return matches;
return this._matchesTextFilter(this.messageFilter, logEntryEvent.Message__c);
}

_meetsOriginLocationFilter(logEntryEvent) {
// TODO support for regex searches in OriginLocation__c
let matches = false;
if (!this.originLocationFilter || logEntryEvent.OriginLocation__c.includes(this.originLocationFilter)) {
matches = true;
}
return matches;
return this._matchesTextFilter(this.originLocationFilter, logEntryEvent.OriginLocation__c);
}

_meetsOriginTypeFilter(logEntryEvent) {
// TODO support for regex searches in Message__c
return this._matchesTextFilter(this.originTypeFilter, logEntryEvent.OriginType__c);
}

_meetsScenarioFilter(logEntryEvent) {
return this._matchesTextFilter(this.scenarioFilter, logEntryEvent.Scenario__c);
}

_matchesTextFilter(filterCriteria = '', text = '') {
let matches = false;
if (!this.originTypeFilter || logEntryEvent.OriginType__c === this.originTypeFilter) {
if (!filterCriteria || text.includes(filterCriteria) || text.match(filterCriteria)) {
matches = true;
}
return matches;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"test": "npm run test:lwc && npm run test:apex",
"test:apex": "sfdx force:apex:test:run --verbose --testlevel RunLocalTests --wait 30 --resultformat human --codecoverage --detailedcoverage --outputdir ./tests/apex",
"test:apex:suites": "sfdx force:apex:test:run --verbose --suitenames LoggerConfiguration,LoggerEngine,LoggerLogManagement,LoggerPluginFramework --wait 30 --resultformat human --codecoverage --detailedcoverage --outputdir ./tests/apex",
"test:lwc": "sfdx-lwc-jest --coverage"
"test:lwc": "sfdx-lwc-jest --coverage --skipApiVersionCheck"
},
"dependencies": {
"set-value": ">=4.0.1",
Expand Down

0 comments on commit 9ddcbce

Please sign in to comment.