mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-15 18:40:40 +00:00
Cleanup in cqi over socketio
This commit is contained in:
@ -1,13 +1,15 @@
|
||||
class CorpusAnalysisApp {
|
||||
constructor(corpusId) {
|
||||
this.corpusId = corpusId;
|
||||
|
||||
this.data = {};
|
||||
|
||||
// HTML elements
|
||||
this.elements = {
|
||||
container: document.querySelector('#corpus-analysis-app-container'),
|
||||
extensionCards: document.querySelector('#corpus-analysis-app-extension-cards'),
|
||||
extensionTabs: document.querySelector('#corpus-analysis-app-extension-tabs'),
|
||||
initModal: document.querySelector('#corpus-analysis-app-init-modal'),
|
||||
overview: document.querySelector('#corpus-analysis-app-overview')
|
||||
initModal: document.querySelector('#corpus-analysis-app-init-modal')
|
||||
};
|
||||
// Materialize elements
|
||||
this.elements.m = {
|
||||
@ -17,52 +19,49 @@ class CorpusAnalysisApp {
|
||||
|
||||
this.extensions = {};
|
||||
|
||||
this.settings = {
|
||||
corpusId: corpusId
|
||||
};
|
||||
this.settings = {};
|
||||
}
|
||||
|
||||
init() {
|
||||
async init() {
|
||||
this.disableActionElements();
|
||||
this.elements.m.initModal.open();
|
||||
// Init data
|
||||
this.data.cqiClient = new cqi.CQiClient('/cqi_over_sio', this.settings.corpusId);
|
||||
this.data.cqiClient.connect('anonymous', '')
|
||||
.then((cqiStatus) => {
|
||||
return this.data.cqiClient.corpora.list();
|
||||
})
|
||||
.then((cqiCorpora) => {
|
||||
this.data.corpus = {o: cqiCorpora[0]};
|
||||
console.log(this.data.corpus.o.staticData);
|
||||
// this.renderGeneralCorpusInfo();
|
||||
// this.renderTextInfoList();
|
||||
// this.renderTextProportionsGraphic()
|
||||
// this.renderFrequenciesGraphic();
|
||||
// this.renderBoundsGraphic();
|
||||
|
||||
// TODO: Don't do this hgere
|
||||
this.data.corpus.o.updateDb();
|
||||
this.enableActionElements();
|
||||
for (let extension of Object.values(this.extensions)) {extension.init();}
|
||||
this.elements.m.initModal.close();
|
||||
},
|
||||
(cqiError) => {
|
||||
let errorString = `${cqiError.code}: ${cqiError.constructor.name}`;
|
||||
let errorsElement = this.elements.initModal.querySelector('.errors');
|
||||
let progressElement = this.elements.initModal.querySelector('.progress');
|
||||
errorsElement.innerText = errorString;
|
||||
errorsElement.classList.remove('hide');
|
||||
app.flash(errorString, 'error');
|
||||
progressElement.classList.add('hide');
|
||||
}
|
||||
);
|
||||
|
||||
// Add event listeners
|
||||
for (let extensionSelectorElement of this.elements.overview.querySelectorAll('.extension-selector')) {
|
||||
|
||||
// Setup CQi over SocketIO connection and gather data from the CQPServer
|
||||
let cqiClient;
|
||||
let cqiCorpus;
|
||||
try {
|
||||
cqiClient = new cqi.CQiClient('/cqi_over_sio');
|
||||
let response = await cqiClient.api.socket.emitWithAck('init', this.corpusId);
|
||||
if (response.code !== 200) {throw new Error();}
|
||||
await cqiClient.connect('anonymous', '');
|
||||
cqiCorpus = await cqiClient.corpora.get(`NOPAQUE-${this.corpusId.toUpperCase()}`);
|
||||
// TODO: Don't do this hgere
|
||||
await cqiCorpus.updateDb();
|
||||
} catch (error) {
|
||||
// TODO: Currently we can only handle CQiErrors here,
|
||||
// but we should also handle other errors.
|
||||
let errorString = `${error.code}: ${error.constructor.name}`;
|
||||
let errorsElement = this.elements.initModal.querySelector('.errors');
|
||||
let progressElement = this.elements.initModal.querySelector('.progress');
|
||||
errorsElement.innerText = errorString;
|
||||
errorsElement.classList.remove('hide');
|
||||
progressElement.classList.add('hide');
|
||||
return;
|
||||
}
|
||||
this.data.cqiClient = cqiClient;
|
||||
this.data.cqiCorpus = cqiCorpus;
|
||||
this.data.corpus = {o: cqiCorpus}; // legacy
|
||||
|
||||
// Initialize extensions
|
||||
for (let extension of Object.values(this.extensions)) {extension.init();}
|
||||
for (let extensionSelectorElement of this.elements.extensionCards.querySelectorAll('.extension-selector')) {
|
||||
extensionSelectorElement.addEventListener('click', () => {
|
||||
this.elements.m.extensionTabs.select(extensionSelectorElement.dataset.target);
|
||||
});
|
||||
}
|
||||
|
||||
this.enableActionElements();
|
||||
this.elements.m.initModal.close();
|
||||
}
|
||||
|
||||
registerExtension(extension) {
|
||||
@ -71,7 +70,6 @@ class CorpusAnalysisApp {
|
||||
return;
|
||||
}
|
||||
this.extensions[extension.name] = extension;
|
||||
if ('cQiClient' in this.data && this.data.cQiClient.connected) {extension.init();}
|
||||
}
|
||||
|
||||
disableActionElements() {
|
||||
|
@ -1,18 +1,16 @@
|
||||
cqi.api.APIClient = class APIClient {
|
||||
/**
|
||||
* @param {string} host
|
||||
* @param {string} corpusId
|
||||
* @param {number} [timeout=60] timeout
|
||||
* @param {string} [version=0.1] version
|
||||
*/
|
||||
constructor(host, corpus_id, timeout = 60, version = '0.1') {
|
||||
constructor(host, timeout = 60, version = '0.1') {
|
||||
this.host = host;
|
||||
this.timeout = timeout * 1000; // convert seconds to milliseconds
|
||||
this.version = version;
|
||||
this.socket = io(
|
||||
this.host,
|
||||
{
|
||||
auth: {corpus_id: corpus_id},
|
||||
transports: ['websocket'],
|
||||
upgrade: false
|
||||
}
|
||||
@ -24,22 +22,16 @@ cqi.api.APIClient = class APIClient {
|
||||
* @param {object} [fn_args={}]
|
||||
* @returns {Promise}
|
||||
*/
|
||||
#request(fn_name, fn_args = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// this.socket.timeout(this.timeout).emit('cqi', {fn_name: fn_name, fn_args: fn_args}, (timeoutError, response) => {
|
||||
// if (timeoutError) {
|
||||
// reject(timeoutError);
|
||||
// }
|
||||
this.socket.emit('cqi', fn_name, fn_args, (response) => {
|
||||
if (response.code === 200) {
|
||||
resolve(response.payload);
|
||||
} else if (response.code === 500) {
|
||||
reject(new Error(`[${response.code}] ${response.msg}`));
|
||||
} else if (response.code === 502) {
|
||||
reject(new cqi.errors.lookup[response.payload.code]());
|
||||
}
|
||||
});
|
||||
});
|
||||
async #request(fn_name, fn_args = {}) {
|
||||
// TODO: implement timeout
|
||||
let response = await this.socket.emitWithAck('exec', fn_name, fn_args);
|
||||
if (response.code === 200) {
|
||||
return response.payload;
|
||||
} else if (response.code === 500) {
|
||||
throw new Error(`[${response.code}] ${response.msg}`);
|
||||
} else if (response.code === 502) {
|
||||
throw new cqi.errors.lookup[response.payload.code]();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,13 +1,12 @@
|
||||
cqi.CQiClient = class CQiClient {
|
||||
/**
|
||||
* @param {string} host
|
||||
* @param {string} corpusId
|
||||
* @param {number} [timeout=60] timeout
|
||||
* @param {string} [version=0.1] version
|
||||
*/
|
||||
constructor(host, corpusId, timeout = 60, version = '0.1') {
|
||||
constructor(host, timeout = 60, version = '0.1') {
|
||||
/** @type {cqi.api.APIClient} */
|
||||
this.api = new cqi.api.APIClient(host, corpusId, timeout, version);
|
||||
this.api = new cqi.api.APIClient(host, timeout, version);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user