class CCPInfrastructureList extends HTMLElement{
#boot;
#infrastructures;
#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").
then(resp=>{
console.log("Received resp for infrastructures ", resp.status)
return resp.json()
}).then(data=>{
this.#infrastructures = data
this.updateList()
}).catch(err=>{
alert("Error while downloading methods: ", err)
})
}
updateList(resp){
this.connectBroadcast()
this.#infrastructures.forEach(i=>{
this.#data[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){
console.log("Request for ", rts)
const available = Object.keys(this.#data).reduce((acc, i) => {
console.log(this.#data[i].runtimes)
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);