cdn-experiments/ccp/js/infrastructurelistcontrolle...

75 lines
2.2 KiB
JavaScript

class CCPInfrastructureList extends HTMLElement{
#boot;
#socket;
#data = {};
#serviceurl = "https://nubis1.int.d4science.net:8080"
#broadcasturl = "ws://nubis1.int.d4science.net:8989/ws/notification"
constructor(){
super()
this.#boot = document.querySelector("d4s-boot-2")
}
connectedCallback(){
this.fetchInfrastructures()
}
get data(){
return this.#data
}
fetchInfrastructures(){
console.log("Calling fetch infrastructures")
this.#boot.secureFetch(this.#serviceurl + "/infrastructures/cache").
then(resp=>{
return resp.json()
}).then(data=>{
this.#data = data
/*this.updateList()*/
}).catch(err=>{
alert("Error while downloading methods: ", err)
})
}
/* updateList(resp){
this.connectBroadcast()
this.#infrastructures.forEach(i=>{
this.#data[i.id] = { id : i.id, status : "unknown", type : i.type, name : i.name, runtimes : [] }
this.#boot.secureFetch(this.#serviceurl + `/infrastructures/${i.id}/status`).then(
()=>console.log("Requested async status update for infrastructure ", i.id)
)
})
}
*/
/* connectBroadcast(){
this.#socket = new WebSocket(this.#broadcasturl + "/infrastructures");
this.#socket.onmessage = event=>{
const data = JSON.parse(event.data)
this.#data[data.infrastructure].status = data.status.toLowerCase()
if(data.type.toLowerCase() === "update" && data.status.toLowerCase() === "ok"){
this.#data[data.infrastructure].runtimes = data.data.runtimes
}else{
this.#data[data.infrastructure].runtimes = []
}
console.log("New Infrastructure status", this.#data)
}
window.setInterval( ()=>this.#socket.send("ping"), 30000)
}
*/
getCompatibleRuntimes(rts){
const available = Object.keys(this.#data).reduce((acc, i) => {
const compatiblerts = this.#data[i].runtimes.
filter(r=>rts.indexOf(r["descriptor-id"]) >= 0).
map(cr=>{ return { runtime : cr, infrastructure : this.#data[i] } })
return acc.concat(compatiblerts)
}, [])
return available
}
}
window.customElements.define('d4s-ccp-infrastructurelist', CCPInfrastructureList);