<commit_before>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package services

import com.codahale.metrics.MetricRegistry
import com.kenshoo.play.metrics.Metrics
import engine.AuditInfo
import model.Location
import play.api.Play
import play.api.mvc.{AnyContent, Request}
import uk.gov.hmrc.play.http.HeaderCarrier
import uk.gov.hmrc.play.http.logging.MdcLoggingExecutionContext.fromLoggingDetails

import scala.concurrent.Future

object MetricsMonitoringService extends MetricsMonitoringService {
  override val metricsRegistry = Play.current.injector.instanceOf[Metrics].defaultRegistry
}

trait MetricsMonitoringService {

  val metricsRegistry: MetricRegistry

  def sendMonitoringEvents(auditInfo: AuditInfo, throttledLocation: Location)(implicit request: Request[AnyContent], hc: HeaderCarrier): Future[Unit] = {

    Future {
      val destinationNameBeforeThrottling: Option[String] = auditInfo.throttlingInfo.map(_.initialDestination.name)
      val destinationNameAfterThrottling: String = throttledLocation.name
      val throttleKey: String = if (destinationNameBeforeThrottling.isDefined && destinationNameBeforeThrottling.get != destinationNameAfterThrottling) {
        s".throttled-from-${destinationNameBeforeThrottling.get}"
      } else ".not-throttled"

      auditInfo.ruleApplied.foreach { ruleApplied =>
        metricsRegistry.meter(s"routed.to-${throttledLocation.name}.because-$ruleApplied$throttleKey").mark()
      }

      val trueConditions = auditInfo.routingReasons.filter { case (_, Some(v)) => v }.keys
      trueConditions.foreach(reason => metricsRegistry.meter(reason.key).mark())

      val falseConditions = auditInfo.routingReasons.filter { case (_, Some(v)) => !v }.keys
      falseConditions.foreach(reason => metricsRegistry.meter(s"not-${reason.key}").mark())
    }
  }

}<commit_msg>TSV-500: Improved maintainability and readability by refactoring sendMonitoringEvents to avoid the use of .get on an option. DC/CE<commit_after>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package services

import com.codahale.metrics.MetricRegistry
import com.kenshoo.play.metrics.Metrics
import engine.AuditInfo
import model.Location
import play.api.Play
import play.api.mvc.{AnyContent, Request}
import uk.gov.hmrc.play.http.HeaderCarrier
import uk.gov.hmrc.play.http.logging.MdcLoggingExecutionContext.fromLoggingDetails

import scala.concurrent.Future

object MetricsMonitoringService extends MetricsMonitoringService {
  override val metricsRegistry = Play.current.injector.instanceOf[Metrics].defaultRegistry
}

trait MetricsMonitoringService {

  val metricsRegistry: MetricRegistry

  def sendMonitoringEvents(auditInfo: AuditInfo, throttledLocation: Location)(implicit request: Request[AnyContent], hc: HeaderCarrier): Future[Unit] = {

    Future {
      val destinationNameBeforeThrottling = auditInfo.throttlingInfo.map(_.initialDestination.name)

      val throttleKey = destinationNameBeforeThrottling match {
        case Some(name) if name != throttledLocation.name => s".throttled-from-$name"
        case _ => ".not-throttled"
      }

      auditInfo.ruleApplied.foreach { ruleApplied =>
        metricsRegistry.meter(s"routed.to-${throttledLocation.name}.because-$ruleApplied$throttleKey").mark()
      }

      val trueConditions = auditInfo.routingReasons.filter { case (_, Some(v)) => v }.keys
      trueConditions.foreach(reason => metricsRegistry.meter(reason.key).mark())

      val falseConditions = auditInfo.routingReasons.filter { case (_, Some(v)) => !v }.keys
      falseConditions.foreach(reason => metricsRegistry.meter(s"not-${reason.key}").mark())
    }
  }

}
{'fraction_non_alphanumeric': 0.09487666034155598, 'fraction_numerical': 0.004005903436643475, 'mean_word_length': 4.555035128805621, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '122225', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1316, 'n_words': 350}