2019-10-21 18:46:21 +02:00
/ * *
* TheCitadelofRicks
*
2020-01-15 13:42:40 +01:00
* Copyright 2019 - 2020 < seil0 @mosad . xyz >
2019-10-21 18:46:21 +02:00
*
* 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 .
*
* /
2019-10-20 11:52:44 +02:00
package org.mosad.thecitadelofricks.controller
2019-10-21 18:46:21 +02:00
import org.mosad.thecitadelofricks.controller.StatusController.Companion.getTotalRequests
2019-10-20 11:52:44 +02:00
import org.slf4j.Logger
import org.slf4j.LoggerFactory
2019-10-21 18:46:21 +02:00
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
2019-10-20 11:52:44 +02:00
import java.net.HttpURLConnection
import java.net.URL
class CachetAPIController {
companion object {
private val logger : Logger = LoggerFactory . getLogger ( CachetAPIController :: class . java )
2019-10-21 18:46:21 +02:00
private var oldTotalRequests = 0
fun postTotalRequests ( ) {
try {
2019-11-14 18:43:00 +01:00
val url = URL ( " ${StartupController.cachetBaseURL} /api/v1/metrics/1/points " )
2019-10-21 18:46:21 +02:00
val jsonInputString = " { \" value \" : ${getTotalRequests() -oldTotalRequests} , \" timestamp \" : \" ${(System.currentTimeMillis() / 1000)} \" } "
oldTotalRequests = getTotalRequests ( )
val con = url . openConnection ( ) as HttpURLConnection
con . requestMethod = " POST "
con . setRequestProperty ( " Content-Type " , " application/json; utf-8 " )
con . setRequestProperty ( " Accept " , " application/json " )
2019-11-14 18:43:00 +01:00
con . setRequestProperty ( " X-Cachet-Token " , StartupController . cachetAPIKey )
2019-10-21 18:46:21 +02:00
con . doOutput = true
2019-10-20 11:52:44 +02:00
2019-10-21 18:46:21 +02:00
val os = con . outputStream
val input = jsonInputString . toByteArray ( charset ( " utf-8 " ) )
os . write ( input , 0 , input . size )
2019-10-20 11:52:44 +02:00
2019-10-21 18:46:21 +02:00
val br = BufferedReader ( InputStreamReader ( con . inputStream , " utf-8 " ) )
val response = StringBuilder ( )
var responseLine : String ?
2019-10-20 11:52:44 +02:00
2019-10-21 18:46:21 +02:00
while ( br . readLine ( ) . also { responseLine = it } != null ) {
response . append ( responseLine !! . trim { it <= ' ' } )
}
logger . info ( response . toString ( ) )
2019-10-20 11:52:44 +02:00
2019-10-21 18:46:21 +02:00
} catch ( e1 : IOException ) {
e1 . printStackTrace ( )
}
2019-10-20 11:52:44 +02:00
}
}
}