# DT.Total_usage
lib(reshape2)
lib(ggplot2)


-------------------------------
kCols.DUS <- c("month", "Units", "StreamLength")
kCols.DCUS <- c("month", "country", "Units", "StreamLength")
kCols.DOUS <- c("month", "Orchard_vs_Spotify", "Units", "StreamLength")
kCols.ts <- c("country", "StreamLength", "Units")
kCols.US <- c("Units", "StreamLength")

numericCols <- nwhich(canBeNumeric(DT.Total_usage))
DT.Total_usage[, (numericCols) := lapply(.SD, as.numeric), .SDcols = numericCols]

## MELTED DATA (reshaped in the future)
DT.Total_usage.melt <- melt(DT.Total_usage, id.vars=c("month", "country"), value.name="count", measure.vars=numericCols)
DT.Total_usage.melt[, Orchard_vs_Spotify := ifelse(grepl("orchard", variable), "Orchard", "Spotify")]
DT.Total_usage.melt[, Units := ifelse(grepl("minutes", variable), "Minutes", "Streams")]
DT.Total_usage.melt[Units == "Streams", StreamLength := ifelse(grepl("15", variable), 15,  ifelse(grepl("30", variable), 30, 0))]
-------------------------------

## 0 Seconds vs 15 seconds vs 30 Seconds
DT.Total_usage.melt.aggdByC <- sumTheTable(DT.Total_usage.melt, byCols=kCols.DOUS, colsUsing="count")
# DT.Total_usage.melt.aggdByC[, Counting := ifelse(StreamLength == 0, "00 ~ 15 Seconds [Unpaid]", ifelse(StreamLength == 15, " ~ 15 Seconds [Unpaid]",  )
DT.Total_usage.melt.aggdByC[, Counting := ifelse(Units == "Minutes", "Total Minutes Streamed Per Month", sprintf("Total Streams longer than %02i Seconds", StreamLength))]
{
  ggplot(DT.Total_usage.melt.aggdByC, aes(x=month, y=count, color=Counting)) + 
    geom_line(aes(linetype=Orchard_vs_Spotify)) + 
    facet_grid(Units ~ ., scale="free_y") + 
    millions.y() + ylab("Count in Millions") + 
    title("Usage by Month (Minutes or Streams) -- Orchard vs Spotify")
}
-------------------------------


## START BY dcasting
DT.Total_usage.dcast <- dcast.data.table(DT.Total_usage.melt, month + Units + StreamLength ~ Orchard_vs_Spotify, value.var="count", fun.aggregate=sum)

setkeyIfNot(DT.Total_usage.dcast, kCols.DUS, verbose=FALSE, organize=TRUE)

## Add ratio column
DT.Total_usage.dcast[, orch_to_spot_ratio := Orchard / Spotify]
DT.Total_usage.dcast[, DELTA_orch_to_spot_ratio := percentIncrease(orch_to_spot_ratio), keyby=kCols.US]

## SEE 
DT.Total_usage.dcast[.(as.Date("2014-07-01"))]

## Scale Uniform to [0, 1]
# DT.Total_usage.dcast[, Orch.scaled_01 := scaleunif(Orchard), by=kCols.US]
# DT.Total_usage.dcast[, Spot.scaled_01 := scaleunif(Spotify), by=kCols.US]

## Scale to SD
DT.Total_usage.dcast[, Orch.scaled := scale(Orchard), by=kCols.US]
DT.Total_usage.dcast[, Spot.scaled := scale(Spotify), by=kCols.US]

## Percent of increase
DT.Total_usage.dcast[, Orch.perc_change := percentIncrease(Orchard), by=kCols.US]
DT.Total_usage.dcast[, Spot.perc_change := percentIncrease(Spotify), by=kCols.US]

##
DT.Total_usage.dcast[, `Cut Off` := ifelse(Units == "Streams", sprintf("Total Streams Greater Than %s Secs.", StreamLength), "Total Minutes")]

## SEE 
DT.Total_usage.dcast[.(as.Date(c("2014-04-01", "2014-05-01")))][order(StreamLength)]
DT.Total_usage.dcast[.(as.Date(c("2014-04-01", "2014-05-01")))][order(StreamLength)]



TITLE <- "Number of Stream (or Total Minutes)\nComparing Orchard to Spotify, by Month and Stream cut-off" 
## OLD
ggplot(DT.Total_usage.dcast, aes(x=month, y=orch_to_spot_ratio, color=factor(StreamLength))) + geom_line(size=1) + geom_point(alpha=.3, size=2.5) + facet_grid(Units ~ ., scale="free_y") + percent.y()

ggplot(DT.Total_usage.dcast[Units != "Minutes"], aes(x=month, y=DELTA_orch_to_spot_ratio, color=`Cut Off`)) + geom_line(size=1) + geom_point(alpha=.3, size=2.5) + percent.y()

## NEW 20141020
DT.plot.total_usage <- copy(DT.Total_usage.dcast)
ggplot(DT.Total_usage.dcast, aes(x=month, y=orch_to_spot_ratio, color=`Cut Off`)) + geom_line(size=1) + geom_point(alpha=.3, size=2.5) + percent.y() + labs(title=TITLE, y="Orchard as a percent of Spotify")


---------------------------------------
## Same as boave but with country added
## START BY dcasting
DT.Total_usage.dcast <- dcast.data.table(DT.Total_usage.melt, month + country + Units + StreamLength ~ Orchard_vs_Spotify, value.var="count", fun.aggregate=sum)

