Skip to content

Commit f3b1a07

Browse files
committed
Changes to reflect 0.9.5 API
1 parent 0351ee8 commit f3b1a07

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package de.upb.cs.swt.delphi.cli.artifacts
2+
3+
import org.joda.time.DateTime
4+
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson._
5+
import org.joda.time.format.{DateTimeFormatter, ISODateTimeFormat}
6+
import spray.json.{DefaultJsonProtocol, DeserializationException, JsString, JsValue, RootJsonFormat}
7+
8+
case class SearchResults(totalHits : Long, hits : Array[SearchResult], queried : DateTime = DateTime.now())
9+
10+
11+
12+
object SearchResultsJson extends DefaultJsonProtocol {
13+
implicit object DateJsonFormat extends RootJsonFormat[DateTime] {
14+
15+
private val parserISO: DateTimeFormatter = ISODateTimeFormat.dateTime()
16+
17+
override def write(obj: DateTime) = JsString(parserISO.print(obj))
18+
19+
override def read(json: JsValue): DateTime = json match {
20+
case JsString(s) => parserISO.parseDateTime(s)
21+
case _ => throw new DeserializationException("Error info you want here ...")
22+
}
23+
}
24+
25+
implicit val SearchResultsFormat = jsonFormat3(SearchResults)
26+
}

src/main/scala/de/upb/cs/swt/delphi/cli/commands/SearchCommand.scala

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import java.util.concurrent.TimeUnit
2121
import com.softwaremill.sttp._
2222
import com.softwaremill.sttp.sprayJson._
2323
import de.upb.cs.swt.delphi.cli.Config
24-
import de.upb.cs.swt.delphi.cli.artifacts.SearchResult
25-
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson._
24+
import de.upb.cs.swt.delphi.cli.artifacts.{SearchResult, SearchResults}
25+
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultsJson._
2626
import spray.json._
2727

2828
import scala.concurrent.duration._
@@ -43,10 +43,8 @@ object SearchCommand extends Command with DefaultJsonProtocol{
4343

4444
information.apply(s"Searching for artifacts matching ${'"'}$query${'"'}.")
4545

46-
47-
val queryParams = Map("pretty" -> "")
4846
val queryPayload: Query = Query(query,config.limit)
49-
val searchUri = uri"${config.server}/search?$queryParams"
47+
val searchUri = uri"${config.server}/search"
5048

5149
val request = sttp.body(queryPayload.toJson).post(searchUri)
5250

@@ -64,7 +62,7 @@ object SearchCommand extends Command with DefaultJsonProtocol{
6462

6563
if (res.code == timeoutCode) {
6664

67-
error.apply(s"The query timed out after ${took.toSeconds} seconds. " +
65+
error.apply(s"The query timed out after ${took.toSeconds}%.0f seconds. " +
6866
"To set a longer timeout, use the --timeout option.")
6967
}
7068
val resStr = res.body match {
@@ -83,28 +81,27 @@ object SearchCommand extends Command with DefaultJsonProtocol{
8381
reportResult.apply(res)
8482
}
8583
if (!(config.raw || res.equals("")) || !config.csv.equals("")) {
86-
val jsonArr = res.parseJson.asInstanceOf[JsArray].elements
87-
val retrieveResults = jsonArr.map(r => r.convertTo[SearchResult]).toList
88-
onProperSearchResults(retrieveResults)
89-
}
90-
91-
def onProperSearchResults(sr: List[SearchResult]) = {
92-
84+
val retrieveResults = res.parseJson.convertTo[SearchResults]
85+
val sr = retrieveResults.hits.toList
9386
val capMessage = {
94-
config.limit match {
95-
case Some(limit) if (limit <= sr.size)
96-
=> s"Results may be capped by result limit set to $limit."
97-
case None if (sr.size >= 50)
98-
=> "Results may be capped by default limit of 50 returned results. Use --limit to extend the result set."
99-
case _
100-
=> ""
87+
if (sr.size < retrieveResults.totalHits ) {
88+
config.limit match {
89+
case Some(limit) if (limit <= sr.size)
90+
=> s"Results are capped by results limit set to $limit."
91+
case None if (sr.size >= 50)
92+
=> "Results are capped by default limit of 50 returned results. Use --limit to extend the result set."
93+
case _
94+
=> ""
95+
}
96+
} else {
97+
""
10198
}
10299
}
103100

104-
success.apply(s"Found ${sr.size} item(s). $capMessage")
101+
success.apply(s"Found ${retrieveResults.totalHits} item(s). $capMessage")
105102
reportResult.apply(sr)
106103

107-
information.apply(f"Query took ${queryRuntime.toUnit(TimeUnit.SECONDS)}%.2fs.")
104+
information.apply(f"Query roundtrip took ${queryRuntime.toUnit(TimeUnit.MILLISECONDS)}%.0fms.")
108105

109106
if (!config.csv.equals("")) {
110107
exportResult.apply(sr)

0 commit comments

Comments
 (0)