
if (!exists("QRES"))
  loadFromJesus("QRES")

DT <- as.data.table(QRES[[ length(QRES) ]])
# rm(QRES)

desc(DT)


### 
### NOTE:  The query is based off of UPCs.  The list of UPCs comes from which campaigns were run.
###         It is possible that artists be represented multiple times in the list of UPCs
###         Therefore, after agg'ing, we'll create an additional column with a unique artist name per UPC. 
###         

## This is to check duplicated.data.frame against duplicated.data.table
checkUnique <- FALSE

cols.user.info <- c("mobile", "zip", "product", "country", "gender", "birthyear")

# Aggregate downloads by track or upc
DT[, trackSum := .N, by=list(trackid, download_date)]
DT[, upcSum := .N, by=list(upc, download_date)]

# aggregated, dropping customer info
DT.agg <- setkey(DT, upc, trackid, download_date)
DT.agg[, (cols.user.info) := NULL]

## check on `unique.data.table`
## the use of `diffs` is simply to check that duplicated.data.table is giving us the results we expect
diffs <- if (checkUnique) (duplicated.data.frame(DT.agg) != duplicated(DT.agg, by=names(DT.agg))) else FALSE

    if (any(diffs)) {
        stop("unique is not working right")
    } else {
        DT.agg <- unique(DT.agg, by=names(DT.agg))
    }


setkeyIfNot(DT.flat, upc)  # CR <- DT.flat
setkeyIfNot(DT.agg, upc)
DT.agg[DT.flat, c("startDate", "endDate", "campCountry", "preRelDate", "streetDate") := list(startDate, endDate, campCountry, preRelDate, streetDate)]

## Ensure dateCols are Dates
dateCols <- getDateColNames(DT.agg)
DT.agg[, c(dateCols) := lapply(.SD, as.Date, origin="1970-01-01", format="%Y-%m-%d"), .SDcols=dateCols]

## Add unique artist name and campaign country
# first convert topropper (simply for aesthetics in printouts)
DT.agg[, aName.uniq := topropper(artistname), by=artistname]

# next, add a serial number pertaining to the upc
DT.agg[, aName.uniq := paste0(  if (lunique(upc) > 1)   paste(topropper(artistname), "-", fw0(as.numeric(factor(upc)), 2)) 
                                else                    topropper(artistname)
                              , " [", campCountry, "]")
       , by=list(artistname, campCountry) ]

# ~~~~~~~~~~~~~~ MI

## Make into TS DT
DT.ts <- DT.agg[, list(time=as.numeric(download_date), value=upcSum, group=aName.uniq, upc, artistname, campCountry
                     , startDate, endDate, preReleaseDate=preRelDate, releaseDate=streetDate)]
DT.ts <- unique(DT.ts, by=names(DT.ts))



## Add Moving Avg
MA <- function(x, order, ...) {
    if (length(x) - trunc(order/2) < 1)
        return(NA_real_)
    return(forecast::ma(x=x, order=order, ...))
}

mkMAnm <- function(x) paste0("MA_", x, "Day")

DT.ts[, value := as.numeric(value)]
DT.ts[, value := txform(x=value, tx="scale.0.1"), by=group]


MA.ints <- c(3,7,14,21,28)  # c(7, 21)
DT.ts[, mkMAnm(MA.ints) := lapply(MA.ints, function(ord) MA(x=value, order=ord)), by=group]

## col ordering for aesthetics
setcolorderpt(DT.ts, c("time", "value", "group", "startDate", "endDate"))


skip <- NULL
MA.cols   <- mkMAnm(c(7, 21))
MA.colors <- c("dark blue", "plum2")
MA.alphas <- c(1, .4)
MA.sizes  <- c(0.35, 1.6)


groupOrder <- DT.ts[, list(D=min(startDate)), by=group][order(D), group]
DT.ts[, group := factor(group, levels=groupOrder)]
setkey(DT.ts, group)

Pl <- DT.ts[TRUE, list(Plots=list(createPlot2(.SD, Title="Spotify", nm=group, dots.alpha=0.1
                                        , TS.alpha=0.03, dots.on=TRUE, log.y=FALSE
                                        , MA.col=MA.cols, MA.alpha=MA.alphas, MA.color=MA.colors
                                        , MA.size=MA.sizes, MA.skip=skip
                                        , short.name=FALSE)))
            , keyby=list(group = as.character(group))]

## PDF PARAMS
pdfFile <- as.path(outDir, "Spotify Spain & Italy Campaigns.pdf")
cols <- 1
rows <- 3

Pl[, gridPages(Plots, pdfFile, rows=rows, cols=cols, width=14, height=17, noWarnings=TRUE, open=TRUE, main="\nSpotify Advertising Campaigns in Spain & Italy\n")]

# Pl[c(7, 12, 2), gridPages(Plots, pdfFile, rows=rows, cols=cols, width=14, height=30, noWarnings=TRUE, open=TRUE, main="\nSpotify Advertising Campaigns in Spain & Italy\n")]






#  
#  
#  
#  
#  
#  
#  DT.Plots <- DT.ts[, list(P=list(createPlot2(.SD, title="Spotify Adds Campaign", nm=group))), by=group]
#  
#  for (D in DT.Plots[, P]) {
#      dev.new()
#      plot(D)
#  }
#  
#  ## GIVEN CR DATA.TABLE (from Cropped)
#  setkey(DT.ts, upc)
#  setkey(CR, upc)
#  
#  DT.ts[]
#  CR[DT.ts, group := group]
#  CR
#  setattr(Plots, "names", DT.ts[, unique(group)])
#  
#  Plots[[1]] + ggtitle(paste("Spotify Campaign for", names(Plots) [[1]]))
#  
#  [, setNames("P", group)]
#  names(Plots) 
#  Plots[[1]]
#  P <- createPlot(DT.ts, ModelName="Spotify Adds Campaign")
#  P + facet_grid(group~.)
#  
#  length(which(diffs))
#  DT[, list(upc, download_date, upcSum)]
#  
#  
