Skip to content

Commit caa4099

Browse files
committed
Switch to layoutz
1 parent 4072480 commit caa4099

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

build.sbt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ libraryDependencies ++= Seq(
9999
"ca.uhn.hapi" % "hapi-structures-v25" % "2.3",
100100
"ca.uhn.hapi" % "hapi-structures-v281" % "2.3",
101101

102-
"org.apache.opennlp" % "opennlp-tools" % "2.5.4",
102+
"org.apache.opennlp" % "opennlp-tools" % "2.5.5",
103103

104104
"org.apache.httpcomponents.client5" % "httpclient5" % "5.4",
105105
"org.apache.httpcomponents.core5" % "httpcore5" % "5.3",
@@ -158,6 +158,9 @@ libraryDependencies ++= Seq(
158158
"dev.langchain4j" % "langchain4j-embeddings-bge-small-en-v15-q" % "1.2.0-beta8",
159159
"dev.langchain4j" % "langchain4j-embeddings-all-minilm-l6-v2-q" % "1.2.0-beta8",
160160

161+
// Beautiful CLI output formatting
162+
"xyz.matthieucourt" %% "layoutz" % "0.1.0",
163+
161164
// https://docs.gatling.io/reference/integrations/build-tools/sbt-plugin
162165
"io.gatling" % "gatling-core" % gatlingVersion,
163166
"io.gatling.highcharts" % "gatling-charts-highcharts" % gatlingVersion,

src/main/scala/alpakka/sse_to_elasticsearch/WikipediaEditsAnalyser.scala

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import io.circe.generic.auto.*
1818
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
1919
import io.circe.parser.*
2020
import io.circe.syntax.*
21+
import layoutz.*
2122
import opennlp.tools.namefind.{NameFinderME, TokenNameFinderModel}
2223
import opennlp.tools.tokenize.{TokenizerME, TokenizerModel}
2324
import opennlp.tools.util.Span
@@ -388,40 +389,22 @@ object WikipediaEditsAnalyser extends App {
388389

389390

390391
/**
391-
* Formats data as an ASCII table with proper borders and alignment.
392+
* Formats data as an ASCII table using the layoutz library.
392393
*
393394
* @param title The title to display at the top of the table
394395
* @param headers Column headers
395396
* @param data Table data as rows of columns
396-
* @return A formatted ASCII table as a string
397+
* @return A formatted ASCII table as a string using layoutz
397398
*/
398399
private def formatAsAsciiTable(title: String, headers: Array[String], data: Array[Array[String]]): String = {
399-
// Calculate column widths (max width of each column)
400-
val colWidths = headers.indices.map { i =>
401-
val headerWidth = headers(i).length
402-
val maxDataWidth = data.map(_(i).length).max
403-
math.max(headerWidth, maxDataWidth) + 2 // +2 for padding
404-
}.toArray
405-
406-
val totalWidth = colWidths.sum + colWidths.length + 1
407-
val border = "+" + colWidths.map(w => "-" * w).mkString("+") + "+"
408-
409-
def formatRow(row: Array[String]): String = {
410-
"| " + row.zip(colWidths).map { case (cell, width) =>
411-
cell.padTo(width - 1, ' ') + " "
412-
}.mkString("| ") + "|"
413-
}
414-
415-
val tableBuilder = new StringBuilder
416-
tableBuilder.append(border).append("\n")
417-
tableBuilder.append("| ").append(title.padTo(totalWidth - 4, ' ')).append(" |").append("\n")
418-
tableBuilder.append(border).append("\n")
419-
tableBuilder.append(formatRow(headers)).append("\n")
420-
tableBuilder.append(border).append("\n")
421-
data.foreach(row => tableBuilder.append(formatRow(row)).append("\n"))
422-
tableBuilder.append(border)
423-
424-
tableBuilder.toString
400+
layout(
401+
section(title)(
402+
table(
403+
headers = headers.toSeq,
404+
rows = data.toSeq.map(_.toSeq)
405+
)
406+
)
407+
).render
425408
}
426409

427410
private val nerProcessingFlow: Flow[Change, Ctx, NotUsed] = Flow[Change]
@@ -458,7 +441,7 @@ object WikipediaEditsAnalyser extends App {
458441
.map(_ => query())
459442
.runWith(Sink.ignore)
460443

461-
private def aiClient() = {
444+
private def aiClient(): Unit = {
462445
val assistant = createAssistant()
463446
startConversationWith(assistant)
464447
}
@@ -522,7 +505,7 @@ object WikipediaEditsAnalyser extends App {
522505
private def searchPersons(query: String): Future[List[Person]] = {
523506
logger.info(s"Searching for persons with query: $query")
524507

525-
val wildcard = "**";
508+
val wildcard = "**"
526509
val searchQuery = if (query.equals(wildcard)) {
527510
"""{
528511
"exists": {
@@ -563,28 +546,28 @@ object WikipediaEditsAnalyser extends App {
563546
}
564547
}
565548

566-
final case class QueryRequest(query: String)
549+
private final case class QueryRequest(query: String)
567550

568-
final case class QueryResponse(answer: String)
551+
private final case class QueryResponse(answer: String)
569552

570-
final case class PersonSearchRequest(query: String)
553+
private final case class PersonSearchRequest(query: String)
571554

572-
final case class PersonSearchResponse(persons: List[Person])
555+
private final case class PersonSearchResponse(persons: List[Person])
573556

574-
final case class ProcessingControlRequest(enabled: Boolean)
557+
private final case class ProcessingControlRequest(enabled: Boolean)
575558

576-
final case class ProcessingControlResponse(enabled: Boolean, message: String)
559+
private final case class ProcessingControlResponse(enabled: Boolean, message: String)
577560

578-
final case class SearchIndexUrlResponse(url: String)
561+
private final case class SearchIndexUrlResponse(url: String)
579562

580563
private def startConversationWith(assistant: Assistant): Unit = {
581564
def enableProcessing(): ProcessingControlResponse = {
582565
if (!isProcessingEnabled.get()) {
583566
isProcessingEnabled.set(true)
584567
logger.info("Processing enabled - resuming LLM calls and indexing")
585-
ProcessingControlResponse(true, "Processing enabled - resuming LLM calls and indexing")
568+
ProcessingControlResponse(enabled = true, "Processing enabled - resuming LLM calls and indexing")
586569
} else {
587-
ProcessingControlResponse(true, "Processing already enabled")
570+
ProcessingControlResponse(enabled = true, "Processing already enabled")
588571
}
589572
}
590573

@@ -593,9 +576,9 @@ object WikipediaEditsAnalyser extends App {
593576
isProcessingEnabled.set(false)
594577
val msg = "Processing disabled - suspending LLM calls and indexing (flow and local NER continues)"
595578
logger.info(msg)
596-
ProcessingControlResponse(false, msg)
579+
ProcessingControlResponse(enabled = false, msg)
597580
} else {
598-
ProcessingControlResponse(false, "Processing already disabled")
581+
ProcessingControlResponse(enabled = false, "Processing already disabled")
599582
}
600583
}
601584

@@ -702,7 +685,7 @@ object WikipediaEditsAnalyser extends App {
702685
Instant.ofEpochSecond(timestamp).atZone(ZoneId.systemDefault).toLocalDateTime.toString
703686
}
704687

705-
// Note that the size of the collection can also be fetched via a GET request, eg
688+
// Note that the size of the collection can also be fetched via a GET request, e.g.
706689
// http://localhost:57321/wikipediaedits/_count
707690
private def query(): Unit = {
708691
logger.info(s"About to execute scrolled read queries...")

0 commit comments

Comments
 (0)