clean up APICOntroller & new CacheController
* added a CacheController to hold all cache variables and clean up the APIController * more consistent naming of variables * update mensaMenu and courseList only if the request was successful
This commit is contained in:
@ -22,13 +22,8 @@
|
||||
|
||||
package org.mosad.thecitadelofricks
|
||||
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.mosad.thecitadelofricks.hsoparser.CourseListParser
|
||||
import org.mosad.thecitadelofricks.hsoparser.MensaParser
|
||||
import org.mosad.thecitadelofricks.hsoparser.TimetableParser
|
||||
import org.mosad.thecitadelofricks.CacheController.Companion.courseList
|
||||
import org.mosad.thecitadelofricks.CacheController.Companion.mensaMenu
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
@ -40,74 +35,37 @@ import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
import kotlin.collections.HashSet
|
||||
import kotlin.concurrent.scheduleAtFixedRate
|
||||
|
||||
@RestController
|
||||
class APIController {
|
||||
|
||||
// TODO clean up and move stuff to a CacheController
|
||||
// Controller stuff
|
||||
var logger: Logger = LoggerFactory.getLogger(APIController::class.java)
|
||||
private var requestCount = 0
|
||||
private val logger: Logger = LoggerFactory.getLogger(APIController::class.java)
|
||||
private val cache = CacheController()
|
||||
|
||||
private val softwareVersion = "1.1.2"
|
||||
private val apiVersion = "1.1.1"
|
||||
private val startTime = System.currentTimeMillis() / 1000
|
||||
private val softwareVersion = "1.1.1"
|
||||
private val apiVersion = "1.1.0"
|
||||
|
||||
// hso parser links (hardcoded)
|
||||
private val mensaLink = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
|
||||
private val mensaName = "Offenburg"
|
||||
|
||||
// cache objects
|
||||
private var coursesLinkList = ArrayList<Course>()
|
||||
private var coursesLastUpdate: Long = 0
|
||||
|
||||
private var timetableList = ArrayList<TimetableCourseWeek>() // this list contains all timetables
|
||||
|
||||
private var mensaCurrentWeek = MensaWeek()
|
||||
private var mensaNextWeek = MensaWeek()
|
||||
private var mensaLastUpdate: Long = 0
|
||||
|
||||
init {
|
||||
initUpdates() // without this 5-10 seconds after startup the response will be empty
|
||||
|
||||
val currentTime = System.currentTimeMillis()
|
||||
val delay24h = (86400000 - ((currentTime + 3600000) % 86400000)) + 60000
|
||||
val delay3h = (10800000 - ((currentTime + 3600000) % 10800000)) + 60000
|
||||
|
||||
// update courses every 24 hours (time in ms)
|
||||
Timer().scheduleAtFixedRate(delay24h, 86400000) {
|
||||
asyncUpdateCourses()
|
||||
}
|
||||
|
||||
// update courses every 3 hours (time in ms)
|
||||
Timer().scheduleAtFixedRate(delay3h, 10800000) {
|
||||
asyncUpdateMensa()
|
||||
}
|
||||
|
||||
// update all already existing timetables every 3 hours (time in ms)
|
||||
Timer().scheduleAtFixedRate(delay3h, 10800000) {
|
||||
asyncUpdateTimetables()
|
||||
}
|
||||
}
|
||||
private var requestCount = 0
|
||||
|
||||
@Deprecated("courses is replaced by courseList", replaceWith = ReplaceWith("courseList()"))
|
||||
@RequestMapping("/courses")
|
||||
fun courses(): CoursesList {
|
||||
fun courses(): CourseList {
|
||||
return courseList()
|
||||
}
|
||||
|
||||
@RequestMapping("/courseList")
|
||||
fun courseList(): CoursesList {
|
||||
fun courseList(): CourseList {
|
||||
logger.info("courseList request at ${LocalDateTime.now()}!")
|
||||
requestCount++
|
||||
return CoursesList(CoursesMeta(coursesLastUpdate, coursesLinkList.size), coursesLinkList)
|
||||
return courseList
|
||||
}
|
||||
|
||||
@RequestMapping("/mensamenu")
|
||||
fun mensamenu(): Mensa {
|
||||
fun mensamenu(): MensaMenu {
|
||||
logger.info("mensamenu request at ${LocalDateTime.now()}!")
|
||||
requestCount++
|
||||
return Mensa(MensaMeta(mensaLastUpdate, mensaName), mensaCurrentWeek, mensaNextWeek)
|
||||
return mensaMenu
|
||||
}
|
||||
|
||||
@RequestMapping("/timetable")
|
||||
@ -117,7 +75,7 @@ class APIController {
|
||||
): TimetableCourseWeek {
|
||||
logger.info("timetable request at ${LocalDateTime.now()}!")
|
||||
requestCount++
|
||||
return getTimetable(courseName, week)
|
||||
return cache.getTimetable(courseName, week)
|
||||
}
|
||||
|
||||
@RequestMapping("/lessonSubjectList")
|
||||
@ -130,7 +88,7 @@ class APIController {
|
||||
requestCount++
|
||||
|
||||
// get every lesson subject for the given week
|
||||
val flatMap = getTimetable(courseName, week).timetable.days.flatMap { it.timeslots.asIterable() }
|
||||
val flatMap = cache.getTimetable(courseName, week).timetable.days.flatMap { it.timeslots.asIterable() }
|
||||
flatMap.forEach {
|
||||
it.stream().filter { x -> x.lessonSubject.isNotEmpty() }.findAny().ifPresent { x -> lessonSubjectList.add(x.lessonSubject) }
|
||||
}
|
||||
@ -147,7 +105,7 @@ class APIController {
|
||||
val lessonList = ArrayList<Lesson>()
|
||||
|
||||
// get all lessons from the weeks timetable
|
||||
val flatMap = getTimetable(courseName, week).timetable.days.flatMap { it.timeslots.asIterable() }
|
||||
val flatMap = cache.getTimetable(courseName, week).timetable.days.flatMap { it.timeslots.asIterable() }
|
||||
flatMap.forEach {
|
||||
it.forEach { lesson ->
|
||||
if(lesson.lessonSubject.contains(lessonSubject)) {
|
||||
@ -194,105 +152,11 @@ class APIController {
|
||||
apiVersion,
|
||||
softwareVersion,
|
||||
requestCount,
|
||||
Date(coursesLastUpdate * 1000),
|
||||
Date(mensaLastUpdate * 1000),
|
||||
Date(courseList.meta.updateTime * 1000),
|
||||
Date(mensaMenu.meta.updateTime * 1000),
|
||||
hsoCode,
|
||||
swfrCode
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* this function updates the courses list
|
||||
* during the update process the old data will be returned for a API request
|
||||
*/
|
||||
private fun asyncUpdateCourses() = GlobalScope.launch {
|
||||
coursesLinkList = CourseListParser().getCourseLinks()
|
||||
coursesLastUpdate = System.currentTimeMillis() / 1000
|
||||
|
||||
logger.info("updated courses successful at ${Date(coursesLastUpdate * 1000)}")
|
||||
}
|
||||
|
||||
/**
|
||||
* this function updates the mensa menu list
|
||||
* during the update process the old data will be returned for a API request
|
||||
*/
|
||||
private fun asyncUpdateMensa() = GlobalScope.launch {
|
||||
mensaCurrentWeek = MensaParser().getMensaMenu(mensaLink)
|
||||
mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(mensaLink))
|
||||
mensaLastUpdate = System.currentTimeMillis() / 1000
|
||||
|
||||
logger.info("updated mensamenu successful at ${Date(mensaLastUpdate * 1000)}")
|
||||
}
|
||||
|
||||
/**
|
||||
* this function updates all existing timetables
|
||||
* during the update process the old data will be returned for a API request
|
||||
*/
|
||||
private fun asyncUpdateTimetables() = GlobalScope.launch {
|
||||
timetableList.forEach { timetableCourse ->
|
||||
val updateURL = timetableCourse.meta.link
|
||||
timetableCourse.timetable = TimetableParser().getTimeTable(updateURL)
|
||||
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
|
||||
}
|
||||
logger.info("updated ${timetableList.size} timetables successful!")
|
||||
}
|
||||
|
||||
/**
|
||||
* this function checks if we need to update the timetable for a given course and if so does it
|
||||
* 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, week: Int) = runBlocking {
|
||||
val currentTime = System.currentTimeMillis() / 1000
|
||||
var timetable = TimetableWeek()
|
||||
// check if the timetable already exists and is up to date
|
||||
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 = TimetableCourseMeta(currentTime, courseName, week, courseLink.replace("week=0","week=$week"))
|
||||
|
||||
val jobTimetable = GlobalScope.async {
|
||||
timetable = TimetableParser().getTimeTable(timetableMeta.link)
|
||||
}
|
||||
|
||||
jobTimetable.await()
|
||||
|
||||
timetableList.add(TimetableCourseWeek(timetableMeta, timetable))
|
||||
logger.info("added new timetable for $courseName, week $week")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getTimetable(courseName: String, week: Int): TimetableCourseWeek {
|
||||
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)
|
||||
}
|
||||
|
||||
private fun initUpdates() = runBlocking {
|
||||
// get all courses on startup
|
||||
val jobCourseUpdate = GlobalScope.async{
|
||||
coursesLinkList = CourseListParser().getCourseLinks()
|
||||
coursesLastUpdate = System.currentTimeMillis() / 1000
|
||||
}
|
||||
|
||||
// get the current and next weeks mensa menus
|
||||
val jobCurrentMensa = GlobalScope.async{
|
||||
mensaCurrentWeek = MensaParser().getMensaMenu(mensaLink)
|
||||
}
|
||||
|
||||
val jobNextMensa = GlobalScope.async{
|
||||
mensaCurrentWeek = MensaParser().getMensaMenu(mensaLink)
|
||||
mensaLastUpdate = System.currentTimeMillis() / 1000
|
||||
}
|
||||
|
||||
jobCourseUpdate.await()
|
||||
jobCurrentMensa.await()
|
||||
jobNextMensa.await()
|
||||
|
||||
logger.info("init updates successful")
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user