# +---------------------------------------------------------------------------------------------+
# |                                                                                             |
# |                                                                                             |
# |                                   ##  UPDATE 2015-02-17:                                    |
# |                                                                                             |
# |                                                                                             |
# |                 The code in this file was written at least six months ago.                  |
# |          A better version is available in Spotify_Streams_Per_Top_Artist project.           |
# |                            (requires modification to DT.revshare)                           |
# |                                                                                             |
# |  "~/git/orch/src/Spotify_Streams_Per_Top_Artist/load DTs/load and modify 02 DT.revshare.r"  |
# |    "~/git/orch/src/Spotify_Streams_Per_Top_Artist/load DTs/load and modify 03 DT.users.r"   |
# |                                                                                             |
# |                                                                                             |
# +---------------------------------------------------------------------------------------------+






##   What is the Spotify User Count per territory? 
##   
##   ----------------------
##   findColumnInDT("user")
##   ----------------------
##      DT.Activity_and_usage :: No._of_active_users_last_7_days_(average_over_last_30_days), No._of_active_users_last_30_days, registered_users
##      DT.Conversion        :: active_users_30_(ad-supported)
##      DT.revshare               :: registered_users, active_users, new_product_users
##   ----------------------
##   
##   A: Ad-Supported
##   P: Premium
##   PD: Premium Student Discount
##   U: Unlimited


byCols <- c("activity_month", "product", "country_code")

assignIfNotExist("productsUsing.forUserCounts", c("A", "P", "PD", "U"))

# formnumb(DT.revshare[activity_month == "2014-11-01"][product %in% names(getDict.SpotifyAccountingProduct())][, lapply(.SD, sumn), .SDcols=c("registered_users", "active_users"), by=byCols][order(active_users, decreasing=TRUE)], round=0)
DT.userCounts <- DT.revshare[isTRUE(productsUsing.forUserCounts) | product %in% productsUsing.forUserCounts
                           ][, lapply(.SD, sumn), .SDcols=c("registered_users", "active_users"), keyby=byCols]
setInfo(DT.userCounts, "Aggregated from DT.revshare.\nCropped to only A, P, PD, U products")

## ------------------------------------------------------------------------------ ##
## 2015-10-25  TODO: 
## ------------------------------------------------------------------------------ ##
## How is DT.userCounts different from
##    DT.revshare[, c(byCols, "registered_users", "active_users"), with=FALSE]
## ------------------------------------------------------------------------------ ##

DT.userCounts[, product_description := getDict.SpotifyAccountingProduct()[match(product, names(getDict.SpotifyAccountingProduct()))]]
DT.userCounts[product_description == "Ad funded", product_description := "AdFunded"]

jesusForData(DT.userCounts)


cat(" --- TOP COUNTRIES FOR PREMIUM ACTIVE USERS ---")
print(formnumb(DT.revshare[(activity_month == max(activity_month)) & product == "P"][order(active_users, decreasing=TRUE)][1:12, list(country_code, active_users)]))
for (cc in c("US", "GB", "SE", "ES", "NO", "MX", "BR")) {
  cat("\n\n", pasteR(" ", 15), pasteR(20), cc, pasteR(20), "\n")
  print(formnumb(DT.revshare[country_code == cc & activity_month == max(activity_month)][order(active_users, decreasing=TRUE)][, list(product_description, product, active_users, registered_users, pro_rata_share)]))
}
