






#                       THIS FILE IS DEPRACATED --  USE 
#                             ~/git/misc/rscripts/utils/itunes_544.r
#                       INSTEAD




warning ("\nThis file ('~/git/orch/src/chartio/itunes_544.r') is depracted.\nInstead use the function:\n\n\titunes_544(minDate, maxDate, out='DT')\n")








# itunes_544.r  -- OLD

.us()

## How many days in each period.  sum(daysPerPeriod) is how many days in a quarter
daysPerPeriod <- c(5, 4, 4) * 7

#  OLD VERSION:  Using Years out.  Instead just use Sys.Date()
#  ## How many years out we want to count days for
#  Start5 <- as.Date("2013-09-29")
#  YearsOut <- 2
#  End <- Start5 + YearsOut*(4*sum(daysPerPeriod)) - 1  ## End is YearsOut * Number of days in a 5-4-4 year minus one for off-by-one for end date


## Known Start for a 5-4-4 year
Start5 <- as.Date("2012-09-30")
Start5 <- as.Date("2011-10-02")
End    <- Sys.Date() + 90


quarters <- ceiling(c((End-Start5+1) / sum(daysPerPeriod)))


cut_points <- cumsum(rep(daysPerPeriod, times=quarters))
inds <- mapply(function(s, e) s:e, c(1, head(cut_points+1, -1)), cut_points)
# sapply(inds, length)

dates <- as.Date(seq.Date(Start5, (End+34), by="day"))
dates <- lapply(inds, function(ind) dates[ind])

## Combine into a DT and remove any NAs
DT.iTunes544 <- rbindlist(lapply(dates, function(d) data.table(start=min(d), end=max(d), dseq=list(d)))) [!is.na(start)]
DT.iTunes544[, month := as.Date(format(dseq[[1]][[15]], "%Y-%m-01")), by=start]
setcolorderpt(DT.iTunes544, c("month", "start", "end"))
setkey(DT.iTunes544, month)


dateCol    <- "download_activity_date"
dateColAS  <- "iTunesMonth"
elseValue  <- "''"  ## alternaive is    elseValue <- dateCol
clause.when <- paste(" WHEN", dateCol, "between '%s' and '%s' THEN '%s'" )

clause.case <- DT.iTunes544[, paste("CASE\n", pasteC(sprintf(clause.when, start, end, month), C="\n "), "\nELSE ", elseValue, "\nEND AS ", dateColAS,"\n")]



cat(clause.case)




# TO_CHAR(download_activity_date, 'YYYY-mm-01') as month
##   
##   # --------------
##   #x  ### ___ THIS IS WRONG _____.   See May 2013
##   #x  ## If the 1st is Thurs, Fri or Sat, then The month starts that next Sunday
##   #x  ## Else the month starts that day or the previous sunday
##   #x  DT.iTunes544[, start_calcd := as.Date({.wd <- wday(month); ifelse(.wd >= 5, month + (8 - .wd), month - (.wd - 1) )}, origin=.origin)]
##   #x  DT.iTunes544[(start_calcd != start)]
##   #x  ### ___ THIS IS WRONG _____.   See May 2013
##   # ---------------------
##   
##   
##   
##   # ---------------------
##   ### MOD Formuala
##   w <- 1:52
##   (w-1) %% 13 + 1
##   ## 
##   DT.w <- data.table(w=w[35:52])
##   DT.w[, m13 := (w-1) %% 13 + 1]
##   rep(month.abb[8:12])
##   # ---------------------
##   
##   # --------------
##   ### SQL 
##   SELECT extract('Week' from download_activity_date+1) as Week, 
##   download_activity_date
##   FROM aggregated_analytics
##   WHERE download_activity_date between '2013-09-20' AND '2013-11-15'
##   GROUP BY download_activity_date, Week
##   ORDER BY download_activity_date
##   # --------------
##   
##   
