Skip to content

Release/0.9.5 #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
fcc916f
Adding --csv option to search and retrieve (Issue #4 )
sideresearch Nov 21, 2018
21560a2
Fixed the Buildinfo placeholder
nguyenLisa Nov 21, 2018
20427e6
Fixing code style issues.
sideresearch Nov 22, 2018
72add90
Fixing last code style issue
sideresearch Nov 22, 2018
655272a
Merge pull request #28 from delphi-hub/csv-option
bhermann Nov 24, 2018
40e8743
Catch the TimeoutException in the search command.
sideresearch Nov 24, 2018
d398c5b
Fixing code style.
sideresearch Nov 24, 2018
fb14f37
Fixed last style issues
sideresearch Nov 24, 2018
6ee305e
Merge pull request #30 from delphi-hub/search-timeout
bhermann Nov 28, 2018
96f1842
Refactoring delphi cli sttp dependency for native image generation
Jan 15, 2019
3972bd4
Refactored retrieve command to sttp
Jan 15, 2019
1de2c18
Fixing encoded url issue for retrieve command
Jan 23, 2019
b252d4e
Refactoring search command to sttp
Jan 24, 2019
0724269
Implementing codacy suggestions
Jan 25, 2019
cc33060
Fixing codacy issue
Mar 7, 2019
7d8068a
Renaming the project to delphi
Mar 7, 2019
1396373
Merge pull request #32 from delphi-hub/Feature/27/NativeExe
bhermann Mar 9, 2019
e179a15
Merge branch 'master' into develop
Nov 19, 2019
2ee1c51
Upgrading akka to 2.5.16
Nov 20, 2019
f1fcde7
Upgrading sbt-codacy to 1.3.14
Nov 20, 2019
4e6e968
Merge pull request #33 from delphi-hub/vulnerability_fix
bhermann Nov 21, 2019
b0d3e8d
Updated sbt file
bhermann Nov 21, 2019
37db71e
Merge branch 'develop' of github.com:delphi-hub/delphi-cli into develop
bhermann Nov 21, 2019
2835ad1
- Switching back scala version to Scala 2.12
Nov 22, 2019
d0d4452
Merge pull request #35 from delphi-hub/vulnerability_fix
bhermann Nov 26, 2019
8e7a377
Made scala 2.12 default
bhermann Dec 24, 2019
4062a33
Merge branch 'develop' of github.com:delphi-hub/delphi-cli into develop
bhermann Dec 24, 2019
f4c3e58
Update README.md
bhermann Jan 7, 2020
f78e63e
Removed coverage for now
bhermann Jan 7, 2020
066f666
SBT version bumped to 1.3.5
bhermann Jan 7, 2020
d9af2e3
Bumped dependencies, moved to Scala 2.13
bhermann Jan 7, 2020
32a45f0
Deactivated GraalVM specific lines. Bugfix for help text
bhermann Jan 7, 2020
0351ee8
Changes to reflect 0.9.5 API
bhermann Jan 7, 2020
f3b1a07
Changes to reflect 0.9.5 API
bhermann Jan 7, 2020
d48bf52
Added features command. Closes #25
bhermann Jan 7, 2020
70cad00
Updated README to new console parameters
bhermann Jan 7, 2020
568818e
Made a helpful runnable docker container possible
bhermann Jan 8, 2020
b7d5950
Bumped sbt and project version
bhermann Jan 8, 2020
518f76a
Reverted to sbt 1.2.8 because of travis *sigh*
bhermann Jan 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactoring search command to sttp
  • Loading branch information
Hariharan Ramanathan committed Jan 24, 2019
commit b252d4e608051ecc363a5e66c6f76f970c0990fd
2 changes: 1 addition & 1 deletion src/main/scala/de/upb/cs/swt/delphi/cli/DelphiCLI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object DelphiCLI {
config.mode match {
case "test" => TestCommand.execute
case "retrieve" => RetrieveCommand.execute
// case "search" => SearchCommand.execute(config)
case "search" => SearchCommand.execute
case x => config.consoleOutput.outputError(s"Unknown command: $x")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import scala.io.Source
* The implementation of the retrieve command.
* Retrieves the contents of the file at the endpoint specified by the config file, and prints them to stdout
*/
object RetrieveCommand extends Command with DefaultJsonProtocol {
object RetrieveCommand extends Command {


override def execute(implicit config: Config, backend: SttpBackend[Id, Nothing]): Unit = {
Expand All @@ -45,7 +45,6 @@ object RetrieveCommand extends Command with DefaultJsonProtocol {
}
}

println(config.id)
val result = executeGet(
Seq("retrieve", checkTarget),
Map("pretty" -> "")
Expand Down
152 changes: 65 additions & 87 deletions src/main/scala/de/upb/cs/swt/delphi/cli/commands/SearchCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,124 +16,102 @@

package de.upb.cs.swt.delphi.cli.commands

import java.util.concurrent.{TimeUnit, TimeoutException}

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import akka.util.ByteString
import de.upb.cs.swt.delphi.cli.Config
import java.util.concurrent.TimeUnit

import com.softwaremill.sttp._
import com.softwaremill.sttp.sprayJson._
import de.upb.cs.swt.delphi.cli.artifacts.SearchResult
import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson._
import spray.json.DefaultJsonProtocol
import de.upb.cs.swt.delphi.cli.{Config, artifacts}
import spray.json._

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContextExecutor, Future}
import scala.util.{Failure, Success, Try}

object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProtocol {
object SearchCommand extends Command with DefaultJsonProtocol{

val searchTimeout: Int = 10
val searchTimeout = 10.seconds
val TIMEOUT_CODE = 408

/**
* Executes the command implementation
*
* @param config The current configuration for the command
*/
def execute(config: Config)(implicit system: ActorSystem): Unit = {
implicit val ec = system.dispatcher
implicit val materializer = ActorMaterializer()
override def execute(implicit config: Config, backend: SttpBackend[Id, Nothing]): Unit = {

def query = config.query

information(config)(s"Searching for artifacts matching ${'"'}$query${'"'}.")
val start = System.nanoTime()
information.apply(s"Searching for artifacts matching ${'"'}$query${'"'}.")

implicit val queryFormat = jsonFormat2(Query)
val baseUri = Uri(config.server)
val prettyParam = Map("pretty" -> "")
val searchUri = baseUri.withPath(baseUri.path + "/search").withQuery(akka.http.scaladsl.model.Uri.Query(prettyParam))
val responseFuture = Marshal(Query(query, config.limit)).to[RequestEntity] flatMap { entity =>
Http().singleRequest(HttpRequest(uri = searchUri, method = HttpMethods.POST, entity = entity))
}

Try(Await.result(responseFuture, Duration(config.timeout.getOrElse(searchTimeout) + " seconds"))).
map(response => parseResponse(response, config, start)(ec, materializer)).
recover {
case e : TimeoutException => {
error(config)("The query timed out after " + (System.nanoTime() - start).nanos.toUnit(TimeUnit.SECONDS) +
" seconds. To set a longer timeout, use the --timeout option.")
Failure(e)
}
}
val queryParams = Map("pretty" -> "")
val queryPayload: Query = Query(query,config.limit)
val searchUri = uri"${config.server}/search?$queryParams"

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

val (res, time) = processRequest(request)
res.foreach(processResults(_, time))
}

private def parseResponse(response: HttpResponse, config: Config, start: Long)
(implicit ec: ExecutionContextExecutor, materializer: ActorMaterializer): Unit = {
private def processRequest(req: Request[String, Nothing])
(implicit config: Config,
backend: SttpBackend[Id, Nothing]): (Option[String], FiniteDuration) = {
val start = System.nanoTime()
val res: Id[Response[String]] = req.readTimeout(searchTimeout).send()
val end = System.nanoTime()
val took = (end - start).nanos

val resultFuture: Future[String] = response match {
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
entity.dataBytes.runFold(ByteString(""))(_ ++ _).map { body =>
body.utf8String
}
case resp@HttpResponse(code, _, _, _) => {
error(config)("Request failed, response code: " + code)
resp.discardEntityBytes()
Future("")
}
}
if (res.code == TIMEOUT_CODE) {

val result = Await.result(resultFuture, Duration.Inf)
error.apply(s"The query timed out after ${took.toSeconds} seconds. " +
"To set a longer timeout, use the --timeout option.")
}
val resStr = res.body match {
case Left(v) =>
error.apply(s"Search request failed \n $v")
println(v)
None
case Right(v) =>
Some(v)
}
(resStr, took)
}

val took = (System.nanoTime() - start).nanos.toUnit(TimeUnit.SECONDS)
private def processResults(res: String, queryRuntime: FiniteDuration)(implicit config: Config) = {

if (config.raw || result.equals("")) {
reportResult(config)(result)
if (config.raw || res.equals("")) {
reportResult.apply(res)
}
if (!(config.raw || res.equals("")) || !config.csv.equals("")) {
import artifacts.SearchResultJson._
val jsonArr = res.parseJson.asInstanceOf[JsArray].elements
val retrieveResults = jsonArr.map(r => r.convertTo[SearchResult]).toList
onProperSearchResults(retrieveResults)
}

if(!(config.raw || result.equals("")) || !config.csv.equals("")) {
val unmarshalledFuture = Unmarshal(result).to[List[SearchResult]]
def onProperSearchResults(sr: List[SearchResult]) = {

val processFuture = unmarshalledFuture.transform {
case Success(unmarshalled) => {
processResults(config, unmarshalled, took)
Success(unmarshalled)
}
case Failure(e) => {
error(config)(result)
Failure(e)
val capMessage = {
config.limit match {
case Some(limit) if (limit <= sr.size)
=> s"Results may be capped by result limit set to $limit."
case None if (sr.size >= 50)
=> "Results may be capped by default limit of 50 returned results. Use --limit to extend the result set."
case _
=> ""
}
}
}
}

private def processResults(config: Config, results: List[SearchResult], queryRuntime: Double) = {
val capMessage = {
config.limit match {
case Some(limit) if (limit <= results.size)
=> s"Results may be capped by result limit set to $limit."
case None if (results.size >= 50)
=> "Results may be capped by default limit of 50 returned results. Use --limit to extend the result set."
case _
=> ""
}
}
success(config)(s"Found ${results.size} item(s). $capMessage")
reportResult(config)(results)
success.apply(s"Found ${sr.size} item(s). $capMessage")
reportResult.apply(sr)

information(config)(f"Query took $queryRuntime%.2fs.")
information.apply(f"Query took ${queryRuntime.toUnit(TimeUnit.SECONDS)}%.2fs.")

if(!config.csv.equals("")) {
exportResult(config)(results)
information(config)("Results written to file '" + config.csv + "'")
if (!config.csv.equals("")) {
exportResult.apply(sr)
information.apply("Results written to file '" + config.csv + "'")
}
}
}

case class Query(query: String,
limit: Option[Int] = None)

}
28 changes: 28 additions & 0 deletions src/main/scala/de/upb/cs/swt/delphi/cli/commands/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package de.upb.cs.swt.delphi.cli

import spray.json._

package object commands extends DefaultJsonProtocol {


case class Query(query: String,
limit: Option[Int] = None)

implicit val queryFormat = jsonFormat2(Query)

}