##  Stats to compare: 
##  
##      orchard streams
##      spotify streams
##      RED Streams
##      top50 streams
##      number of users
##  
##      free user
##      paid user
##      active / dormant
##  



computeRatioCols_ <- function(DT) {
## DT.users is a DT that will get aggregated at a few different points. 
## Certain Ratio and Percentage columns would be incorrect to add. 
## Thus after aggregating, run this. 


##  We will look at 
##  
##    * activeRatio ::  active_users / registered_users
##  
##    * MarketShare_byStreams :: orchard streams / total streams
##    * MarketShare_byRevenue :: orchard revenue / total revenue
##  
##    * Spot_RPaU        ::  Spot revenue / active user
##    * Orch_RPaU        ::  Orch revenue / active user
##    * Spot_RPrU        ::  Spot revenue / registered user
##    * Orch_RPrU        ::  Orch revenue / registered user
##  
##    * Spot_RPtS        ::  Spot revenue / total_streams
##    * Orch_RPtS        ::  Orch revenue / total_streams   <<~~~~~
##    * Orch_RPoS        ::  Orch revenue / orchard_and_osc_streams
##  
##  


  ratioCols <- c("activeRatio", "MarketShare_byStreams", "MarketShare_byRevenue", "Spot_RPaU", "Orch_RPaU", "Spot_RPrU", "Orch_RPrU", "Spot_RPtS", "Orch_RPtS", "Spot_RPoS", "Orch_RPoS")
  assign("ratioCols", ratioCols, envir=globalenv())

  DT[, activeRatio := active_users / registered_users]

  DT[, MarketShare_byStreams := orchard_and_osc_streams     / total_streams]
  DT[, MarketShare_byRevenue := orchard_and_osc_revenue_usd / spotify_net_revenue_usd]

  DT[, Spot_RPaU := spotify_net_revenue_usd     / active_users]
  DT[, Orch_RPaU := orchard_and_osc_revenue_usd / active_users]
  DT[, Spot_RPrU := spotify_net_revenue_usd     / registered_users]
  DT[, Orch_RPrU := orchard_and_osc_revenue_usd / registered_users]

  DT[, Spot_RPtS := spotify_net_revenue_usd     / total_streams]
  DT[, Orch_RPtS := orchard_and_osc_revenue_usd / total_streams]
  DT[, Spot_RPoS := spotify_net_revenue_usd     / total_streams]
  DT[, Orch_RPoS := orchard_and_osc_revenue_usd / orchard_and_osc_streams]


  ## Clean up MarketShare_byRevenue as some division by 0 issues arrise. 
  ## Also drop any extreme outliers with tiny revenue
  DT[!is.finite(MarketShare_byRevenue), MarketShare_byRevenue := NA]
  DT[spotify_net_revenue_usd < 2000 & MarketShare_byRevenue > 2, MarketShare_byRevenue := NA]

  ## Confirm that MarketShare_byStreams does not have the same issues as MarketShare_byRevenue
  if (any(DT[, !is.finite(MarketShare_byStreams)])) warning ("DT has MarketShare_byStreams with non-finite values")
  if (any(DT[, MarketShare_byStreams > 1])) warning ("DT has MarketShare_byStreams greater than 1 for some row(s)")

  ## All newly added columns will be given class "perc"
  DT[, (ratioCols) := lapply(.SD, as.perc), .SDcols=ratioCols]

  return(invisible(DT))
}
