aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/com/drivergrp/core/stats.scala
blob: 2af4b6a412dbbbfb15d4d27d288af9425ee40364 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.drivergrp.core

import java.io.File
import java.lang.management.ManagementFactory
import java.lang.reflect.Modifier

import com.drivergrp.core.logging.Logger
import com.drivergrp.core.time.{Time, TimeRange}

object stats {

  type StatsKey  = String
  type StatsKeys = Seq[StatsKey]

  trait Stats {

    def recordStats(keys: StatsKeys, interval: TimeRange, value: BigDecimal): Unit

    def recordStats(keys: StatsKeys, interval: TimeRange, value: Int): Unit =
      recordStats(keys, interval, BigDecimal(value))

    def recordStats(key: StatsKey, interval: TimeRange, value: BigDecimal): Unit =
      recordStats(Vector(key), interval, value)

    def recordStats(key: StatsKey, interval: TimeRange, value: Int): Unit =
      recordStats(Vector(key), interval, BigDecimal(value))

    def recordStats(keys: StatsKeys, time: Time, value: BigDecimal): Unit =
      recordStats(keys, TimeRange(time, time), value)

    def recordStats(keys: StatsKeys, time: Time, value: Int): Unit =
      recordStats(keys, TimeRange(time, time), BigDecimal(value))

    def recordStats(key: StatsKey, time: Time, value: BigDecimal): Unit =
      recordStats(Vector(key), TimeRange(time, time), value)

    def recordStats(key: StatsKey, time: Time, value: Int): Unit =
      recordStats(Vector(key), TimeRange(time, time), BigDecimal(value))
  }

  class LogStats(log: Logger) extends Stats {
    def recordStats(keys: StatsKeys, interval: TimeRange, value: BigDecimal): Unit = {
      val valueString = value.bigDecimal.toPlainString
      log.audit(s"${keys.mkString(".")}(${interval.start.millis}-${interval.end.millis})=$valueString")
    }
  }

  final case class MemoryStats(free: Long, total: Long, max: Long)

  final case class GarbageCollectorStats(totalGarbageCollections: Long, garbageCollectionTime: Long)

  final case class FileRootSpace(path: String, totalSpace: Long, freeSpace: Long, usableSpace: Long)

  object SystemStats {

    def memoryUsage: MemoryStats = {
      val runtime = Runtime.getRuntime
      MemoryStats(runtime.freeMemory, runtime.totalMemory, runtime.maxMemory)
    }

    def availableProcessors: Int = {
      Runtime.getRuntime.availableProcessors()
    }

    def garbageCollectorStats: GarbageCollectorStats = {
      import scala.collection.JavaConverters._

      val (totalGarbageCollections, garbageCollectionTime) =
        ManagementFactory.getGarbageCollectorMXBeans.asScala.foldLeft(0L -> 0L) {
          case ((total, collectionTime), gc) =>
            (total + math.max(0L, gc.getCollectionCount)) -> (collectionTime + math.max(0L, gc.getCollectionTime))
        }

      GarbageCollectorStats(totalGarbageCollections, garbageCollectionTime)
    }

    def fileSystemSpace: Array[FileRootSpace] = {
      File.listRoots() map { root =>
        FileRootSpace(root.getAbsolutePath, root.getTotalSpace, root.getFreeSpace, root.getUsableSpace)
      }
    }

    def operatingSystemStats: Map[String, String] = {
      val operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean
      operatingSystemMXBean.getClass.getDeclaredMethods
        .map(method => { method.setAccessible(true); method })
        .filter(method => method.getName.startsWith("get") && Modifier.isPublic(method.getModifiers))
        .map { method =>
          try {
            method.getName -> String.valueOf(method.invoke(operatingSystemMXBean))
          } catch {
            case t: Throwable => method.getName -> t.getMessage
        }
      } toMap
    }
  }
}