Use external metrics library

This commit is contained in:
Everett Sochowski 2014-03-27 14:30:45 +01:00
parent dc8446a405
commit b25200c382
9 changed files with 60 additions and 97 deletions

View File

@ -153,6 +153,11 @@
<groupId>net.liftmodules</groupId>
<artifactId>amqp_2.6_${scala.version}</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.tesobe</groupId>
<artifactId>OBP-Mongo-Metrics</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -54,6 +54,9 @@ import javax.mail.{ Authenticator, PasswordAuthentication }
import java.io.FileInputStream
import java.io.File
import code.model.dataAccess.BankAccountCreationListener
import code.metrics.Metrics
import code.metrics.MongoMetric
import code.metrics.MongoMetrics
/**
* A class that's instantiated early and run. It allows the application
* to modify lift's environment
@ -233,5 +236,6 @@ class Boot extends Loggable{
val useMessageQueue = Props.getBool("messageQueue.createBankAccounts", false)
if(useMessageQueue)
BankAccountCreationListener.startListen
}
}

View File

@ -63,7 +63,7 @@ import net.liftweb.mongodb.{ Skip, Limit }
import _root_.net.liftweb.http.S._
import _root_.net.liftweb.mapper.view._
import com.mongodb._
import code.model.dataAccess.{ Account, OBPEnvelope, OBPUser,APIMetric, HostedAccount, LocalStorage}
import code.model.dataAccess.{ Account, OBPEnvelope, OBPUser, HostedAccount, LocalStorage}
import code.model.{ModeratedTransaction, ModeratedBankAccount, View, BankAccount, Bank, User}
import code.model.dataAccess.OBPEnvelope._
import java.util.Date
@ -73,6 +73,7 @@ import net.liftweb.json.Extraction
import _root_.net.liftweb.json.Serialization
import net.liftweb.json.NoTypeHints
import code.api.OAuthHandshake.getUser
import code.injections.MetricsInjector
object OBPAPI1_0 extends RestHelper with Loggable {
@ -80,12 +81,10 @@ object OBPAPI1_0 extends RestHelper with Loggable {
val dateFormat = ModeratedTransaction.dateFormat
private def logAPICall =
APIMetric.createRecord.
url(S.uriAndQueryString.getOrElse("")).
date((now: TimeSpan)).
save
private def logAPICall = {
MetricsInjector.m.vend.logAPICall(S.uriAndQueryString.getOrElse(""), now)
}
serve("obp" / "v1.0" prefix {
case Nil JsonGet json => {

View File

@ -59,9 +59,9 @@ import com.mongodb._
import code.model._
import java.util.Date
import code.api.OAuthHandshake._
import code.model.dataAccess.APIMetric
import code.model.dataAccess.OBPEnvelope.{OBPOrder, OBPLimit, OBPOffset, OBPOrdering, OBPFromDate, OBPToDate, OBPQueryParam}
import java.net.URL
import code.injections.MetricsInjector
case class TagJSON(
value : String,
@ -162,11 +162,9 @@ object OBPAPI1_1 extends RestHelper with Loggable {
}
}
private def logAPICall =
APIMetric.createRecord.
url(S.uriAndQueryString.getOrElse("")).
date((now: TimeSpan)).
save
private def logAPICall = {
MetricsInjector.m.vend.logAPICall(S.uriAndQueryString.getOrElse(""), now)
}
private def isFieldAlreadySet(field : String) : Box[String] =
if(field.isEmpty)

View File

@ -38,9 +38,9 @@ import net.liftweb.http.rest._
import net.liftweb.json.Extraction
import net.liftweb.json.JsonAST._
import _root_.net.liftweb.util.Helpers._
import code.model.dataAccess.APIMetric
import java.util.Date
import java.util.Calendar
import code.injections.MetricsInjector
case class APICallAmount(
url: String,
@ -61,34 +61,12 @@ object Metrics extends RestHelper {
serve("obp" / "metrics" prefix {
case "demo-bar" :: Nil JsonGet json => {
def byURL(metric : APIMetric) : String =
metric.url.get
def byUsage(x : APICallAmount, y : APICallAmount) =
x.amount > y.amount
val results = APICallAmounts(APIMetric.findAll.groupBy[String](byURL).toSeq.map(t => APICallAmount(t._1,t._2.length)).toList.sortWith(byUsage))
val results = MetricsInjector.m.vend.getAPICallAmounts
JsonResponse(Extraction.decompose(results))
}
case "demo-line" :: Nil JsonGet json => {
def byDay(metric : APIMetric) : Date = {
val metricDate = metric.date.get
val cal = Calendar.getInstance()
cal.setTime(metricDate)
cal.set(Calendar.HOUR,0)
cal.set(Calendar.MINUTE,0)
cal.set(Calendar.SECOND,0)
cal.set(Calendar.MILLISECOND,0)
cal.getTime
}
def byOldestDate(x : APICallsForDay, y : APICallsForDay) : Boolean =
x.date before y.date
val results = APICallsPerDay(APIMetric.findAll.groupBy[Date](byDay).toSeq.map(t => APICallsForDay(t._2.length,t._1)).toList.sortWith(byOldestDate))
val results = MetricsInjector.m.vend.getAPICallsPerDay
JsonResponse(Extraction.decompose(results))
}

View File

@ -0,0 +1,19 @@
package code.injections
import net.liftweb.util.SimpleInjector
import code.metrics.Metrics
import code.metrics.MongoMetrics
import code.model.dataAccess.MongoConfig
object MetricsInjector extends SimpleInjector {
private val mongoMetrics =
new MongoMetrics(
host = MongoConfig.host,
port = MongoConfig.port,
dbName = MongoConfig.dbName)
def buildOne: Metrics = mongoMetrics
val m = new Inject(buildOne _) {}
}

View File

@ -41,18 +41,25 @@ object AdminDb extends MongoIdentifier {
}
object MongoConfig {
def init: Unit = {
val srvr = new ServerAddress(
Props.get("mongo.host", "localhost"),
Props.getInt("mongo.port", 27017)
)
val defaultDatabase =
lazy val defaultDatabase =
Props.mode match {
case Props.RunModes.Test => "test"
case _ => "OBP006"
}
lazy val host = Props.get("mongo.host", "localhost")
lazy val port = Props.getInt("mongo.port", 27017)
lazy val dbName = Props.get("mongo.dbName", defaultDatabase)
def init: Unit = {
val srvr = new ServerAddress(
host,
port
)
MongoDB.defineDb(DefaultMongoIdentifier, new Mongo(srvr), Props.get("mongo.dbName", defaultDatabase))
MongoDB.defineDb(DefaultMongoIdentifier, new Mongo(srvr), dbName)
MongoDB.defineDb(AdminDb, new Mongo(srvr), "admin")
}
}

View File

@ -1,45 +0,0 @@
/**
Open Bank Project - API
Copyright (C) 2011, 2013, TESOBE / Music Pictures Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Email: contact@tesobe.com
TESOBE / Music Pictures Ltd
Osloerstrasse 16/17
Berlin 13359, Germany
This product includes software developed at
TESOBE (http://www.tesobe.com/)
by
Simon Redfern : simon AT tesobe DOT com
Stefan Bethge : stefan AT tesobe DOT com
Everett Sochowski : everett AT tesobe DOT com
Ayoub Benali: ayoub AT tesobe DOT com
*/
package code.model.dataAccess
import net.liftweb.mongodb.record.field.{ObjectIdPk,DateField}
import net.liftweb.record.field.{StringField}
import net.liftweb.mongodb.record.{MongoRecord,MongoMetaRecord}
class APIMetric extends MongoRecord[APIMetric] with ObjectIdPk[APIMetric] {
def meta = APIMetric
object url extends StringField(this,255)
object date extends DateField(this)
}
object APIMetric extends APIMetric with MongoMetaRecord[APIMetric]

View File

@ -32,7 +32,6 @@ Berlin 13359, Germany
package code.util
import code.model.dataAccess.APIMetric
import code.api.v1_2.ErrorMessage
import net.liftweb.http.JsonResponse
import net.liftweb.json.Extraction
@ -43,6 +42,7 @@ import net.liftweb.util.Helpers._
import net.liftweb.http.S
import net.liftweb.http.js.JE.JsRaw
import scala.collection.JavaConversions.asScalaSet
import code.injections.MetricsInjector
object APIUtil {
@ -65,12 +65,10 @@ object APIUtil {
case _ => false
}
}
def logAPICall =
APIMetric.createRecord.
url(S.uriAndQueryString.getOrElse("")).
date((now: TimeSpan)).
save
def logAPICall = {
MetricsInjector.m.vend.logAPICall(S.uriAndQueryString.getOrElse(""), now)
}
def gitCommit : String = {
val commit = tryo{