fix request synchro

This commit is contained in:
Jean-Marie Place 2023-11-22 17:37:21 +01:00
parent f51f05cbab
commit 9bb7a1add7
2 changed files with 206 additions and 64 deletions

View File

@ -0,0 +1,198 @@
const studentsdemo = [
{ "name": "Alice", "wait": 500, "sems": [0, 2] },
{ "name": "Bob", "wait": 500, "sems": [1, 2, 3]}
];
const semsdemo = [
{ "id": 0, "wait": 4, "notes": {"Alice": 13 } },
{ "id": 1, "wait": 3, "notes": {"Bob": 6 } },
{ "id": 2, "wait": 8, "notes": {"Alice": 11, "Bob": 18 } },
{ "id": 3, "wait": 7, "notes": {"Bob": 17 } },
];
/*
getStudents <= [ "Alice", #Bob" ]
getSems("Alice") <= [ 1, 3]
getSems("Bob") <= [ 2, 3, 4]
getMarks(1) <= {"Alice": 13 }
getMarks(2) <= {"Alice": 13 }...
getMarks(3) <= {"Alice": 13 }
getMarks(4) <= {"Alice": 13 }
*/
const api = {
getStudents() {
return new Promise((resolve) => {
setTimeout(() => {
resolve(studentsdemo);
}, 500);
});
},
getStudentSems(student) {
return new Promise(
resolve => { setTimeout(resolve, student.wait, student.sems); }
);
},
getMarks(sem_id) {
return new Promise(
resolve => { setTimeout(resolve, semsdemo[sem_id].wait, semsdemo[sem_id].notes ); }
)
}
}
$(document).ready(function() {
$("#ok").click(function(){
do_export();
});
});
function getData() {
// let students = api.getStudents()
// console.log(students);
// let promises =
// students.map((student) => api.getStudentSems(student));
// return Promise.all(promises);
}
async function asyncCall() {
// const sem_requests = new Map();
console.log('***************** start *****************');
const students = await api.getStudents();
console.log('***************** got students *****************');
console.log(students)
console.log('***************** seeking sems and marks *****************');
const student_requests = students.map((student) => {
return api.getStudentSems(student);
});
const sems = await Promise.all(student_requests);
const full_sem = sems.flat();
const set_sem = [...new Set(full_sem)];
const studentsGrades = await Promise.all(set_sem.map(sem => api.getMarks(sem)));
debugger;
/*
console.log("lookup for " + student.name + "'s sems.");
const sems = api.getStudentSems(student)
console.log(`***************** got ${student.name}'s sems *****************`);
console.log(sems);
return sems;
});
const sems.forEach((sem) => {
console.log(`looking for mark for sem ${sem}`);
console.log("** current sems requests");
console.log(sem_requests)
if (sem_requests[sem] === 'undefined') {
console.log(`seeking ${sem} marks`)
sem_requests[sem] = api.getMarks(sem);
}
})
sems.forEach((sem) => {
console.log(`looking for mark for sem ${sem}`);
console.log("** current sems requests");
console.log(sem_requests)
if (sem_requests[sem] === 'undefined') {
console.log(`seeking ${sem} marks`)
sem_requests[sem] = api.getMarks(sem);
}
})
let result_sems = await Promise.all(sem_requests)
console.log('***************** sems ok *****************');
console.log(result_sems);
console.log('***************** stop *****************');
*/
// Expected output: "resolved"
}
function do_export() {
event.preventDefault();
// students = getData();
// console.log(students);
asyncCall();
}
// let loaded_sems = [];
//
// async function doit() {
// getStudents()
// .then(students => {
// return Promise.all(students.map(student => {
// api.getStudentSems(student)
// .then(sems_id => {
// sem_requests = []
// sems_id.forEach(sem_id => {
// if (typeof sem_requests[sem_id] === 'undefined') {
// sem_requests[sem_id](api.getMarks(sem_id));
// });
// });
// then(allMarks => {
// console.log(allMarks);
// });
// });
// }
/*
let dataSrc = "/ScoDoc/{{dept}}/api/formsemestre/{{formsemestre.id}}/etudiants/long";
let formsemestres = [];
fetch(dataSrc)
.then(r => { return r.json(); })
.then(json => {
let etudiants = json;
promises_etud_semestres = []
promises_semestres = []
etudiants.forEach((etudiant) => {
let url =`/ScoDoc/{{dept}}/api/etudiant/nip/${etudiant.code_nip}/formsemestres`;
console.log(url);
promises_etud_semestres.push(
fetch(url)
.then(r => { return r.json(); })
.then(semestre => {
formsemestre_id = semestre.formsemestre_id;
})
)
// etudiant.formsemestres_id = [];
fetch(url)
.then(r => {return r.json; })
.then(json => {
let forms = json;
console.log(etudiant.code_nip + " " + forms.formsemestre_id);
// forms.forEach((formsem) => {
// if (formsem.is_apc) {
// etudiant.formsemestres_id.push(formsem.formsemestre_id);
// if (formsem.formsemestre_id in_array()) {
// }
// }
// });
});
});
//let releve = document.querySelector("releve-but");
//releve.showData = json;
// Style custom à ajouter
// let style = document.createElement("style");
// style.textContent = `
// .module>div,
// .dateInscription,
// .numerosEtudiant,
// .dateNaissance{
// display: none;
// }`;
//releve.shadowRoot.appendChild(style);
})
.catch(error => { console.log(error)});
// .catch(error => {
// let div = document.createElement("div");
// div.innerText = "Une erreur s'est produite lors du transfert des données.";
// div.style.fontSize = "24px";
// div.style.color = "#d93030";
// let releve = document.querySelector("releve-but");
// releve.after(div);
// releve.remove();
// throw 'Fin du script - données invalides';
// });
*/

