use HashMap insted of ArrayList to store the timetables
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
fe72c02562
commit
f9029bf1c3
|
@ -29,9 +29,9 @@ import kotlin.collections.HashMap
|
||||||
// data classes for the course part
|
// data classes for the course part
|
||||||
data class Course(val courseName: String, val courseLink: String)
|
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 classes for the Mensa part
|
||||||
data class Meal(val day: String, val heading: String, val parts: ArrayList<String>, val additives: String)
|
data class Meal(val day: String, val heading: String, val parts: ArrayList<String>, val additives: String)
|
||||||
|
|
|
@ -23,10 +23,7 @@
|
||||||
package org.mosad.thecitadelofricks.controller
|
package org.mosad.thecitadelofricks.controller
|
||||||
|
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.async
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import org.mosad.thecitadelofricks.*
|
import org.mosad.thecitadelofricks.*
|
||||||
import org.mosad.thecitadelofricks.hsoparser.CourseListParser
|
import org.mosad.thecitadelofricks.hsoparser.CourseListParser
|
||||||
import org.mosad.thecitadelofricks.hsoparser.MensaParser
|
import org.mosad.thecitadelofricks.hsoparser.MensaParser
|
||||||
|
@ -39,6 +36,7 @@ import java.io.FileWriter
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
import kotlin.collections.ArrayList
|
import kotlin.collections.ArrayList
|
||||||
|
import kotlin.collections.HashMap
|
||||||
import kotlin.collections.HashSet
|
import kotlin.collections.HashSet
|
||||||
import kotlin.concurrent.scheduleAtFixedRate
|
import kotlin.concurrent.scheduleAtFixedRate
|
||||||
|
|
||||||
|
@ -52,9 +50,9 @@ class CacheController {
|
||||||
companion object{
|
companion object{
|
||||||
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
|
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 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
|
* 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])
|
* @return timetable of the course (Type: [TimetableCourseWeek])
|
||||||
*/
|
*/
|
||||||
fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
|
fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
|
||||||
val currentTime = System.currentTimeMillis() / 1000
|
return@runBlocking timetableList.getOrPut("$courseName-$weekIndex") {
|
||||||
var timetable = TimetableWeek()
|
val timetableLink = courseList.courses[courseName]
|
||||||
var weekNumberYear = 0
|
?.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
|
val timetableJobs = listOf(
|
||||||
when (timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)) {
|
async { timetable = TimetableParser().getTimeTable(timetableLink) },
|
||||||
// there is no such course yet, create one
|
async { weekNumberYear = TimetableParser().getWeekNumberYear(timetableLink) }
|
||||||
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 {
|
timetableJobs.awaitAll()
|
||||||
timetable = TimetableParser().getTimeTable(timetableLink)
|
TimetableCourseWeek(TimetableCourseMeta(currentTime, courseName, weekIndex, weekNumberYear, timetableLink), timetable)
|
||||||
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.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -170,10 +160,10 @@ class CacheController {
|
||||||
try {
|
try {
|
||||||
timetableList.forEach { timetableCourse ->
|
timetableList.forEach { timetableCourse ->
|
||||||
executor.execute {
|
executor.execute {
|
||||||
timetableCourse.timetable = TimetableParser().getTimeTable(timetableCourse.meta.link)
|
timetableCourse.value.timetable = TimetableParser().getTimeTable(timetableCourse.value.meta.link)
|
||||||
timetableCourse.meta.updateTime = System.currentTimeMillis() / 1000
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,7 +134,8 @@ class StartupController {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val timetableObject = JsonParser.parseString(bufferedReader.readLine()).asJsonObject
|
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) {
|
} catch (ex: Exception) {
|
||||||
logger.error("error while reading cache", ex)
|
logger.error("error while reading cache", ex)
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -36,17 +36,15 @@ class CourseListParser {
|
||||||
* @param courseListURL the url to the course list page
|
* @param courseListURL the url to the course list page
|
||||||
* @return a ArrayList<Course> with all courses or null if the request was not successful
|
* @return a ArrayList<Course> with all courses or null if the request was not successful
|
||||||
*/
|
*/
|
||||||
fun getCourseLinks(courseListURL: String): ArrayList<Course>? {
|
fun getCourseLinks(courseListURL: String): HashMap<String, Course>? {
|
||||||
val courseLinkList = ArrayList<Course>()
|
val courseLinkList = HashMap<String, Course>()
|
||||||
try {
|
try {
|
||||||
val courseHTML = Jsoup.connect(courseListURL).get()
|
val courseHTML = Jsoup.connect(courseListURL).get()
|
||||||
|
|
||||||
courseHTML.select("ul.index-group").select("li.Class").select("a[href]").forEachIndexed { _, element ->
|
courseHTML.select("ul.index-group").select("li.Class").select("a[href]").forEachIndexed { _, element ->
|
||||||
courseLinkList.add(
|
courseLinkList[element.text()] = Course(
|
||||||
Course(
|
element.text(),
|
||||||
element.text(),
|
element.attr("href").replace("http", "https")
|
||||||
element.attr("href").replace("http", "https")
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch (ex: SocketTimeoutException) {
|
} catch (ex: SocketTimeoutException) {
|
||||||
|
|
Loading…
Reference in New Issue