Skip to content

Commit

Permalink
[PS] IndexedMetaData will now only be resolved if config specifies us…
Browse files Browse the repository at this point in the history
…ing elasticsearch
  • Loading branch information
beyond-code-github committed Feb 15, 2021
1 parent 1e482a5 commit 1dc14a4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 36 deletions.
43 changes: 10 additions & 33 deletions app/Module.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,33 @@
import bfpo.BFPOFileParser
import bfpo.outmodel.BFPO
import cats.effect.IO
import com.google.inject.{AbstractModule, Provides, TypeLiteral}

import javax.inject.Singleton
import com.google.inject.{AbstractModule, Provides}
import com.kenshoo.play.metrics.Metrics
import config.ConfigHelper
import doobie.Transactor
import javax.inject.Singleton
import osgb.services._
import play.api.inject.ApplicationLifecycle
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.{Configuration, Environment}
import repositories.{AddressLookupRepository, RdsQueryConfig, TransactorProvider}
import uk.gov.hmrc.address.services.es.{ESAdminImpl, ElasticSettings, ElasticsearchHelper, IndexMetadata}
import uk.gov.hmrc.address.services.es.{ElasticSettings, IndexMetadata}
import uk.gov.hmrc.logging.{LoggerFacade, SimpleLogger}

import scala.concurrent.ExecutionContext

