Browse Source

use HashMap insted of ArrayList to store the timetables

pull/18/head
Jannik 2 years ago
parent
commit
f9029bf1c3
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 4
      src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt
  2. 54
      src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt
  3. 3
      src/main/kotlin/org/mosad/thecitadelofricks/controller/StartupController.kt
  4. 12
      src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/CourseListParser.kt

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

@ -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)

54
src/main/kotlin/org/mosad/thecitadelofricks/controller/CacheController.kt

@ -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 {
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 jobTimetable = async {
timetable = TimetableParser().getTimeTable(timetableLink)
weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink)
}
jobTimetable.await()
timetableList.add(TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink), timetable))
logger.info("added new timetable for $courseName, week $weekIndex")
}
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
val timetableJobs = listOf(
async { timetable = TimetableParser().getTimeTable(timetableLink) },
async { weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink) }
)
timetableJobs.awaitAll()
TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink), timetable)
}
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
}
}

3
src/main/kotlin/org/mosad/thecitadelofricks/controller/StartupController.kt

@ -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 {

12
src/main/kotlin/org/mosad/thecitadelofricks/hsoparser/CourseListParser.kt

@ -36,17 +36,15 @@ 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(
element.text(),
element.attr("href").replace("http", "https")
)
courseLinkList[element.text()] = Course(
element.text(),
element.attr("href").replace("http", "https")
)
}
} catch (ex: SocketTimeoutException) {

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