Browse Source
* API version 1.1.4 * added /health, returns 200 (OK, for status checks) * moved status code to a separate class * added status.mosad.xyz reportingpull/18/head 1.1.6
5 changed files with 205 additions and 94 deletions
@ -0,0 +1,127 @@
|
||||
/** |
||||
* TheCitadelofRicks |
||||
* |
||||
* Copyright 2019 <seil0@mosad.xyz> |
||||
* |
||||
* This program is free software; you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation; either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* This program is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with this program; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
||||
* MA 02110-1301, USA. |
||||
* |
||||
*/ |
||||
|
||||
package org.mosad.thecitadelofricks.controller |
||||
|
||||
import org.mosad.thecitadelofricks.APIController.Companion.apiVersion |
||||
import org.mosad.thecitadelofricks.APIController.Companion.softwareVersion |
||||
import org.mosad.thecitadelofricks.APIController.Companion.startTime |
||||
import org.mosad.thecitadelofricks.Status |
||||
import org.mosad.thecitadelofricks.TimetableCounter |
||||
import org.slf4j.Logger |
||||
import org.slf4j.LoggerFactory |
||||
import java.net.HttpURLConnection |
||||
import java.net.URL |
||||
import java.time.LocalDateTime |
||||
import java.util.* |
||||
import kotlin.collections.ArrayList |
||||
|
||||
class StatusController { |
||||
|
||||
companion object { |
||||
private val logger: Logger = LoggerFactory.getLogger(StatusController::class.java) |
||||
|
||||
private var totalRequests = 0 |
||||
private var mensaMenuRequests = 0 |
||||
private var timetableRequests = ArrayList<TimetableCounter>() |
||||
|
||||
fun updateTotalRequests() { |
||||
totalRequests++ |
||||
} |
||||
|
||||
fun updateMensaMenuRequests() { |
||||
mensaMenuRequests++ |
||||
} |
||||
|
||||
/** |
||||
* if a timetable is requested update the request counter |
||||
*/ |
||||
fun updateTimetableRequests(courseName: String) { |
||||
when (timetableRequests.stream().filter { x -> x.courseName == courseName }.findAny().orElse(null)) { |
||||
null -> timetableRequests.add(TimetableCounter(courseName, 0)) |
||||
} |
||||
timetableRequests.stream().filter { x ->x.courseName == courseName }.findFirst().ifPresent { x -> x.requests++ } |
||||
|
||||
// TODO Java 11 |
||||
// timetableRequests.stream().filter { it.courseName == courseName }.findFirst().ifPresentOrElse({ |
||||
// it.requests++ |
||||
// }, { |
||||
// |
||||
// }) |
||||
} |
||||
|
||||
fun getTotalRequests(): Int { |
||||
return totalRequests |
||||
} |
||||
|
||||
fun getMensaMenuRequests(): Int { |
||||
return mensaMenuRequests |
||||
} |
||||
|
||||
fun getTimetableRequests(): ArrayList<TimetableCounter> { |
||||
return timetableRequests |
||||
} |
||||
|
||||
fun getStatus(): Status { |
||||
val currentTime = System.currentTimeMillis() / 1000 |
||||
val minutes = (currentTime - startTime) % 3600 / 60 |
||||
val hours = (currentTime - startTime) % 86400 / 3600 |
||||
val days = (currentTime - startTime) / 86400 |
||||
|
||||
var hsoCode = 999 |
||||
var swfrCode = 999 |
||||
|
||||
try { |
||||
val hsoURL = URL("https://www.hs-offenburg.de/") |
||||
val swfrURL = URL("https://www.swfr.de/") |
||||
|
||||
var connection = hsoURL.openConnection() as HttpURLConnection |
||||
connection.requestMethod = "HEAD" |
||||
|
||||
connection.connectTimeout = 15000 |
||||
hsoCode = connection.responseCode |
||||
|
||||
connection = swfrURL.openConnection() as HttpURLConnection |
||||
connection.connectTimeout = 15000 |
||||
swfrCode = connection.responseCode |
||||
} catch (e: Exception) { |
||||
logger.error("Error while fetching url response codes!", e) |
||||
} |
||||
|
||||
return Status( |
||||
LocalDateTime.now(), |
||||
"$days days, $hours:$minutes", |
||||
apiVersion, |
||||
softwareVersion, |
||||
getTotalRequests(), |
||||
getMensaMenuRequests(), |
||||
getTimetableRequests(), |
||||
CacheController.timetableList.size, |
||||
Date(CacheController.courseList.meta.updateTime * 1000), |
||||
Date(CacheController.mensaMenu.meta.updateTime * 1000), |
||||
hsoCode, |
||||
swfrCode |
||||
) |
||||
} |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue