<commit_before>package beyond.metrics

import akka.actor.Actor
import akka.actor.ActorLogging
import java.lang.management.ManagementFactory
import java.lang.management.MemoryMXBean
import java.lang.management.MemoryUsage
import java.lang.management.OperatingSystemMXBean
import javax.management.MBeanServerConnection
import javax.management.ObjectName

object SystemMetricsMonitor {
  val Name: String = "systemMetricsMonitor"

  sealed trait SystemMetricsRequest
  case object SystemLoadAverageRequest extends SystemMetricsRequest
  case object HeapMemoryUsageRequest extends SystemMetricsRequest
  case object NonHeapMemoryUsageRequest extends SystemMetricsRequest
  case object SwapMemoryUsageRequest extends SystemMetricsRequest

  sealed trait SystemMetricsReply
  case class SystemLoadAverageReply(loadAverage: Double) extends SystemMetricsReply
  case class HeapMemoryUsageReply(usage: MemoryUsage) extends SystemMetricsReply
  case class NonHeapMemoryUsageReply(usage: MemoryUsage) extends SystemMetricsReply
  case class SwapMemoryUsageReply(free: Long, total: Long, used: Long) extends SystemMetricsReply
}

class SystemMetricsMonitor extends Actor with ActorLogging with JMXConnectorMixin {
  import SystemMetricsMonitor._

  override def preStart() {
    val mbsc: MBeanServerConnection = jmxConnector.getMBeanServerConnection

    val osMXBean: OperatingSystemMXBean = ManagementFactory.newPlatformMXBeanProxy(
      mbsc, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, classOf[OperatingSystemMXBean])
    val memoryMXBean: MemoryMXBean = ManagementFactory.newPlatformMXBeanProxy(
      mbsc, ManagementFactory.MEMORY_MXBEAN_NAME, classOf[MemoryMXBean])
    val sigarSwap = new ObjectName("sigar:type=Swap")

    context.become(receiveWithMBeans(mbsc, osMXBean, memoryMXBean, sigarSwap))

    log.info("SystemMetricsMonitor started")
  }

  override def postStop() {
    super.postStop()
    log.info("SystemMetricsMonitor stopped")
  }

  override def receive: Receive = Map.empty

  private def receiveWithMBeans(mbsc: MBeanServerConnection,
    osMXBean: OperatingSystemMXBean, memoryMXBean: MemoryMXBean, sigarSwap: ObjectName): Receive = {
    case SystemLoadAverageRequest =>
      sender ! SystemLoadAverageReply(osMXBean.getSystemLoadAverage)
    case HeapMemoryUsageRequest =>
      sender ! HeapMemoryUsageReply(memoryMXBean.getHeapMemoryUsage)
    case NonHeapMemoryUsageRequest =>
      sender ! NonHeapMemoryUsageReply(memoryMXBean.getNonHeapMemoryUsage)
    case SwapMemoryUsageRequest =>
      val free = mbsc.getAttribute(sigarSwap, "Free").asInstanceOf[Long]
      val total = mbsc.getAttribute(sigarSwap, "Total").asInstanceOf[Long]
      val used = mbsc.getAttribute(sigarSwap, "Used").asInstanceOf[Long]
      sender ! SwapMemoryUsageReply(free, total, used)
  }
}
<commit_msg>Refactoring SystemMetricsMonitor.<commit_after>package beyond.metrics

import akka.actor.Actor
import akka.actor.ActorLogging
import java.lang.management.ManagementFactory
import java.lang.management.MemoryMXBean
import java.lang.management.MemoryUsage
import java.lang.management.OperatingSystemMXBean
import javax.management.MBeanServerConnection
import javax.management.ObjectName

object SystemMetricsMonitor {
  val Name: String = "systemMetricsMonitor"

  sealed trait SystemMetricsRequest
  case object SystemLoadAverageRequest extends SystemMetricsRequest
  case object HeapMemoryUsageRequest extends SystemMetricsRequest
  case object NonHeapMemoryUsageRequest extends SystemMetricsRequest
  case object SwapMemoryUsageRequest extends SystemMetricsRequest

  sealed trait SystemMetricsReply
  case class SystemLoadAverageReply(loadAverage: Double) extends SystemMetricsReply
  case class HeapMemoryUsageReply(usage: MemoryUsage) extends SystemMetricsReply
  case class NonHeapMemoryUsageReply(usage: MemoryUsage) extends SystemMetricsReply
  case class SwapMemoryUsageReply(free: Long, total: Long, used: Long) extends SystemMetricsReply
}

class SystemMetricsMonitor extends Actor with ActorLogging with JMXConnectorMixin {
  import SystemMetricsMonitor._

  private val mbsc: MBeanServerConnection = jmxConnector.getMBeanServerConnection
  private val osMXBean: OperatingSystemMXBean = ManagementFactory.newPlatformMXBeanProxy(
    mbsc, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, classOf[OperatingSystemMXBean])
  private val memoryMXBean: MemoryMXBean = ManagementFactory.newPlatformMXBeanProxy(
    mbsc, ManagementFactory.MEMORY_MXBEAN_NAME, classOf[MemoryMXBean])

  override def preStart() {
    log.info("SystemMetricsMonitor started")
  }

  override def postStop() {
    super.postStop()
    log.info("SystemMetricsMonitor stopped")
  }

  override def receive: Receive = {
    case SystemLoadAverageRequest =>
      sender ! SystemLoadAverageReply(osMXBean.getSystemLoadAverage)
    case HeapMemoryUsageRequest =>
      sender ! HeapMemoryUsageReply(memoryMXBean.getHeapMemoryUsage)
    case NonHeapMemoryUsageRequest =>
      sender ! NonHeapMemoryUsageReply(memoryMXBean.getNonHeapMemoryUsage)
    case SwapMemoryUsageRequest =>
      val sigarSwap = new ObjectName("sigar:type=Swap")
      val free = mbsc.getAttribute(sigarSwap, "Free").asInstanceOf[Long]
      val total = mbsc.getAttribute(sigarSwap, "Total").asInstanceOf[Long]
      val used = mbsc.getAttribute(sigarSwap, "Used").asInstanceOf[Long]
      sender ! SwapMemoryUsageReply(free, total, used)
  }
}

{'fraction_non_alphanumeric': 0.06270810210876804, 'fraction_numerical': 0.0, 'mean_word_length': 6.133245382585752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1075441', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1448, 'n_words': 330}