add serializable library

This commit is contained in:
Martin Ptáček
2023-05-31 11:00:16 +02:00
parent 9ad25689e8
commit 252a9b1090
8 changed files with 73 additions and 47 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="1.7.0" />
<option name="version" value="1.8.21" />
</component>
</project>

3
client/.idea/misc.xml generated
View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlinx-serialization'
}
android {
@ -37,7 +38,7 @@ android {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.2.0'
kotlinCompilerExtensionVersion '1.4.7'
}
packagingOptions {
resources {
@ -54,9 +55,6 @@ dependencies {
implementation 'io.prometheus:simpleclient:0.16.0'
implementation 'io.prometheus:simpleclient_common:0.16.0'
// custom - exponential backoff
implementation("io.github.reugn:kotlin-backoff:0.4.0")
// custom - kotlin coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.0-RC"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.0-RC"
@ -82,6 +80,7 @@ dependencies {
// custom - yaml configuration parsing
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.1")
implementation("com.charleskorn.kaml:kaml:0.54.0")

View File

@ -69,11 +69,12 @@ class PromViewModel(): ViewModel() {
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()
tempPromConfiguration = PromConfiguration.loadFromConfigFile(getContext())
_uiState.update { current ->
current.copy(

View File

@ -1,11 +1,28 @@
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compose
import android.content.Context
import android.util.Log
import androidx.work.Data
import androidx.work.workDataOf
import kotlinx.serialization.Serializable
import java.io.File
private val defaultPrometheusServerPort : Int = 10101 //TODO register with prometheus foundation
private val defaultRemoteWriteScrapeInterval : Int = 30
private const val TAG : String = "CONFIGURATION"
private const val defaultPrometheusServerPort : Int = 10101 //TODO register within prometheus foundation
private const val defaultRemoteWriteScrapeInterval : Int = 30
@Serializable
data class PromConfigurationFile(
val prometheusServerEnabled : Boolean = true,
val prometheusServerPort : Int = defaultPrometheusServerPort,
val pushproxEnabled : Boolean = false,
val pushproxFqdn : String = "",
val pushproxProxyUrl : String = "",
val remoteWriteEnabled : Boolean = false,
val remoteWriteScrapeInterval : Int = defaultRemoteWriteScrapeInterval,
val remoteWriteEndpoint : String = "",
)
data class PromConfiguration(
// the following are default values for various configuration settings
@ -18,16 +35,19 @@ data class PromConfiguration(
val remoteWriteScrapeInterval : Int = defaultRemoteWriteScrapeInterval,
val remoteWriteEndpoint : String = "",
) {
private val filepath : String = "config.yaml"
private val alternativeFilepath : String = "config.yml"
companion object {
// 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 {
//TODO implement this asap
return false
// using app-specific storage
val file = File(context.filesDir, filename)
val alternativeFile = File(context.filesDir, alternativeFilename)
return file.exists() || alternativeFile.exists()
}
fun fromWorkData(data : Data) : PromConfiguration{
fun fromWorkData(data : Data) : PromConfiguration {
return PromConfiguration(
prometheusServerEnabled = data.getBoolean("0", true),
prometheusServerPort = data.getInt("1", defaultPrometheusServerPort),
@ -40,8 +60,22 @@ data class PromConfiguration(
)
}
suspend fun loadFromConfigFile(): PromConfiguration {
//TODO open file, parse yaml, throw exception possibly
suspend fun loadFromConfigFile(context : Context): PromConfiguration {
Log.v(TAG, context.filesDir.absolutePath)
val file = File(context.filesDir, filename)
val alternativeFile = File(context.filesDir, alternativeFilename)
val fileContents : String
if (file.exists()){
fileContents = file.readText()
}else if (alternativeFile.exists()){
fileContents = alternativeFile.readText()
}else{
throw Exception("configuration file does not exist!")
}
//TODO implement this asap
return PromConfiguration()
}
}

View File

@ -1,8 +1,6 @@
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.worker
import android.util.Log
import io.github.reugn.kotlin.backoff.StrategyBackoff
import io.github.reugn.kotlin.backoff.strategy.ExponentialStrategy
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.post
@ -58,7 +56,6 @@ data class PushProxContext(
val client : HttpClient,
val pollUrl : String,
val pushUrl : String,
val backoff : StrategyBackoff<Unit>,
val fqdn : String,
)
@ -95,7 +92,6 @@ class PushProxClient(
client,
pollURL,
pushURL,
newBackoffFromFlags(),
config.pushProxFqdn,
)
}
@ -160,39 +156,30 @@ class PushProxClient(
}
}
private fun newBackoffFromFlags() : StrategyBackoff<Unit> {
return StrategyBackoff<Unit>(
strategy = ExponentialStrategy(
expBase = 2,
baseDelayMs = (retryInitialWaitSeconds * 1000).toLong(),
maxDelayMs = (retryMaxWaitSeconds * 1000).toLong(),
),
)
}
//TODO migrate to work manager
private suspend fun loop(context : PushProxContext) {
var shouldContinue : Boolean = true
while (shouldContinue) {
log("pushprox main loop", "loop start")
// register poll error using try-catch block
var result = context.backoff.withRetries {
try {
doPoll(context)
}catch(e : CancellationException){
shouldContinue = false
}
catch (e: Exception) {
for(exception in e.suppressed){
if(exception is CancellationException){
shouldContinue = false
}
}
log("exception encountered!", e.toString())
counters.pollError()
throw e
}
}
//TODO backoff strategy
// var result = context.backoff.withRetries {
// try {
// doPoll(context)
// }catch(e : CancellationException){
// shouldContinue = false
// }
// catch (e: Exception) {
// for(exception in e.suppressed){
// if(exception is CancellationException){
// shouldContinue = false
// }
// }
// log("exception encountered!", e.toString())
// counters.pollError()
// throw e
// }
// }
log("pushprox main loop", "loop end")
}
}

View File

@ -7,4 +7,8 @@ plugins {
id 'com.android.application' version '8.0.2' apply false
id 'com.android.library' version '8.0.2' apply false
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
// serialization plugins
id 'org.jetbrains.kotlin.jvm' version '1.8.21'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.8.21'
}