Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | 5x 5x 5x 5x 5x 5x 5x 23x 84x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 378x 378x 378x 12x 12x 12x 5x 21x 21x 21x 17x 17x 3x 3x 18x 18x 15x 18x 17x 17x 17x 1x 5x 31x 31x 31x 31x 31x 29x 31x 75x 27x 19x 17x 4x 17x 2x 19x 19x 8x 31x 31x 31x 31x 5x 12x 12x 12x 12x 12x 1x 1x 11x 11x 11x 328x 3x 3x 3x 11x 11x 11x 35x 35x 35x 35x 4x 50x 35x 35x 12x 35x 35x 35x 35x 10x 6x 6x 4x 4x 4x 4x 25x 35x 23x 35x 20x 12x 12x 5x 1x 1x 1x 1x | /*
* Copyright 2025 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 SockJS from 'sockjs-client';
import { Client } from '@stomp/stompjs';
import log from 'js-logger';
import SkillsConfiguration from '../config/SkillsConfiguration';
import skillsService from '../SkillsService';
const SUCCESS_EVENT = 'skills-report-success';
const FAILURE_EVENT = 'skills-report-error';
const successHandlerCache = new Set();
const errorHandlerCache = new Set();
let retryIntervalId = null;
const callSuccessHandlers = (event) => {
successHandlerCache.forEach((it) => it(event));
};
const callErrorHandlers = (event) => {
// eslint-disable-next-line no-console
console.error('Error reporting skill:', event);
errorHandlerCache.forEach((it) => it(event));
};
let stompClient;
const connectWebsocket = (serviceUrl) => {
if (!stompClient || !stompClient.active) {
const wsUrl = `${serviceUrl}/skills-websocket`;
log.info(`SkillsClient::SkillsReporter::connecting websocket using SockJS to [${wsUrl}]`);
Eif (!stompClient) {
stompClient = new Client();
}
let headers = {};
Eif (!SkillsConfiguration.isPKIMode()) {
log.debug('SkillsClient::SkillsReporter::adding Authorization header to web socket connection');
headers = { Authorization: `Bearer ${SkillsConfiguration.getAuthToken()}` };
}
stompClient.configure({
webSocketFactory: () => new SockJS(wsUrl),
connectHeaders: headers,
onConnect: () => {
log.info('SkillsClient::SkillsReporter::stompClient connected');
const topic = `/user/queue/${SkillsConfiguration.getProjectId()}-skill-updates`;
log.info(`SkillsClient::SkillsReporter::stompClient subscribing to topic [${topic}]`);
stompClient.subscribe(topic, (update) => {
log.debug(`SkillsClient::SkillsReporter::ws message [${update.body}] received over topic [${topic}]. calling success handlers...`);
callSuccessHandlers(JSON.parse(update.body));
log.debug('SkillsClient::SkillsReporter::Done calling success handlers...');
});
window.postMessage({ skillsWebsocketConnected: true }, window.location.origin);
log.debug('SkillsClient::SkillsReporter::window.postMessage skillsWebsocketConnected');
},
onStompError: (frame) => {
log.error(`Received STOMP error event. headers [${JSON.stringify(headers)}] frame [${frame}]`);
if (!SkillsConfiguration.isPKIMode() && frame && frame.headers && frame.headers.message && frame.headers.message.includes('invalid_token')) {
skillsService.getAuthenticationToken(SkillsConfiguration.getAuthenticator(), SkillsConfiguration.getServiceUrl(), SkillsConfiguration.getProjectId())
.then((token) => {
SkillsConfiguration.setAuthToken(token);
stompClient.deactivate();
connectWebsocket(SkillsConfiguration.getServiceUrl());
})
.catch((err) => {
log.error(`SkillsReporter::Unable to retrieve auth token when attempting to reconnect web socket [${err}]`);
});
}
},
onDisconnect: (frame) => {
log.info(`Received disconnect event. frame [${frame}]`);
},
// debug: (str) => {
// console.log(new Date(), str);
// },
});
log.debug('SkillsClient::SkillsReporter::activating stompClient...');
stompClient.activate();
log.debug('SkillsClient::SkillsReporter::stompClient activated');
} else E{
log.warn('SkillsClient::SkillsReporter::websocket already connecting, preventing duplicate connection.', stompClient.active);
}
};
const retryQueueKey = 'skillTreeRetryQueue';
const defaultMaxRetryQueueSize = 1000;
const defaultRetryInterval = 60000;
const defaultMaxRetryAttempts = 1440;
const retryErrors = function retryErrors() {
const retryQueue = JSON.parse(localStorage.getItem(retryQueueKey));
localStorage.removeItem(retryQueueKey);
if (retryQueue !== null) {
retryQueue.forEach((item) => {
log.info(`SkillsClient::SkillsReporter::retryErrors retrying skillId [${item.skillId}], timestamp [${item.timestamp}], retryAttempt [${item.retryAttempt}]`);
this.reportSkill(item.skillId, item.timestamp, true, item.retryAttempt);
});
}
};
const addToRetryQueue = (skillId, timeReported, retryAttempt, xhr, maxQueueSize) => {
const status = xhr ? xhr.status : null;
log.info(`SkillsClient::SkillsReporter::addToRetryQueue [${skillId}], timeReported [${timeReported}], retryAttempt[${retryAttempt}], status [${status}]`);
if (xhr && xhr.response) {
const xhrResponse = JSON.parse(xhr.response);
if (xhrResponse && xhrResponse.errorCode === 'SkillNotFound') {
log.info('not adding to retry queue because the skillId does not exist.');
return;
}
}
let retryQueue = JSON.parse(localStorage.getItem(retryQueueKey));
if (retryQueue == null) {
retryQueue = [];
}
if (retryQueue.length < maxQueueSize) {
const timestamp = (timeReported == null) ? Date.now() : timeReported;
retryQueue.push({ skillId, timestamp, retryAttempt });
localStorage.setItem(retryQueueKey, JSON.stringify(retryQueue));
} else {
log.warn(`Max retry queue size has been reached (${maxQueueSize}), Unable to retry skillId [${skillId}]`);
}
};
const reportInternal = (resolve, reject, userSkillId, timestamp, isRetry, retryAttempt, maxRetryAttempts, maxRetryQueueSize, notifyIfSkillNotApplied) => {
SkillsConfiguration.validate();
const xhr = new XMLHttpRequest();
xhr.open('POST', `${SkillsConfiguration.getServiceUrl()}/api/projects/${SkillsConfiguration.getProjectId()}/skills/${userSkillId}`);
xhr.withCredentials = true;
if (!SkillsConfiguration.isPKIMode()) {
xhr.setRequestHeader('Authorization', `Bearer ${SkillsConfiguration.getAuthToken()}`);
}
xhr.onreadystatechange = () => {
// some browsers don't understand XMLHttpRequest.Done, which should be 4
if (xhr.readyState === 4) {
if (xhr.status !== 200) {
if (retryAttempt <= maxRetryAttempts) {
if ((xhr.status === 401 || xhr.status === 0) && !SkillsConfiguration.isPKIMode()) {
SkillsConfiguration.setAuthToken(null);
}
addToRetryQueue(userSkillId, timestamp, retryAttempt, xhr, maxRetryQueueSize);
} else {
log.warn(`Max retry attempts has been reached (${maxRetryAttempts}), Unable to retry skillId [${userSkillId}]`);
}
if (xhr.response) {
reject(JSON.parse(xhr.response));
} else E{
reject(new Error(`Error occurred reporting skill [${userSkillId}], status returned [${xhr.status}]`));
}
} else {
resolve(JSON.parse(xhr.response));
}
}
};
const body = JSON.stringify({ timestamp, notifyIfSkillNotApplied, isRetry });
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.send(body);
log.info(`SkillsClient::SkillsReporter::reporting skill request sent: ${body}`);
};
const SkillsReporter = {
configure({
notifyIfSkillNotApplied, retryInterval = defaultRetryInterval, maxRetryQueueSize = defaultMaxRetryQueueSize, maxRetryAttempts = defaultMaxRetryAttempts,
}) {
this.notifyIfSkillNotApplied = notifyIfSkillNotApplied;
this.retryInterval = retryInterval;
this.maxRetryQueueSize = maxRetryQueueSize;
this.maxRetryAttempts = maxRetryAttempts;
if (SkillsConfiguration.isDisabled()) {
log.info('SkillsClient::SkillsReporter::configure: SkillsConfiguration is disabled. No reporting will occur');
return;
}
Eif (retryInterval != null) {
// cancel existing RetryChecker, then start w/ passed in retryInterval
this.cancelRetryChecker();
}
log.info(`SkillsClient::SkillsReporter::Enabling retries. retryInterval [${this.retryInterval}]`);
retryIntervalId = setInterval(() => { retryErrors.call(this); }, this.retryInterval || defaultRetryInterval);
},
addSuccessHandler(handler) {
Iif (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. successHandlerCache size [${successHandlerCache.size}]`);
},
addErrorHandler(handler) {
Iif (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. errorHandlerCache size [${errorHandlerCache.size}]`);
},
reportSkill(userSkillId, timestamp = null, isRetry = false, retryAttempt = undefined) {
Iif (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) {
log.info(`SkillsClient::SkillsReporter::Enabling retries. retryInterval [${this.retryInterval}]`);
retryIntervalId = setInterval(() => { retryErrors.call(this); }, this.retryInterval || defaultRetryInterval);
}
let retryAttemptInternal = 1;
if (retryAttempt !== undefined) {
retryAttemptInternal = retryAttempt + 1;
}
const maxRetryAttempts = this.maxRetryAttempts || defaultMaxRetryAttempts;
const maxRetryQueueSize = this.maxRetryQueueSize || defaultMaxRetryQueueSize;
const promise = new Promise((resolve, reject) => {
if (!SkillsConfiguration.getAuthToken() && !SkillsConfiguration.isPKIMode()) {
skillsService.getAuthenticationToken(SkillsConfiguration.getAuthenticator(), SkillsConfiguration.getServiceUrl(), SkillsConfiguration.getProjectId())
.then((token) => {
SkillsConfiguration.setAuthToken(token);
reportInternal(resolve, reject, userSkillId, timestamp, isRetry, retryAttemptInternal, maxRetryAttempts, maxRetryQueueSize, this.notifyIfSkillNotApplied);
})
.catch((err) => {
if (retryAttemptInternal <= maxRetryAttempts) {
addToRetryQueue(userSkillId, timestamp, retryAttemptInternal, null, maxRetryQueueSize);
} else E{
log.warn(`Max retry attempts has been reached (${this.maxRetryAttempts}), Unable to retry skillId [${userSkillId}]`);
}
log.error(`SkillsReporter::Unable to retrieve auth token reporting skill [${userSkillId}]`);
reject(err);
});
} else {
reportInternal(resolve, reject, userSkillId, timestamp, isRetry, retryAttemptInternal, maxRetryAttempts, maxRetryQueueSize, this.notifyIfSkillNotApplied);
}
});
promise.catch((error) => {
callErrorHandlers(error);
});
return promise;
},
getConf() {
return SkillsConfiguration;
},
cancelRetryChecker() {
if (retryIntervalId != null) {
clearInterval(retryIntervalId);
retryIntervalId = null;
}
},
};
SkillsConfiguration.afterConfigure().then(() => {
connectWebsocket(SkillsConfiguration.getServiceUrl());
Eif (!retryIntervalId) {
log.info(`SkillsClient::SkillsReporter::Enabling retries. retryInterval [${SkillsReporter.retryInterval}]`);
retryIntervalId = setInterval(() => { retryErrors.call(SkillsReporter); }, SkillsReporter.retryInterval || defaultRetryInterval);
}
});
export {
SkillsReporter,
SUCCESS_EVENT,
FAILURE_EVENT,
};
|