source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/web/WeblogParser.java @ 74

Last change on this file since 74 was 74, checked in by sherbold, 13 years ago
  • refactoring
  • Property svn:mime-type set to text/plain
File size: 4.1 KB
Line 
1package de.ugoe.cs.eventbench.web;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileReader;
6import java.io.IOException;
7import java.text.ParseException;
8import java.text.SimpleDateFormat;
9import java.util.ArrayList;
10import java.util.HashMap;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Map;
14
15import de.ugoe.cs.eventbench.web.data.WebEvent;
16import de.ugoe.cs.util.FileTools;
17import de.ugoe.cs.util.console.Console;
18
19public class WeblogParser {
20       
21        private long timeout;
22       
23        private int minLength = 2;
24       
25        private List<List<WebEvent>> sequences;
26       
27        private static final String ROBOTFILTERFILE = "misc/robotfilter.txt";
28       
29        private String robotRegex = ".*";
30       
31        public WeblogParser() {
32                timeout = 3600000; // 1 hour session-timeout as default
33        }
34       
35        public WeblogParser(long timeout) {
36                this.timeout = timeout;
37        }
38       
39        public List<List<WebEvent>> getSequences() {
40                return sequences;
41        }
42       
43        public void setTimeout(long timeout) {
44                this.timeout = timeout;
45        }
46       
47        public void setMinLength(int minLength) {
48                this.minLength = minLength;
49        }
50       
51        public void parseFile(String filename) throws IOException, FileNotFoundException, ParseException {
52                String[] lines = FileTools.getLinesFromFile(filename);
53               
54                Map<String, List<Integer>> cookieSessionMap = new HashMap<String, List<Integer>>();
55                int lastId = -1;
56               
57                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
58                loadRobotRegex();
59               
60                sequences = new ArrayList<List<WebEvent>>();
61               
62                for( String line : lines ) {
63                        String[] values = line.substring(1, line.length()-1).split("\" \"");
64                       
65                        // use cookie as session identifier
66                        int cookieStart = values[0].lastIndexOf('.');
67                        String cookie = values[0].substring(cookieStart+1);
68                        String dateString = values[1];
69                        long timestamp = dateFormat.parse(dateString).getTime();
70                        String uri = values[2];
71                        // String ref = values[3]; // referer is not yet used!
72                        String agent = values[4]; // agent is not yet used!
73                        List<String> postedVars = new ArrayList<String>();
74                        if( values.length==6 ) { // post vars found
75                                for( String postVar : values[5].split(" ") ) {
76                                        postedVars.add(postVar);
77                                }
78                        }
79                        if( !isRobot(agent) ) {
80                                WebEvent event = new WebEvent(uri, timestamp, postedVars);
81                               
82                                // find session and add event
83                                List<Integer> sessionIds = cookieSessionMap.get(cookie);
84                                if( sessionIds==null ) {
85                                        sessionIds = new ArrayList<Integer>();
86                                        // start new session
87                                        sessionIds.add(++lastId);
88                                        cookieSessionMap.put(cookie, sessionIds);
89                                        sequences.add(new LinkedList<WebEvent>());
90                                }
91                                Integer lastSessionIndex = sessionIds.get(sessionIds.size()-1);
92                                List<WebEvent> lastSession = sequences.get(lastSessionIndex);
93                                long lastEventTime = timestamp;
94                                if( !lastSession.isEmpty() ) {
95                                        lastEventTime = lastSession.get(lastSession.size()-1).getTimestamp();
96                                }
97                                if( timestamp-lastEventTime>timeout ) {
98                                        sessionIds.add(++lastId);
99                                        List<WebEvent> newSession = new LinkedList<WebEvent>();
100                                        newSession.add(event);
101                                        sequences.add(newSession);
102                                } else {
103                                        lastSession.add(event);
104                                }
105                        }
106                }
107                pruneShortSequences();
108        }
109
110        private void pruneShortSequences() {
111                Console.traceln(""+sequences.size()+ " user sequences found");
112                // prune sequences shorter than min-length
113                int i=0;
114                while( i<sequences.size() ) {
115                        if( sequences.get(i).size()<minLength ) {
116                                sequences.remove(i);
117                        } else {
118                                i++;
119                        }
120                }
121                Console.traceln(""+sequences.size()+ " remaining after pruning of sequences shorter than " + minLength);
122        }
123       
124        private void loadRobotRegex() throws IOException, FileNotFoundException {
125                String[] lines = FileTools.getLinesFromFile(ROBOTFILTERFILE);
126                StringBuilder regex = new StringBuilder();
127                for( int i=0; i<lines.length; i++ ) {
128                        regex.append("(.*"+lines[i]+".*)");
129                        if( i!=lines.length-1 ) {
130                                regex.append("|");
131                        }
132                }
133                robotRegex = regex.toString();
134        }
135       
136        private boolean isRobot(String agent) {
137                return agent.matches(robotRegex);
138        }
139}
Note: See TracBrowser for help on using the repository browser.