View File

@ -9,80 +9,24 @@
<H1>Export xlsx de fichier de jury</H1> <H1>Export xlsx de fichier de jury</H1>
<form onsubmit="do_export()"> <form>
Formsemestre: {{ formsemestre.id }} <p>Formsemestre: {{ formsemestre.id }}</p><br/>
API root: {{ api_root }} <p>API root: /ScoDoc/{{dept}}/api/</p><br/>
<button>Exporter</button> <button id="ok">Exporter</button>
</form> </form>
{% endblock %} {% endblock %}
{% block scripts %} {% block scripts %}
{{super()}} {{super()}}
<script>
let formsemestre_id = {{ formsemestre.id }};
let dept = "{{ dept }}";
</script>
<script src="{{scu.STATIC_DIR}}/js/export-jury-but.js"></script> <script src="{{scu.STATIC_DIR}}/js/export-jury-but.js"></script>
<script> <script>
let dataSrc = "/ScoDoc/{{dept}}/api/formsemestre/{{formsemestre.id}}/etudiants/long";
let formsemestres = [];
fetch(dataSrc)
.then(r => { return r.json(); })
.then(json => {
let etudiants = json;
promises_etud_semestres = []
promises_semestres = []
etudiants.forEach((etudiant) => {
let url =`/ScoDoc/{{dept}}/api/etudiant/nip/${etudiant.code_nip}/formsemestres`;
console.log(url);
promises_etud_semestres.push(
fetch(url)
.then(r => { return r.json(); })
.then(semestre => {
formsemestre_id = semestre.formsemestre_id;
})
)
// etudiant.formsemestres_id = [];
fetch(url)
.then(r => {return r.json; })
.then(json => {
let forms = json;
console.log(etudiant.code_nip + " " + forms.formsemestre_id);
// forms.forEach((formsem) => {
// if (formsem.is_apc) {
// etudiant.formsemestres_id.push(formsem.formsemestre_id);
// if (formsem.formsemestre_id in_array()) {
// }
// }
// });
});
});
//let releve = document.querySelector("releve-but");
//releve.showData = json;
// Style custom à ajouter
// let style = document.createElement("style");
// style.textContent = `
// .module>div,
// .dateInscription,
// .numerosEtudiant,
// .dateNaissance{
// display: none;
// }`;
//releve.shadowRoot.appendChild(style);
})
.catch(error => { console.log(error)});
// .catch(error => {
// let div = document.createElement("div");
// div.innerText = "Une erreur s'est produite lors du transfert des données.";
// div.style.fontSize = "24px";
// div.style.color = "#d93030";
// let releve = document.querySelector("releve-but");
// releve.after(div);
// releve.remove();
// throw 'Fin du script - données invalides';
// });
document.querySelector("html").style.scrollBehavior = "smooth"; document.querySelector("html").style.scrollBehavior = "smooth";
</script> </script>