added more status metrics, api version 1.1.3 (/lesson -> /lessons)
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
* updated spring boot 2.1.8 -> 2.1.9 * closes #7
This commit is contained in:
parent
3be98f6129
commit
6a687eb297
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
buildscript {
|
buildscript {
|
||||||
ext.kotlin_version = '1.3.50'
|
ext.kotlin_version = '1.3.50'
|
||||||
ext.spring_boot_version = '2.1.8.RELEASE'
|
ext.spring_boot_version = '2.1.9.RELEASE'
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
|
|
|
@ -34,7 +34,6 @@ import java.net.HttpURLConnection
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.stream.Collectors
|
|
||||||
import kotlin.collections.ArrayList
|
import kotlin.collections.ArrayList
|
||||||
import kotlin.collections.HashSet
|
import kotlin.collections.HashSet
|
||||||
|
|
||||||
|
@ -44,11 +43,13 @@ class APIController {
|
||||||
private val logger: Logger = LoggerFactory.getLogger(APIController::class.java)
|
private val logger: Logger = LoggerFactory.getLogger(APIController::class.java)
|
||||||
private val cache = CacheController()
|
private val cache = CacheController()
|
||||||
|
|
||||||
private val apiVersion = "1.1.2"
|
private val apiVersion = "1.1.3"
|
||||||
private val softwareVersion = "1.1.4"
|
private val softwareVersion = "1.1.5"
|
||||||
private val startTime = System.currentTimeMillis() / 1000
|
private val startTime = System.currentTimeMillis() / 1000
|
||||||
|
|
||||||
private var requestCount = 0
|
private var totalRequests = 0
|
||||||
|
private var mensaMenuRequests = 0
|
||||||
|
private var timetableRequests = ArrayList<TimetableCounter>()
|
||||||
|
|
||||||
@Deprecated("courses is replaced by courseList", replaceWith = ReplaceWith("courseList()"))
|
@Deprecated("courses is replaced by courseList", replaceWith = ReplaceWith("courseList()"))
|
||||||
@RequestMapping("/courses")
|
@RequestMapping("/courses")
|
||||||
|
@ -59,14 +60,15 @@ class APIController {
|
||||||
@RequestMapping("/courseList")
|
@RequestMapping("/courseList")
|
||||||
fun courseList(): CourseList {
|
fun courseList(): CourseList {
|
||||||
logger.info("courseList request at ${LocalDateTime.now()}!")
|
logger.info("courseList request at ${LocalDateTime.now()}!")
|
||||||
requestCount++
|
totalRequests++
|
||||||
return courseList
|
return courseList
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/mensamenu")
|
@RequestMapping("/mensamenu")
|
||||||
fun mensamenu(): MensaMenu {
|
fun mensamenu(): MensaMenu {
|
||||||
logger.info("mensamenu request at ${LocalDateTime.now()}!")
|
logger.info("mensamenu request at ${LocalDateTime.now()}!")
|
||||||
requestCount++
|
mensaMenuRequests++
|
||||||
|
totalRequests++
|
||||||
return mensaMenu
|
return mensaMenu
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +78,8 @@ class APIController {
|
||||||
@RequestParam(value = "week", defaultValue = "0") week: Int
|
@RequestParam(value = "week", defaultValue = "0") week: Int
|
||||||
): TimetableCourseWeek {
|
): TimetableCourseWeek {
|
||||||
logger.info("timetable request at ${LocalDateTime.now()}!")
|
logger.info("timetable request at ${LocalDateTime.now()}!")
|
||||||
requestCount++
|
updateTimetableRequests(courseName)
|
||||||
|
totalRequests++
|
||||||
return cache.getTimetable(courseName, week)
|
return cache.getTimetable(courseName, week)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,18 +89,18 @@ class APIController {
|
||||||
@RequestParam(value = "week", defaultValue = "0") week: Int
|
@RequestParam(value = "week", defaultValue = "0") week: Int
|
||||||
): HashSet<String> {
|
): HashSet<String> {
|
||||||
logger.info("lessonSubjectList request at ${LocalDateTime.now()}!")
|
logger.info("lessonSubjectList request at ${LocalDateTime.now()}!")
|
||||||
requestCount++
|
totalRequests++
|
||||||
return getLessonSubjectList(courseName, week)
|
return getLessonSubjectList(courseName, week)
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/lesson")
|
@RequestMapping("/lessons")
|
||||||
fun lesson(
|
fun lesson(
|
||||||
@RequestParam(value = "courseName", defaultValue = "AI4") courseName: String,
|
@RequestParam(value = "courseName", defaultValue = "AI4") courseName: String,
|
||||||
@RequestParam(value = "lessonSubject", defaultValue = "Mathematik 4") lessonSubject: String,
|
@RequestParam(value = "lessonSubject", defaultValue = "Mathematik 4") lessonSubject: String,
|
||||||
@RequestParam(value = "week", defaultValue = "0") week: Int
|
@RequestParam(value = "week", defaultValue = "0") week: Int
|
||||||
): ArrayList<Lesson> {
|
): ArrayList<Lesson> {
|
||||||
logger.info("lesson request at ${LocalDateTime.now()}!")
|
logger.info("lesson request at ${LocalDateTime.now()}!")
|
||||||
requestCount++
|
totalRequests++
|
||||||
return getLesson(courseName, lessonSubject, week)
|
return getLesson(courseName, lessonSubject, week)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,11 +111,17 @@ class APIController {
|
||||||
|
|
||||||
// non direct api functions
|
// non direct api functions
|
||||||
|
|
||||||
private fun getLessonSubjectList(courseName: String, week: Int): HashSet<String> {
|
/**
|
||||||
|
* get every explicit lesson in a week
|
||||||
|
* @param courseName the name of the course to be requested
|
||||||
|
* @param weekIndex request week number (current week = 0)
|
||||||
|
* @return a HashSet of explicit lessons for one week
|
||||||
|
*/
|
||||||
|
private fun getLessonSubjectList(courseName: String, weekIndex: Int): HashSet<String> {
|
||||||
val lessonSubjectList = ArrayList<String>()
|
val lessonSubjectList = ArrayList<String>()
|
||||||
|
|
||||||
// get every lesson subject for the given week
|
// get every lesson subject for the given week
|
||||||
val flatMap = cache.getTimetable(courseName, week).timetable.days.flatMap { it.timeslots.asIterable() }
|
val flatMap = cache.getTimetable(courseName, weekIndex).timetable.days.flatMap { it.timeslots.asIterable() }
|
||||||
flatMap.forEach {
|
flatMap.forEach {
|
||||||
it.stream().filter { x -> x.lessonSubject.isNotEmpty() }.findAny().ifPresent { x -> lessonSubjectList.add(x.lessonSubject) }
|
it.stream().filter { x -> x.lessonSubject.isNotEmpty() }.findAny().ifPresent { x -> lessonSubjectList.add(x.lessonSubject) }
|
||||||
}
|
}
|
||||||
|
@ -120,11 +129,18 @@ class APIController {
|
||||||
return HashSet(lessonSubjectList)
|
return HashSet(lessonSubjectList)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getLesson(courseName: String, lessonSubject: String, week: Int): ArrayList<Lesson> {
|
/**
|
||||||
|
* get every explicit lesson in a week
|
||||||
|
* @param courseName the name of the course to be requested
|
||||||
|
* @param lessonSubject the lesson subject to be requested
|
||||||
|
* @param weekIndex request week number (current week = 0)
|
||||||
|
* @return a ArrayList of every lesson with lessonSubject for one week
|
||||||
|
*/
|
||||||
|
private fun getLesson(courseName: String, lessonSubject: String, weekIndex: Int): ArrayList<Lesson> {
|
||||||
val lessonList = ArrayList<Lesson>()
|
val lessonList = ArrayList<Lesson>()
|
||||||
|
|
||||||
// get all lessons from the weeks timetable
|
// get all lessons from the weeks timetable
|
||||||
val flatMap = cache.getTimetable(courseName, week).timetable.days.flatMap { it.timeslots.asIterable() }
|
val flatMap = cache.getTimetable(courseName, weekIndex).timetable.days.flatMap { it.timeslots.asIterable() }
|
||||||
flatMap.forEach {
|
flatMap.forEach {
|
||||||
it.forEach { lesson ->
|
it.forEach { lesson ->
|
||||||
if(lesson.lessonSubject.contains(lessonSubject)) {
|
if(lesson.lessonSubject.contains(lessonSubject)) {
|
||||||
|
@ -138,15 +154,23 @@ class APIController {
|
||||||
return lessonList
|
return lessonList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if a timetable is requested update the request counter
|
||||||
|
*/
|
||||||
|
private fun updateTimetableRequests(courseName: String) {
|
||||||
|
timetableRequests.stream().filter { it.courseName == courseName }.findFirst().ifPresentOrElse({
|
||||||
|
it.requests++
|
||||||
|
}, {
|
||||||
|
timetableRequests.add(TimetableCounter(courseName, 1))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
private fun getStatus(): Status {
|
private fun getStatus(): Status {
|
||||||
val currentTime = System.currentTimeMillis() / 1000
|
val currentTime = System.currentTimeMillis() / 1000
|
||||||
val minutes = (currentTime - startTime) % 3600 / 60
|
val minutes = (currentTime - startTime) % 3600 / 60
|
||||||
val hours = (currentTime - startTime) % 86400 / 3600
|
val hours = (currentTime - startTime) % 86400 / 3600
|
||||||
val days = (currentTime - startTime) / 86400
|
val days = (currentTime - startTime) / 86400
|
||||||
|
|
||||||
val timetableListSize = timetableList.size
|
|
||||||
val timetableListNames = timetableList.stream().map { x -> x.meta.courseName }.collect(Collectors.toList())
|
|
||||||
|
|
||||||
var hsoCode = 999
|
var hsoCode = 999
|
||||||
var swfrCode = 999
|
var swfrCode = 999
|
||||||
logger.info("status request at ${LocalDateTime.now()}!")
|
logger.info("status request at ${LocalDateTime.now()}!")
|
||||||
|
@ -173,9 +197,10 @@ class APIController {
|
||||||
"$days days, $hours:$minutes",
|
"$days days, $hours:$minutes",
|
||||||
apiVersion,
|
apiVersion,
|
||||||
softwareVersion,
|
softwareVersion,
|
||||||
requestCount,
|
totalRequests,
|
||||||
timetableListSize,
|
mensaMenuRequests,
|
||||||
HashSet(timetableListNames).toString(),
|
timetableRequests,
|
||||||
|
timetableList.size,
|
||||||
Date(courseList.meta.updateTime * 1000),
|
Date(courseList.meta.updateTime * 1000),
|
||||||
Date(mensaMenu.meta.updateTime * 1000),
|
Date(mensaMenu.meta.updateTime * 1000),
|
||||||
hsoCode,
|
hsoCode,
|
||||||
|
|
|
@ -58,12 +58,18 @@ class CacheController {
|
||||||
scheduledUpdates()
|
scheduledUpdates()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get a timetable, since they may not cached, we need to make sure it's cached, otherwise download
|
||||||
|
* @param courseName the name of the course to be requested
|
||||||
|
* @param weekIndex request week number (current week = 0)
|
||||||
|
* @return the timetable of a course (courseName)
|
||||||
|
*/
|
||||||
fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
|
fun getTimetable(courseName: String, weekIndex: Int): TimetableCourseWeek = runBlocking {
|
||||||
val currentTime = System.currentTimeMillis() / 1000
|
val currentTime = System.currentTimeMillis() / 1000
|
||||||
var timetable = TimetableWeek()
|
var timetable = TimetableWeek()
|
||||||
var weekNumberYear = 0
|
var weekNumberYear = 0
|
||||||
// check if the timetable already exists and is up to date
|
|
||||||
|
|
||||||
|
// 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)) {
|
when (timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.weekIndex == weekIndex }.findAny().orElse(null)) {
|
||||||
// there is no such course yet, create one
|
// there is no such course yet, create one
|
||||||
null -> {
|
null -> {
|
||||||
|
@ -90,9 +96,8 @@ class CacheController {
|
||||||
* during the update process the old data will be returned for a API request
|
* during the update process the old data will be returned for a API request
|
||||||
*/
|
*/
|
||||||
private fun asyncUpdateCourseList() = GlobalScope.launch {
|
private fun asyncUpdateCourseList() = GlobalScope.launch {
|
||||||
val result = CourseListParser().getCourseLinks(courseListURL)
|
CourseListParser().getCourseLinks(courseListURL)?.let {
|
||||||
if (result != null) {
|
courseList = CourseList(CourseMeta(System.currentTimeMillis() / 1000, it.size), it)
|
||||||
courseList = CourseList(CourseMeta(System.currentTimeMillis() / 1000, result.size), result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("updated courses successful at ${Date(courseList.meta.updateTime * 1000)}")
|
logger.info("updated courses successful at ${Date(courseList.meta.updateTime * 1000)}")
|
||||||
|
@ -134,9 +139,8 @@ class CacheController {
|
||||||
private fun initUpdates() = runBlocking {
|
private fun initUpdates() = runBlocking {
|
||||||
// get all courses on startup
|
// get all courses on startup
|
||||||
val jobCourseUpdate = GlobalScope.async {
|
val jobCourseUpdate = GlobalScope.async {
|
||||||
val result = CourseListParser().getCourseLinks(courseListURL)
|
CourseListParser().getCourseLinks(courseListURL)?.let {
|
||||||
if (result != null) {
|
courseList = CourseList(CourseMeta(System.currentTimeMillis() / 1000, it.size), it)
|
||||||
courseList = CourseList(CourseMeta(System.currentTimeMillis() / 1000, result.size), result)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,14 +62,17 @@ data class TimetableCourseWeek(val meta: TimetableCourseMeta, var timetable: Tim
|
||||||
|
|
||||||
|
|
||||||
// data classes for the status part
|
// data classes for the status part
|
||||||
|
data class TimetableCounter(var courseName: String, var requests: Int)
|
||||||
|
|
||||||
data class Status(
|
data class Status(
|
||||||
val time: LocalDateTime,
|
val time: LocalDateTime,
|
||||||
val uptime: String,
|
val uptime: String,
|
||||||
val apiVersion: String,
|
val apiVersion: String,
|
||||||
val softwareVersion: String,
|
val softwareVersion: String,
|
||||||
val requestCount: Int,
|
val requestCount: Int,
|
||||||
|
val mensaMenuRequests: Int,
|
||||||
|
val timetableRequests: ArrayList<TimetableCounter>,
|
||||||
val timetableListSize: Int,
|
val timetableListSize: Int,
|
||||||
val timetableListNames: String,
|
|
||||||
val coursesLastUpdate: Date,
|
val coursesLastUpdate: Date,
|
||||||
val mensaLastUpdate: Date,
|
val mensaLastUpdate: Date,
|
||||||
val hsoResponseCode: Int,
|
val hsoResponseCode: Int,
|
||||||
|
|
Loading…
Reference in New Issue