()
| 48 | } |
| 49 | |
| 50 | loadData () { |
| 51 | var fetchBatch = (baseUrl, results, beforeId) => { |
| 52 | let url = `${baseUrl}?options[limit]=${this.batchSize}` |
| 53 | if (beforeId) { url += `&options[beforeId]=${beforeId}` } |
| 54 | return new Promise(resolve => setTimeout(resolve.bind(null, Promise.resolve($.get(url))), 200)) |
| 55 | .then(batchResults => { |
| 56 | if (this.destroyed) { return Promise.resolve([]) } |
| 57 | results = results.concat(batchResults) |
| 58 | if (batchResults.length < this.batchSize) { |
| 59 | this.updateLoadingState(`Received ${results.length} from ${baseUrl} TOTAL`) |
| 60 | return Promise.resolve(results) |
| 61 | } else { |
| 62 | this.updateLoadingState(`Received ${results.length} from ${baseUrl} so far`) |
| 63 | return fetchBatch(baseUrl, results, batchResults[batchResults.length - 1]._id) |
| 64 | } |
| 65 | }).catch(error => { |
| 66 | console.log(new Date().toISOString(), `ERROR! Trying ${baseUrl} ${beforeId} again`, error.status, error.statusText) |
| 67 | return fetchBatch(baseUrl, results, beforeId) |
| 68 | }) |
| 69 | } |
| 70 | |
| 71 | return Promise.all([ |
| 72 | fetchBatch('/db/classroom/-/users', []), |
| 73 | fetchBatch('/db/course_instance/-/non-hoc', []), |
| 74 | fetchBatch('/db/user/-/students', []), |
| 75 | fetchBatch('/db/user/-/teachers', []), |
| 76 | fetchBatch('/db/trial.request/-/users', []) |
| 77 | ]) |
| 78 | .then((...args) => { |
| 79 | let country, district, school, state, studentID, teacherID, val |
| 80 | const [classrooms, courseInstances, students, teachers, trialRequests] = Array.from(args[0]) |
| 81 | const teacherMap = {} // Used to make sure teachers and students only counted once |
| 82 | const studentMap = {} // Used to make sure teachers and students only counted once |
| 83 | const studentNonHocMap = {} // Used to exclude HoC users |
| 84 | const teacherStudentMap = {} // Used to link students to their teacher locations |
| 85 | let unknownSchoolCount = 1 // Used to separate unique but unknown schools |
| 86 | |
| 87 | this.updateLoadingState(`Processing ${courseInstances.length} course instances...`) |
| 88 | for (const courseInstance of Array.from(courseInstances)) { |
| 89 | studentNonHocMap[courseInstance.ownerID] = true |
| 90 | for (studentID of Array.from(courseInstance.members != null ? courseInstance.members : [])) { studentNonHocMap[studentID] = true } |
| 91 | } |
| 92 | |
| 93 | console.log(new Date().toISOString(), `Processing ${teachers.length} teachers...`) |
| 94 | this.state = `Processing ${courseInstances.length} course instances...` |
| 95 | for (const teacher of Array.from(teachers)) { |
| 96 | teacherMap[teacher._id] = teacher.geo != null ? teacher.geo : {} |
| 97 | } |
| 98 | |
| 99 | this.updateLoadingState(`Processing ${classrooms.length} classrooms...`) |
| 100 | for (const classroom of Array.from(classrooms)) { |
| 101 | teacherID = classroom.ownerID |
| 102 | if (teacherMap[teacherID] == null) { teacherMap[teacherID] = {} } |
| 103 | if (teacherStudentMap[teacherID] == null) { teacherStudentMap[teacherID] = {} } |
| 104 | for (studentID of Array.from(classroom.members)) { |
| 105 | if (teacherMap[studentID]) { continue } |
| 106 | if (!studentNonHocMap[studentID]) { continue } |
| 107 | studentMap[studentID] = {} |
no test coverage detected