use HashMap insted of ArrayList to store the timetables
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Jannik 2020-06-06 23:07:23 +02:00
parent fe72c02562
commit f9029bf1c3
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
4 changed files with 29 additions and 40 deletions

View File

@ -29,9 +29,9 @@ import kotlin.collections.HashMap
// data classes for the course part
data class Course(val courseName: String, val courseLink: String)
data class CoursesMeta(val updateTime: Long, val totalCourses: Int)
data class CoursesMeta(val updateTime: Long = 0, val totalCourses: Int = 0)
data class CoursesList(val meta: CoursesMeta, val courses: ArrayList<Course>)
data class CoursesList(val meta: CoursesMeta = CoursesMeta(), val courses: HashMap<String, Course> = HashMap())
// data classes for the Mensa part
data class Meal(val day: String, val heading: String, val parts: ArrayList<String>, val additives: String)

View File

@ -23,10 +23,7 @@
package org.mosad.thecitadelofricks.controller
import com.google.gson.Gson
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import org.mosad.thecitadelofricks.*
import org.mosad.thecitadelofricks.hsoparser.CourseListParser
import org.mosad.thecitadelofricks.hsoparser.MensaParser
@ -39,6 +36,7 @@ import java.io.FileWriter
import java.util.*
import java.util.concurrent.Executors
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.HashSet
import kotlin.concurrent.scheduleAtFixedRate
@ -52,9 +50,9 @@ class CacheController {
companion object{
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
var courseList = CoursesList(CoursesMeta(0, 0), ArrayList())
var courseList = CoursesList()
var mensaMenu = MensaMenu(MensaMeta(0,""), MensaWeek(), MensaWeek())
var timetableList = ArrayList<TimetableCourseWeek>() // this list contains all timetables
var timetableList = HashMap<String, TimetableCourseWeek>() // this list contains all timetables
/**
* get a timetable, since they may not be cached, we need to make sure it's cached, otherwise download
@ -63,30 +61,22 @@ class CacheController {
* @return timetable of the course (Type: [TimetableCourseWeek])
*/
fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
return@runBlocking timetableList.getOrPut("$courseName-$weekIndex") {
val timetableLink = courseList.courses[courseName]
?.courseLink
?.replace("week=0", "week=$weekIndex") ?: ""
val currentTime = System.currentTimeMillis() / 1000
var timetable = TimetableWeek()
var weekNumberYear = 0
// check if the timetable already exists and is up to date
when (timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)) {
// there is no such course yet, create one
null -> {
val courseLink = courseList.courses.stream().filter { x -> x.courseName == courseName }.findFirst().orElse(null).courseLink
val timetableLink = courseLink.replace("week=0","week=$weekIndex")
val timetableJobs = listOf(
async { timetable = TimetableParser().getTimeTable(timetableLink) },
async { weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink) }
)
val jobTimetable = async {
timetable = TimetableParser().getTimeTable(timetableLink)
weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink)
timetableJobs.awaitAll()
TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink), timetable)
}
jobTimetable.await()
timetableList.add(TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink), timetable))
logger.info("added new timetable for $courseName, week $weekIndex")
}
}
return@runBlocking timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)
}
/**
@ -170,10 +160,10 @@ class CacheController {
try {
timetableList.forEach { timetableCourse ->
executor.execute {
timetableCourse.timetable = TimetableParser().getTimeTable(timetableCourse.meta.link)
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
timetableCourse.value.timetable = TimetableParser().getTimeTable(timetableCourse.value.meta.link)
timetableCourse.value.meta.updateTime = System.currentTimeMillis() / 1000
saveTimetableToCache(timetableCourse) // save the updated timetable to the cache directory
saveTimetableToCache(timetableCourse.value) // save the updated timetable to the cache directory
}
}

View File

@ -134,7 +134,8 @@ class StartupController {
try {
val timetableObject = JsonParser.parseString(bufferedReader.readLine()).asJsonObject
CacheController.timetableList.add(Gson().fromJson(timetableObject, TimetableCourseWeek().javaClass))
val timetable = Gson().fromJson(timetableObject, TimetableCourseWeek().javaClass)
CacheController.timetableList.put("${timetable.meta.courseName}-${timetable.meta.weekIndex}", timetable)
} catch (ex: Exception) {
logger.error("error while reading cache", ex)
} finally {

View File

@ -36,18 +36,16 @@ class CourseListParser {
* @param courseListURL the url to the course list page
* @return a ArrayList<Course> with all courses or null if the request was not successful
*/
fun getCourseLinks(courseListURL: String): ArrayList<Course>? {
val courseLinkList = ArrayList<Course>()
fun getCourseLinks(courseListURL: String): HashMap<String, Course>? {
val courseLinkList = HashMap<String, Course>()
try {
val courseHTML = Jsoup.connect(courseListURL).get()
courseHTML.select("ul.index-group").select("li.Class").select("a[href]").forEachIndexed { _, element ->
courseLinkList.add(
Course(
courseLinkList[element.text()] = Course(
element.text(),
element.attr("href").replace("http", "https")
)
)
}
} catch (ex: SocketTimeoutException) {
logger.warn("timeout from hs-offenburg.de, updating on next attempt!")