Commit ee417a0d authored by 赵威's avatar 赵威

request type to Either

parent efc8e8f8
...@@ -63,47 +63,60 @@ object ES { ...@@ -63,47 +63,60 @@ object ES {
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global
def request(req: SearchRequest): Future[IndexedSeq[Content]] = { def request(req: SearchRequest): Either[Throwable, Future[IndexedSeq[Content]]] = {
val client: ElasticClient = ESClient.create("172.16.52.33", 9200, "elastic", "gengmei!@#") val client: ElasticClient = ESClient.create("172.16.52.33", 9200, "elastic", "gengmei!@#")
// println(req.show) // println(req.show)
client.execute(req).map(resp => resp.result.to[Content]) try {
Right(client.execute(req).map(resp => resp.result.to[Content]))
} catch {
case e: Throwable =>
DingTalk.send(
Map(
"method" -> "ES.request",
"error" -> e.getStackTrace.mkString("\n"),
"request" -> req.show.toString
),
contentType = "exception"
)
Left(e)
}
// client.close() // client.close()
} }
def test( // def test(
diaryRequest: SearchRequest, // diaryRequest: SearchRequest,
tractateRequest: SearchRequest, // tractateRequest: SearchRequest,
answerRequest: SearchRequest, // answerRequest: SearchRequest,
serviceDiaryReq: SearchRequest // serviceDiaryReq: SearchRequest
) = { // ) = {
// TODO read from config // // TODO read from config
val client: ElasticClient = ESClient.create("172.16.52.33", 9200, "elastic", "gengmei!@#") // val client: ElasticClient = ESClient.create("172.16.52.33", 9200, "elastic", "gengmei!@#")
val multiReq = multi( // val multiReq = multi(
diaryRequest, // diaryRequest,
tractateRequest, // tractateRequest,
answerRequest, // answerRequest,
serviceDiaryReq // serviceDiaryReq
) // )
println(multiReq.show) // println(multiReq.show)
// TODO remove await // // TODO remove await
val resp = client.execute(multiReq).await // val resp = client.execute(multiReq).await
// println(resp) // // println(resp)
val a = resp.result.to[Content] // val a = resp.result.to[Content]
println("%%%%%%%%%%%") // println("%%%%%%%%%%%")
println(a.size) // println(a.size)
a.foreach { x => // a.foreach { x =>
println(x.id, x.index, x.projects) // println(x.id, x.index, x.projects)
} // }
println("%%%%%%%%%%%") // println("%%%%%%%%%%%")
resp // resp
} // }
def generateDiaryRequest( def generateDiaryRequest(
projects: List[String], projects: List[String],
......
...@@ -24,35 +24,39 @@ object Redis { ...@@ -24,35 +24,39 @@ object Redis {
} }
def save( def save(
contentF: Future[IndexedSeq[Content]], contentEitherFuture: Either[Throwable, Future[IndexedSeq[Content]]],
deviceId: String, deviceId: String,
contentType: String, contentType: String,
timestampBegin: Long timestampBegin: Long
): Unit = { ): Unit = {
val key = s"streaming:candidate:${contentType}:device_id:${deviceId}" val key = s"streaming:candidate:${contentType}:device_id:${deviceId}"
pRc4.withClient { client => contentEitherFuture match {
contentF.foreach { seq => case Left(e) => e.printStackTrace()
val ids = seq.map(c => c.id) case Right(contentFuture) =>
if (ids.size > 0) { pRc4.withClient { client =>
client.del(key) contentFuture.foreach { seq =>
ids.foreach { id => val ids = seq.map(c => c.id)
// println(id) if (ids.size > 0) {
client.rpush(key, id) client.del(key)
} ids.foreach { id =>
client.expire(key, 60 * 60 * 24 * 15) // println(id)
if (deviceId == "64695DE0-B926-4188-9C62-D987DC20BEDF") { client.rpush(key, id)
DingTalk.send( }
Map( client.expire(key, 60 * 60 * 24 * 15)
"method" -> "Redis.save", if (deviceId == "64695DE0-B926-4188-9C62-D987DC20BEDF") {
"deviceId" -> deviceId, DingTalk.send(
"contentType" -> contentType, Map(
"streamTotalSeconds" -> s"${(Instant.now.getEpochSecond - timestampBegin)}s" "method" -> "Redis.save",
) "deviceId" -> deviceId,
) "contentType" -> contentType,
"streamTotalSeconds" -> s"${(Instant.now.getEpochSecond * 1000 - timestampBegin * 1000)}ms"
)
)
}
}
} }
} }
}
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment