add new scripts
[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", fmt="%b-%d")
177 {
178     dates <-seq(as.Date(from), as.Date(to), type)
179     months <- format(dates, fmt)
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     print(h$counts);
185     if ( max == 0 ) {
186         max = max(h$counts)
187     }
188     plot(h, ylim=c(0,max), main=main, axes=FALSE)
189     axis(1, labels=months, at=hbreaks)
190     axis(2)
191     abline(mean(h$counts), 0, col='grey')
192     #qqnorm(h$counts)
193     #qqline(h$counts)
194     return (h);
195 }
196
197 year_hist_unique <- function (t, year, from, to, max, type="week", title="Histogram for Tickets in")
198 {
199     dates <-seq(as.Date(from), as.Date(to), type)
200     months <- format(dates, "%b-%d")
201     hbreaks<-unclass(as.POSIXct(dates))
202
203     rows <- NULL
204     for ( d in hbreaks )
205     {
206         d_end <- d+60*60*24
207         t_sub <- t[which(t$start > d & t$start <= d_end),]
208         rows <- rbind(rows, c('start'=d, 'reboots'=length(unique(t_sub$hostname))) )
209     }
210     rows <- data.frame(rows)
211
212     if ( max == 0 ) {
213         max = max(rows$reboots)
214     }
215     main<-sprintf(paste(title, "%s: MEAN %s\n"), year, mean(rows$reboots))
216     print(main);
217     barplot(rows$reboots, ylim=c(0,max), main=main, axes=FALSE, space=0)
218     #plot(h, ylim=c(0,max), main=main, axes=FALSE)
219     axis(1, labels=months, at=seq(1,length(hbreaks)))
220     axis(2)
221     abline(mean(rows$reboots), 0, col='grey')
222     #qqnorm(h$counts)
223     #qqline(h$counts)
224     return (rows);
225 }
226
227 year_hist_unique_recent <- function (t, year, from, to, max, blocks=c(1,3,7,14,30), type="week", title="Histogram for Tickets in")
228 {
229     dates <-seq(as.Date(from), as.Date(to), type)
230     months <- format(dates, "%b-%d")
231     hbreaks<-unclass(as.POSIXct(dates))
232
233     rows <- NULL
234
235
236     for ( d in hbreaks )
237     {
238         # initialize row for this iteration
239         row <- NULL
240         row[as.character(0)] <- 0
241         for ( block in blocks ) {
242             row[as.character(block)] <- 0
243         }
244
245         # find the range : d plus a day
246         d_end <- d+60*60*24
247         # find unique hosts in this day range
248         t_sub <- t[which(t$start > d & t$start <= d_end),]
249         unique_hosts <- unique(t_sub$hostname)
250         if (length(unique_hosts) == 0 ) { 
251             rows <- rbind(rows, c('start'=d, row))
252             next 
253         }
254
255         #print(sprintf("unique_hosts: %s\n", unique_hosts));
256         print(sprintf("unique_hosts: %s\n", length(unique_hosts)));
257
258         for ( host in as.character(unique_hosts) ) 
259         {
260             found <- 0
261             for ( block in blocks )
262             {
263                 #print(sprintf("date: %s, block: -%s, %s\n", d, block, host));
264                 #print(sprintf("row: %s\n", row));
265                 # find the range : 'block' days ago to 'd'
266                 d_back <- d - 60*60*24 * block
267                 t_back_sub <- t[which(t$start > d_back & t$start <= d),]
268                 u <- unique(t_back_sub$hostname)
269                 if ( length(u[u==host]) >= 1) 
270                 {
271     #               add to block_count and go to next host.
272                     found <- 1
273                     i <- as.character(block)
274                     row[i] <- row[i] + 1
275                     break
276                 }
277             }
278             if ( found == 0 )
279             {
280                 # no range found
281                 row['0'] <- row['0'] + 1
282             }
283         }
284         rows <- rbind(rows, c('start'=d, row))
285     }
286
287     rows <- data.frame(rows)
288
289     if ( max == 0 ) {
290         max = max(rows['0'])
291     }
292     #main<-sprintf(paste(title, "%s: MEAN %s\n"), year, mean(rows$reboots))
293     #print(main);
294     #barplot(rows$reboots, ylim=c(0,max), main=main, axes=FALSE, space=0)
295     ##plot(h, ylim=c(0,max), main=main, axes=FALSE)
296     #axis(1, labels=months, at=seq(1,length(hbreaks)))
297     #axis(2)
298     #abline(mean(rows$reboots), 0, col='grey')
299     #qqnorm(h$counts)
300     #qqline(h$counts)
301     return (rows);
302 }
303
304 source("myImagePlot.R")
305 reboot_image <- function (t, year, from, to, max=0, type="week", title="")
306 {
307     dates <-seq(as.Date(from), as.Date(to), type)
308     months <- format(dates, "%b-%d")
309     hbreaks<-unclass(as.POSIXct(dates))
310
311     rows <- NULL
312     image <- matrix(data=0, nrow=max(as.numeric(t$hostname)), ncol=length(hbreaks))
313     #image <- matrix(data=0, nrow=length(unique(t$hostname)), ncol=length(hbreaks))
314
315     #for ( d in hbreaks )
316     for ( i in seq(1, length(hbreaks)) )
317     {
318         # find the range : d plus a day
319         d <- hbreaks[i]
320         d_end <- d+60*60*24
321         # find unique hosts in this day range
322         t_sub <- t[which(t$start > d & t$start <= d_end),]
323         unique_hosts <- unique(t_sub$hostname)
324         if (length(unique_hosts) == 0 ) { next }
325
326         for ( host in unique_hosts ) 
327         {
328             image[host,i] <- 1
329         }
330     }
331
332     myImagePlot(image, xLabels=months, yLabels=c(""), title=title)
333
334             #found <- 0
335             #for ( block in blocks )
336             #{
337                 #print(sprintf("date: %s, block: -%s, %s\n", d, block, host));
338                 #print(sprintf("row: %s\n", row));
339                 # find the range : 'block' days ago to 'd'
340             #    d_back <- d - 60*60*24 * block
341             #    t_back_sub <- t[which(t$start > d_back & t$start <= d),]
342             #    u <- unique(t_back_sub$hostname)
343             #    if ( length(u[u==host]) >= 1) 
344             #    {
345     #       #        add to block_count and go to next host.
346             #        found <- 1
347             #        i <- as.character(block)
348             #        row[i] <- row[i] + 1
349             #        break
350             #    }
351             #}
352             #if ( found == 0 )
353             #{
354             #    # no range found
355             #    row['0'] <- row['0'] + 1
356             #}
357         #}
358         #rows <- rbind(rows, c('start'=d, row))
359
360     #rows <- data.frame(rows)
361
362     #if ( max == 0 ) {
363     #    max = max(rows['0'])
364     #}
365     #main<-sprintf(paste(title, "%s: MEAN %s\n"), year, mean(rows$reboots))
366     #print(main);
367     #barplot(rows$reboots, ylim=c(0,max), main=main, axes=FALSE, space=0)
368     ##plot(h, ylim=c(0,max), main=main, axes=FALSE)
369     #axis(1, labels=months, at=seq(1,length(hbreaks)))
370     #axis(2)
371     #abline(mean(rows$reboots), 0, col='grey')
372     #qqnorm(h$counts)
373     #qqline(h$counts)
374     return (image);
375 }
376
377 add_year <- function (t)
378 {
379     t$year <- c(0)  # assign new column with zero value initially
380     for ( i in 1:length(t$start) )
381     {
382         d <- as.POSIXlt(t$start[i], origin="1970-01-01")
383         year <- d$year + 1900 # as.numeric(format(d, "%Y"))
384         t$year[i] <- year
385     }
386     return (t);
387 }
388
389 add_timestamp <- function (t)
390 {
391     t$start <- c(0)  # assign new column with zero value initially
392     for ( i in 1:length(t$date) )
393     {
394         tstamp <-unclass(as.POSIXct(t$date[i], origin="1970-01-01"))[1]
395         t$start[i] <- tstamp
396     }
397     return (t);
398 }
399
400 abline_at_date <- function (date, col='black', lty=1, format="%Y-%m-%d")
401 {
402     ts <-unclass(as.POSIXct(date, format=format, origin="1970-01-01"))[1]
403     abline(v=ts, col=col, lty=lty)
404     return (ts);
405 }
406
407 tstamp <- function (date, format="%Y-%m-%d")
408 {
409     ts <- unclass(as.POSIXct(date, format=format, origin="1970-01-01"))[1]
410     return (ts)
411 }