class Module(environment: Environment,
configuration: Configuration) extends AbstractModule {

private lazy val numShards = configuration.getConfig("elastic.shards").map(
_.entrySet.foldLeft(Map.empty[String, Int])((m, a) => m + (a._1 -> a._2.unwrapped().asInstanceOf[Int]))
).getOrElse(Map.empty[String, Int])
class Module(environment: Environment, configuration: Configuration) extends AbstractModule {

def configure(): Unit = {
bind(classOf[ElasticSettingsProvider])
bind(classOf[IndexedMetadataProvider])
bind(classOf[CannedData]).to(classOf[CannedDataImpl]).asEagerSingleton()
}

@Provides
@Singleton
def provideLogger: SimpleLogger = new LoggerFacade(play.api.Logger.logger)

@Provides
@Singleton
def provideElasticSettings(configHelper: ConfigHelper): ElasticSettings = {
val localMode = configHelper.getConfigString("elastic.localMode").exists(_.toBoolean)
val homeDir = configHelper.getConfigString("elastic.homeDir")
val preDelete = configHelper.getConfigString("elastic.preDelete").exists(_.toBoolean)
val clusterName = configHelper.mustGetConfigString("elastic.clusterName")
val connectionString = configHelper.mustGetConfigString("elastic.uri")
val isCluster = configHelper.getConfigString("elastic.isCluster").exists(_.toBoolean)
ElasticSettings(localMode, homeDir, preDelete, connectionString, isCluster, clusterName, numShards)
}

@Provides
@Singleton
def provideRdsQueryConfig(configHelper: ConfigHelper): RdsQueryConfig = {
Expand All @@ -71,15 +55,6 @@ class Module(environment: Environment,
RdsQueryConfig(queryTimeoutMillis, queryResultsLimit)
}

@Provides
@Singleton
def provideIndexMetadata(configHelper: ConfigHelper, logger: SimpleLogger, ec: ExecutionContext,
settings: ElasticSettings): IndexMetadata = {
val clients = ElasticsearchHelper.buildClients(settings, new LoggerFacade(play.api.Logger.logger))
val esImpl = new ESAdminImpl(clients, logger, ec, settings)
new IndexMetadata(esImpl, settings.isCluster, numShards, logger, ec)
}

@Provides
@Singleton
def provideTransactorOptional(configHelper: ConfigHelper, configuration: Configuration,
Expand All @@ -92,18 +67,20 @@ class Module(environment: Environment,

@Provides
@Singleton
def provideAddressSearcher(indexMetadata: IndexMetadata, metrics: Metrics, configuration: Configuration,
def provideAddressSearcher(indexMetadataProvider: IndexedMetadataProvider, metrics: Metrics, configuration: Configuration,
configHelper: ConfigHelper, rdsQueryConfig: RdsQueryConfig, executionContext: ExecutionContext,
settings: ElasticSettings, applicationLifecycle: ApplicationLifecycle,
settingsProvider: ElasticSettingsProvider, applicationLifecycle: ApplicationLifecycle,
logger: SimpleLogger): AddressSearcher = {
val dbEnabled = isDbEnabled(configHelper)

val searcher = if (dbEnabled) {
val transactor = new TransactorProvider(configuration, applicationLifecycle).get(executionContext)
new AddressLookupRepository(transactor, rdsQueryConfig)
} else {
val indexMetadata = indexMetadataProvider.indexedMetaData
val indexName: String = configHelper.getConfigString("elastic.indexName").getOrElse(IndexMetadata.ariAliasName)

val settings = settingsProvider.elasticSettings
new AddressESSearcher(indexMetadata.clients.head, indexName, "GB", defaultContext, settings, logger)
}

Expand Down
9 changes: 6 additions & 3 deletions app/osgb/services/CannedData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import scala.io.Source

trait CannedData

class CannedDataImpl @Inject()(indexMetadata: IndexMetadata, logger: SimpleLogger, env: Environment, configHelper: ConfigHelper, ec: ExecutionContext) extends CannedData {
class CannedDataImpl @Inject()(indexMetadataProvider: IndexedMetadataProvider, logger: SimpleLogger, env: Environment,
configHelper: ConfigHelper, ec: ExecutionContext) extends CannedData {
val cannedData = "conf/data/testaddresses.csv"

def upload(): String = {
Expand All @@ -43,9 +44,11 @@ class CannedDataImpl @Inject()(indexMetadata: IndexMetadata, logger: SimpleLogge
}

def uploadToElasticSearch(file: File): String = {
val indexedMetadata = indexMetadataProvider.indexedMetaData

val indexName = IndexName("test", Some(1), Some(IndexName.newTimestamp))
val noProvenance = BuildProvenance(None, None)
val out = new OutputESWriter(indexName, logger, indexMetadata, WriterSettings.default, ec, noProvenance)
val out = new OutputESWriter(indexName, logger, indexedMetadata, WriterSettings.default, ec, noProvenance)

out.begin()

Expand All @@ -60,7 +63,7 @@ class CannedDataImpl @Inject()(indexMetadata: IndexMetadata, logger: SimpleLogge
}
out.end(true)

indexMetadata.setIndexInUse(indexName)
indexedMetadata.setIndexInUse(indexName)

val status =
s"""
Expand Down
39 changes: 39 additions & 0 deletions app/osgb/services/ElasticSettingsProvider.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2021 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 osgb.services

import config.ConfigHelper
import javax.inject.{Inject, Singleton}
import play.api.Configuration
import uk.gov.hmrc.address.services.es.ElasticSettings

@Singleton
class ElasticSettingsProvider @Inject() (configuration: Configuration, configHelper: ConfigHelper) {
lazy val numShards: Map[String, Int] = configuration.getConfig("elastic.shards").map(
_.entrySet.foldLeft(Map.empty[String, Int])((m, a) => m + (a._1 -> a._2.unwrapped().asInstanceOf[Int]))
).getOrElse(Map.empty[String, Int])

lazy val elasticSettings: ElasticSettings = {
val localMode = configHelper.getConfigString("elastic.localMode").exists(_.toBoolean)
val homeDir = configHelper.getConfigString("elastic.homeDir")
val preDelete = configHelper.getConfigString("elastic.preDelete").exists(_.toBoolean)
val clusterName = configHelper.mustGetConfigString("elastic.clusterName")
val connectionString = configHelper.mustGetConfigString("elastic.uri")
val isCluster = configHelper.getConfigString("elastic.isCluster").exists(_.toBoolean)
ElasticSettings(localMode, homeDir, preDelete, connectionString, isCluster, clusterName, numShards)
}
}
37 changes: 37 additions & 0 deletions app/osgb/services/IndexedMetadataProvider.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2021 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 osgb.services

import javax.inject.{Inject, Singleton}
import uk.gov.hmrc.address.services.es.{ESAdminImpl, ElasticsearchHelper, IndexMetadata}
import uk.gov.hmrc.logging.{LoggerFacade, SimpleLogger}

import scala.concurrent.ExecutionContext

@Singleton
class IndexedMetadataProvider @Inject()(settingsProvider: ElasticSettingsProvider, logger: SimpleLogger,
ec: ExecutionContext) {

lazy val indexedMetaData: IndexMetadata = {
val numShards = settingsProvider.numShards
val settings = settingsProvider.elasticSettings

val clients = ElasticsearchHelper.buildClients(settings, new LoggerFacade(play.api.Logger.logger))
val esImpl = new ESAdminImpl(clients, logger, ec, settings)
new IndexMetadata(esImpl, settings.isCluster, numShards, logger, ec)
}
}

0 comments on commit 1dc14a4

Please sign in to comment.