promviewmodel refactoring

This commit is contained in:
Martin Ptáček
2023-05-31 13:12:10 +02:00
parent 78e1746712
commit 15fe164855
4 changed files with 32 additions and 40 deletions

View File

@ -305,6 +305,9 @@ private fun ConfigFileErrorPage(
){ ){
//TODO implement this //TODO implement this
Text("Config File error page") Text("Config File error page")
if (uiState.fileLoadException != null) {
Text(uiState.fileLoadException!!)
}
} }
} }

View File

@ -1,6 +1,5 @@
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compose package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compose
import android.content.ComponentName
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
@ -9,14 +8,10 @@ import androidx.work.Constraints
import androidx.work.Data import androidx.work.Data
import androidx.work.ExistingWorkPolicy import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequest
import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.OutOfQuotaPolicy import androidx.work.OutOfQuotaPolicy
import androidx.work.WorkManager import androidx.work.WorkManager
import androidx.work.multiprocess.RemoteListenableWorker
import androidx.work.multiprocess.RemoteWorkerService
import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.worker.PromWorker import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.worker.PromWorker
import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.worker.PushProxWorker
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
@ -59,7 +54,9 @@ data class PromUiState(
class PromViewModel(): ViewModel() { class PromViewModel(): ViewModel() {
private val PROM_UNIQUE_WORK : String = "prom_unique_job" companion object {
private const val PROM_UNIQUE_WORK : String = "prom_unique_job"
}
private val _uiState = MutableStateFlow(PromUiState()) private val _uiState = MutableStateFlow(PromUiState())
val uiState : StateFlow<PromUiState> = _uiState.asStateFlow() val uiState : StateFlow<PromUiState> = _uiState.asStateFlow()
@ -69,33 +66,31 @@ class PromViewModel(): ViewModel() {
private fun loadConfigurationFile(){ private fun loadConfigurationFile(){
Log.v(TAG, "Checking for configuration file") Log.v(TAG, "Checking for configuration file")
viewModelScope.launch { Log.v(TAG, getContext().filesDir.absolutePath)
Log.v(TAG, getContext().filesDir.absolutePath) val fileExists = PromConfiguration.configFileExists(context = getContext())
val fileExists = PromConfiguration.configFileExists(context = getContext()) if (fileExists) {
if (fileExists) { val tempPromConfiguration : PromConfiguration
val tempPromConfiguration : PromConfiguration try {
try { tempPromConfiguration = PromConfiguration.loadFromConfigFile(getContext())
tempPromConfiguration = PromConfiguration.loadFromConfigFile(getContext())
_uiState.update { current ->
current.copy(
promConfig = tempPromConfiguration,
configFileState = ConfigFileState.SUCCESS,
)
}
}catch (e : Exception){
_uiState.update { current ->
current.copy(
configFileState = ConfigFileState.ERROR,
fileLoadException = e.toString(),
)
}
}
}else{
_uiState.update { current -> _uiState.update { current ->
current.copy(configFileState = ConfigFileState.MISSING) current.copy(
promConfig = tempPromConfiguration,
configFileState = ConfigFileState.SUCCESS,
)
} }
}catch (e : Exception){
_uiState.update { current ->
current.copy(
configFileState = ConfigFileState.ERROR,
fileLoadException = e.toString(),
)
}
}
}else {
_uiState.update { current ->
current.copy(configFileState = ConfigFileState.MISSING)
} }
} }
} }

View File

@ -90,7 +90,7 @@ data class PromConfiguration(
// data/user/0/com.birdthedeveloper.prometheus.android.prometheus.android.exporter/files // data/user/0/com.birdthedeveloper.prometheus.android.prometheus.android.exporter/files
private const val filename : String = "config.yaml" private const val filename : String = "config.yaml"
private const val alternativeFilename : String = "config.yml" private const val alternativeFilename : String = "config.yml"
suspend fun configFileExists(context : Context): Boolean { fun configFileExists(context : Context): Boolean {
// using app-specific storage // using app-specific storage
val file = File(context.filesDir, filename) val file = File(context.filesDir, filename)
val alternativeFile = File(context.filesDir, alternativeFilename) val alternativeFile = File(context.filesDir, alternativeFilename)
@ -103,7 +103,7 @@ data class PromConfiguration(
return Json.decodeFromString<PromConfiguration>(jsonString) return Json.decodeFromString<PromConfiguration>(jsonString)
} }
suspend fun loadFromConfigFile(context : Context): PromConfiguration { fun loadFromConfigFile(context : Context): PromConfiguration {
Log.v(TAG, context.filesDir.absolutePath) Log.v(TAG, context.filesDir.absolutePath)
val file = File(context.filesDir, filename) val file = File(context.filesDir, filename)

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log import android.util.Log
import androidx.work.CoroutineWorker import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compose.PromConfiguration
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIO
import io.ktor.client.statement.HttpResponse import io.ktor.client.statement.HttpResponse
@ -19,14 +20,13 @@ class PromWorker(
) : CoroutineWorker(context, parameters) { ) : CoroutineWorker(context, parameters) {
override suspend fun doWork(): Result { override suspend fun doWork(): Result {
//val inputConfiguration : PromConfiguration = PromConfiguration.fromWorkData(inputData) val inputConfiguration : PromConfiguration = PromConfiguration.fromWorkData(inputData)
while(true){ while(true){
Log.v(TAG, "Worker is working " + LocalDateTime.now().toString()) Log.v(TAG, "Worker is working " + LocalDateTime.now().toString())
//TODO curl localhost //TODO curl localhost
delay(1000L) delay(1000L)
curlLocalhost()
} }
//TODO implement this asap //TODO implement this asap
@ -34,10 +34,4 @@ class PromWorker(
return Result.success() return Result.success()
} }
private suspend fun curlLocalhost(){
val client = HttpClient(CIO)
val response: HttpResponse = client.get("http://localhost:8000")
Log.v(TAG, response.toString())
}
} }