mirror of
https://github.com/mii443/prometheus-android-exporter.git
synced 2025-12-04 19:48:20 +00:00
ViewModel done without WorkManager
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter
|
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.text.BoringLayout.Metrics
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import io.prometheus.client.CollectorRegistry
|
import io.prometheus.client.CollectorRegistry
|
||||||
@@ -15,12 +14,11 @@ data class PromUiState(
|
|||||||
val serverTurnedOn : Boolean = false,
|
val serverTurnedOn : Boolean = false,
|
||||||
val pushProxTurnedOn : Boolean = false,
|
val pushProxTurnedOn : Boolean = false,
|
||||||
val serverPort : Int? = null, // if null, use default port
|
val serverPort : Int? = null, // if null, use default port
|
||||||
val myFqdn : String? = null,
|
val fqdn : String? = null,
|
||||||
val pushProxURL : String? = null,
|
val pushProxURL : String? = null,
|
||||||
)
|
)
|
||||||
|
|
||||||
val TAG : String = "PROMVIEWMODEL"
|
val TAG : String = "PROMVIEWMODEL"
|
||||||
val DEFAULT_SERVER_PORT : Int = 10101 //TODO register with prometheus community
|
|
||||||
|
|
||||||
class PromViewModel(): ViewModel() {
|
class PromViewModel(): ViewModel() {
|
||||||
private val _uiState = MutableStateFlow(PromUiState())
|
private val _uiState = MutableStateFlow(PromUiState())
|
||||||
@@ -55,10 +53,37 @@ class PromViewModel(): ViewModel() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getPromServerPort() : Int{
|
||||||
|
val DEFAULT_SERVER_PORT : Int = 10101 //TODO register with prometheus community
|
||||||
|
return if(_uiState.value.serverPort != null){
|
||||||
|
_uiState.value.serverPort!!
|
||||||
|
}else{
|
||||||
|
DEFAULT_SERVER_PORT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun performScrape() : String {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// if result is not null, it contains an error message
|
// if result is not null, it contains an error message
|
||||||
fun turnServerOn() : String?{
|
fun turnServerOn() : String?{
|
||||||
//TODO implement
|
try{
|
||||||
|
prometheusServer.startInBackground(
|
||||||
|
PrometheusServerConfig(
|
||||||
|
getPromServerPort(),
|
||||||
|
::performScrape
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}catch(e : Exception){
|
||||||
|
return "Prometheus server failed!"
|
||||||
|
}
|
||||||
|
|
||||||
|
_uiState.update { current ->
|
||||||
|
current.copy(
|
||||||
|
serverTurnedOn = true
|
||||||
|
)
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,9 +91,39 @@ class PromViewModel(): ViewModel() {
|
|||||||
//TODO implement
|
//TODO implement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun validatePushProxSettings() : String? {
|
||||||
|
_uiState.value.pushProxURL?: return "Please fill in PushProx URL with port"
|
||||||
|
_uiState.value.fqdn?: return "Please fill in your Fully Qualified Domain Name"
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
// if result is not null, it contains an error message
|
// if result is not null, it contains an error message
|
||||||
fun turnPushProxOn() : String?{
|
fun turnPushProxOn() : String?{
|
||||||
//TODO implement
|
|
||||||
|
val error : String? = validatePushProxSettings()
|
||||||
|
if(error != null){
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
|
||||||
|
try{
|
||||||
|
pushProxClient.startBackground(
|
||||||
|
PushProxConfig(
|
||||||
|
performScrape = ::performScrape,
|
||||||
|
fqdn = _uiState.value.fqdn!!,
|
||||||
|
proxyUrl = _uiState.value.pushProxURL!!,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}catch(e : Exception){
|
||||||
|
return "PushProx client failed!"
|
||||||
|
}
|
||||||
|
|
||||||
|
_uiState.update { current ->
|
||||||
|
current.copy(
|
||||||
|
pushProxTurnedOn = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,10 +132,18 @@ class PromViewModel(): ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun updatePushProxURL(url : String){
|
fun updatePushProxURL(url : String){
|
||||||
//TODO implement
|
_uiState.update { current ->
|
||||||
|
current.copy(
|
||||||
|
pushProxURL = url
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updatePushProxFQDN(fqdn : String){
|
fun updatePushProxFQDN(fqdn : String){
|
||||||
//TODO implement
|
_uiState.update { current ->
|
||||||
|
current.copy(
|
||||||
|
fqdn = fqdn
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,8 +18,11 @@ data class PrometheusServerConfig(
|
|||||||
|
|
||||||
// Expose metrics on given port using Ktor http server with CIO engine
|
// Expose metrics on given port using Ktor http server with CIO engine
|
||||||
class PrometheusServer(){
|
class PrometheusServer(){
|
||||||
|
private var isRunning : Boolean = false
|
||||||
|
|
||||||
fun startBackground(config : PrometheusServerConfig){
|
fun startInBackground(config : PrometheusServerConfig){
|
||||||
|
if (!isRunning){
|
||||||
|
isRunning = true
|
||||||
//TODO dispose server
|
//TODO dispose server
|
||||||
|
|
||||||
val server = configureServer(config)
|
val server = configureServer(config)
|
||||||
@@ -31,6 +34,7 @@ class PrometheusServer(){
|
|||||||
|
|
||||||
log("startBackground", "done")
|
log("startBackground", "done")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun getMetrics(performScrape: suspend () -> String) : String{
|
private suspend fun getMetrics(performScrape: suspend () -> String) : String{
|
||||||
val result : String = try{
|
val result : String = try{
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ private class Counters(collectorRegistry: CollectorRegistry?) {
|
|||||||
// Configuration object for this pushprox client
|
// Configuration object for this pushprox client
|
||||||
data class PushProxConfig(
|
data class PushProxConfig(
|
||||||
val fqdn: String,
|
val fqdn: String,
|
||||||
val proxyURL: String,
|
val proxyUrl: String,
|
||||||
val performScrape: suspend () -> String,
|
val performScrape: suspend () -> String,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -95,7 +95,7 @@ class PushProxClient(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun processConfig(client : HttpClient, config : PushProxConfig) : PushProxContext {
|
private fun processConfig(client : HttpClient, config : PushProxConfig) : PushProxContext {
|
||||||
val modifiedProxyURL = config.proxyURL.trim('/')
|
val modifiedProxyURL = config.proxyUrl.trim('/')
|
||||||
val pollURL : String = "$modifiedProxyURL/poll"
|
val pollURL : String = "$modifiedProxyURL/poll"
|
||||||
val pushURL : String = "$modifiedProxyURL/push"
|
val pushURL : String = "$modifiedProxyURL/push"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user