## This is just for manual execution 
if (FALSE) 
  {
    fresh()
    START.TIME <- proc.time()
    iter <- TRUE
    source("/Users/rsaporta/git/orch/src/MktingReport/Funcs.r")
    source("/Users/rsaporta/git/orch/src/MktingReport/01 FB Report Load & Clean.r")
    source("/Users/rsaporta/git/orch/src/MktingReport/02 fb cleanup.r")
    source("/Users/rsaporta/git/orch/src/MktingReport/04 googleVis.r")
    source("/Users/rsaporta/git/orch/src/MktingReport/04b googleVis - NEW.r")
    source("/Users/rsaporta/git/orch/src/MktingReport/05 Gvis on Date.r")
    END.TIME <- proc.time()
    msgBox("Time to Source functions", capture.output(END.TIME-START.TIME))
    # runShiny("FBShiny_app")
  }

# quick wrapper function to source the funcs file
.fs <- function()   source("/Users/rsaporta/git/orch/src/MktingReport/Funcs.r")

#  Note To Self, these columns have no values (as of late)
#       Conversion.Website 
#       Conversion.OtherWebsite           
#       EventResponse 
#  which(colSums(fb[,sapply(fb, is.numeric),with=FALSE] )==0)


# --------------------------------------------------------------- #
    utilSource()
    pkgs <- c("data.table", "Hmisc", "googleVis", "shiny")
    suppressPackageStartupMessages( lib(pkgs, update=FALSE, quietly=TRUE) )

    projName <- "MktingReport"
    wrkDir <- "~/git/orch"
    setScience(wrkDir=wrkDir, project=projName, subl=FALSE)
    
    source(as.path(srcDir, "Funcs.r"))
# --------------------------------------------------------------- #


## CSV Files being used
fbRaw.fileName <- "! Rick - Daily (No Calc) - Last 28 Days, Account The Orchard, 2013-04-01 - 2013-09-25.csv"
fbRaw.dirName  <- as.path(dataDir, "DLd from Source")
fbRaw.file  <- as.path(fbRaw.dirName, fbRaw.fileName)

## Dictionary File, for column names
fbCols.dict.file <- as.path(dataDir, "../dicts/fbAddsReportColNames.dict.csv")


## Read in the raw file
fb <- as.data.table(read.table(fbRaw.file, sep=",", quote="\"", header=TRUE))

## Make the column names work
headernms <- readLines(fbRaw.file, n=1)
headernms <- read.table(text=headernms, sep=",", quote="\"", header=FALSE, stringsAsFactors=FALSE)
headernms <- unlist(headernms)

dict.fbCols <- makeDictFromCSV(fbCols.dict.file)

## Check if there are any names not accounted for in the names of the DT
NamesNotInDict <- setdiff(headernms, names(dict.fbCols))
if(length(NamesNotInDict)!=0) {
  warning("The following column names have no predefined value in the dictionary.\n\t", paste0(NamesNotInDict, collapse="\n\t"), "\n")  
}

## Set the names, watching out for NA's. 
newNames <- dict.fbCols[headernms]
setnames(fb, names(fb)[!is.na(newNames)], newNames[!is.na(newNames)])

## Clean up Char Cols
cols.factor.char <- c("CampaignID", "AdID")
fb[, c(cols.factor.char) := lapply(.SD, as.factor), .SDcols=cols.factor.char]

### Clean Up Date Values
## All lines should be 1-date-per-row.
if(fb[, any(StartDate != EndDate)]) {
  stop ("Data should have only one date per row. (Not aggregated by week, etc). Please re export the Facebook data and try again.")
}
fb[, c("Date") := list(as.Date(as.character(StartDate)))]
fb[, c("StartDate", "EndDate") := NULL]

# -------------------- #
## Clean up a few values & Add LabelID info
# -------------------- #
  # clean Age, unknown age
  fb[Age=="", Age := "unknown"]
  # Upon Further inspection, it appears that these are erroneous to some extent.
  fb <- fb[Age != "unknown"]

  # clean Campaign, remove unnecessary quotes
  fb[, Campaign := as.character(Campaign)]
  fb[grepl("^Promoting", Campaign), Campaign := paste0(gsub("^Promoting \"|\"$", "", Campaign), " - Promo"), by=Campaign]
  fb[, Campaign := cleanWS(gsub("\"", "", Campaign))]

  ## Add LabelID & Info
  fb[, c("LabelID", "Label") :=  extractInfoFromCampaign(Campaign)[, list(LabelID, Label)]
     , by = Campaign]

  # fb <- fb[!(Spend  < 0.01)]

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



