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

Last change on this file since 72 was 72, checked in by sherbold, 13 years ago
  • changed de.ugoe.cs.eventbench.web.WeblogParser? to new log format that includes user agents

+ added agent-based robot filter to de.ugoe.cs.eventbench.web.WeblogParser?

  • Property svn:mime-type set to text/plain
File size: 4.4 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.console.Console;
17
18public class WeblogParser {
19       
20        private long timeout;
21       
22        private int minLength = 2;
23       
24        private List<List<WebEvent>> sequences;
25       
26        private static final String ROBOTFILTERFILE = "misc/robotfilter.txt";
27       
28        private String robotRegex = ".*";
29       
30        public WeblogParser() {
31                timeout = 3600000; // 1 hour session-timeout as default
32        }
33       
34        public WeblogParser(long timeout) {
35                this.timeout = timeout;
36        }
37       
38        public List<List<WebEvent>> getSequences() {
39                return sequences;
40        }
41       
42        public void setTimeout(long timeout) {
43                this.timeout = timeout;
44        }
45       
46        public void setMinLength(int minLength) {
47                this.minLength = minLength;
48        }
49       
50        public void parseFile(String filename) throws IOException, FileNotFoundException, ParseException {
51                File f = new File(filename);
52                FileReader reader = new FileReader(f);
53                char[] buffer = new char[(int) f.length()];
54                reader.read(buffer);
55                reader.close();
56                String[] lines = (new String(buffer)).split("\r\n");
57               
58                Map<String, List<Integer>> cookieSessionMap = new HashMap<String, List<Integer>>();
59                int lastId = -1;
60               
61                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
62                loadRobotRegex();
63               
64                sequences = new ArrayList<List<WebEvent>>();
65               
66                for( String line : lines ) {
67                        String[] values = line.substring(1, line.length()-1).split("\" \"");
68                       
69                        // use cookie as session identifier
70                        int cookieStart = values[0].lastIndexOf('.');
71                        String cookie = values[0].substring(cookieStart+1);
72                        String dateString = values[1];
73                        long timestamp = dateFormat.parse(dateString).getTime();
74                        String uri = values[2];
75                        // String ref = values[3]; // referer is not yet used!
76                        String agent = values[4]; // agent is not yet used!
77                        List<String> postedVars = new ArrayList<String>();
78                        if( values.length==6 ) { // post vars found
79                                for( String postVar : values[5].split(" ") ) {
80                                        postedVars.add(postVar);
81                                }
82                        }
83                        if( !isRobot(agent) ) {
84                                WebEvent event = new WebEvent(uri, timestamp, postedVars);
85                               
86                                // find session and add event
87                                List<Integer> sessionIds = cookieSessionMap.get(cookie);
88                                if( sessionIds==null ) {
89                                        sessionIds = new ArrayList<Integer>();
90                                        // start new session
91                                        sessionIds.add(++lastId);
92                                        cookieSessionMap.put(cookie, sessionIds);
93                                        sequences.add(new LinkedList<WebEvent>());
94                                }
95                                Integer lastSessionIndex = sessionIds.get(sessionIds.size()-1);
96                                List<WebEvent> lastSession = sequences.get(lastSessionIndex);
97                                long lastEventTime = timestamp;
98                                if( !lastSession.isEmpty() ) {
99                                        lastEventTime = lastSession.get(lastSession.size()-1).getTimestamp();
100                                }
101                                if( timestamp-lastEventTime>timeout ) {
102                                        sessionIds.add(++lastId);
103                                        List<WebEvent> newSession = new LinkedList<WebEvent>();
104                                        newSession.add(event);
105                                        sequences.add(newSession);
106                                } else {
107                                        lastSession.add(event);
108                                }
109                        }
110                }
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                                Console.traceln(""+sequences.get(i).size());
119                                if( sequences.get(i).size() > 152 ) {
120                                        Console.traceln(sequences.get(i).toString().replaceAll(", ", "\n"));
121                                }
122                                i++;
123                        }
124                }
125                Console.traceln(""+sequences.size()+ " remaining after pruning of sequences shorter than " + minLength);
126        }
127       
128        private void loadRobotRegex() throws IOException, FileNotFoundException {
129                File f = new File(ROBOTFILTERFILE);
130                FileReader reader = new FileReader(f);
131                char[] buffer = new char[(int) f.length()];
132                reader.read(buffer);
133                reader.close();
134                String[] lines = (new String(buffer)).split("\r\n");
135                StringBuilder regex = new StringBuilder();
136                for( int i=0; i<lines.length; i++ ) {
137                        regex.append("(.*"+lines[i]+".*)");
138                        if( i!=lines.length-1 ) {
139                                regex.append("|");
140                        }
141                }
142                robotRegex = regex.toString();
143        }
144       
145        private boolean isRobot(String agent) {
146                return agent.matches(robotRegex);
147        }
148}
Note: See TracBrowser for help on using the repository browser.