
## .... not sure if this is still needed??? 

## staging_raw_itunes_iradio_event :: MAIN
## 
## BRING IN 'station_type' 'station_name'  by station_id                           FROM staging_raw_itunes_iradio_station
## 
## BRING IN 'postal_code', 'user_type'     by customer_identifier                  FROM staging_raw_itunes_iradio_customer
## 
## BRING IN 'artist', 'title', 'isrc', 'extracted_upc'   by apple_identifier     FROM staging_raw_itunes_iradio_content


try(source("~/git/orch/src/iRadio/iRadioMeta.r"))
DT.Glitch.iradio <- merge_iRadio(base.nm="DT.Glitch.iradio_", clean=TRUE, verbose=TRUE)

  ### THERE IS SOME ISSUE OF NON-UNIQUENESS.   FOR NOW, JUST DISREGARD
  if (FALSE)  {
      kcols <- c("event_start_time", "customer_identifier", "apple_identifier")
      kcols2 <- c("start_time", "customerid", "appleid")

      e.count <-  DT.Glitch.iradio_event[, .N, keyby=kcols]
      m.count <-  DT.Glitch.iradio[, .N, keyby=kcols2]

      e.count[N>1]
      m.count[N>1]


      setkeyIfNot(DT.Glitch.iradio_event, kcols)
      identical(key(DT.Glitch.iradio_event), key(e.count))
      DT.Glitch.iradio_event[ e.count[N>1] ] [order(customer_identifier, event_start_time)]

      setkeyIfNot(DT.Glitch.iradio, kcols2)
      identical(key(DT.Glitch.iradio), key(m.count))
      DT.Glitch.iradio[ m.count[N>1] ] [order(customerid, start_time)] 

      DT.Glitch.iradio[customerid %in% e.count[N>1, customer_identifier] & appleid %in% e.count[N>1, apple_identifier]]
  } else {
    DT.Glitch.iradio <- unique(DT.Glitch.iradio, by=c("start_time", "customerid", "appleid"))
    ## this does not do anything.  Just display info
    unique(DT.Glitch.itunes_raw, by=c("customer_identifier", "apple_id", "sale_return", "product_type_identifier"))
    findDupsBy(DT.Glitch.itunes_raw, by=c("customer_identifier", "apple_id", "sale_return", "product_type_identifier"))
    ## a lot of the duplicates are by in the parent_id or sometimes  order_id+download_activity_date
  }

## Clean UPCs
DT.Glitch.itunes_raw[upc == 0]
grep("isrc", names(DT.Glitch.itunes_raw), value=TRUE, ignore.case=TRUE)

## Show the ISRC columns
DT.Glitch.isrc_info [, c("upc", grep("isrc", names(DT.Glitch.isrc_info), value=TRUE, ignore.case=TRUE)), with=FALSE]
DT.Glitch.itunes_raw[, grep("isrc", names(DT.Glitch.itunes_raw), value=TRUE, ignore.case=TRUE), with=FALSE]

jesusForData(DT.Glitch.iradio, info="RePulled on March 20")

# --------------------------------- #

## Set Keys
setkeyIfNot(DT.Glitch.itunes_raw, isrc)
setkeyIfNot(DT.Glitch.isrc_info, isrc)

## The only missing ISRCs should be the blanks
isrc.missing <- setdiff(DT.Glitch.itunes_raw[["isrc"]], DT.Glitch.isrc_info[["isrc"]])
stopifnot(00 == nrow(DT.Glitch.itunes_raw[isrc %in% isrc.missing & upc == 0]))



## THERE ARE DUPLICATE ISRC's --- NOT SURE WHY   ....   actually, I think they were do to the commentary version. 
findDupsBy(DT.Glitch.isrc_info, c("isrc"))

DuplicateISRC <- DT.Glitch.isrc_info[DT.Glitch.isrc_info[duplicated(DT.Glitch.isrc_info), .I]]


DT.Glitch.isrc_info <- unique(DT.Glitch.isrc_info, by=c("isrc", "upc"))
 unique(DT.Glitch.isrc_info, by=c("isrc"))

DT.Glitch.itunes_raw[DT.Glitch.isrc_info]
DT.Glitch.itunes_raw[unique(DT.Glitch.isrc_info)]



grep("isrc", names(DT.Glitch.releases), value=TRUE, ignore.case=TRUE)

setkeyIfNot(DT.Glitch.releases,   "isrc")
setkeyIfNot(DT.Glitch.itunes_raw, "isrc")






