diff --git a/platform-api/analytics-api-core/src/main/scala/org/ekstep/analytics/api/service/DeviceProfileService.scala b/platform-api/analytics-api-core/src/main/scala/org/ekstep/analytics/api/service/DeviceProfileService.scala index bf83a3a3b..9b25e5316 100644 --- a/platform-api/analytics-api-core/src/main/scala/org/ekstep/analytics/api/service/DeviceProfileService.scala +++ b/platform-api/analytics-api-core/src/main/scala/org/ekstep/analytics/api/service/DeviceProfileService.scala @@ -5,7 +5,6 @@ import akka.pattern.pipe import com.google.common.net.InetAddresses import com.google.common.primitives.UnsignedInts import com.typesafe.config.Config -import org.apache.logging.log4j.LogManager import org.ekstep.analytics.api.util.{APILogger, DeviceStateDistrict, H2DBUtil, RedisUtil} import redis.clients.jedis.Jedis import redis.clients.jedis.exceptions.JedisConnectionException @@ -23,12 +22,11 @@ class DeviceProfileService(saveMetricsActor: ActorRef, config: Config, redisUtil val geoLocationCityTableName: String = config.getString("postgres.table.geo_location_city.name") val geoLocationCityIpv4TableName: String = config.getString("postgres.table.geo_location_city_ipv4.name") implicit val jedisConnection: Jedis = redisUtil.getConnection(deviceDatabaseIndex) - private val logger = LogManager.getLogger("device-logger") + private val enableDebugLogging = config.getBoolean("device.api.enable.debug.log") def receive = { case deviceProfile: DeviceProfileRequest => try { - logger.info("DeviceProfile API Updater for device id " + deviceProfile.did) val senderActor = sender() val result = getDeviceProfile(deviceProfile) result.pipeTo(senderActor) @@ -54,21 +52,30 @@ class DeviceProfileService(saveMetricsActor: ActorRef, config: Config, redisUtil if (deviceProfileRequest.headerIP.nonEmpty) { val ipLocationFromH2 = resolveLocationFromH2(deviceProfileRequest.headerIP) + val did = deviceProfileRequest.did // logging resolved location details - if (ipLocationFromH2.state.nonEmpty && ipLocationFromH2.districtCustom.nonEmpty) { - println(s"For IP: ${deviceProfileRequest.headerIP}, Location resolved for ${deviceProfileRequest.did} to state: ${ipLocationFromH2.state}, district: ${ipLocationFromH2.districtCustom}") - APILogger.log("", Option(Map("comments" -> s"Location resolved for ${deviceProfileRequest.did} to state: ${ipLocationFromH2.state}, district: ${ipLocationFromH2.districtCustom}")), "getDeviceProfile") + if (ipLocationFromH2.state.nonEmpty) { + if (enableDebugLogging) { + println(s"For IP: ${deviceProfileRequest.headerIP}, Location resolved for $did to state: ${ipLocationFromH2.state}, district: ${ipLocationFromH2.districtCustom}") + } + APILogger.log("", Option(Map("comments" -> s"Location resolved for $did to state: ${ipLocationFromH2.state}, district: ${ipLocationFromH2.districtCustom}")), "getDeviceProfile") } else { - println(s"For IP: ${deviceProfileRequest.headerIP}, Location is not resolved for ${deviceProfileRequest.did}") - APILogger.log("", Option(Map("comments" -> s"Location is not resolved for ${deviceProfileRequest.did}")), "getDeviceProfile") + if (enableDebugLogging) { + println(s"For IP: ${deviceProfileRequest.headerIP}, Location is not resolved for $did") + } + APILogger.log("", Option(Map("comments" -> s"Location is not resolved for $did")), "getDeviceProfile") } - val deviceLocation = redisUtil.getAllByKey(deviceProfileRequest.did) + val deviceLocation = redisUtil.getAllByKey(did) val userDeclaredLoc = if (deviceLocation.nonEmpty && deviceLocation.get.getOrElse("user_declared_state", "").nonEmpty) { Option(Location(deviceLocation.get("user_declared_state"), deviceLocation.get("user_declared_district"))) } else None + if (enableDebugLogging) { + userDeclaredLoc.foreach { declaredLocation => println(s"[did: $did, user_declared_state: ${declaredLocation.state}, user_declared_district: ${declaredLocation.district}") } + } + Future(Some(DeviceProfile(userDeclaredLoc, Option(Location(ipLocationFromH2.state, ipLocationFromH2.districtCustom))))) } else { Future(None) diff --git a/platform-api/analytics-api-core/src/main/scala/org/ekstep/analytics/api/service/DeviceRegisterService.scala b/platform-api/analytics-api-core/src/main/scala/org/ekstep/analytics/api/service/DeviceRegisterService.scala index 535dd8772..c515c7847 100644 --- a/platform-api/analytics-api-core/src/main/scala/org/ekstep/analytics/api/service/DeviceRegisterService.scala +++ b/platform-api/analytics-api-core/src/main/scala/org/ekstep/analytics/api/service/DeviceRegisterService.scala @@ -11,6 +11,7 @@ import is.tagomor.woothee.Classifier import org.apache.logging.log4j.LogManager import org.postgresql.util.PSQLException import redis.clients.jedis.Jedis +import redis.clients.jedis.exceptions.JedisConnectionException import scala.concurrent.ExecutionContext @@ -29,6 +30,7 @@ class DeviceRegisterService(saveMetricsActor: ActorRef, config: Config, redisUti val deviceDatabaseIndex: Int = config.getInt("redis.deviceIndex") implicit val jedisConnection: Jedis = redisUtil.getConnection(deviceDatabaseIndex) private val logger = LogManager.getLogger("device-logger") + private val enableDebugLogging = config.getBoolean("device.api.enable.debug.log") def receive = { case deviceRegDetails: RegisterDevice => @@ -44,6 +46,20 @@ class DeviceRegisterService(saveMetricsActor: ActorRef, config: Config, redisUti "params" -> List(Map("status" -> 500, "method" -> "POST", "rid" -> "registerDevice", "title" -> "registerDevice")), "data" -> errorMessage)), "registerDevice") + case ex: JedisConnectionException => + ex.printStackTrace() + val errorMessage = "DeviceRegisterAPI failed due to " + ex.getMessage + APILogger.log("", Option(Map("type" -> "api_access", + "params" -> List(Map("status" -> 500, "method" -> "POST", + "rid" -> "registerDevice", "title" -> "registerDevice")), "data" -> errorMessage)), + "registerDevice") + case ex: Exception => + ex.printStackTrace() + val errorMessage = "DeviceRegisterAPI failed due to " + ex.getMessage + APILogger.log("", Option(Map("type" -> "api_access", + "params" -> List(Map("status" -> 500, "method" -> "POST", + "rid" -> "registerDevice", "title" -> "registerDevice")), "data" -> errorMessage)), + "registerDevice") } } @@ -55,9 +71,15 @@ class DeviceRegisterService(saveMetricsActor: ActorRef, config: Config, redisUti // logging metrics if(isLocationResolved(location)) { + if (enableDebugLogging) { + println(s"DeviceRegisterService.registerDevice: Location resolved - { did: ${registrationDetails.did}, ip_address: $validIp, state: ${location.state}, city: ${location.city}, district: ${location.districtCustom} }") + } APILogger.log("", Option(Map("comments" -> s"Location resolved for ${registrationDetails.did} to state: ${location.state}, city: ${location.city}, district: ${location.districtCustom}")), "registerDevice") metricsActor.tell(IncrementLocationDbSuccessCount, ActorRef.noSender) } else { + if (enableDebugLogging) { + println(s"DeviceRegisterService.registerDevice: Location not resolved - { did: ${registrationDetails.did}, ip_address: $validIp }") + } APILogger.log("", Option(Map("comments" -> s"Location is not resolved for ${registrationDetails.did}")), "registerDevice") metricsActor.tell(IncrementLocationDbMissCount, ActorRef.noSender) } @@ -70,6 +92,9 @@ class DeviceRegisterService(saveMetricsActor: ActorRef, config: Config, redisUti // Add device profile to redis cache val deviceProfileMap = getDeviceProfileMap(registrationDetails, location) redisUtil.hmset(registrationDetails.did, deviceProfileMap) + if (enableDebugLogging) { + println(s"Redis-cache updated for did: ${registrationDetails.did}") + } APILogger.log(s"Redis-cache updated for did: ${registrationDetails.did}", None, "registerDevice") val deviceProfileLog = DeviceProfileLog(registrationDetails.did, location, Option(deviceSpec), @@ -163,7 +188,7 @@ class DeviceRegisterService(saveMetricsActor: ActorRef, config: Config, redisUti "user_declared_state" -> registrationDetails.user_declared_state.getOrElse(""), "user_declared_district" -> registrationDetails.user_declared_district.getOrElse("")) - (dataMap ++ deviceLocation.toMap()).filter(f => f._2.nonEmpty) + (dataMap ++ deviceLocation.toMap()).filter(data => data._2 != null && data._2.nonEmpty) } } diff --git a/platform-api/analytics-api/app/AppConf.scala b/platform-api/analytics-api/app/AppConf.scala index 65a70caac..7adde2722 100644 --- a/platform-api/analytics-api/app/AppConf.scala +++ b/platform-api/analytics-api/app/AppConf.scala @@ -9,7 +9,7 @@ import scala.collection.JavaConverters.mapAsJavaMapConverter object AppConf { - var actors = scala.collection.mutable.Map[String, ActorRef]() + val actors = scala.collection.mutable.Map[String, ActorRef]() val config: Config = play.Play.application.configuration.underlying() .withFallback(ConfigFactory.parseMap(Map("content2vec.scripts_path" -> "", diff --git a/platform-api/analytics-api/app/Global.scala b/platform-api/analytics-api/app/Global.scala index e8f3d1fea..c4caaae2c 100644 --- a/platform-api/analytics-api/app/Global.scala +++ b/platform-api/analytics-api/app/Global.scala @@ -23,9 +23,9 @@ object Global extends WithFilters(RequestInterceptor) { val deviceProfileRedisUtil = new RedisUtil() val metricsActor: ActorRef = app.actorSystem.actorOf(Props(new SaveMetricsActor(config))) - val deviceRegsiterActor = app.actorSystem + val deviceRegisterActor = app.actorSystem .actorOf(Props(new DeviceRegisterService(metricsActor, config, deviceRegisterRedisUtil)), "deviceRegisterServiceAPIActor") - AppConf.setActorRef("deviceRegisterService", deviceRegsiterActor) + AppConf.setActorRef("deviceRegisterService", deviceRegisterActor) val deviceProfileActor = app.actorSystem .actorOf(Props(new DeviceProfileService(metricsActor, config, deviceProfileRedisUtil)), "deviceProfileServiceAPIActor") diff --git a/platform-api/analytics-api/app/modules/ActorInjector.scala b/platform-api/analytics-api/app/modules/ActorInjector.scala deleted file mode 100644 index 2c997ac5d..000000000 --- a/platform-api/analytics-api/app/modules/ActorInjector.scala +++ /dev/null @@ -1,11 +0,0 @@ -package modules - -import com.google.inject.AbstractModule -import org.ekstep.analytics.api.service.SaveMetricsActor -import play.api.libs.concurrent.AkkaGuiceSupport - -class ActorInjector extends AbstractModule with AkkaGuiceSupport { - override def configure(): Unit = { - bindActor[SaveMetricsActor]("save-metrics-actor") - } -} diff --git a/platform-api/analytics-api/conf/application.conf b/platform-api/analytics-api/conf/application.conf index e2c15f157..466e63e9d 100755 --- a/platform-api/analytics-api/conf/application.conf +++ b/platform-api/analytics-api/conf/application.conf @@ -113,8 +113,6 @@ log4j.appender.kafka.enable="false" log4j.appender.kafka.broker_host="localhost:9092" log4j.appender.kafka.topic="sandbox.telemetry.backend" -play.modules.enabled += "modules.ActorInjector" - device-register-controller { type = "Dispatcher" executor = "fork-join-executor" @@ -325,4 +323,6 @@ deviceRegisterAPI.experiment.enable=false experimentService.redisEmptyValueExpirySeconds=86400 druid.coordinator.host="http://localhost:8081/" -druid.healthcheck.url="druid/coordinator/v1/loadstatus" \ No newline at end of file +druid.healthcheck.url="druid/coordinator/v1/loadstatus" + +device.api.enable.debug.log=true \ No newline at end of file