diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/HomeActivity.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/HomeActivity.kt index 33eb405..24c3def 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/HomeActivity.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/HomeActivity.kt @@ -305,6 +305,9 @@ private fun ConfigFileErrorPage( ){ //TODO implement this Text("Config File error page") + if (uiState.fileLoadException != null) { + Text(uiState.fileLoadException!!) + } } } diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/PromViewModel.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/PromViewModel.kt index e6e9dbd..cb5c11f 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/PromViewModel.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/PromViewModel.kt @@ -1,6 +1,5 @@ package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compose -import android.content.ComponentName import android.content.Context import android.util.Log import androidx.lifecycle.ViewModel @@ -9,14 +8,10 @@ import androidx.work.Constraints import androidx.work.Data import androidx.work.ExistingWorkPolicy import androidx.work.NetworkType -import androidx.work.OneTimeWorkRequest import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy 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.PushProxWorker import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow @@ -59,7 +54,9 @@ data class PromUiState( 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()) val uiState : StateFlow = _uiState.asStateFlow() @@ -69,33 +66,31 @@ class PromViewModel(): ViewModel() { private fun loadConfigurationFile(){ Log.v(TAG, "Checking for configuration file") - viewModelScope.launch { - Log.v(TAG, getContext().filesDir.absolutePath) - val fileExists = PromConfiguration.configFileExists(context = getContext()) - if (fileExists) { - val tempPromConfiguration : PromConfiguration - try { - tempPromConfiguration = PromConfiguration.loadFromConfigFile(getContext()) + Log.v(TAG, getContext().filesDir.absolutePath) + val fileExists = PromConfiguration.configFileExists(context = getContext()) + if (fileExists) { + val tempPromConfiguration : PromConfiguration + try { + 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 -> - 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) } } } diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/configuration.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/configuration.kt index 1cc9d3d..38097b4 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/configuration.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/compose/configuration.kt @@ -90,7 +90,7 @@ data class PromConfiguration( // data/user/0/com.birdthedeveloper.prometheus.android.prometheus.android.exporter/files private const val filename : String = "config.yaml" private const val alternativeFilename : String = "config.yml" - suspend fun configFileExists(context : Context): Boolean { + fun configFileExists(context : Context): Boolean { // using app-specific storage val file = File(context.filesDir, filename) val alternativeFile = File(context.filesDir, alternativeFilename) @@ -103,7 +103,7 @@ data class PromConfiguration( return Json.decodeFromString(jsonString) } - suspend fun loadFromConfigFile(context : Context): PromConfiguration { + fun loadFromConfigFile(context : Context): PromConfiguration { Log.v(TAG, context.filesDir.absolutePath) val file = File(context.filesDir, filename) diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/PromWorker.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/PromWorker.kt index 1fa0634..9b487f8 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/PromWorker.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/PromWorker.kt @@ -4,6 +4,7 @@ import android.content.Context import android.util.Log import androidx.work.CoroutineWorker import androidx.work.WorkerParameters +import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compose.PromConfiguration import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO import io.ktor.client.statement.HttpResponse @@ -19,14 +20,13 @@ class PromWorker( ) : CoroutineWorker(context, parameters) { override suspend fun doWork(): Result { - //val inputConfiguration : PromConfiguration = PromConfiguration.fromWorkData(inputData) + val inputConfiguration : PromConfiguration = PromConfiguration.fromWorkData(inputData) while(true){ Log.v(TAG, "Worker is working " + LocalDateTime.now().toString()) //TODO curl localhost delay(1000L) - curlLocalhost() } //TODO implement this asap @@ -34,10 +34,4 @@ class PromWorker( return Result.success() } - private suspend fun curlLocalhost(){ - val client = HttpClient(CIO) - val response: HttpResponse = client.get("http://localhost:8000") - Log.v(TAG, response.toString()) - } - } \ No newline at end of file