Skip to content

Commit

Permalink
Merge pull request #216 from anandp504/release-2.5.2
Browse files Browse the repository at this point in the history
Issue #SB-15961 fix: Fix logger for DeviceProfileActor
  • Loading branch information
sowmya-dixit authored Nov 26, 2019
2 parents c0fdbd5 + 9b1d794 commit 2130147
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 =>
Expand All @@ -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")
}
}

Expand All @@ -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)
}
Expand All @@ -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),
Expand Down Expand Up @@ -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)
}

}
2 changes: 1 addition & 1 deletion platform-api/analytics-api/app/AppConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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" -> "",
Expand Down
4 changes: 2 additions & 2 deletions platform-api/analytics-api/app/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 0 additions & 11 deletions platform-api/analytics-api/app/modules/ActorInjector.scala

This file was deleted.

6 changes: 3 additions & 3 deletions platform-api/analytics-api/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -325,4 +323,6 @@ deviceRegisterAPI.experiment.enable=false
experimentService.redisEmptyValueExpirySeconds=86400

druid.coordinator.host="http://localhost:8081/"
druid.healthcheck.url="druid/coordinator/v1/loadstatus"
druid.healthcheck.url="druid/coordinator/v1/loadstatus"

device.api.enable.debug.log=true

0 comments on commit 2130147

Please sign in to comment.