DT.Glitch.iradio.event

rid.firstplay <- (-999999)


setkeyIfNot(DT.Glitch.iradio, "isrc")
setkeyIfNot(DT.Glitch.isrc_info, "isrc")

### --- ADD UPC INFO to radio  ------ ###
      DT.Glitch.isrc_info[, tiebreaker := FALSE]
      DT.Glitch.isrc_info[upc %in% UPCs.nonCommentary, tiebreaker := TRUE]

      DT.Glitch.isrc_info[, dups := FALSE]
      DT.Glitch.isrc_info[duplicated(isrc), dups := TRUE]

      DT.Glitch.isrc_info[, commentaryVersion := (dups & !tiebreaker)]

      DT.Glitch.iradio[DT.Glitch.isrc_info[!(commentaryVersion)], upc := i.upc]
### --- ADD UPC INFO to radio  ------ ###



keyCols.iradio <- c("end_reason", "customerid", "stationid", "isrc")
otherCols <- c("station", "station_type", "track_ownership", "postal_code", "user_type")
setkeyIfNot(DT.Glitch.iradio, keyCols.iradio)

## Find the first listen per station per customerid
DT.Glitch.iradio[, first_radio_listen_perU := truncDate(min(start_time)), by=customerid]

DT.Glitch.iradio.counts <- DT.Glitch.iradio[.("Natural"), list(.N, first_radio_listen_perU=first_radio_listen_perU[[1L]]), by=c(keyCols.iradio, otherCols)]

DT.Glitch.iradio.counts <- DT.Glitch.iradio.counts[, .N, by=c(setdiff(c(keyCols.iradio, otherCols, "first_radio_listen_perU"), "isrc"))]
jesusForData(DT.Glitch.iradio.counts)

#### FYI ----------------

nrow(DT.Glitch.iradio.counts)
DT.Glitch.iradio.counts[, lunique(customerid)]

#### FYI ----------------


customers.firstplay <- DT.Glitch.iradio.counts[station=="First Play - The Glitch Mob, Love Death Immortality"
                        , first_radio_listen_perU
                        ,  keyby=CJ(customer_identifier=customerid, rid.firstplay=rid.firstplay)]


=========== LEFT OFF HERE on MARCH 21 8:30AM  


Q.rid <- sprintf("SELECT releaseid, releasename FROM dim_release where releaseid in (%s)",  pasteC(upcs[upcs>0], C=", "))
rid.info <- runQry(Q.rid)

setkeyIfNot(rid.info, "releaseid")
rid.info <- rbind(rid.info, rid.info[.(rid.firstplay), list(releasename = "FirstPlay Promo")])
setkeyIfNot(rid.info, "releaseid")
jesusForData(rid.info)
