lib(ggplot, quiet=TRUE)

.us()
setScience(proj="MgmtReport", load=FALSE, subl=FALSE, quiet=TRUE) 

loadFromJesus("FORECASTS", over=TRUE, verbose=TRUE)
loadFromJesus("DT.Total_Reported_Revenue_By_Month", over=FALSE, verbose=TRUE, dont.fail.ifexists=TRUE)

kCols.fore <- c("date", "fakeToday", "fakeToday.GL")

DT.allforecasts <- rbindlist(FORECASTS)
setnames(DT.allforecasts, "forecasted_value", "forecasted_gross")
setnames(DT.allforecasts, gsub("forecasted_", "", names(DT.allforecasts)))

## Columns that will be aggreagated for forecasting
fore_cols <- c("gross", "lo80", "lo95", "hi80", "hi95")

## aggreagte the monthl forecast
DT.monthlyforecasts <- DT.allforecasts[, lapply(.SD, sumn), .SDcols=fore_cols, keyby=kCols.fore]

## Min date to keep 
minDateUsing.forecast <- as.Date("2014-01-01")
minDateUsing.forecast <- min(minDateUsing.forecast, DT.monthlyforecasts$date)

## Caclulate the days out from target
DT.monthlyforecasts[, daysFromTarget := date - fakeToday]
## round to nearest 15 days
DT.monthlyforecasts[, daysFromTarget := roundOutToX(daysFromTarget, x=15)]
DT.monthlyforecasts[, daysFromTarget.factor := {str <- paste(daysFromTarget, "days"); factor(str, levels=unique(str)[order(unique(daysFromTarget))])}]


&&& FUCK - why are these numbers not even close to accurate? 
Meaning, accurate to what I forecasted when running regular
(side note .. capture the last git commit)
Maybe data missing from 08?  No 
MARGINS!!! 


## PLOT
{
  units_scale <- "thousands"
  
  P <- ggplot()
  P <- P + geom_line(data=DT.monthlyforecasts, aes(x=date, y=gross, color=daysFromTarget.factor))
  P <- P + geom_line(data=DT.Total_Reported_Revenue_By_Month[date > minDateUsing.forecast], aes(x=date, y=reported_gross, color="As-Reported"))
  P <- P + theme(legend.position="right") + dollar.y(units=units_scale) + ylab(sprintf("Gross (in %s)", units_scale))
  P
}

dollar.y(units="twos")

M <- matrix(seq(-10, 15, length.out=12), ncol=3)
roundOutToX(M, 3)

---------

.us()
fresh()
.us()
setScience(proj="MgmtReport", load=FALSE, subl=FALSE, quiet=TRUE) 
# debugOn("jesus")
debugOff("parseBackUpFile")
showBackUpsAvailable(pat="^DT")
.cc(btext())
