Skip to content

Commit

Permalink
#209: added an option to disable skills-client
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-may committed Oct 8, 2024
1 parent 1d36f49 commit 727a2b6
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!--
Copyright 2020 SkillTree
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Skill Client Test</title>

<link rel="icon" type="image/x-icon" href="./skilltree.ico">
<link rel="stylesheet" type="text/css" href="./assets/app.css"/>
<link rel="stylesheet" type="text/css" href="./assets/js/bootstrap/dist/css/bootstrap.css">

<script type="text/javascript" src="./assets/js/jquery/dist/jquery.js"></script>
<script type="text/javascript" src="./assets/js/axios/dist/axios.min.js"></script>
<script type="text/javascript" src="./assets/js/bootstrap/dist/js/bootstrap.bundle.js"></script>

<script type="module">
import { SkillsReporter, SkillsConfiguration, SkillsLevelJS, SkillsDisplayJS, Logger } from './assets/js/@skilltree/skills-client-js/dist/skills-client-js.esm.min.js'

axios.get("/api/config")
.then((result) => {
SkillsConfiguration.configure({...result.data, enabled: false});
});

document.querySelector('#reportSkillBtn').addEventListener('click', () => {
SkillsReporter.reportSkill('someId')
});

SkillsConfiguration.afterConfigure().then(() => {
const initializeSkillsDisplay = () => {
const clientDisplay = new SkillsDisplayJS();
clientDisplay.attachTo(document.querySelector('#skills-client-display'));
};
if (document.readyState === "complete"
|| document.readyState === "loaded"
|| document.readyState === "interactive") {
initializeSkillsDisplay();
} else {
document.addEventListener("DOMContentLoaded", initializeSkillsDisplay);
}
});
</script>

</head>
<body>

<div id="app">
<button
id="reportSkillBtn"
data-cy="reportSkillButton"
class="btn btn-outline-primary">
Report Skill
</button>

<div data-cy="levelComponentRes">Level: [<span id="skillLevel"/>]</div>
<div data-cy="skillsDisplayRes">Skills Display: [<div id="skills-client-display"></div>]</div>
</div>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2020 SkillTree
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Utils from "./Utils";
import selectors from "../support/selectors";

