Skip to content

Commit

Permalink
Merge pull request #68 from hmrc/API-3951
Browse files Browse the repository at this point in the history
API-3951: Added count of applications subscribed to APIs
  • Loading branch information
lburgos authored Oct 8, 2019
2 parents e0a6786 + 081f26c commit 915027b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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*) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 915027b

Please sign in to comment.