<commit_before>package oxalis.user

import akka.actor._
import scala.concurrent.duration._

import models.user.{User, UserService}
import akka.agent.Agent
import com.newrelic.api.agent.NewRelic
import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits._
import reactivemongo.bson.BSONObjectID

case object FlushActivities

case class UserActivity(user: User, time: Long)

class ActivityMonitor extends Actor {
  implicit val system = context.system

  val collectedActivities = Agent(Map[BSONObjectID, Long]().empty)

  val updateCycle = 5 minutes

  override def preStart = {
    context.system.scheduler.schedule(updateCycle, updateCycle, self, FlushActivities)
  }

  def receive = {
    case UserActivity(user, time) =>
      collectedActivities.send(_.updated(user._id, time))

    case FlushActivities =>
      Logger.info("Flushing user activities.")

      collectedActivities.send {
        activities =>
          NewRelic.recordMetric("active-users", activities.size)
          activities.map{
            case (_user, time) =>
              Logger.debug(s"Flushing user activities of: ${_user.stringify} Time: $time")
              UserService.logActivity(_user, time)
          }
          Map[BSONObjectID, Long]().empty
      }
  }<commit_msg>changed metric name<commit_after>package oxalis.user

import akka.actor._
import scala.concurrent.duration._

import models.user.{User, UserService}
import akka.agent.Agent
import com.newrelic.api.agent.NewRelic
import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits._
import reactivemongo.bson.BSONObjectID

case object FlushActivities

case class UserActivity(user: User, time: Long)

class ActivityMonitor extends Actor {
  implicit val system = context.system

  val collectedActivities = Agent(Map[BSONObjectID, Long]().empty)

  val updateCycle = 5 minutes

  override def preStart = {
    context.system.scheduler.schedule(updateCycle, updateCycle, self, FlushActivities)
  }

  def receive = {
    case UserActivity(user, time) =>
      collectedActivities.send(_.updated(user._id, time))

    case FlushActivities =>
      Logger.info("Flushing user activities.")

      collectedActivities.send {
        activities =>
          NewRelic.recordMetric("Custom/ActivityMonitor/active-users", activities.size)
          activities.map{
            case (_user, time) =>
              Logger.debug(s"Flushing user activities of: ${_user.stringify} Time: $time")
              UserService.logActivity(_user, time)
          }
          Map[BSONObjectID, Long]().empty
      }
  }
{'fraction_non_alphanumeric': 0.09848778596355176, 'fraction_numerical': 0.0007754943776657619, 'mean_word_length': 3.41025641025641, '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': '5269945', 'n_tokens_mistral': 758, 'n_tokens_neox': 746, 'n_words': 166}