~ubuntuone-hackers/+junk/ttd-server-side

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
library(ggplot2)
library(reshape)

get.project.name <- function(filename) {
  # Expect 'filename' to be of the form 'projectname-YYYYMMDD.csv'.
  return(unlist(strsplit(basename(filename), "-", fixed=TRUE))[1])
}


load.project <- function(project.name, filename) {
  data <- read.csv(filename)
  data$project <- project.name
  return(data)
}


get.png.filename <- function(project.name) {
  TODAY <- format(Sys.Date(), format="%Y%m%d")
  paste(project.name, "-", TODAY, ".png", sep="")
}

from.unix <- function(timestamp) {
  EPOCH <- "1970-01-01"
  UTC <- "GMT"
  return(as.POSIXct(timestamp, origin=EPOCH, tz=UTC))
}

load.data <- function(filenames) {
  projects <- NULL
  for (filename in filenames) {
    project.name <- get.project.name(filename)
    print(project.name)
    data <- load.project(project.name, filename)
    projects <- rbind(projects, data)
  }
  return(projects)
}

clean.data <- function(projects) {
  projects <- within(projects, {
    started <- from.unix(started)
    posted <- from.unix(posted)
    approved <- from.unix(approved)
    landed <- from.unix(landed)
    deployed <- from.unix(deployed)
  })
  
  units <- "days"
  
  projects <- within(projects, {
    to.mp <- as.numeric(posted - started, units=units)
    to.approve <- as.numeric(approved - posted, units=units)
    to.land <- as.numeric(landed - approved, units=units)
    to.deploy <- as.numeric(deployed - landed, units=units)
    start.to.finish <- as.numeric(deployed - started, units=units)
  })
  
  return(projects)
}

plot.projects <- function(projects) {
  PLOT.WIDTH <- 1000
  PLOT.HEIGHT <- 450
  breakdown <- melt(
    projects, id=c("revno", "started", "project"), 
    measure.vars=c("to.mp", "to.approve", "to.land", "to.deploy"))
  names <- unique(breakdown$project)
  for (name in names) {
    this.project <- subset(breakdown, project == name)
    filename <- get.png.filename(name)
    png(filename, width=PLOT.WIDTH, height=PLOT.HEIGHT)
    print(qplot(data=this.project, fill=variable, x=revno,
                weight=value, ylab="Days from start to deploy", binwidth=1))
    dev.off()
  }
}

main <- function(args) {
  projects <- load.data(args)
  projects <- clean.data(projects)
  plot.projects(projects)
}

main(commandArgs(trailingOnly = TRUE))