Just World - No Country Breakdown

library(ggplot2)
library(gridExtra)
library(forecast)


Kcols_D   <- c("Month") 
Kcols_DPt <- c("Month", "Product_Tier") 



## ------------------------------------------------------------------------ ##
##     Given Unique Users by Product (from Analytics)
##         convert Product to Product_Tier and re-summaraize
## ------------------------------------------------------------------------ ##

    ## Clean up names, we are using first-letter caps for Month / Product
    setnames(DT.UniqueUsers.perDP, c("month", "product"), c("Month", "Product"))

    ## Set Product_Tier
    DT.UniqueUsers.perDP[, Product_Tier := makeFactorUsingDict(vec=Product, dict=getDict.SpotifyTier(), missing_to_NA=FALSE)]

    ## Error-check.  There should be exactly one row per Month/Product
    stopifnot(!DT.UniqueUsers.perDP[, .N, by=c("Month", "Product")][, sum(N>1)])


    ## Aggregate by Product_Tier  (mostly just for OPEN / FREE)
    DT.UniqueUsers.perDPt <- 
        DT.UniqueUsers.perDP[,  list( UniqueUsers.perDPt  = sum(UniqueUsers.perDP)
                                    , TotalStreams.perDPt = sum(TotalStreams.perDP)
                                    )
                            , keyby=Kcols_DPt
                            ]



    DT.UniqueUsers.perD <- 
        DT.UniqueUsers.perDP[,  list( UniqueUsers.perD  = sum(UniqueUsers.perDP)
                                    , TotalStreams.perD = sum(TotalStreams.perDP)
                                    )
                            , keyby=Kcols_D
                            ]

## ------------------------------------------------------------------------ ##

   We have seeveral 
   For cleanliness, move the DT.Acc.perDCnP info to a new table, 
   ..... agg... left this mid step... starting from DT.Acc.perDCnP instead

    DT.Counts <- DT.Acc.Using.perDPt[, 
                        list(Month, Product_Tier, 
                            Revenue=Spotify_Net_Revenue, Streams=Spotify_Total_Tracks, Users=Spotify_Registered_Users)]


    DT.Counts[, `:=`(  Rev_per_Stream   =  Revenue / Streams
                     , Rev_per_User     =  Revenue / Users
                     , Streams_per_User =  Streams / Users
                    )]


## ------------------------------------------------------------------------ ##


DT.Acc.Using <- 
  DT.Acc.perDCnP[,
    list( Month
        , Product_Tier
        , Orchard_Revenue=Payable_Using
        , Spotify_Registered_Users=Registered_users
        , Spotify_Total_Tracks=Total_tracks
        , Orchard_Total_Tracks=Rightholders_tracks
        , Spotify_Gross_Revenue=Gross_revenue
        , Spotify_Net_Revenue=Net_revenue
       )
      ]

# Colors <- c(FreeTier="#55b5e3", LimitedTier="#a58ce3", PremiumTier="#383be3", ThirdPartyPartners="#d01ce3")
# Colors <- c(FreeTier="#2a6e22", LimitedTier="#3c9a67", PremiumTier="#3eb149", ThirdPartyPartners="#509ab1")
Colors <- c(FreeTier="#c5c637", LimitedTier="#3eb149", PremiumTier="#178322", ThirdPartyPartners="#509ab1", Total="#021003")
last_plot() + scale_fill_manual(values=Colors)

## All other tiers become ThirdPartyPartners
DT.Acc.Using[Product_Tier %ni% c("FreeTier", "LimitedTier", "PremiumTier"), Product_Tier := "ThirdPartyPartners"]

## Aggregate
DT.Acc.Using.perDPt <- DT.Acc.Using [, lapply(.SD, function(x) sum(as.numeric(x), na.rm=TRUE)), keyby=Kcols_DPt]
DT.Acc.Using.perD   <- DT.Acc.Using [, lapply(.SD, function(x) sum(as.numeric(x), na.rm=TRUE)), keyby=Kcols_D]


## Merge & Calculate Ratios
DT.Acc.Using.perDPt[DT.UniqueUsers.perDPt, Orchard_Users := UniqueUsers.perDPt]
DT.Acc.Using.perDPt[DT.UniqueUsers.perDPt, TotalStreams.perDPt := TotalStreams.perDPt]
DT.Acc.Using.perDPt[, Orchard_Revenue_Per_User := Orchard_Revenue / Orchard_Users]
DT.Acc.Using.perDPt[, Spotify_Revenue_Per_User := Spotify_Net_Revenue / Spotify_Registered_Users]

DT.Acc.Using.perDPt[, Spotify_Revenue_Per_User_MA := ma(Spotify_Revenue_Per_User, 3), by=Product_Tier ]
DT.Acc.Using.perDPt[, Spotify_Registered_Users_MA := ma(Spotify_Registered_Users, 3), by=Product_Tier ]


DT.Acc.Using.perD[DT.UniqueUsers.perD , Orchard_Users := UniqueUsers.perD]
DT.Acc.Using.perD[DT.UniqueUsers.perD , TotalStreams.perD := TotalStreams.perD]
DT.Acc.Using.perD[, Orchard_Revenue_Per_User := Orchard_Revenue / Orchard_Users]
DT.Acc.Using.perD[, Spotify_Revenue_Per_User := Spotify_Net_Revenue / Spotify_Registered_Users]


DT.Acc.Using.perD[, Spotify_Revenue_Per_User_Annua := Spotify_Revenue_Per_User * 12]
DT.Acc.Using.perD[, Spotify_Revenue_Per_User_Annua_MA := ma(Spotify_Revenue_Per_User_Annua, 3)]
DT.Acc.Using.perD[, Spotify_Registered_Users_MA := ma(Spotify_Registered_Users, 3)]

DT.Acc.Using.perD[, Spotify_Streams_Per_User := (Spotify_Total_Tracks / Spotify_Registered_Users)]
DT.Acc.Using.perD[, Orchard_Streams_Per_Spotify_User := (Orchard_Total_Tracks / Spotify_Registered_Users)]

x <- DT.Acc.Using.perD[Month < as.Date("2014-01-01"), Spotify_Gross_Revenue / Spotify_Registered_Users]
(x * 12)


DT.Acc.Using.perDPt[, list(Month, Product_Tier, Orchard_Revenue_Per_User, Spotify_Revenue_Per_User)]
DT.Acc.Using.perD[, list(Month, Product_Tier, Orchard_Revenue_Per_User, Spotify_Revenue_Per_User)]


DT.Acc.Using.perD[, Orchard_Revenue_MA := ma(Orchard_Revenue, 3)]


# 2013 revenue :  $32,404,433.57
# Rev2013 <- 32404433.57








