All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kalasim.monitors.MetricMonitor.kt Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package org.kalasim.monitors

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics
import org.kalasim.analysis.snapshot.StatisticalSummarySnapshot
import org.kalasim.misc.*
import org.koin.core.Koin
import kotlin.math.roundToInt

class NumericStatisticMonitor(name: String? = null, koin: Koin = DependencyContext.get()) :
    Monitor(name, koin), ValueMonitor {

    private val sumStats = ifEnabled { DescriptiveStatistics() }

    val values: DoubleArray
        get() = sumStats.values

    override fun addValue(value: Number) {
        if(!enabled) return

        sumStats.addValue(value.toDouble())
    }

    /** Increment the current value by 1 and add it as value. Autostart with 0 if there is no prior value. */
    operator fun inc(): NumericStatisticMonitor {
        val roundToInt = (values.lastOrNull() ?: 0.0).roundToInt()
        addValue((roundToInt + 1).toDouble())

        return this
    }

    operator fun dec(): NumericStatisticMonitor {
        val roundToInt = values.last()
        addValue((roundToInt - 1))

        return this
    }


    override fun reset() = sumStats.clear()


//    open fun mean(): Double? = sumStats.mean
//    open fun standardDeviation(): Double? = sumStats.mean

//    fun statistics(): DescriptiveStatistics = DescriptiveStatistics(sumStats.values)

    fun printHistogram(binCount: Int = NUM_HIST_BINS) {
        //    val histJson = JSONArray(GSON.toJson(histogramScaled))

        //    json {
        //        "name" to name
        //        "type" to [email protected] //"queue statistics"
        //        "entries" to n
        //        "mean" to mean
        //        "minimum" to min
        //        "maximum" to max
        //    }.toString(2).println()


//        val histogram = sumStats.buildHistogram()
//        val colWidth = 40.0
//
//        val histogramScaled = histogram.map { (range, value) -> range to colWidth * value / sumStats.n }
        println("Summary of: '${name}'")
        statistics().printThis()

        if(values.size > 2) {
            println("Histogram of: '${name}'")
            sumStats.buildHistogram(binCount).printHistogram()
        } else {
            println("Skipping histogram of '$name' because of to few data")
        }
    }


    fun statistics(excludeZeros: Boolean = false, rollingStats: Boolean = false): StatisticalSummarySnapshot {
        require(!rollingStats) { TODO() }

//        val stats: StatisticalSummary = if(rollingStats) SummaryStatistics() else DescriptiveStatistics()
        val stats = DescriptiveStatistics()

        if(excludeZeros) {
            values.filter { it > 0 }.forEach {
                stats.addValue(it)
            }
            //        SummaryStatistics().apply { values.filter { it > 0 }.forEach { addValue(it) } }
        } else {
            values.forEach { stats.addValue(it) }
        }

        return StatisticalSummarySnapshot(stats)
    }


    override val snapshot
        get() = statistics(false)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy