Skip to content

Commit

Permalink
Fix and improve grid settings configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
imedina committed Oct 23, 2023
1 parent 13f030c commit 379b1b7
Show file tree
Hide file tree
Showing 30 changed files with 274 additions and 584 deletions.
22 changes: 14 additions & 8 deletions src/core/clients/opencga/opencga-catalog-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,22 @@ export default class OpencgaCatalogUtils {
}

// Update grid configuration of the specified browser
static updateGridConfig(opencgaSession, browserName, newGridConfig) {
const newConfig = {
...opencgaSession.user.configs?.IVA,
[browserName]: {
...opencgaSession.user.configs?.IVA[browserName],
grid: newGridConfig,
},
static updateGridConfig(id = "IVA", opencgaSession, toolId, gridConfig) {
const userGridSettings = {
settings: {
...opencgaSession.user.configs?.[id]?.settings,
[toolId]: {
...opencgaSession.user.configs?.[id][toolId],
grid: gridConfig,
},
}
};
const newGridConfig = {
...opencgaSession.user.configs?.[id],
...userGridSettings
};

return opencgaSession.opencgaClient.updateUserConfigs(newConfig)
return opencgaSession.opencgaClient.updateUserConfig(id, newGridConfig)
.then(response => {
// Update user configuration in opencgaSession object
// eslint-disable-next-line no-param-reassign
Expand Down
133 changes: 53 additions & 80 deletions src/core/clients/opencga/opencga-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import User from "./api/User.js";
import Variant from "./api/Variant.js";
import VariantOperation from "./api/VariantOperation.js";
import {CellBaseClient} from "../cellbase/cellbase-client";
import UtilsNew from "../../utils-new";


export class OpenCGAClient {
Expand Down Expand Up @@ -170,14 +171,6 @@ export class OpenCGAClient {
return this.clients.get("meta");
}

admin() {
if (!this.clients.has("admin")) {
this.clients.set("admin", new Admin(this._config));
}
return this.clients.get("admin");
}

// Analysis
alignments() {
if (!this.clients.has("alignments")) {
this.clients.set("alignments", new Alignment(this._config));
Expand Down Expand Up @@ -213,6 +206,13 @@ export class OpenCGAClient {
return this.clients.get("ga4gh");
}

admin() {
if (!this.clients.has("admin")) {
this.clients.set("admin", new Admin(this._config));
}
return this.clients.get("admin");
}

/*
* Convenient function to create a client from the entity name, this is case insensitive.
*/
Expand Down Expand Up @@ -250,59 +250,52 @@ export class OpenCGAClient {
return this.clinical();
case "META":
return this.meta();
case "ADMIN":
return this.admin();
default:
throw new Error("Resource not recognized");
}
}

async login(userId, password) {
try {
const restResponse = await this.users().login({user: userId, password: password});
const restResponse = await this.users()
.login({user: userId, password: password});

// TODO remove userId and token from config and move it to session
this._config.userId = userId;
this._config.token = restResponse.getResult(0).token;

// Check if cookies being used
if (this._config.cookies.active) {
this.setCookies(userId, this._config.token);
this.#setCookies(userId, this._config.token);
}
this.clients.forEach(client => client.setToken(this._config.token));
// this.createSession();
return restResponse;
} catch (restResponse) {
console.error(restResponse);
return restResponse;
}
}

setCookies(userId, token) {
if (userId && token) {
// eslint-disable-next-line no-undef
Cookies.set(this._config.cookies.prefix + "_userId", userId, {secure: true});
// eslint-disable-next-line no-undef
Cookies.set(this._config.cookies.prefix + "_sid", this._config.token, {secure: true});
} else {
// eslint-disable-next-line no-undef
Cookies.expire(this._config.cookies.prefix + "_userId");
// eslint-disable-next-line no-undef
Cookies.expire(this._config.cookies.prefix + "_sid");
}
}

// refresh only works if cookies are enabled
// Refresh only works if cookies are enabled
async refresh() {
const userId = this._config.userId;
const response = await this.users().login({refreshToken: this._config.token});
const response = await this.users()
.login({refreshToken: this._config.token});
this._config.token = response.getResult(0).token;

await this.updateUserConfigs({
lastAccess: new Date().getTime()
});
// Nacho (22/10/2023): We should not update 'lastAccess' date when token is updated automatically
// await this.updateUserConfig({
// lastAccess: new Date().getTime()
// });

// Update cookie with the new token
if (this._config.cookies.active) {
this.setCookies(userId, this._config.token);
this.#setCookies(userId, this._config.token);
}

// Update existing clients with the new token
this.clients.forEach(client => client.setToken(this._config.token));
return response;
}
Expand All @@ -315,11 +308,25 @@ export class OpenCGAClient {

// Remove cookies
if (this._config.cookies.active) {
this.setCookies();
this.#setCookies();
}
return Promise.resolve();
}

#setCookies(userId, token) {
if (userId && token) {
// eslint-disable-next-line no-undef
Cookies.set(this._config.cookies.prefix + "_userId", userId, {secure: true});
// eslint-disable-next-line no-undef
Cookies.set(this._config.cookies.prefix + "_sid", this._config.token, {secure: true});
} else {
// eslint-disable-next-line no-undef
Cookies.expire(this._config.cookies.prefix + "_userId");
// eslint-disable-next-line no-undef
Cookies.expire(this._config.cookies.prefix + "_sid");
}
}

// Creates and return an anonymous session object, it is a sync function.
createAnonymousSession() {
const opencgaSession = {};
Expand All @@ -342,14 +349,12 @@ export class OpenCGAClient {
* opencgaClient object itself.
* @returns {Promise<any>}
*/
// TODO urgent refactor
createSession() {
const _this = this;
return new Promise((resolve, reject) => {
// check that a session exists
// TODO should we check the session has not expired?
if (_this._config.token) {
// _this._notifySessionEvent("signingIn", "Fetching User data");
_this.users().info(_this._config.userId)
.then(async response => {
console.log("Creating session");
Expand All @@ -366,8 +371,7 @@ export class OpenCGAClient {
// serverVersion: _this._config.serverVersion,
};
session.opencgaClient = _this;
// _this._notifySessionEvent("signingIn", "Updating User config");
const userConfig = await this.updateUserConfigs({
const userConfig = await this.updateUserConfig("IVA", {
...session.user.configs.IVA,
lastAccess: new Date().getTime()
});
Expand All @@ -382,7 +386,6 @@ export class OpenCGAClient {

// Fetch authorised Projects and Studies
console.log("Fetching projects and studies");
// _this._notifySessionEvent("signingIn", "Fetching Projects and Studies");
_this.projects()
.search({limit: 100})
.then(async function (response) {
Expand All @@ -401,7 +404,6 @@ export class OpenCGAClient {
for (const study of project.studies) {
// We need to store the user permission for the all the studies fetched
console.log("Fetching user permissions");
// _this._notifySessionEvent("signingIn", "Fetching User permissions");

let acl = null;
const admins = study.groups.find(g => g.id === "@admins");
Expand All @@ -414,7 +416,6 @@ export class OpenCGAClient {

// Fetch all the cohort
console.log("Fetching cohorts");
// _this._notifySessionEvent("signingIn", "Fetching Cohorts");
const cohortsResponse = await _this.cohorts()
.search({study: study.fqn, exclude: "samples", limit: 100});
study.cohorts = cohortsResponse.responses[0].results
Expand Down Expand Up @@ -456,7 +457,7 @@ export class OpenCGAClient {
version: project.cellbase.version.startsWith("v") ? project.cellbase.version : "v" + project.cellbase.version,
species: "hsapiens",
});
// https://ws.zettagenomics.com/cellbase/webservices/rest/v5.1/meta/hsapiens/dataReleases
// Call to: https://ws.zettagenomics.com/cellbase/webservices/rest/v5.1/meta/hsapiens/dataReleases
const promise = cellbaseClient.getMeta("dataReleases");
cellbaseSourcesPromises.push(promise);
indexesMap.push(i);
Expand All @@ -477,7 +478,6 @@ export class OpenCGAClient {

// Fetch the Disease Panels for each Study
console.log("Fetching disease panels");
// _this._notifySessionEvent("signingIn", "Fetching Disease Panels");
const panelPromises = [];
for (const study of studies) {
const promise = _this.panels().search({
Expand Down Expand Up @@ -517,29 +517,6 @@ export class OpenCGAClient {
});
}

// async _fetchCellBaseSources(project) {
// if (project.cellbase?.url && project.cellbase.version !== "v5" && project.cellbase.version !== "v4") {
// const cellbaseClient = new CellBaseClient({
// host: project.cellbase.url,
// version: project.cellbase.version.startsWith("v") ? project.cellbase.version : "v" + project.cellbase.version,
// species: "hsapiens",
// });
// console.log(project.cellbase)
// // https://ws.zettagenomics.com/cellbase/webservices/rest/v5.1/meta/hsapiens/dataReleases
// return cellbaseClient.getMeta("dataReleases");
// }
// }

_notifySessionEvent(id, message) {
globalThis.dispatchEvent(new CustomEvent(id,
{
detail: {
value: message
}
}
));
}

getConfig() {
return this._config;
}
Expand All @@ -557,22 +534,18 @@ export class OpenCGAClient {
return this.users().configs(this._config.userId, "IVA");
}

updateUserConfigs(data) {
// TODO remove this nasty nested bug fix
if (data?.IVA) {
delete data.IVA;
}
const userIvaConfig = this.users().updateConfigs(this._config.userId, {
id: "IVA",
configuration: {
...data
}
});
// Update opencgaSession object
// if (opencgaSession?.user?.configs) {
// opencgaSession.user.configs.IVA = userIvaConfig.responses[0].results[0];
// }
return userIvaConfig;
// Nacho (22/10/2023): This method needs a config ID and VALUE now,
// different sites or apps may need to store configurations.
updateUserConfig(id, newConfig) {
return this.users()
.updateConfigs(this._config.userId, {
id: id,
configuration: {
...newConfig,
date: UtilsNew.getDatetime(),
version: "v2"
}
});
}

}
37 changes: 4 additions & 33 deletions src/sites/api/api-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ class ApiApp extends LitElement {
console.error(e);
this.notificationManager.error("Error creating session", e.message);
}).finally(() => {
this.signingIn = false;
this.requestUpdate();
});
this.signingIn = false;
this.requestUpdate();
});
}

// TODO turn this into a Promise
Expand Down Expand Up @@ -343,35 +343,6 @@ class ApiApp extends LitElement {
window.clearInterval(this.intervalCheckSession);
}

async saveLastStudy(newStudy) {
const userConfig = await this.opencgaClient.updateUserConfigs({
...this.opencgaSession.user.configs.IVA,
lastStudy: newStudy.fqn
});
this.opencgaSession.user.configs.IVA = userConfig.responses[0].results[0];
}

onUrlChange(e) {
let hashFrag = e.detail.id;
if (UtilsNew.isNotUndefined(this.opencgaSession.project) && UtilsNew.isNotEmpty(this.opencgaSession.project.alias)) {

hashFrag += "/" + this.opencgaSession.project.alias;
if (UtilsNew.isNotUndefined(this.opencgaSession.study) && UtilsNew.isNotEmpty(this.opencgaSession.study.alias)) {
hashFrag += "/" + this.opencgaSession.study.alias;
}
}

const myQueryParams = [];
for (const key in e.detail.query) {
myQueryParams.push(key + "=" + e.detail.query[key]);
}
if (myQueryParams.length > 0) {
hashFrag += `?${myQueryParams.join("&")}`;
}

window.location.hash = hashFrag;
}

checkSessionActive() {
// We check if refresh token has updated session id cookie
// let sid = Cookies.get(this.config.opencga.cookie.prefix + "_sid");
Expand Down Expand Up @@ -572,7 +543,7 @@ class ApiApp extends LitElement {

if (studyFound) {
// Update the lastStudy in config iff has changed
this.opencgaClient.updateUserConfigs({...this.opencgaSession.user.configs, lastStudy: studyFqn});
this.opencgaClient.updateUserConfig("IVA", {...this.opencgaSession.user.configs["IVA"], lastStudy: studyFqn});

// Refresh the session
this.opencgaSession = {...this.opencgaSession};
Expand Down
Loading

0 comments on commit 379b1b7

Please sign in to comment.