You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2020/09/06 13:57:21 UTC

[incubator-nlpcraft] branch NLPCRAFT-114 created (now c574058)

This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a change to branch NLPCRAFT-114
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git.


      at c574058  WIP.

This branch includes the following new commits:

     new c574058  WIP.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-nlpcraft] 01/01: WIP.

Posted by se...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-114
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git

commit c574058b72c45932d9d8d17e90ce5fc0b793673a
Author: Sergey Kamov <se...@apache.org>
AuthorDate: Sun Sep 6 16:57:06 2020 +0300

    WIP.
---
 .../nlpcraft/server/ignite/NCIgniteHelpers.scala   | 39 ---------------
 .../inspection/impl/NCSuggestionInspection.scala   |  6 ++-
 .../nlpcraft/server/rest/NCBasicRestApi.scala      | 57 +++++++++++++++++-----
 3 files changed, 49 insertions(+), 53 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/ignite/NCIgniteHelpers.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/ignite/NCIgniteHelpers.scala
index a44427c..988315e 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/ignite/NCIgniteHelpers.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/ignite/NCIgniteHelpers.scala
@@ -19,9 +19,7 @@ package org.apache.nlpcraft.server.ignite
 
 import java.util.UUID
 
-import javax.cache.Cache
 import org.apache.ignite.IgniteCache
-import org.apache.ignite.cache.query.{QueryCursor, SqlQuery}
 import org.apache.ignite.events.CacheEvent
 import org.apache.ignite.lang.{IgniteBiPredicate, IgnitePredicate}
 
@@ -129,42 +127,5 @@ object NCIgniteHelpers extends NCIgniteInstance {
             case v if v != null ⇒ Some(v)
             case null ⇒ None
         }
-
-        /**
-         * SQL request to the cache.
-         *
-         * @param cls Type.
-         * @param clause SQL request.
-         * @param args Arguments for SQL request.
-         * @return
-         */
-        def select(cls: Class[_ <: V], clause: String, args: Any*): QueryCursor[Cache.Entry[K, V]] = {
-            assert(cls != null)
-            assert(clause != null)
-            assert(args != null)
-
-            // TODO: deprecated to what & how?
-            // TODO: Ignite is missing instructions on this migration.
-            // TODO: Perhaps - https://stackoverflow.com/questions/41309941/sqlquery-and-sqlfieldsquery
-            val qry = new SqlQuery[K, V](cls, clause)
-
-            if (args != null && args.nonEmpty)
-                qry.setArgs(args.map(_.asInstanceOf[AnyRef]): _*)
-
-            ic.query(qry)
-        }
-
-        /**
-         * SQL request to the cache.
-         *
-         * @param clause SQL request.
-         * @param args Arguments for SQL request.
-         **/
-        def select(clause: String, args: Any*)(implicit m: Manifest[V]): QueryCursor[Cache.Entry[K, V]] = {
-            assert(clause != null)
-            assert(args != null)
-
-            select(m.runtimeClass.asInstanceOf[Class[V]], clause, args: _*)
-        }
     }
 }
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspection/impl/NCSuggestionInspection.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspection/impl/NCSuggestionInspection.scala
index a9a54f8..90eaf25 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspection/impl/NCSuggestionInspection.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/inspection/impl/NCSuggestionInspection.scala
@@ -42,6 +42,7 @@ import org.apache.nlpcraft.server.probe.NCProbeManager
 import scala.collection.JavaConverters._
 import scala.collection.{Seq, mutable}
 import scala.concurrent.{Future, Promise}
+import scala.util.{Failure, Success}
 
 /**
  * Synonym suggestion inspection.
@@ -132,8 +133,8 @@ object NCSuggestionInspection extends NCInspectionService {
 
             val promise = Promise[NCInspectionResult]()
 
-            NCProbeManager.getModelInfo(mdlId, parent).collect {
-                case m: java.util.Map[String, AnyRef] ⇒
+            NCProbeManager.getModelInfo(mdlId, parent).onComplete {
+                case Success(m) ⇒
                     try {
                         require(
                             m.containsKey("macros") && m.containsKey("elementsSynonyms") && m.containsKey("intentsSamples")
@@ -429,6 +430,7 @@ object NCSuggestionInspection extends NCInspectionService {
 
                             promise.failure(e)
                     }
+                case Failure(e) ⇒ promise.failure(e)
             }(getExecutor)
 
             promise.future
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
index fd9a2cc..44b8b28 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/server/rest/NCBasicRestApi.scala
@@ -519,17 +519,23 @@ class NCBasicRestApi extends NCRestApi with LazyLogging with NCOpenCensusTrace w
 
                 optionalHeaderValueByName("User-Agent") { usrAgent ⇒
                     extractClientIP { rmtAddr ⇒
+                        val fut = NCQueryManager.futureAsk(
+                            getUserId(acsUsr, usrIdOpt, usrExtIdOpt),
+                            txt,
+                            mdlId,
+                            usrAgent,
+                            getAddress(rmtAddr),
+                            data,
+                            enableLog.getOrElse(false),
+                            span
+                        )
+
+                        fut.failed.collect {
+                            case e ⇒ onError(e)
+                        }
+
                         successWithJs(
-                            NCQueryManager.futureAsk(
-                                getUserId(acsUsr, usrIdOpt, usrExtIdOpt),
-                                txt,
-                                mdlId,
-                                usrAgent,
-                                getAddress(rmtAddr),
-                                data,
-                                enableLog.getOrElse(false),
-                                span
-                            ).collect {
+                            fut.collect {
                                 // We have to use GSON (not spray) here to serialize `resBody` field.
                                 case res ⇒ GSON.toJson(
                                     Map(
@@ -549,7 +555,6 @@ class NCBasicRestApi extends NCRestApi with LazyLogging with NCOpenCensusTrace w
     /**
      *
      * @param fut
-     * @return
      */
     private def successWithJs(fut: Future[String]) = onSuccess(fut) {
         js ⇒ complete(HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, js)))
@@ -557,6 +562,28 @@ class NCBasicRestApi extends NCRestApi with LazyLogging with NCOpenCensusTrace w
 
     /**
       *
+      * @param e
+      */
+    private def onError(e: Throwable): Unit = {
+        val errMsg = e.getLocalizedMessage
+        val code =
+            e match {
+                case _: NCE ⇒
+                    // We have to log error reason because even general exceptions are not expected here.
+                    logger.warn(s"Unexpected top level REST API error: $errMsg", e)
+
+                    StatusCodes.BadRequest
+                case _ ⇒
+                    logger.error(s"Unexpected system error: $errMsg", e)
+
+                    StatusCodes.InternalServerError
+            }
+
+        completeError(code, "NC_ERROR", errMsg)
+    }
+
+    /**
+      *
       * @return
       */
     protected def ask$(): Route = {
@@ -734,8 +761,14 @@ class NCBasicRestApi extends NCRestApi with LazyLogging with NCOpenCensusTrace w
                 if (!NCProbeManager.getAllProbes(admUsr.companyId, span).exists(_.models.exists(_.id == mdlId)))
                     throw new NCE(s"Probe not found for model: $mdlId")
 
+                val fut = NCInspectionManager.inspect(mdlId, inspId, args, span)
+
+                fut.failed.collect {
+                    case e ⇒ onError(e)
+                }
+
                 successWithJs(
-                    NCInspectionManager.inspect(mdlId, inspId, args, span).collect {
+                    fut.collect {
                         // We have to use GSON (not spray) here to serialize `result` field.
                         case res ⇒ GSON.toJson(Map("status" → API_OK.toString, "result" → res).asJava)
                     }