#
# manpower-fishing.R, 31 May 26
#
# Data from:
# The CESAW dataset: a conversation
# Derek M. Jones and William R. Nichols

library("colorspace")
library("bizdays")
library("plyr")


par(las=1)
par(bty="l")
par(pch="+")
pal_col=rainbow(2)

wd_cal=create.calendar("workdays", weekdays=c("saturday", "sunday"))


staff_line=function(cur_ph, col_str="black")
{
lines(cur_ph, col=col_str)
}


phase_aggregate=function(df, phases)
{
max_day=max(df$work_day)

   phase_inf=function(df)
   {
   # staff_per_day=ddply(df, .(work_day), function(df) length(unique(df$person_key)))
   mins_per_day=ddply(df, .(work_day), function(df) sum(df$time_log_delta_minutes, na.rm=TRUE))
   cur_ph=rep(0, max_day)
   cur_ph[mins_per_day$work_day]=mins_per_day$V1
   return(cur_ph)
   }

df_ph=subset(df, phase_short_name %in% phases)

all_phases=daply(df_ph, .(phase_short_name), phase_inf)

return(all_phases)
}


# Time facts
tif=read.csv("CESAW_time_fact.csv")

# tif$act_start_date_time=as.POSIXct(tif$task_actual_start_date, format="%m/%d/%Y %H:%M")
# tif$act_start=as.integer(tif$act_start_date_time)
tif$act_start_date=as.Date(tif$time_log_start_date, format="%m/%d/%Y")
tif$act_start_day=as.numeric(tif$act_start_date)
# tif$act_end_date_time=as.POSIXct(tif$task_actual_complete_date, format="%m/%d/%Y %H:%M")
# tif$act_end=as.integer(tif$act_end_date_time)
tif$act_end_date=as.Date(tif$time_log_end_date, format="%m/%d/%Y")
tif$act_end_day=as.numeric(tif$act_end_date)


p614=subset(tif, project_key == 614)
start_day=min(p614$act_start_day, na.rm=TRUE)

p614$work_day=bizdays(start_day, p614$act_start_day, wd_cal)+1

day_cnt=count(p614$act_start_day-start_day)

plot(day_cnt,
	ylim=c(0, 120),
	xlab="Days")


mins_per_day=ddply(p614, .(act_start_day), function(df) sum(df$time_log_delta_minutes, na.rm=TRUE))

plot(mins_per_day$act_start_day-start_day, mins_per_day$V1,
	ylim=c(0, 8e3),
	ylab="Total minutes")

staff_per_day=ddply(p614, .(act_start_day), function(df) length(unique(df$person_key)))

plot(staff_per_day$act_start_day-start_day, staff_per_day$V1,
	ylab="People")

total_proj_days=ddply(tif, .(project_key), function(df) sum(df$time_log_delta_minutes, na.rm=TRUE)/60/7.5)
#  [1]    2.828889   71.646667   94.380000  257.151111  299.900000   99.913333
#  [7]  105.131111   68.911111   76.908889  267.771111  177.488889   74.211111
# [13]  735.311111   41.811111  388.160000  316.055556  350.155556  523.504444
# [19]  120.660000  173.442222  422.068889  110.222222  173.771111  152.248889
# [25]  204.693333  170.993333  300.488889  103.033333  453.631111  324.411111
# [31]  207.353333  446.233333  454.313333  170.453333 1457.022222  950.766667
# [37] 1032.902222 4675.371111 1545.204444

# day_phase=phase_aggregate(p614, c("Design", "Design Inspect", "Code", "Code Inspect", "Test", "Int Test"))
day_phase=phase_aggregate(p614, c("Design", "Code", "Test"))

phase_names=rownames(day_phase)
num_phases=length(phase_names)
pal_col=rainbow(num_phases)


plot(0, type="n",
	xaxs="i", yaxs="i",
	xlim=c(0, ncol(day_phase)), ylim=c(0, max(rowSums(day_phase))),
	xlab="Work days", ylab="Minutes (cumulative)")

dummy=sapply(1:num_phases, function(X) staff_line(cumsum(day_phase[X, ]), pal_col[X]))

legend("topleft", legend=phase_names, cex=1.2,
                        fill=pal_col, border="white", bty="n")


num_weeks=(ncol(day_phase)+4) %/% 5

week_phase=sapply(seq(1, ncol(day_phase)-4, by=5), function(X) rowSums(day_phase[ , X+(0:4)]))

plot(0, type="n",
	xaxs="i", yaxs="i",
	xlim=c(0, ncol(week_phase)), ylim=c(0, max(week_phase/60)),
	xlab="Week", ylab="Hours")

dummy=sapply(1:num_phases, function(X) staff_line(week_phase[X, ]/60, pal_col[X]))

legend("topleft", legend=phase_names, cex=1.2,
                        fill=pal_col, border="white", bty="n")

dev.copy(dev=png, file="p614-hours-week.png")
dev.off()


# Plot timeline of developers working on a project
person_timeline=function(p_df, p_num)
{
task_points=function(df)
   {
   points(df$start_date, df$person_ord, col=pal_col[df$person_ord[1]])
   }


p_unq=unique(p_df$person_key[order(p_df$start_date)])
pal_col=rainbow(length(p_unq))
p_df$person_ord=mapvalues(p_df$person_key, p_unq, 1:length(p_unq))

plot(p_df$start_date[1], 0, type="n",
        xaxs="i", yaxs="i",
        xlim=range(p_df$start_date, na.rm=TRUE), ylim=c(0.8, 0.2+length(p_unq)),
        xlab="Date", ylab="Person id") 

text(p_df$start_date[1]+5, max(p_df$person_ord), p_num, pos=1, cex=1.2)

p_tasks=d_ply(p_df, .(person_ord), task_points)
}

tif$start_date=as.Date(tif$time_log_start_date, format="%m/%d/%Y")
p614=subset(tif, project_key == 614)

length(unique(p614$person_key))

person_timeline(p614, 614)

dev.copy(dev=png, file="devP614-timeline.png")
dev.off()


