progress with room and more metrics

This commit is contained in:
Martin Ptáček
2023-07-10 22:30:25 +02:00
parent c95809c4a5
commit 77aab03120
6 changed files with 180 additions and 122 deletions

View File

@ -4,6 +4,9 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
<application
android:allowBackup="true"

View File

@ -27,7 +27,7 @@ class AndroidCustomExporter(metricEngine: MetricsEngine) : Collector() {
"Current battery percentage", listOf(),
)
val batteryPercentage: Double = metricsEngineRef.getBatteryPercentage().toDouble()
val batteryPercentage: Double = metricsEngineRef.batteryChargeRatio().toDouble()
batteryPercentageGauge.addMetric(listOf(), batteryPercentage)
mfs.add(batteryPercentageGauge)
}

View File

@ -3,10 +3,16 @@ package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.work
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.hardware.SensorManager
import android.os.BatteryManager
import androidx.core.content.ContextCompat.getSystemService
class MetricsEngine(private val context: Context) {
public fun getBatteryPercentage(): Float {
private lateinit var sensorManager : SensorManager;
init {
//sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
}
public fun batteryChargeRatio(): Float {
val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { intFilter ->
context.registerReceiver(null, intFilter)
}
@ -21,4 +27,43 @@ class MetricsEngine(private val context: Context) {
batteryPct ?: return -1.0f
return batteryPct
}
public fun batteryIsCharging(): Float {
TODO("aa")
}
public fun somethingTodo(): Float {
TODO("aa")
}
//TODO
///TYPE_ACCELEROMETER Yes Yes Yes Yes
//TYPE_AMBIENT_TEMPERATURE Yes n/a n/a n/a
//TYPE_GRAVITY Yes Yes n/a n/a
//TYPE_GYROSCOPE Yes Yes n/a1 n/a1
//TYPE_LIGHT Yes Yes Yes Yes
//TYPE_LINEAR_ACCELERATION Yes Yes n/a n/a
//TYPE_MAGNETIC_FIELD Yes Yes Yes Yes
//TYPE_ORIENTATION Yes2 Yes2 Yes2 Yes
//TYPE_PRESSURE Yes Yes n/a1 n/a1
//TYPE_PROXIMITY Yes Yes Yes Yes
//TYPE_RELATIVE_HUMIDITY Yes n/a n/a n/a
//TYPE_ROTATION_VECTOR Yes Yes n/a n/a
//TYPE_TEMPERATURE
// - hardware metrics:
// - basic hw sensors
// - network availability
// - 4G, 5G
// - gps - glonass beidou ...
// - battery, charging
// node exporter metrics
// - cpu
// - ram
// - scrape duration
// - bluetooth - mac bluetooth
// - nfc
// - storage information
// - system information - version .. device name, doba provozu
}

View File

@ -23,10 +23,10 @@ import java.lang.IndexOutOfBoundsException
private const val TAG: String = "REMOTE_WRITE_SENDER"
// This class stores information about scrapes to PROM_SERVER and PUSHPROX
// for purposes of scraping metrics on device and back-filling them later using remote write
//
// Only timestamps of successful scrapes are stored
/// This class stores information about scrapes to PROM_SERVER and PUSHPROX
/// for purposes of scraping metrics on device and back-filling them later using remote write
///
/// Only timestamps of successful scrapes are stored
internal class LastTimeRingBuffer(private val scrapeInterval: Int) {
private val buffer: Array<Long> = Array(hysteresisMemory) { 0 }
private var firstIndex: Int = -1
@ -199,8 +199,7 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) {
client = HttpClient()
try {
//TODO test this being coroutine scope
coroutineScope { //TODO this could be a problem
coroutineScope {
launch {
// check for outage in scrapes, save scrapes to storage
Log.d(TAG, "Launching scraper")

View File

@ -1,113 +1,127 @@
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.worker
import android.content.Context
import androidx.room.Dao
import androidx.room.Database
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Room
import androidx.room.RoomDatabase
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import remote.write.RemoteWrite.TimeSeries
/// Room is a relational database
/// Contains the following tables:
/// - Timeseries table:
/// + labels : List<TimeSeriesLabel> sorted alphabetically and encoded in json
/// - Sample table:
/// + id
/// + timestamp
/// + value
/// + Timeseries foreign key
@Entity
data class RoomTimeSeries (
@PrimaryKey(autoGenerate = false)
val labels : String
)
@Entity
data class RoomSample(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
val timeStamp : Long,
val value : Double,
)
@Serializable
data class TimeSeriesLabelList(
val labels: List<TimeSeriesLabel>
)
@Database(
entities = [RoomTimeSeries::class, RoomSample::class],
version = 1
)
abstract class RemoteWriteDatabase: RoomDatabase() {
abstract val dao: RoomDao
}
@Dao
interface RoomDao {
@Query("")//TODO
fun insertOneTimeSeriesSample(){
}
@Query("") //TODO
fun getNumberOfTimeSeriesSamples(number : Int){
}
@Query("") //TODO
fun getTotalNumberOfSamples(){
}
}
class RemoteWriteSenderDbStorage(getContext: () -> Context) : RemoteWriteSenderStorage(){
companion object{
const val dbName = "prometheus.db"
}
private val roomDb by lazy {
Room.databaseBuilder(
getContext(),
RemoteWriteDatabase::class.java,
dbName,
).build()
}
private fun encodeLabels(labelsList: List<TimeSeriesLabel>) : String{
/// preserve the same order
val sorted : List<TimeSeriesLabel> = labelsList.sortedBy { it.name }
val timeSeriesLabelList = TimeSeriesLabelList(labels = sorted)
return Json.encodeToString(TimeSeriesLabelList.serializer(), timeSeriesLabelList)
}
private fun decodeLabels(labels : String) : List<TimeSeriesLabel> {
return Json.decodeFromString<TimeSeriesLabelList>(labels).labels
}
override fun writeScrapedSample(metricsScrape: MetricsScrape) {
TODO("Not yet implemented")
}
override fun getScrapedSamplesCompressedProtobuf(howMany: Int): ByteArray {
TODO("Not yet implemented")
}
override fun removeNumberOfScrapedSamples(number: Int) {
TODO("Not yet implemented")
}
override fun isEmpty(): Boolean {
TODO("Not yet implemented")
}
override fun getLength(): Int {
TODO("Not yet implemented")
}
}
//import android.content.Context
//import androidx.annotation.EmptySuper
//import androidx.room.Dao
//import androidx.room.Database
//import androidx.room.Embedded
//import androidx.room.Entity
//import androidx.room.PrimaryKey
//import androidx.room.Query
//import androidx.room.Room
//import androidx.room.RoomDatabase
//import kotlinx.serialization.Serializable
//import kotlinx.serialization.json.Json
//import remote.write.RemoteWrite.TimeSeries
//
///// Room is a relational database
///// Contains the following tables:
///// - TimeSeries table:
///// + labels : List<TimeSeriesLabel> sorted alphabetically and encoded in json
///// - Sample table:
///// + id
///// + timestamp
///// + value
///// + TimeSeries foreign key
//
//@Entity
//data class RoomTimeSeries (
// @PrimaryKey(autoGenerate = false)
// val labels : String
//)
//
//@Entity
//data class RoomSample(
// @PrimaryKey(autoGenerate = true)
// val id: Int = 0,
// val timeStamp : Long,
// val value : Double,
//)
//
//@Serializable
//data class TimeSeriesLabelList(
// val labels: List<TimeSeriesLabel>
//)
//
//data class TimeSeriesWithSamples(
// @Embedded val timeSeries: RoomTimeSeries,
// @Embedded val sample : RoomSample,
//)
//
//@Database(
// entities = [RoomTimeSeries::class, RoomSample::class],
// version = 1
//)
//abstract class RemoteWriteDatabase: RoomDatabase() {
// abstract val dao: RoomDao
//}
//
//@Dao
//interface RoomDao {
//
// fun insertOneTimeSeriesSample(){
//
// }
// @Query("")//TODO
// private fun insertTimeSeries(){
//
// }
//
// @Query("")///TODO
// private fun insertSamples(){
//
// }
//
// //@Query("SELECT * ") //TODO
// fun getNumberOfTimeSeriesSamples(number : Int) : List<TimeSeriesWithSamples>
//
// @Query("") //TODO
// fun getTotalNumberOfSamples(){
//
// }
//
//}
//
//class RemoteWriteSenderDbStorage(getContext: () -> Context) : RemoteWriteSenderStorage(){
// companion object{
// const val dbName = "prometheus.db"
// }
//
// private val roomDb by lazy {
// Room.databaseBuilder(
// getContext(),
// RemoteWriteDatabase::class.java,
// dbName,
// ).build()
// }
//
// private fun encodeLabels(labelsList: List<TimeSeriesLabel>) : String{
// /// preserve the same order
// val sorted : List<TimeSeriesLabel> = labelsList.sortedBy { it.name }
// val timeSeriesLabelList = TimeSeriesLabelList(labels = sorted)
// return Json.encodeToString(TimeSeriesLabelList.serializer(), timeSeriesLabelList)
// }
//
// private fun decodeLabels(labels : String) : List<TimeSeriesLabel> {
// return Json.decodeFromString<TimeSeriesLabelList>(labels).labels
// }
// override fun writeScrapedSample(metricsScrape: MetricsScrape) {
// TODO("Not yet implemented")
// }
//
// override fun getScrapedSamplesCompressedProtobuf(howMany: Int): ByteArray {
// TODO("Not yet implemented")
// }
//
// override fun removeNumberOfScrapedSamples(number: Int) {
// TODO("Not yet implemented")
// }
//
// override fun isEmpty(): Boolean {
// TODO("Not yet implemented")
// }
//
// override fun getLength(): Int {
// TODO("Not yet implemented")
// }
//}

View File

@ -10,9 +10,6 @@ private typealias ConverterHashMap = HashMap<List<TimeSeriesLabel>, MutableList<
private const val TAG : String = "REMOTE_WRITE_SENDER_MEMORY_SIMPLE_STORAGE";
//TODO sort this out
class RemoteWriteSenderSimpleMemoryStorage : RemoteWriteSenderStorage() {
private val data: Queue<MetricsScrape> = LinkedList()