PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/MovingAverageLoadUnsortedLog.groovy

http://groovy-log-analyzer.googlecode.com/
Groovy | 46 lines | 42 code | 2 blank | 2 comment | 9 complexity | 48c7f199d02e443d713f4ce1578e84c9 MD5 | raw file
 1def logtimes = []
 2def times = []
 3def movingAverages = []
 4
 5//Ignores type for now: TODO
 6movingAverage = {ln, window , type, membershipFunction ->
 7	        if (!membershipFunction(ln)) return
 8		def matcher = ln =~ /[\[]([^\]]*)[\]]/
 9		if(matcher)
10		{
11			sdate = matcher[0][1]
12			formatter = new java.text.SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss z"); 
13			def date = (Date)formatter.parse(sdate) 
14	                       logtimes << date
15		}
16	    }
17//Ignore type for now
18movingAverageResult = { type, window ->
19        if(logtimes)
20        {
21	        Collections.sort(logtimes)
22	
23	for(date in logtimes){
24		if(times.size() == 0)
25		{
26			times << date.getTime()
27		}else
28		{
29			newTime = date.getTime()
30			while(times.size() > 0 && newTime-times[0] > window)
31			{
32				times.remove(0);
33			}
34			times<<newTime
35			if(times.size() > 1)
36			{
37				def deltaTime = newTime - times[0]
38				if(deltaTime)
39					movingAverages << times.size()*1000/deltaTime
40			}
41		}
42	}
43        logtimes = false
44        }
45        movingAverages
46}