context("Configuration Disabled Tests", () => {

if (Utils.skillsServiceVersionLaterThan('3.5.1')) {
it('export user transcript', () => {
cy.createDefaultTinyProject('proj1')

// visit client display
cy.intercept('**/public/status', cy.spy().as('getStatus'))
cy.intercept('POST', '**/api/projects/proj1/skillsClientVersion', cy.spy().as('reportClientVersion'))
cy.intercept('**/api/projects/proj1/level', cy.spy().as('getLevel'))
cy.intercept('**/skills-websocket/info**', cy.spy().as('initWebsockets'))

cy.intercept('POST', '** /api/projects/proj1/skills/someId', cy.spy().as('reportSkillId'))
cy.intercept('/api/projects/proj1/summary', cy.spy().as('getProjSummary'))

cy.visit('/native/disabledConfig.html');
cy.wait(5000)
cy.get('@getStatus').should('not.have.been.called');
cy.get('@reportClientVersion').should('not.have.been.called');
cy.get('@getLevel').should('not.have.been.called');
cy.get('@initWebsockets').should('not.have.been.called');
cy.get('[data-cy="levelComponentRes"]').contains('Level: []')

cy.get('[data-cy="skillsDisplayRes"] #skills-client-display').should('be.empty')
cy.get('@getProjSummary').should('not.have.been.called');

cy.get('[data-cy="reportSkillButton"]').click()
cy.wait(3000)
cy.get('@reportSkillId').should('not.have.been.called');
});
}
});
1 change: 1 addition & 0 deletions skills-client-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"licenseHeaderAdd": " npx license-check-and-add add -f license-add/license-add-config.json",
"licenseCheck": "npm run licenseHeaderCheck && npm run licenseDepCheck ",
"build": "npm run licenseCheck && npm run test && npm run clean && npm run build:umd && npm run build:esm",
"buildSkipTests": "npm run clean && npm run build:umd && npm run build:esm",
"build:umd": "rollup --config --format umd --file dist/skills-client-js.umd.min.js --name SkillsClient --bundleConfigAsCjs",
"build:esm": "rollup --config --format esm --file dist/skills-client-js.esm.min.js --bundleConfigAsCjs",
"deploy-to-examples": "npm run build && cp ./dist/skills-display-js.umd.min.js ../skills-examples/skills-example-client-js/app/assets/js/@skilltree/skills-client-js/dist",
Expand Down
17 changes: 17 additions & 0 deletions skills-client-js/src/config/SkillsConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,24 @@ const exportObject = {
authenticator,
authToken,
oauthRedirect = false,
enabled = true,
}) {
if (this.isInitialized()) {
log.warn('SkillsConfiguration already initialized.');
return;
}

log.debug(`SkillsConfiguration::configure params serviceUrl=[${serviceUrl}], projectId=[${projectId}], `
+ `authenticator=[${authenticator}], authToken=[${authToken}], oauthRedirect=${oauthRedirect}, enabled=${enabled}`)

this.enabled = enabled
if (!enabled) {
log.useDefaults();
log.setLevel(log.TRACE)
log.info('SkillsConfiguration is disabled.');
return Promise.resolve();
}

if (!this.skillsClientVersion) {
// this will be replaced at build time with the current skills-client-js
// version. extensions of the plain JS client (vue, react, etc), should
Expand Down Expand Up @@ -152,6 +165,10 @@ SkillsConfiguration is a singleton and you only have to do this once. Please see
return this.projectId;
},

isDisabled() {
return !this.enabled;
},

getServiceUrl() {
return this.serviceUrl;
},
Expand Down
5 changes: 5 additions & 0 deletions skills-client-js/src/display/SkillsDisplayJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export default class SkillsDisplayJS {
}

attachTo(selectorOrElement) {
if (SkillsConfiguration.isDisabled()) {
log.info('SkillsClient::SkillsDisplayJS::attachTo: SkillsConfiguration is disabled Skills Display will not be initialized');
return;
}

log.info(`SkillsClient::SkillsDisplayJS::attaching to [${selectorOrElement ? selectorOrElement.toString() : selectorOrElement}]`);
let iframeContainer = selectorOrElement;
if (typeof selectorOrElement === 'string') {
Expand Down
4 changes: 4 additions & 0 deletions skills-client-js/src/display/SkillsLevelJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default class SkillsLevelJS {
}

attachTo(selectorOrElement) {
if (SkillsConfiguration.isDisabled()) {
log.info('SkillsLevelJS.js::attachTo: SkillsConfiguration is disabled Level will not be initialized');
return;
}
log.info(`SkillsClient::SkillsLevelJS::attaching to [${selectorOrElement}]`);
let skillLevelElement = selectorOrElement;
if (typeof selectorOrElement === 'string') {
Expand Down
17 changes: 17 additions & 0 deletions skills-client-js/src/reporter/SkillsReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ const SkillsReporter = {
this.maxRetryQueueSize = maxRetryQueueSize;
this.maxRetryAttempts = maxRetryAttempts;

if (SkillsConfiguration.isDisabled()) {
log.info('SkillsClient::SkillsReporter::configure: SkillsConfiguration is disabled. No reporting will occur');
return;
}

if (retryInterval != null) {
// cancel existing RetryChecker, then start w/ passed in retryInterval
this.cancelRetryChecker();
Expand All @@ -191,14 +196,26 @@ const SkillsReporter = {
},

addSuccessHandler(handler) {
if (SkillsConfiguration.isDisabled()) {
log.info('SkillsClient::SkillsReporter::addSuccessHandler: SkillsConfiguration is disabled handler is not added');
return;
}
successHandlerCache.add(handler);
log.info(`SkillsClient::SkillsReporter::added success handler [${handler ? handler.toString() : handler}]`);
},
addErrorHandler(handler) {
if (SkillsConfiguration.isDisabled()) {
log.info('SkillsClient::SkillsReporter::addErrorHandler: SkillsConfiguration is disabled handler is not added');
return;
}
errorHandlerCache.add(handler);
log.info(`SkillsClient::SkillsReporter::added error handler [${handler ? handler.toString() : handler}]`);
},
reportSkill(userSkillId, timestamp = null, isRetry = false, retryAttempt = undefined) {
if (SkillsConfiguration.isDisabled()) {
log.info(`SkillsClient::SkillsReporter::reportSkill: SkillsConfiguration is disabled. No reporting will occur for skillId=[${userSkillId}]`);
return Promise.resolve();
}
log.info(`SkillsClient::SkillsReporter::reporting skill [${userSkillId}] retryAttempt [${retryAttempt}]`);
SkillsConfiguration.validate();
if (retryIntervalId == null) {
Expand Down

0 comments on commit 727a2b6

Please sign in to comment.