R routines for printing some statistics
[monitor.git] / statistics / functions.r
1 slices <- function (x, components=FALSE) 
2 {
3     m<-x$memsize;
4     d<-x$disksize/250;
5     c<-x$cpuspeed;
6     r<-x$numcores;
7     if ( components ) {
8         a<-c(m,d,c*r);
9     } else {
10         a<-(m+d+c*r);
11     }
12     return(a/2);
13 }
14
15 slices_2 <- function (x, components=FALSE) 
16 {
17     # Define an ideal, then scale each measurement relative to the ideal.
18     # If it matches it will be more or less than 1
19     # does this scale (up or down) linearly, and why not?
20
21     # 4, 2.4x2, 1000; 4, 3.2x1, 320; 1, 2.4x1, 160
22     ideal_m <- 3.4;             # GB
23     ideal_c <- 2.4;             # GHz 
24     ideal_d <- 450;             # GB
25     ideal_r <- 2;
26
27     m<-x$memsize/ideal_m;
28     d<-x$disksize/ideal_d;
29     c<-x$cpuspeed/ideal_c;
30     r<-x$numcores/ideal_r;
31     # ideal is 1
32
33     if ( components ) {
34         a<-c(m,d,c*r);
35     } else {
36         a<-(m+d+c*r);
37     }
38
39     return (a/3*5);
40 }
41
42 slices_3 <- function (x, components=FALSE) 
43 {
44     # Define an ideal, then scale each measurement relative to the ideal.
45     # If it matches it will be more or less than 1
46     # does this scale (up or down) linearly, and why not?
47
48     # 4, 2.4x2, 1000; 4, 3.2x1, 320; 1, 2.4x1, 160
49     ideal_m <- 3.4; #GB
50     ideal_c <- 2.4; #GHz
51     ideal_d <- 450;     #GB
52     ideal_r <- 2;
53         ideal_bw <- 100000;      #Kbps
54
55     m<-x$memsize/ideal_m;
56     d<-x$disksize/ideal_d;
57     c<-x$cpuspeed/ideal_c;
58     r<-x$numcores/ideal_r;
59     b<-log(x$bwlimit)/log(ideal_bw);
60     # ideal is 1
61
62     if ( components ) {
63         a<-c(m,d,c*r,b);
64     } else {
65         a<-(m+d+c*r+b);
66     }
67
68     return (a/4*5);
69 }
70
71 slices_4 <- function (x, components=FALSE) 
72 {
73     # Define an ideal, then scale each measurement relative to the ideal.
74     # If it matches it will be more or less than 1
75     # does this scale (up or down) linearly, and why not?
76
77     # 4, 2.4x2, 1000; 4, 3.2x1, 320; 1, 2.4x1, 160
78     ideal_m <- 3.4; #GB
79     ideal_c <- 2.4; #GHz
80     ideal_d <- 450;     #GB
81     ideal_r <- 2;
82         ideal_bw <- 100000;      #Kbps
83         ideal_pcu <- 1;
84
85     m<-x$memsize/ideal_m;
86     d<-x$disksize/ideal_d;
87     c<-x$cpuspeed/ideal_c;
88     r<-x$numcores/ideal_r;
89     b<-log(x$bwlimit)/log(ideal_bw);
90         p<-x$pcustatus/ideal_pcu;
91     # ideal is 1
92
93     if ( components ) {
94         a<-c(m,d,c*r,b,p);
95     } else {
96         a<-(m+d+c*r+b+p);
97     }
98
99     return (a/5*5);
100 }
101
102 index_of_bin <- function (h, value)
103 {
104     index <- 0;
105
106     for (i in sequence(length(h$breaks))) 
107     {
108         # first bin
109
110         if ( value < h$breaks[1] )
111         {
112             index <- 1;
113             break;
114         } 
115
116         # last bin
117
118         if ( i == length(h$breaks) )
119         {
120             # end of line
121             index <- i;
122             break;
123         } 
124
125         # all other bins
126
127         if ( value > h$breaks[i] && value <= h$breaks[i+1] )
128         {
129             index <- i+1;
130             break;
131         } 
132     }
133     if ( index == 0 ) {
134         warning("index == 0, no bin assigned for value: ", value);
135     }
136
137     return (index);
138 }
139
140 start_image <- function (name, width=480, height=480)
141 {
142     png(name, width=width, height=height);
143 }
144
145 end_image <- function ()
146 {
147     dev.off()
148 }
149
150
151 plot_rt_hist <- function (t, imagename=0)
152 {
153     d2 <- (t$lastreply - t$start)
154     std_dev <- sd(log(d2))
155     m <- mean(log(d2))
156     print(sprintf("mean: %s, stddev: %s\n", m, std_dev));
157
158     if ( imagename != 0 ) { start_image(imagename) }
159
160     h<-hist(log(d2), 
161         xlab="Hours between ticket creation and final reply", 
162         main="Time to Final Reply for RT Tickets", axes=FALSE)
163     
164     a<-exp(h$breaks)/(60*60)    # convert units from log(secs) to hours
165     axis(1,labels=signif(a,2), at=h$breaks)
166     axis(2)
167
168     x<-seq(min(h$breaks),max(h$breaks),length=500)
169     y<-dnorm(x,mean=m, sd=std_dev)
170
171     # scale y to the size of h's 'counts' vector rather than the density function
172     lines(x,y*max(h$counts)/max(y))
173     if ( imagename != 0 ) { end_image() }
174 }
175
176 year_hist <- function (t, year, from, to, max, type="week", title="Histogram for Tickets in")
177 {
178     dates <-seq(as.Date(from), as.Date(to), type)
179     months <- format(dates, "%b-%d")
180     hbreaks<-unclass(as.POSIXct(dates))
181     h<-hist(t$start, breaks=hbreaks, plot=FALSE)
182     main<-sprintf(paste(title, "%s: MEAN %s\n"), year, mean(h$counts))
183     print(main);
184     if ( max == 0 ) {
185         max = max(h$counts)
186     }
187     plot(h, ylim=c(0,max), main=main, axes=FALSE)
188     axis(1, labels=months, at=hbreaks)
189     axis(2)
190     abline(mean(h$counts), 0, col='grey')
191     #qqnorm(h$counts)
192     #qqline(h$counts)
193 }
194
195 year_hist_unique <- function (t, year, from, to, max, type="week", title="Histogram for Tickets in")
196 {
197     dates <-seq(as.Date(from), as.Date(to), type)
198     months <- format(dates, "%b-%d")
199     hbreaks<-unclass(as.POSIXct(dates))
200
201     rows <- NULL
202     for ( d in hbreaks )
203     {
204         d_end <- d+60*60*24
205         t_sub <- t[which(t$start > d & t$start <= d_end),]
206         rows <- rbind(rows, c('start'=d, 'reboots'=length(unique(t_sub$hostname))) )
207     }
208     rows <- data.frame(rows)
209
210     if ( max == 0 ) {
211         max = max(rows$reboots)
212     }
213     main<-sprintf(paste(title, "%s: MEAN %s\n"), year, mean(rows$reboots))
214     print(main);
215     barplot(rows$reboots, ylim=c(0,max), main=main, axes=FALSE, space=0)
216     #plot(h, ylim=c(0,max), main=main, axes=FALSE)
217     axis(1, labels=months, at=seq(1,length(hbreaks)))
218     axis(2)
219     abline(mean(rows$reboots), 0, col='grey')
220     #qqnorm(h$counts)
221     #qqline(h$counts)
222     return (rows);
223 }
224
225 year_hist_unique_recent <- function (t, year, from, to, max, blocks=c(1,3,7,14,30), type="week", title="Histogram for Tickets in")
226 {
227     dates <-seq(as.Date(from), as.Date(to), type)
228     months <- format(dates, "%b-%d")
229     hbreaks<-unclass(as.POSIXct(dates))
230
231     rows <- NULL
232
233
234     for ( d in hbreaks )
235     {
236         # initialize row for this iteration
237         row <- NULL
238         row[as.character(0)] <- 0
239         for ( block in blocks ) {
240             row[as.character(block)] <- 0
241         }
242
243         # find the range : d plus a day
244         d_end <- d+60*60*24
245         # find unique hosts in this day range
246         t_sub <- t[which(t$start > d & t$start <= d_end),]
247         unique_hosts <- unique(t_sub$hostname)
248         if (length(unique_hosts) == 0 ) { 
249             rows <- rbind(rows, c('start'=d, row))
250             next 
251         }
252
253         #print(sprintf("unique_hosts: %s\n", unique_hosts));
254         print(sprintf("unique_hosts: %s\n", length(unique_hosts)));
255
256         for ( host in as.character(unique_hosts) ) 
257         {
258             found <- 0
259             for ( block in blocks )
260             {
261                 #print(sprintf("date: %s, block: -%s, %s\n", d, block, host));
262                 #print(sprintf("row: %s\n", row));
263                 # find the range : 'block' days ago to 'd'
264                 d_back <- d - 60*60*24 * block
265                 t_back_sub <- t[which(t$start > d_back & t$start <= d),]
266                 u <- unique(t_back_sub$hostname)
267                 if ( length(u[u==host]) >= 1) 
268                 {
269     #               add to block_count and go to next host.
270                     found <- 1
271                     i <- as.character(block)
272                     row[i] <- row[i] + 1
273                     break
274                 }
275             }
276             if ( found == 0 )
277             {
278                 # no range found
279                 row['0'] <- row['0'] + 1
280             }
281         }
282         rows <- rbind(rows, c('start'=d, row))
283     }
284
285     rows <- data.frame(rows)
286
287     if ( max == 0 ) {
288         max = max(rows['0'])
289     }
290     #main<-sprintf(paste(title, "%s: MEAN %s\n"), year, mean(rows$reboots))
291     #print(main);
292     #barplot(rows$reboots, ylim=c(0,max), main=main, axes=FALSE, space=0)
293     ##plot(h, ylim=c(0,max), main=main, axes=FALSE)
294     #axis(1, labels=months, at=seq(1,length(hbreaks)))
295     #axis(2)
296     #abline(mean(rows$reboots), 0, col='grey')
297     #qqnorm(h$counts)
298     #qqline(h$counts)
299     return (rows);
300 }
301
302 source("myImagePlot.R")
303 reboot_image <- function (t, year, from, to, max=0, type="week", title="")
304 {
305     dates <-seq(as.Date(from), as.Date(to), type)
306     months <- format(dates, "%b-%d")
307     hbreaks<-unclass(as.POSIXct(dates))
308
309     rows <- NULL
310     image <- matrix(data=0, nrow=max(as.numeric(t$hostname)), ncol=length(hbreaks))
311     #image <- matrix(data=0, nrow=length(unique(t$hostname)), ncol=length(hbreaks))
312
313     #for ( d in hbreaks )
314     for ( i in seq(1, length(hbreaks)) )
315     {
316         # find the range : d plus a day
317         d <- hbreaks[i]
318         d_end <- d+60*60*24
319         # find unique hosts in this day range
320         t_sub <- t[which(t$start > d & t$start <= d_end),]
321         unique_hosts <- unique(t_sub$hostname)
322         if (length(unique_hosts) == 0 ) { next }
323
324         for ( host in unique_hosts ) 
325         {
326             image[host,i] <- 1
327         }
328     }
329
330     myImagePlot(image, xLabels=months, yLabels=c(""), title=title)
331
332             #found <- 0
333             #for ( block in blocks )
334             #{
335                 #print(sprintf("date: %s, block: -%s, %s\n", d, block, host));
336                 #print(sprintf("row: %s\n", row));
337                 # find the range : 'block' days ago to 'd'
338             #    d_back <- d - 60*60*24 * block
339             #    t_back_sub <- t[which(t$start > d_back & t$start <= d),]
340             #    u <- unique(t_back_sub$hostname)
341             #    if ( length(u[u==host]) >= 1) 
342             #    {
343     #       #        add to block_count and go to next host.
344             #        found <- 1
345             #        i <- as.character(block)
346             #        row[i] <- row[i] + 1
347             #        break
348             #    }
349             #}
350             #if ( found == 0 )
351             #{
352             #    # no range found
353             #    row['0'] <- row['0'] + 1
354             #}
355         #}
356         #rows <- rbind(rows, c('start'=d, row))
357
358     #rows <- data.frame(rows)
359
360     #if ( max == 0 ) {
361     #    max = max(rows['0'])
362     #}
363     #main<-sprintf(paste(title, "%s: MEAN %s\n"), year, mean(rows$reboots))
364     #print(main);
365     #barplot(rows$reboots, ylim=c(0,max), main=main, axes=FALSE, space=0)
366     ##plot(h, ylim=c(0,max), main=main, axes=FALSE)
367     #axis(1, labels=months, at=seq(1,length(hbreaks)))
368     #axis(2)
369     #abline(mean(rows$reboots), 0, col='grey')
370     #qqnorm(h$counts)
371     #qqline(h$counts)
372     return (image);
373 }
374
375 add_year <- function (t)
376 {
377     t$year <- c(0)  # assign new column with zero value initially
378     for ( i in 1:length(t$start) )
379     {
380         d <- as.POSIXlt(t$start[i], origin="1970-01-01")
381         year <- d$year + 1900 # as.numeric(format(d, "%Y"))
382         t$year[i] <- year
383     }
384     return (t);
385 }
386
387 add_timestamp <- function (t)
388 {
389     t$start <- c(0)  # assign new column with zero value initially
390     for ( i in 1:length(t$date) )
391     {
392         tstamp <-unclass(as.POSIXct(t$date[i], origin="1970-01-01"))[1]
393         t$start[i] <- tstamp
394     }
395     return (t);
396 }
397
398 abline_at_date <- function (date, col='black', lty=1, format="%Y-%m-%d")
399 {
400     ts <-unclass(as.POSIXct(date, format=format, origin="1970-01-01"))[1]
401     abline(v=ts, col=col, lty=lty)
402     return (ts);
403 }