Skip to content

Commit

Permalink
- Added configName to getJobs API response
Browse files Browse the repository at this point in the history
- getConfig API call now also supports path with .json ending; #39
- Added utility method to load config file from configs folder
  • Loading branch information
NTPape committed Nov 16, 2016
1 parent 3a0155e commit d1649f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
14 changes: 13 additions & 1 deletion modules/api/app/tuktu/api/utils.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package tuktu.api

import java.nio.file.{ Files, Paths }
import java.util.Date
import scala.util.Try
import scala.util.hashing.MurmurHash3
import org.joda.time.DateTime
import play.api.Play
import play.api.Play.current
import play.api.cache.Cache
import play.api.libs.json._
import play.api.libs.iteratee.Enumeratee
import play.api.libs.concurrent.Akka
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.Play
import scala.xml.{ XML, Elem, Node, NodeSeq }
import fastparse.all._

Expand Down Expand Up @@ -379,4 +381,14 @@ object utils {
// Use relative error
diff / math.min(absA + absB, Double.MaxValue) < epsilon
}

/**
* Loads a config from the config folder
*/
def loadConfig(name: String): Try[JsObject] = Try {
val path = Paths.get(
Cache.getAs[String]("configRepo").getOrElse("configs"),
name + { if (name.endsWith(".json")) "" else ".json" })
Json.parse(Files.readAllBytes(path)).as[JsObject]
}
}
53 changes: 22 additions & 31 deletions modules/restapi/app/controllers/restapi/RESTAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import java.nio.file.Paths
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.DurationInt
import scala.util.{ Try, Success, Failure }

import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.ActorRef
import akka.actor.PoisonPill
import akka.actor.Props
import akka.actor._
import akka.pattern.ask
import akka.util.Timeout
import play.api.Play
Expand All @@ -27,7 +24,6 @@ import play.api.libs.json.Json
import play.api.mvc.Action
import play.api.mvc.Controller
import tuktu.api._
import akka.pattern.ask

/**
* Controller for all REST API functionality of Tuktu
Expand Down Expand Up @@ -72,24 +68,26 @@ object RESTAPI extends Controller {
case mor: MonitorOverviewResult => {
// TODO: Extend with more fields per flow
Ok(Json.obj(
"running" -> mor.runningJobs.map(job => {
"running" -> mor.runningJobs.map { case (_, job) =>
Json.obj(
"uuid" -> job._2.uuid,
"instance" -> job._2.instances,
"start_time" -> job._2.startTime,
"finished_instances" -> job._2.finished_instances,
"end_time" -> job._2.endTime.getOrElse(null).asInstanceOf[Long]
"uuid" -> job.uuid,
"config_name" -> job.configName,
"instance" -> job.instances,
"finished_instances" -> job.finished_instances,
"start_time" -> job.startTime,
"end_time" -> job.endTime.getOrElse(null).asInstanceOf[Long]
)
}),
"finished" -> mor.finishedJobs.map(job => {
},
"finished" -> mor.runningJobs.map { case (_, job) =>
Json.obj(
"uuid" -> job._2.uuid,
"instance" -> job._2.instances,
"start_time" -> job._2.startTime,
"finished_instances" -> job._2.finished_instances,
"end_time" -> job._2.endTime.getOrElse(null).asInstanceOf[Long]
"uuid" -> job.uuid,
"config_name" -> job.configName,
"instance" -> job.instances,
"finished_instances" -> job.finished_instances,
"start_time" -> job.startTime,
"end_time" -> job.endTime.getOrElse(null).asInstanceOf[Long]
)
})
}
))
}
}
Expand All @@ -99,17 +97,10 @@ object RESTAPI extends Controller {
* Gets the config for a specific job
*/
def getConfig(name: String) = Action.async { Future {
// Read config from disk
val filename = Cache.getAs[String]("configRepo").getOrElse("configs") + "/" + name + ".json"
if (Files.exists(Paths.get(filename))) {
val configFile = scala.io.Source.fromFile(Cache.getAs[String]("configRepo").getOrElse("configs") +
"/" + name + ".json", "utf-8")
val cfg = Json.parse(configFile.mkString).as[JsObject]
configFile.close

Ok(cfg)
} else {
NotFound(Json.obj("error" -> "The specified config file could not be found."))
utils.loadConfig(name) match {
case Success(obj) => Ok(obj)
case Failure(e: java.io.FileNotFoundException) => NotFound(Json.obj("error" -> "The specified config file could not be found."))
case Failure(e) => BadRequest(Json.obj("error" -> e.getMessage))
}
}}

Expand Down

0 comments on commit d1649f5

Please sign in to comment.