setScience("misc")


setDBall(7)
qShowCols("SPOTIFY")
minDate <- "2015-02-01"
dateCol <- "download_date"
colsToPull <- c(dateCol, "source", "user_id", "product", country="user_country", "device_type")
colsToPull <- c(dateCol, "source", country="user_country")
DT.holly_miranda <- makeQry(tbl="SPOTIFY", aggFunc="count", track_name="All I Want Is to Be Your Girl", dateCol=dateCol, minDate=minDate, colsToPull=colsToPull, key="colsToPull") %>% runQry(cluster=4)
setnames(DT.holly_miranda, "rows", "streams")


top_countries <- DT.holly_miranda[, sum(streams), keyby=country][order(V1, decreasing=TRUE)][1:8, country]


DT.holly_miranda[, perc_of_tot_streams_by_country_by_date := streams / sum(streams), by=list(download_date, country)]

DT.holly_miranda[, country_count := sprintf("%s (%s)", country, formnumb(sum(streams))), keyby=list(country)]
DT.plot <- DT.holly_miranda[source %in% c("others_playlist", "other"), list(download_date, source, country, country_count, perc_of_tot_streams_by_country_by_date, streams)]


title <- "Comparing other vs others_playlist for Holly Miranda (minDate = May 15 2015)"
P.Holy_Miranda_Percentage <- ggLinegraph(DT.plot[download_date >= "2015-05-15"][country %in% c(top_countries, c("CZ", "UY"))], x="download_date", y="perc_of_tot_streams_by_country_by_date", facet_formula="country_count ~ .", title=paste0(title, "Percent"), color="source")

P.Holy_Miranda_Volume <- ggLinegraph(DT.plot[download_date >= "2015-05-15"][country %in% c(top_countries, c("CZ", "UY"))], x="download_date", y="streams", facet_formula="country_count ~ .", title=paste0(title, "Volume"), color="source", facet_scale="free")

ggsave.out(P.Holy_Miranda_Volume, open=TRUE, height=20, width=12)
ggsave.out(P.Holy_Miranda_Percentage, open=TRUE, height=20, width=12)


------------------------

DT.holly_miranda.agg <- DT.holly_miranda[, list(streams=sum(streams)), keyby=list(download_date, source)]
DT.holly_miranda.agg[, perc_of_tot_streams_by_country_by_date := streams / sum(streams), by=list(download_date)]


title <- "Comparing other vs others_playlist for Holly Miranda"
ggLinegraph(DT.holly_miranda.agg, x="download_date", y="streams", title=title, color="source")









# DT.holly_miranda.by_user <- aggregateDT(DT.holly_miranda, by=c("source", "country"), colsToAgg=c("rows"))
DT.holly_miranda.agg <- aggregateDT(DT.holly_miranda, exclude="user_id", colsToAgg=c(streams="rows"))

DT.holly_miranda.agg_by_country <-  aggregateDT(DT.holly_miranda.agg, exclude="user_id", colsToAgg=c(streams="rows"))

DT.holly_miranda.agg[, ]