From 6810e7d9fe37f511bfd6771f5b56674069b9d91a Mon Sep 17 00:00:00 2001 From: DO YUNG YOON Date: Fri, 19 Feb 2016 15:07:02 +0900 Subject: [PATCH] S2GRAPH-34: add option to specify which property value need to be considered for processTimeDecay functions. --- .../scala/com/kakao/s2graph/core/Graph.scala | 23 ++++++++++++++++++- .../com/kakao/s2graph/core/QueryParam.scala | 5 +++- .../s2graph/core/rest/RequestParser.scala | 4 +++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/s2core/src/main/scala/com/kakao/s2graph/core/Graph.scala b/s2core/src/main/scala/com/kakao/s2graph/core/Graph.scala index 111f3df1..58953e23 100644 --- a/s2core/src/main/scala/com/kakao/s2graph/core/Graph.scala +++ b/s2core/src/main/scala/com/kakao/s2graph/core/Graph.scala @@ -85,7 +85,28 @@ object Graph { val tsVal = queryParam.timeDecay match { case None => 1.0 case Some(timeDecay) => - val timeDiff = queryParam.timestamp - edge.ts + val tsVal = try { + val labelMeta = edge.label.metaPropsMap(timeDecay.labelMetaSeq) + val innerValWithTsOpt = edge.propsWithTs.get(timeDecay.labelMetaSeq) + innerValWithTsOpt.map { innerValWithTs => + val innerVal = innerValWithTs.innerVal + labelMeta.dataType match { + case InnerVal.LONG => innerVal.value match { + case n: BigDecimal => n.bigDecimal.longValue() + case _ => innerVal.toString().toLong + } + case _ => innerVal.toString().toLong + } + } getOrElse(edge.ts) + // val innerVal = edge.propsWithTs(timeDecay.labelMetaSeq).innerVal + // + // edge.propsWithTs.get(timeDecay.labelMetaSeq).map(_.toString.toLong).getOrElse(edge.ts) + } catch { + case e: Exception => + logger.error(s"processTimeDecay error. ${edge.toLogString}", e) + edge.ts + } + val timeDiff = queryParam.timestamp - tsVal timeDecay.decay(timeDiff) } diff --git a/s2core/src/main/scala/com/kakao/s2graph/core/QueryParam.scala b/s2core/src/main/scala/com/kakao/s2graph/core/QueryParam.scala index 449764b2..7f52e1ab 100644 --- a/s2core/src/main/scala/com/kakao/s2graph/core/QueryParam.scala +++ b/s2core/src/main/scala/com/kakao/s2graph/core/QueryParam.scala @@ -517,7 +517,10 @@ case class QueryParam(labelWithDir: LabelWithDirection, timestamp: Long = System // } } -case class TimeDecay(initial: Double = 1.0, lambda: Double = 0.1, timeUnit: Double = 60 * 60 * 24) { +case class TimeDecay(initial: Double = 1.0, + lambda: Double = 0.1, + timeUnit: Double = 60 * 60 * 24, + labelMetaSeq: Byte = LabelMeta.timeStampSeq) { def decay(diff: Double): Double = { //FIXME val ret = initial * Math.pow(1.0 - lambda, diff / timeUnit) diff --git a/s2core/src/main/scala/com/kakao/s2graph/core/rest/RequestParser.scala b/s2core/src/main/scala/com/kakao/s2graph/core/rest/RequestParser.scala index 78fc3752..5fc9878c 100644 --- a/s2core/src/main/scala/com/kakao/s2graph/core/rest/RequestParser.scala +++ b/s2core/src/main/scala/com/kakao/s2graph/core/rest/RequestParser.scala @@ -281,11 +281,13 @@ class RequestParser(config: Config) extends JSONParser { } val cacheTTL = (labelGroup \ "cacheTTL").asOpt[Long].getOrElse(-1L) val timeDecayFactor = (labelGroup \ "timeDecay").asOpt[JsObject].map { jsVal => + val propName = (jsVal \ "propName").asOpt[String].getOrElse(LabelMeta.timestamp.name) + val propNameSeq = label.metaPropsInvMap.get(propName).map(_.seq).getOrElse(LabelMeta.timeStampSeq) val initial = (jsVal \ "initial").asOpt[Double].getOrElse(1.0) val decayRate = (jsVal \ "decayRate").asOpt[Double].getOrElse(0.1) if (decayRate >= 1.0 || decayRate <= 0.0) throw new BadQueryException("decay rate should be 0.0 ~ 1.0") val timeUnit = (jsVal \ "timeUnit").asOpt[Double].getOrElse(60 * 60 * 24.0) - TimeDecay(initial, decayRate, timeUnit) + TimeDecay(initial, decayRate, timeUnit, propNameSeq) } val threshold = (labelGroup \ "threshold").asOpt[Double].getOrElse(QueryParam.DefaultThreshold) // TODO: refactor this. dirty