.g()
.us()
options(warn=0)
setScienceIfNot("EU_PricingTest_2015", load=FALSE)

# lib(reshape2, quiet=TRUE)
# lib(Hmisc,    quiet=TRUE)

cluster.using <- 7

## There are some transactions in fact_analytics which have negative units. Should we ignore those. 
ignoreNegativeUnits <- TRUE

## This is the only upc thus far with up and down overrides
RockyHorror.upc <- "886788157644"

## For querying
aggFunc <- "sumn"
# aggFunc <- "meann"
minDate <- "2014-11-01"
stores_using <- c(1, 286)
countries_using <- c(GB=3, DE=4, FR=6, ES=15) #, IE=90)
cluster.raw <- 7
pricingGroups <- c("8.99", "9.99", "10.99")

byCols.upc_country <- c("upc", "country_code")
upc_manuallytested <- 829410710466
upcCols <- c("upc", "daily_avg_album_units_30preTest", "album_was_purchased_incountry_during_experiment")

## Week 1 date range. Other date ranges are based off of this interval
wk1.minDate <- as.Date("2015-02-12")
wk1.maxDate <- as.Date("2015-02-23")
daysPerTest <- wk1.maxDate - wk1.minDate + 1

## Week 2 Date Range
wk2.minDate <- as.Date("2015-02-26")
wk2.maxDate <- wk2.minDate + (daysPerTest - 1)

## Prior to Expirment
preTest.minDate <- as.Date("2015-01-29")
preTest.maxDate <- preTest.minDate + (daysPerTest - 1)

## Prior to VAT change
pre_preTest.minDate <- as.Date("2015-01-08")
pre_preTest.maxDate <- pre_preTest.minDate + (daysPerTest - 1)


## Create date boundaries
dateBoundaries <- list( pre_preTest = c(pre_preTest.minDate, pre_preTest.maxDate)
                      , preTest     = c(preTest.minDate, preTest.maxDate)
                      , Wk1         = c(wk1.minDate, wk1.maxDate)
                      , Wk2         = c(wk2.minDate, wk2.maxDate)
                      )

## CONFIRM:  Same starting day of the week, same ending day of the week, same length of time
stopifnot(
    allEqual(lapply(dateBoundaries, function(x) wday(min(x))))  # same DoW for start
  , allEqual(lapply(dateBoundaries, function(x) wday(max(x))))  # same DoW for end
  , allEqual(lapply(dateBoundaries, function(x) max(x) - min(x))) # same Number of days
  )


upc_addl_metaCols <- c("total_cds", "total_tracks", "discretized_tracks", "OverridePerc", "relative_to_default", "multi_cd")

source.p("01a DT.currency.r" )
source.p("01b DT.groupings.r")
source.p("01b2 DT.upc_has_override.r")
source.p("01c DT.EU_analytics.r")  ## about 30 Seconds
source.p("01d DT.raw.r")   ## about 3 Seconds
source.p("01e DT.EU_aggd.r")   ## about 3 Seconds
source.p("01f DT.EU_aggd.delta.r")   ## about 3 Seconds
saveImageTo()
print({
  jesusForData(DT.EU_analytics
    , DT.EU_analytics.bak
    , DT.raw
    , DT.EU_aggd
    , DT.EU_aggd_reshaped
    , DT.EU_aggd_reshaped.no_country
    , DT.EU_aggd.delta
    , DT.track_and_CD_counts
    , DT.upc_has_override
    ,  subDir=TRUE,
     stampDir=TRUE)
})

