Browse Source

the /timetable request needs a week now

pull/18/head
Jannik 3 years ago
parent
commit
ecfc1356ee
  1. 43
      src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt
  2. 4
      src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt

43
src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt

@ -38,6 +38,7 @@ import java.net.HttpURLConnection
import java.net.URL
import java.time.LocalDateTime
import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.scheduleAtFixedRate
@RestController
@ -56,7 +57,7 @@ class APIController {
private var coursesLinkList = ArrayList<Course>()
private var coursesLastUpdate: Long = 0
private var timetableList = ArrayList<TimetableCourse>() // this list contains all timetables
private var timetableList = ArrayList<TimetableCourseWeek>() // this list contains all timetables
private var mensaCurrentWeek = MensaWeek()
private var mensaNextWeek = MensaWeek()
@ -95,10 +96,13 @@ class APIController {
}
@RequestMapping("/timetable")
fun timetable(@RequestParam(value = "courseName", defaultValue = "AI4") courseName: String): TimetableCourse {
fun timetable(
@RequestParam(value = "courseName", defaultValue = "AI4") courseName: String,
@RequestParam(value = "week", defaultValue = "0") week: Int
): TimetableCourseWeek {
logger.info("timetable request at ${LocalDateTime.now()}!")
checkTimetableCourse(courseName) // check if we need to update and perform the update if so
return timetableList.stream().filter { x -> x.meta.courseName == courseName }.findAny().orElse(null)
checkTimetableCourse(courseName, week) // check if we need to update and perform the update if so
return timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.week == week }.findAny().orElse(null)
}
@RequestMapping("/status")
@ -163,10 +167,9 @@ class APIController {
*/
private fun asyncUpdateTimetables() = GlobalScope.launch {
timetableList.forEach { timetableCourse ->
val updateURL = timetableCourse.meta.courseLink
timetableCourse.currentWeek = TimetableParser().getTimeTable(updateURL)
timetableCourse.nextWeek = TimetableParser().getTimeTable(updateURL.replace("week=0","week=1"))
timetableCourse.meta.time = System.currentTimeMillis() / 1000
val updateURL = timetableCourse.meta.link
timetableCourse.timetable = TimetableParser().getTimeTable(updateURL)
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
}
logger.info("updated ${timetableList.size} timetables successful!")
}
@ -176,32 +179,26 @@ class APIController {
* during the update process the old data will be returned for a API request
* update if the last update was 6 hours ago
*/
private fun checkTimetableCourse(courseName: String) = runBlocking {
private fun checkTimetableCourse(courseName: String, week: Int) = runBlocking {
val currentTime = System.currentTimeMillis() / 1000
var currentWeek = TimetableWeek()
var nextWeek = TimetableWeek()
var timetable = TimetableWeek()
// check if the timetable already exists and is up to date
val result = timetableList.stream().filter { x -> x.meta.courseName == courseName }.findAny().orElse(null)
val result = timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.week == week }.findAny().orElse(null)
when (result) {
// there is no such course yet, create one
null -> {
val courseLink = coursesLinkList.stream().filter { x -> x.courseName == courseName }.findFirst().orElse(null).courseLink
val timetableMeta = TimetableMeta(courseName, courseLink, currentTime)
val timetableMeta = TimetableCourseMeta(currentTime, courseName, week, courseLink.replace("week=0","week=$week"))
val jobCurrent = GlobalScope.async {
currentWeek = TimetableParser().getTimeTable(courseLink)
val jobTimetable = GlobalScope.async {
timetable = TimetableParser().getTimeTable(timetableMeta.link)
}
val jobNext = GlobalScope.async {
nextWeek = TimetableParser().getTimeTable(courseLink.replace("week=0","week=1"))
}
jobCurrent.await()
jobNext.await()
jobTimetable.await()
timetableList.add(TimetableCourse(timetableMeta, currentWeek, nextWeek))
logger.info("added new timetable for $courseName")
timetableList.add(TimetableCourseWeek(timetableMeta, timetable))
logger.info("added new timetable for $courseName, week $week")
}
}
}

4
src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt

@ -56,9 +56,9 @@ data class TimetableDay(val timeslots: Array<ArrayList<Lesson>> = Array(6) { Arr
data class TimetableWeek(val days: Array<TimetableDay> = Array(6) { TimetableDay() })
data class TimetableMeta(val courseName: String, val courseLink: String, var time: Long)
data class TimetableCourseMeta(var updateTime: Long, val courseName: String, val week: Int, val link: String)
data class TimetableCourse(val meta: TimetableMeta, var currentWeek: TimetableWeek, var nextWeek: TimetableWeek)
data class TimetableCourseWeek(val meta: TimetableCourseMeta, var timetable: TimetableWeek)
// data classes for the status part

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close