## THIS IS FOR SOMETHING ELSE, NOT FOR THIS.  JUST USING AS SCRAP PAPER. 
# rm(load)
# load("/mnt/data/home/rsaporta//gitData/ImageSaves/SpotifyAdds_ImageSave_20131119_2214.Rda")
#---------------


unique(DB.using.agg[total.count.by.upc == 0 & mkt_priority != "C" & weeks=="total"], by="upc")


## A & B  UPCs with No Count for first six weeks

UPCs.nostreams <- c("5013929157538", "5013929158030", "5013929158832", "5014524213636", 
"5014524213834", "5032698671588", "5032698671717", "5032698672073", 
"5055300358042", "5065001566462", "605633007138", "605633007336", 
"612651015922", "677967130424", "726670550576", "726670550583", 
"738572137144", "738572139643", "738572142544", "768371006273", 
"788557139755", "788557139953", "788557163811", "788557164016", 
"788557164313", "788557164412", "788557820615", "788557821513", 
"8033116064583", "803341377219", "805520640848", "822252228021", 
"828021611025", "845121057546", "84854201868", "84854206399", 
"84854220661", "84854266034", "858454003234", "8712618626153", 
"8712618805527", "8717931324441", "880893110921", "887396301689", 
"887396455818", "887396559158", "887845196002", "887845315625", 
"887845330222", "887845448323", "887845457400", "887845505521", 
"887845568182", "887845583611", "887845597137", "887845651563", 
"887845651730", "887845652799", "887845652836", "887845652850", 
"887845670380", "887845695369", "887845799487", "887845815859", 
"887845844361", "887845850317", "887845926456", "887845931450", 
"887845984098", "887845998545", "888002011251", "888002021267", 
"888002038791", "888002041531", "888002041692", "888002048073", 
"888002061003", "888002074904", "888002120106", "888002120922", 
"888002140814", "888002144416", "888002184351", "888002300249", 
"888002317537", "888002372178", "888002409713", "888002419033", 
"888002442383", "888002494689", "888002518354", "888002519122", 
"888002519931", "888002532657", "888002532688", "888002567925", 
"888002584069", "888002584151", "888002642103", "888002673435", 
"888002674913", "888002678447", "888002685834", "888002699855", 
"888002700919", "888002712707", "888002720122", "888002720313", 
"888002729804", "888002735249", "888002748621", "888002748652", 
"888002756961", "888002770585", "888002820570", "888002834096", 
"888002868336", "888002894724", "888002932143", "888002943071", 
"888002950017", "888002956361", "888002956859", "888002959270", 
"888002961174", "888002973863", "888003011090", "888003067219", 
"888003110502", "888003111790", "888003112056", "888003125940", 
"888003189690", "888003202863", "888003455283", "888003505803", 
"9789063012311", "99925414122")

  utilSource()

  ## Cleanup before starting
  try(dbDisconnect(con), silent=TRUE)
  try(detach(package:RMySQL), silent=TRUE)

  ## Load library
  library(RPostgreSQL)


QRY.nostreams <- 
paste(
"SELECT upc, COUNT(customerid) AS count, COUNT(trackid) AS countDoubleCheck, download_date",
"FROM production.staging_raw_spotify",
"WHERE upc in ", pasteQ(UPCs.nostreams), "",
"AND download_date >= '2013-03-01'",
"AND download_date <= '2013-09-23'",
"GROUP BY upc, download_date"
)

  ## Run the Query
  con    <- getDB(drv.str="PostgreSQL")
  DB.nostreams <- runQry(QRY.nostreams, connex=con, verbose=TRUE, verbose.max.lines=22L, verbose.max.width=110L)

  # Convert to idcol
  DB.nostreams[, upc := as.idcol(upc)]
  if (!all(DB.nostreams[, count == countdoublecheck])) {
      stop("Count does not confirm")
  } else 
    DB.nostreams[, countdoublecheck := NULL]


  setkey(DB.nostreams, "upc", "download_date")

  loadFromJesus("DB.all.meta")
  setkeyIfNot(DB.all.meta, "upc")

  ## add in releaseDate
  suppressWarnings(DB.nostreams[, releaseDate := NULL])
  DB.nostreams[DB.all.meta, c("releaseDate", "mkt_priority", "genre", "artist", "release") 
                           := list(i.releaseDate, i.mkt_priority, i.genre, i.artist, i.release)]

  DB.nostreams[ , releaseDate := as.Date(releaseDate, origin="1970-01-01")]

  DB.nostreams[, FirstSixWeeks  := sum(count[download_date < releaseDate + (7*6)]), by=upc]
  DB.nostreams[, SecondSixWeeks := sum(count[download_date >= releaseDate + (7*6) & download_date < releaseDate + (7*12)]), by=upc]

  DB.nostreams.unique <- copy(DB.nostreams)[, c("download_date", "count") := NULL]
  DB.nostreams.unique <- unique(DB.nostreams.unique, by="upc")
  setcolorderpt(DB.nostreams.unique, c("upc", "releaseDate", "FirstSixWeeks", "SecondSixWeeks"))
  DB.nostreams.unique[FirstSixWeeks < 1][order(releaseDate, decreasing=TRUE)][order(mkt_priority)]

  # Cleanup Again
  ## ------------------------------ ##
  try(dbDisconnect(con), silent=TRUE)
  try(detach(package:RPostgreSQL), silent=TRUE)
  ## ------------------------------ ##

  if (!exists(dataDir))
    dataDir <- "~/git/orch/data/SpotifyAdds"
  jesusForData(DB.nostreams)
