# Product Shares.r

folder.plots <- plots.p("Feb 2015")

ProductsUsing <- c("A", "P", "PD", "U", "KHB", "PD", "TSA", "VFHB")
prodCols <- c("product", "product_description")
ctryCols <- c("region_group", "country")
userCols <- c("active_users", "registered_users")
marketShareCols <- c("MarketShare_byStreams", "MarketShare_byRevenue")

# assignWithInfo("DT.productshare", copy(DT.users))
# colsToKeep <- c("date", "revenue_share", prodCols, ctryCols, userCols)
# suppressWarnings(DT.productshare[, nwhich(sapply(DT.productshare, is.perc)) := NULL])


## Create monthly aggregate
DT.productshare.agg_monthly <- aggregate_and_clean(DT.users, exclude=c(ctryCols, "revenue_share"))

## add year to the table
DT.productshare.agg_monthly[, year := factor(year(date))]
setkeyIfNot(DT.productshare.agg_monthly, c("year", key(DT.productshare.agg_monthly)), verbose=FALSE, organize=TRUE)

## Create yearly aggregate
DT.productshare.agg_yearly <- aggregate_and_clean(DT.productshare.agg_monthly[date < "2015-01-01"], exclude=("date"))

## identify (1) the top products by Orch Revenue last month
TopProducts.byRevDec2014 <- DT.productshare.agg_monthly[date == "2014-12-01"][order(orchard_and_osc_revenue_usd, decreasing=TRUE), as.character(unique(product))]
## identify (2) the top products by change in MarketShare_byStreams
stopifnot( DT.productshare.agg_yearly[, .N <= 2, by=product][, V1])
TopProducts.byDelta_MarketShare_2014v2013 <- DT.productshare.agg_yearly[, if (.N == 2) MarketShare_byStreams[year == 2014] - MarketShare_byStreams[year == 2013] else NA_real_, by=product][order(V1, decreasing=TRUE), as.character(unique(product))]

## All products other than top products will be labeled "Other"
TopProducts.using <- c(TopProducts.byRevDec2014[1:4]
                    , setdiff(TopProducts.byDelta_MarketShare_2014v2013, TopProducts.byRevDec2014[1:4])[1:4])

## Clean up the product names
DT.productshare <- aggregate_and_clean(DT.users, products_to_include=TopProducts.using, drop_other=FALSE)
DT.productshare.agg_monthly  <- aggregate_and_clean(DT.productshare.agg_monthly, products_to_include=TopProducts.using, drop_other=FALSE)
DT.productshare.agg_yearly   <- aggregate_and_clean(DT.productshare.agg_yearly,  products_to_include=TopProducts.using, drop_other=FALSE)

### Active User Ratio
(P.ActiveUserRatio_by_Product <- ggLinegraph(DT.productshare.agg_monthly, y="activeRatio", ylims=c(0, 1.5), title="Percent of Registered Users that Are Active", title_append="by Product type - aggregated across all territories", x_relative=.7, ylab_line3="It is unclear why some products have ratios above 100%", smooth=TRUE, x="date", color="product_description", size=.75, dotsize.scale=1, xlab_line3="Dots are exact value. Line is 3-month MA"))
printToPDF(P.ActiveUserRatio_by_Product, f.dir=folder.plots)

## Market Share by Revenue and Streams
P.MarketShare_Absolute_by_Product <- list(
    ggLinegraph(DT.productshare.agg_monthly, y="MarketShare_byRevenue", ylims=c(2, 10) / 100, title="Revenue-based Market Share", x_relative=.7, smooth=TRUE, x="date", color=c(` ` = "product_description"), size=.75, dotsize.scale=1, xlab_line3="Dots are exact value. Line is 3-month MA") 
  , ggLinegraph(DT.productshare.agg_monthly, y="MarketShare_byStreams", ylims=c(2, 10) / 100, title="Stream-based Market Share",  x_relative=.7, smooth=TRUE, x="date", color=c(` ` = "product_description"), size=.75, dotsize.scale=1, xlab_line3="Dots are exact value. Line is 3-month MA")
  )
printToPDF(P.MarketShare_Absolute_by_Product, f.dir=folder.plots, ncol=2, legend.plot=TRUE, legend.position="top", height=8, main="Market Share by Product type, aggregated across all territories")

## Montly Change Market Share by Revenue and Streams
tmp_DT.plot <- DT.productshare.agg_monthly[order(date, descreasing=FALSE)][, (marketShareCols) := lapply(.SD, percentIncrease), .SDcols=marketShareCols]
ylims <- c(-45, 185) / 100
P.MarketShare_MonthlyChange_by_Product <- list(
    ggLinegraph(tmp_DT.plot[!is.na(MarketShare_byRevenue)], y="MarketShare_byRevenue", ylims=ylims, title="Revenue-based Market Share", x_relative=.7, smooth=TRUE, x="date", color=c(` ` = "product_description"), size=.75, dotsize.scale=1, xlab_line3="Dots are exact value. Line is 3-month MA") 
  , ggLinegraph(tmp_DT.plot[!is.na(MarketShare_byStreams)], y="MarketShare_byStreams", ylims=ylims, title="Stream-based Market Share",  x_relative=.7, smooth=TRUE, x="date", color=c(` ` = "product_description"), size=.75, dotsize.scale=1, xlab_line3="Dots are exact value. Line is 3-month MA")
  )
printToPDF(P.MarketShare_MonthlyChange_by_Product, f.dir=folder.plots, ncol=2, legend.plot=TRUE, legend.position="top", height=8, main="Percent of Change in Spotify Market Share\nOrchard+RED by Product type, aggregated across all territories")



# 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
#   * Spot_RPrU        ::  Spot revenue / registered user
#   * Orch_RPaU        ::  Orch revenue / active user
#   * Orch_RPrU        ::  Orch revenue / registered user
# 
#   * Spot_RPsS        ::  Spot revenue / total_streams
#   * Orch_RPsS        ::  Orch revenue / total_streams   <<~~~~~
#   * Orch_RPoS        ::  Orch revenue / orchard_and_osc_streams
# 
# 
# 
# Calculate Percent of total orchard revenue by 
# DT.productshare[, ]
# 
# 
#   DT[, Spotify_RPaU := spotify_net_revenue_usd / active_users]
#   DT[, Orchard_RPaU := orchard_and_osc_revenue_usd / active_users]
# 
#   DT[, Spotify_RPtS := spotify_net_revenue_usd / total_streams]
#   DT[, Orchard_RPtS := orchard_and_osc_revenue_usd / total_streams]
#   DT[, Orchard_RPoS := orchard_and_osc_revenue_usd / orchard_and_osc_streams]