Skip to content
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

Instrumenting with stats around the serialization of json. #356

Merged
merged 1 commit into from
Aug 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 19 additions & 9 deletions app/collins/controllers/ApiResponse.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package collins.controllers

import scala.concurrent.Future

import play.api.mvc.Result

import play.api.libs.json.JsArray
import play.api.libs.json.JsBoolean
import play.api.libs.json.JsNull
Expand All @@ -9,18 +13,20 @@ import play.api.libs.json.JsString
import play.api.libs.json.JsUndefined
import play.api.libs.json.JsValue
import play.api.libs.json.Json

import play.api.mvc.AnyContent
import play.api.mvc.Controller
import play.api.mvc.Request
import play.api.mvc.Results

import collins.util.Stats
import collins.util.BashOutput
import collins.util.HtmlOutput
import collins.util.JsonOutput
import collins.util.OutputType
import collins.util.OutputType.contentTypeWithCharset
import collins.util.TextOutput
import scala.concurrent.Future
import play.api.mvc.Result


object ApiResponse extends ApiResponse {
import OutputType.contentTypeWithCharset
Expand Down Expand Up @@ -125,19 +131,23 @@ trait ApiResponse extends Controller {
case o: BashOutput =>
response.status(formatBashResponse(response.data) + "\n").as(contentTypeWithCharset(o)).withHeaders(response.headers:_*)
case o: JsonOutput =>
val rewritten = ApiResponse.isJsonErrorMessage(response.data) match {
case true => response.data
case false => ApiResponse.formatJsonMessage(response.status, response.data)
}
response.status(Json.stringify(rewritten)).as(contentTypeWithCharset(o)).withHeaders(response.headers:_*)
response.status(formatJsonResponse(response.status, response.data)).as(contentTypeWithCharset(o)).withHeaders(response.headers:_*)
case o: HtmlOutput =>
val e = new Exception("Unhandled view")
e.printStackTrace()
throw e
}
}

