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.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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 <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -27,7 +27,7 @@ class AndroidCustomExporter(metricEngine: MetricsEngine) : Collector() {
"Current battery percentage", listOf(), "Current battery percentage", listOf(),
) )
val batteryPercentage: Double = metricsEngineRef.getBatteryPercentage().toDouble() val batteryPercentage: Double = metricsEngineRef.batteryChargeRatio().toDouble()
batteryPercentageGauge.addMetric(listOf(), batteryPercentage) batteryPercentageGauge.addMetric(listOf(), batteryPercentage)
mfs.add(batteryPercentageGauge) 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.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.hardware.SensorManager
import android.os.BatteryManager import android.os.BatteryManager
import androidx.core.content.ContextCompat.getSystemService
class MetricsEngine(private val context: Context) { 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 -> val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { intFilter ->
context.registerReceiver(null, intFilter) context.registerReceiver(null, intFilter)
} }
@ -21,4 +27,43 @@ class MetricsEngine(private val context: Context) {
batteryPct ?: return -1.0f batteryPct ?: return -1.0f
return batteryPct 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" private const val TAG: String = "REMOTE_WRITE_SENDER"
// This class stores information about scrapes to PROM_SERVER and PUSHPROX /// 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 /// for purposes of scraping metrics on device and back-filling them later using remote write
// ///
// Only timestamps of successful scrapes are stored /// Only timestamps of successful scrapes are stored
internal class LastTimeRingBuffer(private val scrapeInterval: Int) { internal class LastTimeRingBuffer(private val scrapeInterval: Int) {
private val buffer: Array<Long> = Array(hysteresisMemory) { 0 } private val buffer: Array<Long> = Array(hysteresisMemory) { 0 }
private var firstIndex: Int = -1 private var firstIndex: Int = -1
@ -199,8 +199,7 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) {
client = HttpClient() client = HttpClient()
try { try {
//TODO test this being coroutine scope coroutineScope {
coroutineScope { //TODO this could be a problem
launch { launch {
// check for outage in scrapes, save scrapes to storage // check for outage in scrapes, save scrapes to storage
Log.d(TAG, "Launching scraper") Log.d(TAG, "Launching scraper")

View File

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