Skip to content

Commit

Permalink
Scala Common Enrich: converting transactions from given currency to h…
Browse files Browse the repository at this point in the history
…ome currency (closes snowplow#370)
  • Loading branch information
AALEKH committed Feb 6, 2015
1 parent e2726e2 commit ffd272c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ object EnrichmentManager {
val currencyConversion = (trCurrency |@| trAmounts |@| tiCurrency |@| tiPrice) {
currency.convertCurrencies(_, _, _, _)
}
// TODO: remove val assignment
currencyConversion
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package common
package enrichments
package registry

import utils.{ConversionUtils => CU}

import java.lang.{Integer => JInteger}
import java.math.{BigDecimal => JBigDecimal}
import java.lang.{Byte => JByte}
Expand Down Expand Up @@ -107,6 +109,31 @@ case class CurrencyConversionEnrichment(
}
}

/*
val newCurrencyTr = fx.convert(toDouble(trAmounts.total), trCurrency).to(baseCurrency).nowish
+ val newCurrencyTi = fx.convert(toDouble(tiPrice), trCurrency).to(baseCurrency).nowish
+ val newTrAmountsTax = fx.convert(toDouble(trAmounts.tax), trCurrency).to(baseCurrency).nowish
+ val newTrAmountsShipping = fx.convert(toDouble(trAmounts.shipping), trCurrency).to(baseCurrency).nowish
+ val transactionAmounts = (eitherToValidation(newCurrencyTr).toValidationNel |@| eitherToValidation(newTrAmountsTax).toValidationNel |@| eitherToValidation(newTrAmountsShipping).toValidationNel ) { //e
+ TransactionAmounts(_, _, _)
+ }
+ val trasactionAmountsTuple = (transactionAmounts |@| eitherToValidation(newCurrencyTi).toValidationNel) { //e
+ (_, _)
+ }
+ trasactionAmountsTuple
(trCurrency.toValidationNel |@| CU.stringToDouble("Error in Tr Amount Tax",trAmounts.tax).toValidationNel |@| CU.stringToDouble("Error in Tr Amount Tax",trAmounts.shipping).toValidationNel |@| CU.stringToDouble("Error in TI Price",tiPrice).toValidationNel |@| CU.stringToDouble("Error in TI Price",trAmounts.total).toValidationNel) { //e
*/
def returnTransactionAmount(fx: Forex, trCurrency: String, baseCurrency: String, newTax: Double, newShippingDouble: Double, newTiDouble: Double, newTotalDouble: Double) ={
val newCurrencyTr = fx.convert(newTotalDouble, trCurrency).to(baseCurrency).nowish
val newCurrencyTi = fx.convert(newTiDouble, trCurrency).to(baseCurrency).nowish
val newTrAmountsTax = fx.convert(newTax, trCurrency).to(baseCurrency).nowish
val newTrAmountsShipping = fx.convert(newShippingDouble, trCurrency).to(baseCurrency).nowish
val trasactionAmountsTuple = (eitherToValidation(newCurrencyTr).toValidationNel |@| eitherToValidation(newTrAmountsTax).toValidationNel |@| eitherToValidation(newTrAmountsShipping).toValidationNel |@| eitherToValidation(newCurrencyTi).toValidationNel) { //e
(_, _, _, _)
}
trasactionAmountsTuple
}
/**
* Convert's currency for a given
* set of currency, using
Expand All @@ -127,22 +154,16 @@ case class CurrencyConversionEnrichment(
* format and Ttansaction
* Item Price
*/
def convertCurrencies(trCurrency: String, trAmounts: TransactionAmounts, tiCurrency: String, tiPrice: String): ValidationNel[String, (TransactionAmounts, String)] = {

def convertCurrencies(trCurrency: String, trAmounts: TransactionAmounts, tiCurrency: String, tiPrice: String): ValidationNel[String,(String, String, String, String)] = {

try{
val fx = Forex(ForexConfig( nowishCacheSize = 0, nowishSecs = 0, eodCacheSize= 0, baseCurrency = baseCurrency), OerClientConfig(apiKey, DeveloperAccount))
val newCurrencyTr = fx.convert(toDouble(trAmounts.total), trCurrency).to(baseCurrency).nowish
val newCurrencyTi = fx.convert(toDouble(tiPrice), trCurrency).to(baseCurrency).nowish
val newTrAmountsTax = fx.convert(toDouble(trAmounts.tax), trCurrency).to(baseCurrency).nowish
val newTrAmountsShipping = fx.convert(toDouble(trAmounts.shipping), trCurrency).to(baseCurrency).nowish
val transactionAmounts = (eitherToValidation(newCurrencyTr).toValidationNel |@| eitherToValidation(newTrAmountsTax).toValidationNel |@| eitherToValidation(newTrAmountsShipping).toValidationNel ) { //e
TransactionAmounts(_, _, _)
}
val trasactionAmountsTuple = (transactionAmounts |@| eitherToValidation(newCurrencyTi).toValidationNel) { //e
(_, _)
}
trasactionAmountsTuple


val returnConverted = (CU.stringToDouble("Error in Tr Amount Tax",trAmounts.tax) |@| CU.stringToDouble("Error in Tr Amount Tax",trAmounts.shipping) |@| CU.stringToDouble("Error in TI Price",tiPrice) |@| CU.stringToDouble("Error in TI Price",trAmounts.total)) { //e
returnTransactionAmount(fx, trCurrency, baseCurrency,_, _, _, _)
}
returnConverted
} catch {
case e : NoSuchElementException =>"Provided Currency not supported : %s".format(e).failNel
case f : UnknownHostException => "Could not extract Convert Currencies from OER Service :%s".format(f).failNel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,30 @@ object ConversionUtils {
"Field [%s]: cannot convert [%s] to Double-like String".format(field, str).fail
}

/**
* Convert a String to a String containing a
* Redshift-compatible Double.
*
* @param str The String which we hope contains
* a Double
* @param field The name of the field we are
* validating. To use in our error message
* @return a Scalaz Validation, being either
* a Failure String or a Success Double
*/
def stringToDouble(field: String,str: String):Validation[String, Double] = {
try {
if (Option(str).isEmpty || str == "null") { // "null" String check is LEGACY to handle a bug in the JavaScript tracker
null.asInstanceOf[Double].success
} else {
val jbigdec = new JBigDecimal(str)
jbigdec.doubleValue().success
}
} catch {
case nfe: NumberFormatException =>
"Field [%s]: cannot convert [%s] to Double-like String".format(field, str).fail
}
}
/**
* Extract a Java Byte representing
* 1 or 0 only from a String, or error.
Expand Down

0 comments on commit ffd272c

Please sign in to comment.