[+] Custom influx type
This commit is contained in:
@@ -20,6 +20,7 @@ import com.google.android.material.snackbar.Snackbar
|
|||||||
import com.google.gson.*
|
import com.google.gson.*
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
import com.influxdb.client.InfluxDBClient
|
import com.influxdb.client.InfluxDBClient
|
||||||
|
import com.influxdb.client.InfluxDBClientFactory
|
||||||
import com.influxdb.client.domain.DeletePredicateRequest
|
import com.influxdb.client.domain.DeletePredicateRequest
|
||||||
import com.influxdb.client.domain.WritePrecision
|
import com.influxdb.client.domain.WritePrecision
|
||||||
import com.influxdb.client.kotlin.InfluxDBClientKotlin
|
import com.influxdb.client.kotlin.InfluxDBClientKotlin
|
||||||
@@ -59,10 +60,26 @@ interface Prefs {
|
|||||||
var infBucket: String?
|
var infBucket: String?
|
||||||
var infToken: String?
|
var infToken: String?
|
||||||
|
|
||||||
fun createInflux(): InfluxDBClientKotlin
|
fun createInflux(): Influx
|
||||||
suspend fun influxPing()
|
suspend fun influxPing()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data class Influx(
|
||||||
|
val kt: InfluxDBClientKotlin,
|
||||||
|
val java: InfluxDBClient,
|
||||||
|
|
||||||
|
val prec: WritePrecision = WritePrecision.MS
|
||||||
|
) {
|
||||||
|
val writer get() = kt.getWriteKotlinApi()
|
||||||
|
|
||||||
|
suspend operator fun <T> plusAssign(m: T) = writer.writeMeasurement(m, prec)
|
||||||
|
suspend operator fun <T> plusAssign(m: Iterable<T>) = writer.writeMeasurements(m, prec)
|
||||||
|
suspend operator fun <T> plusAssign(m: Flow<T>) = writer.writeMeasurements(m, prec)
|
||||||
|
|
||||||
|
fun dropAll(bucket: String, org: String) = java.deleteApi.delete(DeletePredicateRequest()
|
||||||
|
.start(Date(0).offset).stop(Date(5999999999999).offset), bucket, org)
|
||||||
|
}
|
||||||
|
|
||||||
val Context.pref get() = PreferenceManager.getDefaultSharedPreferences(this)
|
val Context.pref get() = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
val Context.prefs get() = object : Prefs {
|
val Context.prefs get() = object : Prefs {
|
||||||
val prop = object {
|
val prop = object {
|
||||||
@@ -78,11 +95,13 @@ val Context.prefs get() = object : Prefs {
|
|||||||
override var infBucket by prop
|
override var infBucket by prop
|
||||||
override var infToken by prop
|
override var infToken by prop
|
||||||
|
|
||||||
override fun createInflux() = InfluxDBClientKotlinFactory
|
override fun createInflux() = Influx(
|
||||||
.create(infUrl ?: "", (infToken ?: "").toCharArray(), infOrg ?: "", infBucket ?: "")
|
InfluxDBClientKotlinFactory.create(infUrl!!, infToken!!.toCharArray(), infOrg!!, infBucket!!),
|
||||||
|
InfluxDBClientFactory.create(infUrl!!, infToken!!.toCharArray(), infOrg!!, infBucket!!),
|
||||||
|
)
|
||||||
|
|
||||||
override suspend fun influxPing() = with(createInflux()) {
|
override suspend fun influxPing() = with(createInflux()) {
|
||||||
getWriteKotlinApi().writeRecord("ping host=\"${Build.MODEL}\"", WritePrecision.MS)
|
writer.writeRecord("ping host=\"${Build.MODEL}\"", WritePrecision.MS)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,13 +117,6 @@ inline fun <reified T> Context.act()
|
|||||||
fun ComponentActivity.actCallback(fn: ActivityResultCallback<ActivityResult>) =
|
fun ComponentActivity.actCallback(fn: ActivityResultCallback<ActivityResult>) =
|
||||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult(), fn)
|
registerForActivityResult(ActivityResultContracts.StartActivityForResult(), fn)
|
||||||
|
|
||||||
suspend infix fun <T> InfluxDBClientKotlin.add(meas: T) = getWriteKotlinApi().writeMeasurement(meas, WritePrecision.MS)
|
|
||||||
suspend infix fun <T> InfluxDBClientKotlin.add(meas: Iterable<T>) = getWriteKotlinApi().writeMeasurements(meas, WritePrecision.MS)
|
|
||||||
suspend infix fun <T> InfluxDBClientKotlin.add(meas: Flow<T>) = getWriteKotlinApi().writeMeasurements(meas, WritePrecision.MS)
|
|
||||||
|
|
||||||
fun InfluxDBClient.dropAll(bucket: String, org: String) =
|
|
||||||
deleteApi.delete(DeletePredicateRequest().start(Date(0).offset).stop(Date(5999999999999).offset), bucket, org)
|
|
||||||
|
|
||||||
fun Any.reflectToString(): String {
|
fun Any.reflectToString(): String {
|
||||||
val s = ArrayList<String>()
|
val s = ArrayList<String>()
|
||||||
var clazz: Class<in Any>? = javaClass
|
var clazz: Class<in Any>? = javaClass
|
||||||
|
|||||||
Reference in New Issue
Block a user