




"NOTES:  This file was used specifically for checking YouTube carveouts for a specific project for the video services team.
Following that, I began to adopt it to migrate the Carveout information

Now (as of Dec 2015) use 
     ~/git/orch/src/DeNormalizing/Additional Tables/carve_outs label level.r

There is also a similar file
     ~/git/orch/src/DeNormalizing/Additional Tables/carve_outs.r

Those need work
"






































library(reshape2)
setScience(proj="misc", subProj="carveouts")
setGitBranchToSystem(); .g()

setDBall(drv="mysql")


## Pull the data
DT.carveouts <- ingest.p("qrys", "carveout.sql") %>% sqlFileToQry(clear=TRUE) %>% runQry

## Convert the carved_out column to logical
DT.carveouts[, carved_out := as.logical(carved_out)]

## get the different store names with youtube
youtube_store_names <- extract("youtube", DT.carveouts$store_carveouts) %>% unique %>% sort

## Drop the rows that are not the 
{
  metaCols <- c("labelid", "owner", "label_name")
  setkeyIfNot(DT.carveouts, labelid, store_carveouts, organize=FALSE, verbose=TRUE)
  tmp_CJs <- CJ_values_of_columns(DT.carveouts, cols=c("labelid", "store_carveouts"))
  DT.carveouts2 <- DT.carveouts[tmp_CJs, allow=TRUE]
  DT.carveouts2[is.na(carved_out), carved_out := FALSE]
  permeate_(DT.carveouts2, col="owner", by="labelid")
  permeate_(DT.carveouts2, col="label_name", by="labelid")
  DT.carveouts <- DT.carveouts2[(store_carveouts %in% youtube_store_names)]
  rm(tmp_CJs)
}

## identify labels with the given store_carveouts
frmla <- makeFormula(Left=metaCols, Right="store_carveouts")
DT.RED_YouTube_carveouts <- dcast(DT.carveouts[store_carveouts %in% youtube_store_names], frmla, fun.aggregate=function(x) any(x), value.var="carved_out") %>% setDT

DT.RED_YouTube_carveouts[, "Both YT and YTMK" := YouTube & `YouTube Music Key`]
DT.RED_YouTube_carveouts[, "Neither YT nor YTMK" := !YouTube & !`YouTube Music Key`]

## Total labels per
DT.count_of_labels <- as.data.table(t(DT.RED_YouTube_carveouts[, lapply(.SD, sumn), .SD=c(youtube_store_names, "Both YT and YTMK", "Neither YT nor YTMK")]), keep.rownames=TRUE) %>% setnames(c("Carveout Type", "Total Labels"))

## convert from logical to string
DT.RED_YouTube_carveouts[, "Neither YT nor YTMK" := ifelse(`Neither YT nor YTMK`, "X", "--")]

## Export to XLSX
f.out <- out.p("RED_YouTube_carveouts", ext="xlsx")
exportXLS.usingXLConnect(f.out=f.out, DTs.list=list(RED_YouTube_carveouts=DT.RED_YouTube_carveouts, count_of_labels=DT.count_of_labels))

## email the file
quickEmail(getRS(), file=f.out)

