# 04 googleVis.r

### THIS FILE'S TITLE NEEDS TO BE RENAMED. 
### 
###  This is the file that creates all of the stand alone tables with aggregations. 


# :   ** Run 01. Load
# :   Then right to here (skip 2)

require(Hmisc)

# For now... not using these. 
cols.notUsing <- c("uCTR", "uCPM", "uCPC", "uATR", "uCPA", "uSharesPerM", "Soc.uSharesPerM"
                 , "Conversion.Website", "Conversion.OtherWebsite", "Soc.Reach"  # , "uClicks"
                , "Views.PagePhoto", "Comments.Post", "Likes.Post", "Likes.Page", "Views.VideoPlays"
                , "Mentions.Page", "EventResponse", "Engagement.Page", "Engagement.Post", "Clicks.Link")

# NOT BEING USED FOR NOW.  
## Identify the important columns
cols.mets  <- c("Spend", 
                # Views
                "Impressions", "Reach",  "Soc.Impressions", "Soc.Reach",
                # Clicks
                "Clicks", "uClicks",  "Soc.Clicks", 
                ## Actions
                "Actions", "PeopleActioning", 
                ## Other Stuff (most of it included under "Actions")
                "Clicks.Link", 
                "EventResponse",
                "Likes.Page",  "Likes.Post", 
                "Engagement.Page", "Engagement.Post",
                "Comments.Post", "Shares.post", 
                "Mentions.Page", 
                "Views.PagePhoto", "Views.VideoPlays", 
                "Conversion.Website", "Conversion.OtherWebsite"
                )
cols.calcs <- c("CTR", "CPC", "CPM", "uCTR",  "uCPM", "uCPC", "Freq")
cols.demog <- c("Age", "Gender") 
cols.info.main  <- c("CampaignID", "AdID", "LabelID") 
cols.info.extra <- c("Campaign",   "Ad", "Label")
cols.date <- c("Date")

cols.info <- sort(c(cols.info.main, cols.info.extra))
cols.key <- c(cols.info.main, cols.demog)



# ~~~~~~~~~~~~~~~~~~~~~~ #
## The parameters are constant throughout the for loop
# ~~~~~~~~~~~~~~~~~~~~~~ #
  addUniques = FALSE
  verbose = FALSE
  verboseIter = TRUE # = verbose
  iterOverDate = TRUE

  pos = 3
  checkEnvir = TRUE
# ~~~~~~~~~~~~~~~~~~~~~~ #

envir <- parent.frame(pos)

## Environment Check
ee <- list()
for (i in seq(12))
  ee[[i]] <- parent.frame(i)

## if flagged on, and we are not in the global env
if(checkEnvir && !identical(environment() , .GlobalEnv)) {
  minPosForGlobal <- min(which(sapply(ee, identical, .GlobalEnv)))
  if (pos != minPosForGlobal)
    warning("\n\nCheck the environment level:\n\t pos has been set to       ", pos, "\n\t while minPosForGlobal is  ", minPosForGlobal, "\n")
}

DateIn <- FALSE
cols.agging   <- c(cols.mets)
## More columns to drop
addlToDropAlways <- c(cols.calcs, "Year", "Cid_Age_Gend")
if (exists("cols.notUsing")) {
  addlToDropAlways <- c(addlToDropAlways, cols.notUsing)
  cols.agging <- setdiff(cols.agging, cols.notUsing)
}

## This is a dict of columns to be dropped based on the iteration value
##   can also be used in reverse as columns to keep for a specific key
addlToDropByKey  <- list(AdID=c("Ad"), CampaignID=c("Campaign"), LabelID=c("Label"))


## ----------------------------------------------------------------- ##
##  Create all the needed DTs, aggregating over different variables  ##
## ----------------------------------------------------------------- ##

maxGroupSize <- length(cols.key)
indsMatrix <- createIndsMatrix(cols.key, maxGroupSize, returnIndecies=TRUE, sortBySize=TRUE)


cls(4)
fbDTnms <- list()
start.time <- proc.time()
for (DateIn in unique(c(TRUE, !iterOverDate)) ) {
  for(ind in c(0, indsMatrix)) {  ## Adds a `0` to indsMatrix for "Just Date"
    cl <- cols.key[ind]

    #  -----  Verbose Output  -----   #
      dashlen=52
      verboseMsg(verboseIter, "\n\n", pasteR("-", dashlen), time=FALSE)
      verboseMsg(verboseIter, center(pasteC("ITERATION: ", paste0(cl, collapse=" + "), ifelse(DateIn, "+ Date", " (No Date)")), width.min=dashlen-15, align="l"),"   \n", pasteR("-", dashlen), endl=0)
    #  -----  Verbose Output  -----   #
    
    DT.nm  <- makeAggName(cl, DateIn, c("fb","agg"))
    fbDTnms[mkDTListNm(cl, DateIn)] <- DT.nm
    byCols <- cl
      
    cols.keeping <- unlist(addlToDropByKey[cl])
    ## Build up which columns to drop
    # Start with the calculated columns (since these will need to be recomputed)
    cols.dropping <- c(setdiff(cols.key, cl))
    cols.dropping <- c(addlToDropAlways, cols.dropping, 
                      unlist(addlToDropByKey[cols.dropping]))
    # Add from cols.key all other columns not used this iteration
    # Add any additional columns based on cl
      
    assign(DT.nm, envir=envir,
      addCalcCols_(addGVisID=TRUE, addUniques=addUniques, addSplits=TRUE, 
        DT=aggBy(fb, byCols=byCols, DateIn=DateIn, DateOut=!DateIn
                , cols.agging=cols.agging, cols.dropping=cols.dropping
                , cols.keeping=cols.keeping, verbose=verbose))
    )
  }
}
end.time <- proc.time()
msgBox("Time to Create: \n", capture.output(end.time - start.time), prelines=3, shiftLeft=6)

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