This is an automated email from the ASF dual-hosted git repository.
aradzinski pushed a commit to branch NLPCRAFT-108
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
The following commit(s) were added to refs/heads/NLPCRAFT-108 by this push:
new 954d482 WIP.
954d482 is described below
commit 954d4828e9ee0551d9a7dd56d698dfa90b65ab34
Author: Aaron Radzinski <[email protected]>
AuthorDate: Mon Sep 28 16:35:23 2020 -0700
WIP.
---
bin/nlpcraft.cmd | 1 +
bin/nlpcraft.sh | 1 +
.../org/apache/nlpcraft/common/ansi/NCAnsi.scala | 1 -
.../nlpcraft/common/ansi/NCAnsiSpinner.scala | 38 ++++-----
.../model/tools/cmdline/NCCommandLine.scala | 97 ++++++++++++++++++----
.../nlpcraft/server/rest/NCBasicRestApi.scala | 2 +-
6 files changed, 101 insertions(+), 39 deletions(-)
diff --git a/bin/nlpcraft.cmd b/bin/nlpcraft.cmd
index d1d7a87..132ad06 100644
--- a/bin/nlpcraft.cmd
+++ b/bin/nlpcraft.cmd
@@ -103,6 +103,7 @@ set JVM_OPTS= ^
-server ^
-XX:+UseG1GC ^
-XX:MaxMetaspaceSize=256m ^
+ -DNLPCRAFT_CLI= ^
-DNLPCRAFT_CLI_SCRIPT="%SCRIPT_NAME%" ^
-DNLPCRAFT_CLI_INSTALL_HOME="%INSTALL_HOME%"
diff --git a/bin/nlpcraft.sh b/bin/nlpcraft.sh
index 74f3c08..4cd8ab6 100644
--- a/bin/nlpcraft.sh
+++ b/bin/nlpcraft.sh
@@ -100,6 +100,7 @@ JVM_OPTS="\
-server \
-XX:+UseG1GC \
-XX:MaxMetaspaceSize=256m \
+ -DNLPCRAFT_CLI= \
-DNLPCRAFT_CLI_SCRIPT=$SCRIPT_NAME \
-DNLPCRAFT_CLI_INSTALL_HOME=$INSTALL_HOME"
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
index 575ace1..82a29c7 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsi.scala
@@ -70,7 +70,6 @@ sealed trait NCAnsi extends LazyLogging {
private final val CURSOR_HIDE = "\u001b[?25l"
private final val CURSOR_SHOW = "\u001b[?25h"
-
def isEnabled: Boolean = !U.isSysEnvTrue(PROP)
// Color functions.
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiSpinner.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiSpinner.scala
index 9dc91f2..25badfe 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiSpinner.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/common/ansi/NCAnsiSpinner.scala
@@ -25,42 +25,38 @@ import org.apache.nlpcraft.common._
/**
*
*/
-class NCAnsiSpinner(out: PrintStream = System.out, ansiColor: String =
ansiCyanFg) {
+class NCAnsiSpinner(out: PrintStream = System.out, ansiColor: String =
ansiCyanFg, useAnsi: Boolean = true) {
@volatile var thread: Thread = _
- final val IS_ANSI = NCAnsi.isEnabled
-
final val SPIN_CHARS = Seq('-', '\\', '|', '/')
final val SPIN_CHARS_SIZE = SPIN_CHARS.size
+ var frame = 0
+
/**
*
*/
- def start(): Unit = {
- thread = U.mkThread("ansi-spinner") { t ⇒
- var i = 0
-
- if (IS_ANSI)
+ def start(): Unit =
+ if (useAnsi) {
+ thread = U.mkThread("ansi-spinner") { t ⇒
out.print(s"$ansiCursorHide")
- while (!t.isInterrupted) {
- if (IS_ANSI) {
- if (i > 0)
+ while (!t.isInterrupted) {
+ if (frame > 0)
out.print(s"$ansiCursorLeft$ansiClearLineAfter")
- out.print(s"$ansiColor${SPIN_CHARS(i %
SPIN_CHARS_SIZE)}$ansiReset")
+ out.print(s"$ansiColor${SPIN_CHARS(frame %
SPIN_CHARS_SIZE)}$ansiReset")
- i += 1
- }
- else
- out.print(".")
+ frame += 1
- Thread.sleep(if (IS_ANSI) 200 else 1000)
+ Thread.sleep(200)
+ }
}
- }
- thread.start()
- }
+ thread.start()
+ }
+ else
+ out.print("... ")
/**
*
@@ -68,7 +64,7 @@ class NCAnsiSpinner(out: PrintStream = System.out, ansiColor:
String = ansiCyanF
def stop(): Unit = {
U.stopThread(thread)
- if (IS_ANSI)
+ if (useAnsi && frame > 0)
out.print(s"$ansiCursorLeft$ansiClearLineAfter$ansiCursorShow")
}
}
diff --git
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
index f80ac07..be5964a 100644
---
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
+++
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCommandLine.scala
@@ -21,9 +21,12 @@ import java.io.{File, FileInputStream, IOException,
ObjectInputStream}
import java.net.URL
import com.google.gson._
+import javax.net.ssl.SSLException
import org.apache.commons.lang3.SystemUtils
+import org.apache.http.HttpResponse
import org.apache.http.client.ResponseHandler
-import org.apache.http.client.methods.HttpPost
+import org.apache.http.client.methods.{HttpGet, HttpPost}
+import org.apache.http.client.utils.URIBuilder
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.nlpcraft.common.ascii.NCAsciiTable
@@ -37,6 +40,7 @@ import scala.collection.mutable
import scala.compat.java8.OptionConverters._
import scala.util.Try
import scala.collection.JavaConverters._
+import scala.compat.Platform.currentTime
/**
* 'nlpcraft' script entry point.
@@ -205,7 +209,7 @@ object NCCommandLine extends App {
optional = true,
desc =
"REST server endpoint in 'http{s}://hostname:port'
format. " +
- "Default value is https://localhost:8081"
+ "Default value is http://localhost:8081"
),
Parameter(
id = "number",
@@ -224,10 +228,10 @@ object NCCommandLine extends App {
Example(
usage = Seq(
s"$PROMPT $SCRIPT_NAME ping-server ",
- s" --endpoint=https://localhost:1234 ",
+ s" --endpoint=http://localhost:1234 ",
s" -n=10"
),
- desc = "Pings REST server at 'https://localhost:1234'
endpoint 10 times."
+ desc = "Pings REST server at 'http://localhost:1234'
endpoint 10 times."
)
)
),
@@ -327,9 +331,9 @@ object NCCommandLine extends App {
private def cmdPingServer(cmd: Command, args: Seq[Argument]): Unit = {
val endpoint = args.find(_.parameter.id == "endpoint") match {
case Some(arg) ⇒ new URL(arg.value.get).toURI.toString
- case None ⇒ "https://localhost:8081"
+ case None ⇒ "http://localhost:8081"
}
- var num = args.find(_.parameter.id == "number") match {
+ val num = args.find(_.parameter.id == "number") match {
case Some(arg) ⇒
try
Integer.parseInt(arg.value.get)
@@ -343,24 +347,50 @@ object NCCommandLine extends App {
var i = 0
while (i < num) {
- `>>`(s"Pinging ${ansiBlue(endpoint)} ")
+ `>>`(s"Pinging REST server at ${ansiBlue(endpoint)} ")
- val spinner = new NCAnsiSpinner()
+ val spinner = new NCAnsiSpinner(
+ System.out,
+ ansiCyanFg,
+ // ANSI is NOT disabled & we ARE NOT running from IDEA or
Eclipse...
+ NCAnsi.isEnabled && U.sysEnv("NLPCRAFT_CLI").isDefined
+ )
spinner.start()
- i += 1
- }
+ val startMs = currentTime
+
+ try
+ httpGet(endpoint, "health", new ResponseHandler[Int]() {
+ override def handleResponse(resp: HttpResponse): Int =
resp.getStatusLine.getStatusCode
+ }) match {
+ case 200 ⇒
+ spinner.stop()
- Thread.sleep(100000)
+ log(ansiGreen("OK") + " " + ansiCyan(s"[${currentTime
- startMs}ms]"))
+ case code: Int ⇒
+ spinner.stop()
+ log(ansiRed("FAIL") + s" [HTTP
$ansiYellowFg$code$ansiReset]")
+ }
+ catch {
+ case _: SSLException ⇒
+ spinner.stop()
+ log(ansiRed("FAIL") + s" ${ansiYellow("[SSL error]")}")
+ case _: IOException ⇒
+ spinner.stop()
+ log(ansiRed("FAIL") + s" ${ansiYellow("[I/O error]")}")
+ }
+ i += 1
- // TODO
+ // Pause between pings.
+ Thread.sleep(1000)
+ }
}
/**
@@ -608,16 +638,26 @@ object NCCommandLine extends App {
}
/**
- * Posts REST request.
*
- * @param url
+ * @param baseUrl
+ * @param cmd
+ * @return
+ */
+ private def prepUrl(baseUrl: String, cmd: String): String =
+ if (baseUrl.endsWith("/")) s"${baseUrl}api/v1/$cmd" else
s"$baseUrl/api/v1/$cmd"
+
+ /**
+ * Posts HTTP POST request.
+ *
+ * @param baseUrl Base endpoint URL.
+ * @param cmd REST call command.
* @param resp
* @param jsParams
* @return
* @throws IOException
*/
- private def post[T](url: String, resp: ResponseHandler[T], jsParams:
(String, AnyRef)*): T = {
- val post = new HttpPost(url)
+ private def httpPost[T](baseUrl: String, cmd: String, resp:
ResponseHandler[T], jsParams: (String, AnyRef)*): T = {
+ val post = new HttpPost(prepUrl(baseUrl, cmd))
post.setHeader("Content-Type", "application/json")
post.setEntity(new StringEntity(gson.toJson(jsParams.filter(_._2 !=
null).toMap.asJava), "UTF-8"))
@@ -629,6 +669,29 @@ object NCCommandLine extends App {
}
/**
+ * Posts HTTP GET request.
+ *
+ * @param baseUrl Base endpoint URL.
+ * @param cmd REST call command.
+ * @param resp
+ * @param jsParams
+ * @return
+ * @throws IOException
+ */
+ private def httpGet[T](baseUrl: String, cmd: String, resp:
ResponseHandler[T], jsParams: (String, AnyRef)*): T = {
+ val bldr = new URIBuilder(prepUrl(baseUrl, cmd))
+
+ jsParams.foreach(p ⇒ bldr.setParameter(p._1, p._2.toString))
+
+ val get = new HttpGet(bldr.build())
+
+ try
+ HttpClients.createDefault().execute(get, resp)
+ finally
+ get.releaseConnection()
+ }
+
+ /**
*
* @param cmd
* @param args
@@ -694,6 +757,8 @@ object NCCommandLine extends App {
if (exitStatus != 0)
errorHelp()
+ log()
+
sys.exit(exitStatus)
}
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 92d573c..fe6aa23 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
@@ -1887,7 +1887,7 @@ class NCBasicRestApi extends NCRestApi with LazyLogging
with NCOpenCensusTrace w
override def getRoute: Route = {
val timeoutResp = HttpResponse(
StatusCodes.EnhanceYourCalm,
- entity = "Unable to serve response within time limit, please
enhance your calm."
+ entity = "Unable to serve response within time limit."
)
handleExceptions(getExceptionHandler) {