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

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