protected def formatBashResponse(jsobject: JsValue, prefix: String = ""): String = {
private[this] def formatJsonResponse(status: Results.Status, jsobject: JsValue): String = Stats.time("Response.AsJson") {
val rewritten = ApiResponse.isJsonErrorMessage(jsobject) match {
case true => jsobject
case false => ApiResponse.formatJsonMessage(status, jsobject)
}
Json.stringify(rewritten)
}

private[this] def formatBashResponse(jsobject: JsValue, prefix: String = ""): String = Stats.time("Response.AsBash") {
def formatBasic(jsvalue: JsValue): String = {
jsvalue match {
case JsNull => ""
Expand Down Expand Up @@ -194,7 +204,7 @@ trait ApiResponse extends Controller {
}.mkString("\n")
}

protected def formatTextResponse(jsobject: JsValue, depth: Int = 0): String = {
private[this] def formatTextResponse(jsobject: JsValue, depth: Int = 0): String = Stats.time("Response.AsText") {
def formatBasic(jsvalue: JsValue): String = {
jsvalue match {
case JsNull => "null"
Expand Down
26 changes: 10 additions & 16 deletions app/collins/models/AssetType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import play.api.libs.json.JsSuccess
import play.api.libs.json.JsValue
import play.api.libs.json.Json

import collins.util.Stats

import collins.models.cache.Cache
import collins.models.shared.AnormAdapter
import collins.models.shared.ValidatedEntity
Expand All @@ -35,20 +33,16 @@ object AssetType extends Schema with AnormAdapter[AssetType] with AssetTypeKeys
))

implicit object AssetTypeFormat extends Format[AssetType] {
override def reads(json: JsValue) = Stats.time("AssetType.Reads") {
JsSuccess(AssetType(
(json \ "NAME").as[String],
(json \ "LABEL").as[String],
(json \ "ID").asOpt[Int].getOrElse(0)
))
}
override def writes(at: AssetType) = Stats.time("AssetType.Writes") {
JsObject(Seq(
"ID" -> Json.toJson(at.id),
"NAME" -> Json.toJson(at.name),
"LABEL" -> Json.toJson(at.label)
))
}
override def reads(json: JsValue) = JsSuccess(AssetType(
(json \ "NAME").as[String],
(json \ "LABEL").as[String],
(json \ "ID").asOpt[Int].getOrElse(0)
))
override def writes(at: AssetType) = JsObject(Seq(
"ID" -> Json.toJson(at.id),
"NAME" -> Json.toJson(at.name),
"LABEL" -> Json.toJson(at.label)
))
}

def findById(id: Int): Option[AssetType] = Cache.get(findByIdKey(id), inTransaction {
Expand Down
36 changes: 15 additions & 21 deletions app/collins/models/State.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import play.api.libs.json.JsSuccess
import play.api.libs.json.JsValue
import play.api.libs.json.Json

import collins.util.Stats

import collins.models.cache.Cache
import collins.validation.Pattern.isAlphaNumericString
import collins.validation.Pattern.isNonEmptyString

import collins.models.cache.Cache
import collins.models.shared.AnormAdapter
import collins.models.shared.ValidatedEntity
import collins.models.Status.StatusFormat
Expand All @@ -36,24 +34,20 @@ object State extends Schema with AnormAdapter[State] with StateKeys {

implicit object StateFormat extends Format[State] {
import Status.StatusFormat
override def reads(json: JsValue) = Stats.time("State.Reads") {
JsSuccess(State(
(json \ "ID").asOpt[Int].getOrElse(0),
(json \ "STATUS").asOpt[Int].getOrElse(ANY_STATUS),
(json \ "NAME").as[String],
(json \ "LABEL").as[String],
(json \ "DESCRIPTION").as[String]
))
}
override def writes(state: State) = Stats.time("State.Writes") {
JsObject(Seq(
"ID" -> Json.toJson(state.id),
"STATUS" -> Json.toJson(Status.findById(state.status)),
"NAME" -> Json.toJson(state.name),
"LABEL" -> Json.toJson(state.label),
"DESCRIPTION" -> Json.toJson(state.description)
))
}
override def reads(json: JsValue) = JsSuccess(State(
(json \ "ID").asOpt[Int].getOrElse(0),
(json \ "STATUS").asOpt[Int].getOrElse(ANY_STATUS),
(json \ "NAME").as[String],
(json \ "LABEL").as[String],
(json \ "DESCRIPTION").as[String]
))
override def writes(state: State) = JsObject(Seq(
"ID" -> Json.toJson(state.id),
"STATUS" -> Json.toJson(Status.findById(state.status)),
"NAME" -> Json.toJson(state.name),
"LABEL" -> Json.toJson(state.label),
"DESCRIPTION" -> Json.toJson(state.description)
))
}
override val tableDef = table[State]("state")
on(tableDef)(s => declare(
Expand Down
26 changes: 10 additions & 16 deletions app/collins/models/Status.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import play.api.libs.json.JsSuccess
import play.api.libs.json.JsValue
import play.api.libs.json.Json

import collins.util.Stats

import collins.models.cache.Cache
import collins.models.shared.AnormAdapter
import collins.models.shared.ValidatedEntity
Expand Down Expand Up @@ -43,20 +41,16 @@ object Status extends Schema with AnormAdapter[Status] with StatusKeys {
def Unallocated = Status.findByName("Unallocated")

implicit object StatusFormat extends Format[Status] {
override def reads(json: JsValue) = Stats.time("Status.Reads") {
JsSuccess(Status(
(json \ "NAME").as[String],
(json \ "DESCRIPTION").as[String],
(json \ "ID").as[Int]
))
}
override def writes(status: Status) = Stats.time("Status.Writes") {
JsObject(Seq(
"ID" -> JsNumber(status.id),
"NAME" -> JsString(status.name),
"DESCRIPTION" -> JsString(status.description)
))
}
override def reads(json: JsValue) = JsSuccess(Status(
(json \ "NAME").as[String],
(json \ "DESCRIPTION").as[String],
(json \ "ID").as[Int]
))
override def writes(status: Status) = JsObject(Seq(
"ID" -> JsNumber(status.id),
"NAME" -> JsString(status.name),
"DESCRIPTION" -> JsString(status.description)
))
}

override val tableDef = table[Status]("status")
Expand Down
7 changes: 2 additions & 5 deletions app/collins/models/asset/AllAttributes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import play.api.libs.json.JsString
import play.api.libs.json.JsValue
import play.api.libs.json.Json

import collins.util.Stats

import collins.models.Asset
import collins.models.AssetMetaValue
import collins.models.IpAddresses
Expand All @@ -17,7 +15,6 @@ import collins.models.MetaWrapper
import collins.models.PowerHelper
import collins.models.conversions.IpAddressFormat
import collins.models.conversions.IpmiFormat

import collins.util.LldpRepresentation
import collins.util.LshwRepresentation
import collins.util.config.Feature
Expand All @@ -26,7 +23,7 @@ import collins.util.power.PowerUnits
import collins.util.power.PowerUnits

object AllAttributes {
def get(asset: Asset): AllAttributes = Stats.time("Asset.GetAllAttributes") {
def get(asset: Asset): AllAttributes = {
if (asset.isConfiguration) {
AllAttributes(asset,
LshwRepresentation.empty,
Expand Down Expand Up @@ -74,7 +71,7 @@ case class AllAttributes(
}
}

def toJsValue(): JsValue = Stats.time("SerializeAsset.AllAttributes") {
def toJsValue(): JsValue = {
val outSeq = Seq(
"ASSET" -> asset.toJsValue,
"HARDWARE" -> lshw.toJsValue,
Expand Down
46 changes: 20 additions & 26 deletions app/collins/models/asset/conversions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import play.api.libs.json.JsSuccess
import play.api.libs.json.JsValue
import play.api.libs.json.Json

import collins.util.Stats

import collins.models.Asset
import collins.models.AssetType
import collins.models.State
Expand All @@ -22,29 +20,25 @@ import collins.models.conversions.TimestampFormat
object conversions {
import collins.models.State.StateFormat
implicit object AssetFormat extends Format[AssetView] {
override def reads(json: JsValue) = Stats.time("AssetView.Reads") {
JsSuccess(Asset(
(json \ "TAG").as[String],
Status.findByName((json \ "STATUS").as[String]).map(_.id).get,
AssetType.findByName((json \ "TYPE").as[String]).map(_.id).get,
(json \ "CREATED").as[Timestamp],
(json \ "UPDATED").asOpt[Timestamp],
(json \ "DELETED").asOpt[Timestamp],
(json \ "ID").as[Long],
(json \ "STATE").asOpt[State].map(_.id).getOrElse(0)
))
}
override def writes(asset: AssetView): JsObject = Stats.time("AssetView.Writes") {
JsObject(Seq(
"ID" -> JsNumber(asset.id),
"TAG" -> JsString(asset.tag),
"STATE" -> Json.toJson(State.findById(asset.stateId)),
"STATUS" -> JsString(asset.getStatusName),
"TYPE" -> Json.toJson(AssetType.findById(asset.assetTypeId).map(_.name)),
"CREATED" -> Json.toJson(asset.created),
"UPDATED" -> Json.toJson(asset.updated),
"DELETED" -> Json.toJson(asset.deleted)
))
}
override def reads(json: JsValue) = JsSuccess(Asset(
(json \ "TAG").as[String],
Status.findByName((json \ "STATUS").as[String]).map(_.id).get,
AssetType.findByName((json \ "TYPE").as[String]).map(_.id).get,
(json \ "CREATED").as[Timestamp],
(json \ "UPDATED").asOpt[Timestamp],
(json \ "DELETED").asOpt[Timestamp],
(json \ "ID").as[Long],
(json \ "STATE").asOpt[State].map(_.id).getOrElse(0)
))
override def writes(asset: AssetView): JsObject = JsObject(Seq(
"ID" -> JsNumber(asset.id),
"TAG" -> JsString(asset.tag),
"STATE" -> Json.toJson(State.findById(asset.stateId)),
"STATUS" -> JsString(asset.getStatusName),
"TYPE" -> Json.toJson(AssetType.findById(asset.assetTypeId).map(_.name)),
"CREATED" -> Json.toJson(asset.created),
"UPDATED" -> Json.toJson(asset.updated),
"DELETED" -> Json.toJson(asset.deleted)
))
}
}
Loading