From 081f26cb20c2f64c02a993a23f01fcc18b43158e Mon Sep 17 00:00:00 2001 From: Chris Rowe Date: Tue, 8 Oct 2019 14:23:01 +0100 Subject: [PATCH] API-3951: Added count of applications subscribed to APIs --- .../metrics/MetricsModule.scala | 3 +- .../metrics/SubscriptionMetrics.scala | 39 ++++++++++++ .../metrics/SubscriptionMetricsSpec.scala | 61 +++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 app/uk/gov/hmrc/thirdpartyapplication/metrics/SubscriptionMetrics.scala create mode 100644 test/unit/uk/gov/hmrc/thirdpartyapplication/metrics/SubscriptionMetricsSpec.scala diff --git a/app/uk/gov/hmrc/thirdpartyapplication/metrics/MetricsModule.scala b/app/uk/gov/hmrc/thirdpartyapplication/metrics/MetricsModule.scala index b456da8db..9478d41bc 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/metrics/MetricsModule.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/metrics/MetricsModule.scala @@ -88,8 +88,9 @@ class MetricsOrchestratorProvider @Inject()(configuration: Configuration, @Singleton class MetricsSourcesProvider @Inject()(applicationCount: ApplicationCount, rateLimitMetrics: RateLimitMetrics, + subscriptionMetrics: SubscriptionMetrics, missingMongoFields: MissingMongoFields) extends Provider[MetricsSources] { - override def get(): MetricsSources = MetricsSources(applicationCount, rateLimitMetrics, missingMongoFields) + override def get(): MetricsSources = MetricsSources(applicationCount, rateLimitMetrics, subscriptionMetrics, missingMongoFields) } case class MetricsSources(metricSources: MetricSource*) { diff --git a/app/uk/gov/hmrc/thirdpartyapplication/metrics/SubscriptionMetrics.scala b/app/uk/gov/hmrc/thirdpartyapplication/metrics/SubscriptionMetrics.scala new file mode 100644 index 000000000..e8cdc709b --- /dev/null +++ b/app/uk/gov/hmrc/thirdpartyapplication/metrics/SubscriptionMetrics.scala @@ -0,0 +1,39 @@ +/* + * Copyright 2019 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 uk.gov.hmrc.thirdpartyapplication.metrics + +import javax.inject.Inject +import uk.gov.hmrc.metrix.domain.MetricSource +import uk.gov.hmrc.thirdpartyapplication.models.APIIdentifier +import uk.gov.hmrc.thirdpartyapplication.repository.SubscriptionRepository + +import scala.concurrent.{ExecutionContext, Future} + +class SubscriptionMetrics @Inject()(val subscriptionRepository: SubscriptionRepository) extends MetricSource { + override def metrics(implicit ec: ExecutionContext): Future[Map[String, Int]] = { + def subscriptionCountKey(apiName: String): String = s"subscriptionCount.$apiName" + + numberOfSubscriptionsByApi.map(subscriptionCounts => subscriptionCounts.map(count => subscriptionCountKey(count._1) -> count._2)) + } + + def numberOfSubscriptionsByApi(implicit ec: ExecutionContext): Future[Map[String, Int]] = { + def apiName(apiIdentifier: APIIdentifier): String = s"${apiIdentifier.context}--${apiIdentifier.version}" + + subscriptionRepository.findAll() + .map(subscriptions => subscriptions.map(subscription => apiName(subscription.apiIdentifier) -> subscription.applications.size).toMap) + } +} diff --git a/test/unit/uk/gov/hmrc/thirdpartyapplication/metrics/SubscriptionMetricsSpec.scala b/test/unit/uk/gov/hmrc/thirdpartyapplication/metrics/SubscriptionMetricsSpec.scala new file mode 100644 index 000000000..d59bd178e --- /dev/null +++ b/test/unit/uk/gov/hmrc/thirdpartyapplication/metrics/SubscriptionMetricsSpec.scala @@ -0,0 +1,61 @@ +/* + * Copyright 2019 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 unit.uk.gov.hmrc.thirdpartyapplication.metrics + +import java.util.UUID + +import org.mockito.Mockito.when +import org.scalatest.mockito.MockitoSugar +import uk.gov.hmrc.play.test.UnitSpec +import uk.gov.hmrc.thirdpartyapplication.metrics.SubscriptionMetrics +import uk.gov.hmrc.thirdpartyapplication.models.{APIIdentifier, SubscriptionData} +import uk.gov.hmrc.thirdpartyapplication.repository.SubscriptionRepository + +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.Future + +class SubscriptionMetricsSpec extends UnitSpec with MockitoSugar { + + trait Setup { + val mockSubscriptionsRepository: SubscriptionRepository = mock[SubscriptionRepository] + + val metricUnderTest: SubscriptionMetrics = new SubscriptionMetrics(mockSubscriptionsRepository) + } + + "metrics refresh" should { + def subscriptionDetails(subscription: (String, String, Int)): SubscriptionData = + SubscriptionData(new APIIdentifier(subscription._1, subscription._2), Seq.fill(subscription._3)(UUID.randomUUID()).toSet) + + def expectedAPIName(subscription: (String, String, Int)): String = s"subscriptionCount.${subscription._1}--${subscription._2}" + + "update subscription counts" in new Setup { + private val api1v1 = ("apiOne", "1.0", 5) + private val api1v2 = ("apiOne", "2.0", 10) + private val api2 = ("apiTwo", "1.0", 100) + + when(mockSubscriptionsRepository.findAll()) + .thenReturn(Future.successful( + List(subscriptionDetails(api1v1), subscriptionDetails(api1v2), subscriptionDetails(api2)))) + + private val result = await(metricUnderTest.metrics) + + result(expectedAPIName(api1v1)) shouldBe api1v1._3 + result(expectedAPIName(api1v2)) shouldBe api1v2._3 + result(expectedAPIName(api2)) shouldBe api2._3 + } + } +}