<commit_before>package registration.services

import com.amazonaws.services.cloudwatch.model.StandardUnit
import error.NotificationsError
import metrics.{MetricDataPoint, Metrics}
import models.Provider.{Azure, FCM}
import models._
import registration.services.fcm.FcmRegistrar

import scala.concurrent.ExecutionContext

class MigratingRegistrarProvider(
  azureRegistrarProvider: RegistrarProvider,
  fcmRegistrar: NotificationRegistrar,
  metrics: Metrics
)(implicit executionContext: ExecutionContext) extends RegistrarProvider {
  override def registrarFor(platform: Platform, deviceToken: DeviceToken, currentProvider: Option[Provider]): Either[NotificationsError, NotificationRegistrar] = deviceToken match {
    case AzureToken(_) =>
      metrics.send(MetricDataPoint(name = "RegistrationAzure", value = 1d, unit = StandardUnit.Count))
      azureRegistrarProvider.registrarFor(platform, deviceToken, currentProvider)
    case FcmToken(_) =>
      metrics.send(MetricDataPoint(name = "RegistrationFcm", value = 1d, unit = StandardUnit.Count))
      Right(fcmRegistrar)
    case BothTokens(_, _) if platform == Android =>
      metrics.send(MetricDataPoint(name = "RegistrationBoth", value = 1d, unit = StandardUnit.Count))
      androidMigration(platform, deviceToken, currentProvider)
    case BothTokens(_, _) if platform == iOS =>
      metrics.send(MetricDataPoint(name = "RegistrationBoth", value = 1d, unit = StandardUnit.Count))
      iosMigration(platform, deviceToken, currentProvider)
  }

  private def androidMigration(platform: Platform, deviceToken: DeviceToken, currentProvider: Option[Provider]): Either[NotificationsError, NotificationRegistrar] = {
    azureRegistrarProvider
      .registrarFor(platform, deviceToken, Some(Azure))
      .map(azureRegistrar => new MigratingRegistrar(
        providerIdentifier = "AzureToFirebaseRegistrar",
        fromRegistrar = azureRegistrar,
        toRegistrar = fcmRegistrar
      ))
  }

  private def iosMigration(platform: Platform, deviceToken: DeviceToken, currentProvider: Option[Provider]): Either[NotificationsError, NotificationRegistrar] = {
    currentProvider match {
      case Some(FCM) =>
        azureRegistrarProvider
          .registrarFor(platform, deviceToken, Some(Azure))
          .map(azureRegistrar => new MigratingRegistrar(
            providerIdentifier = "FirebaseToAzureRegistrar",
            fromRegistrar = fcmRegistrar,
            toRegistrar = azureRegistrar
          ))
      case _ => azureRegistrarProvider.registrarFor(platform, deviceToken, Some(Azure))
    }
  }

  override def registrarFor(registration: Registration): Either[NotificationsError, NotificationRegistrar] =
    registrarFor(registration.platform, registration.deviceToken, registration.provider)

  override def withAllRegistrars[T](fn: NotificationRegistrar => T): List[T] =
    azureRegistrarProvider.withAllRegistrars(fn)
}<commit_msg>Add metrics to follow the migration<commit_after>package registration.services

import com.amazonaws.services.cloudwatch.model.StandardUnit
import error.NotificationsError
import metrics.{MetricDataPoint, Metrics}
import models.Provider.{Azure, FCM}
import models._
import registration.services.fcm.FcmRegistrar

import scala.concurrent.ExecutionContext

class MigratingRegistrarProvider(
  azureRegistrarProvider: RegistrarProvider,
  fcmRegistrar: NotificationRegistrar,
  metrics: Metrics
)(implicit executionContext: ExecutionContext) extends RegistrarProvider {
  override def registrarFor(platform: Platform, deviceToken: DeviceToken, currentProvider: Option[Provider]): Either[NotificationsError, NotificationRegistrar] = deviceToken match {
    case AzureToken(_) =>
      metrics.send(MetricDataPoint(name = "RegistrationAzure", value = 1d, unit = StandardUnit.Count))
      azureRegistrarProvider.registrarFor(platform, deviceToken, currentProvider)
    case FcmToken(_) =>
      metrics.send(MetricDataPoint(name = "RegistrationFcm", value = 1d, unit = StandardUnit.Count))
      Right(fcmRegistrar)
    case BothTokens(_, _) if platform == Android =>
      metrics.send(MetricDataPoint(name = "RegistrationBoth", value = 1d, unit = StandardUnit.Count))
      androidMigration(platform, deviceToken, currentProvider)
    case BothTokens(_, _) if platform == iOS =>
      iosMigration(platform, deviceToken, currentProvider)
  }

  private def androidMigration(platform: Platform, deviceToken: DeviceToken, currentProvider: Option[Provider]): Either[NotificationsError, NotificationRegistrar] = {
    azureRegistrarProvider
      .registrarFor(platform, deviceToken, Some(Azure))
      .map(azureRegistrar => new MigratingRegistrar(
        providerIdentifier = "AzureToFirebaseRegistrar",
        fromRegistrar = azureRegistrar,
        toRegistrar = fcmRegistrar
      ))
  }

  private def iosMigration(platform: Platform, deviceToken: DeviceToken, currentProvider: Option[Provider]): Either[NotificationsError, NotificationRegistrar] = {
    currentProvider match {
      case Some(FCM) =>
        metrics.send(MetricDataPoint(name = "IosFcmToAzure", value = 1d, unit = StandardUnit.Count))
        azureRegistrarProvider
          .registrarFor(platform, deviceToken, Some(Azure))
          .map(azureRegistrar => new MigratingRegistrar(
            providerIdentifier = "FirebaseToAzureRegistrar",
            fromRegistrar = fcmRegistrar,
            toRegistrar = azureRegistrar
          ))
      case _ =>
        metrics.send(MetricDataPoint(name = "RegistrationAzure", value = 1d, unit = StandardUnit.Count))
        azureRegistrarProvider.registrarFor(platform, deviceToken, Some(Azure))
    }
  }

  override def registrarFor(registration: Registration): Either[NotificationsError, NotificationRegistrar] =
    registrarFor(registration.platform, registration.deviceToken, registration.provider)

  override def withAllRegistrars[T](fn: NotificationRegistrar => T): List[T] =
    azureRegistrarProvider.withAllRegistrars(fn)
}
{'fraction_non_alphanumeric': 0.08933959101575595, 'fraction_numerical': 0.0015085484411666108, 'mean_word_length': 4.9491525423728815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '5147415', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1629, 'n_words': 333}