setkeyIfNot(DT.Total_usage.dcast, kCols.DCUS, verbose=FALSE, organize=TRUE)

## Add ratio column
DT.Total_usage.dcast[, orch_to_spot_ratio := Orchard / Spotify]

## SEE 
DT.Total_usage.dcast[.(as.Date("2014-07-01"), "US")]

## Scale Uniform to [0, 1]
# DT.Total_usage.dcast[, Orch.scaled_01 := scaleunif(Orchard), by=kCols.ts]
# DT.Total_usage.dcast[, Spot.scaled_01 := scaleunif(Spotify), by=kCols.ts]

## Scale to SD
DT.Total_usage.dcast[, Orch.scaled := scale(Orchard), by=kCols.ts]
DT.Total_usage.dcast[, Spot.scaled := scale(Spotify), by=kCols.ts]

## Percent of increase
DT.Total_usage.dcast[, Orch.perc_change := percentIncrease(Orchard), by=kCols.ts]
DT.Total_usage.dcast[, Spot.perc_change := percentIncrease(Spotify), by=kCols.ts]

## SEE 
DT.Total_usage.dcast[.(as.Date(c("2014-04-01", "2014-05-01")), "US")][order(StreamLength)]
DT.Total_usage.dcast[.(as.Date(c("2014-04-01", "2014-05-01")), "UY")][order(StreamLength)]


ggplot(DT.Total_usage.dcast, aes(x=month, y=orch_to_spot_ratio, color=factor(StreamLength))) + geom_line() + facet_grid(Units ~ ., scale="free_y") 

---------------------------------------
kCols.region_group <- gsub("^country$", "region_group", kCols.DCUS)
DT.plot.agg <- melt(DT.Total_usage.dcast[!is.na(StreamLength) & StreamLength == 30], id.vars=kCols.DCUS)[, variable := as.character(variable)]
DT.plot.agg[, Orchard_vs_Spotify := ifelse(grepl("orch", as.character(variable), ignore.case=TRUE), "Orchard", "Spotify")]
addColsFrom_(DT.plot.agg, DT.country, joinCols="country")
DT.plot.agg <- DT.plot.agg[region_group != "Canada"]
DT.plot.agg <- DT.plot.agg[grepl("perc_change$", variable), lapply(.SD, sumn), keyby=c(kCols.region_group, "Orchard_vs_Spotify"), .SDcols="value"]

DT.plot.agg[.(as.Date("2014-04-01"))]

y.var <- "value"
ggplot(data = DT.plot.agg[month > min(month)], aes_string(x="month", y=y.var, color="region_group", linetype="Orchard_vs_Spotify")) + geom_line() + geom_point(alpha=.5) +  ylab(paste0(gsub("_", " ", y.var), " (percent)"))

+ facet_grid(type_of_trial ~ ., scales="free_y") 
---------------------------------------

DT.plot.agg <- melt(DT.Total_usage.dcast[!is.na(StreamLength) & StreamLength == 30], id.vars=kCols.DCUS)[, variable := as.character(variable)]
DT.plot.agg[, Orchard_vs_Spotify := ifelse(grepl("orch", as.character(variable), ignore.case=TRUE), "Orchard", "Spotify")]
addColsFrom_(DT.plot.agg, DT.country, joinCols="country")

y.var <- "value"
ggplot(data = DT.plot.agg[grepl("scaled_01", variable)], aes_string(x="month", y=y.var, color="region_group", linetype="Orchard_vs_Spotify")) + geom_line() + geom_point(alpha=.5) +  ylab(paste0(gsub("_", " ", y.var), " (percent)")) 

+ facet_grid(type_of_trial ~ ., scales="free_y") 
---------------------------------------


whose <- c(orchard="orchard_streams", all="all_streams")
length <- c(touched="", short="_above_15_sec", full="_above_30_sec")

## ------------------------------------ ##
##      STILL TODO                      ## 
## ------------------------------------ ##
    for (l in length)
      DT.Total_usage[, paste0("orch_to_all_ratio", l) := get(paste0("orchard_streams", l)) / get(paste0("all_streams", l))]
    DT.Total_usage[, orch_to_all_ratio_minutes := orchard_minutes_streamed / minutes_streamed]
    ##
    DT.Total_usage[, perc.songs_skipped := 1 - all_streams_above_30_sec/all_streams]
    DT.Total_usage[, perc.orchard_songs_skipped := 1 - orchard_streams_above_30_sec/orchard_streams]
## ------------------------------------

addColsFrom_(DT.Total_usage, DT.country, joinCols="country")

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


plotSpotifyData <- function(DT, y.vars=c("perc.songs_skipped"))
## Look at USA vs ROW
DT.plot <- copy(DT.Trial_uptake)
y.var = "No._of_trials_starting_during_the_month"

addColsFrom_(DT.plot, DT.country, joinCols="country")

byCols <- c(setdiff(key(DT.plot), "country"), "region_group")
DT.plot[, country := NULL]


## Aggregate
DT.plot.agg <- DT.plot[, lapply(.SD, sumn), keyby=byCols]
## Drop any type that only has one or two data points
DT.plot.agg <- DT.plot.agg[type_of_trial %in% DT.plot.agg[, .N, by=type_of_trial][N > 2, type_of_trial]]

lib(ggplot2)
ggplot(data = DT.plot.agg, aes_string(x="month", y=y.var, color="region_group")) + geom_line() + facet_grid(type_of_trial ~ ., scales="free_y") + millions.y() + ylab(paste0(gsub("_", " ", y.var), " (in millions)"))
----------------------------
