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

77 lines
2.3 KiB
JavaScript
Raw Normal View History

2022-03-29 16:06:46 +02:00
class CCPInfrastructureList extends HTMLElement{
#boot;
2022-04-12 19:51:38 +02:00
#infrastructures;
#socket;
#data = {};
2022-03-29 16:06:46 +02:00
#serviceurl = "https://nubis1.int.d4science.net:8080"
2022-04-12 19:51:38 +02:00
#broadcasturl = "ws://nubis1.int.d4science.net:8989/ws/notification"
2022-03-29 16:06:46 +02:00
constructor(){
super()
this.#boot = document.querySelector("d4s-boot-2")
2022-04-12 19:51:38 +02:00
}
connectedCallback(){
2022-03-29 16:06:46 +02:00
this.fetchInfrastructures()
}
2022-04-12 19:51:38 +02:00
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)
})
2022-03-29 16:06:46 +02:00
}
updateList(resp){
2022-04-12 19:51:38 +02:00
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
2022-03-29 16:06:46 +02:00
}
}
window.customElements.define('d4s-ccp-infrastructurelist', CCPInfrastructureList);