1 | package de.ugoe.cs.eventbench.web;
|
---|
2 |
|
---|
3 | import java.io.FileNotFoundException;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.net.URI;
|
---|
6 | import java.net.URISyntaxException;
|
---|
7 | import java.text.ParseException;
|
---|
8 | import java.text.SimpleDateFormat;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 |
|
---|
15 | import de.ugoe.cs.eventbench.web.data.WebEvent;
|
---|
16 | import de.ugoe.cs.util.FileTools;
|
---|
17 | import de.ugoe.cs.util.console.Console;
|
---|
18 |
|
---|
19 | public 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, URISyntaxException {
|
---|
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 uriString = values[2];
|
---|
71 | // String ref = values[3]; // referer is not yet used!
|
---|
72 | String agent;
|
---|
73 | if( values.length>4 ) {
|
---|
74 | agent = values[4];
|
---|
75 | } else {
|
---|
76 | agent = "noagent";
|
---|
77 | }
|
---|
78 |
|
---|
79 | List<String> postedVars = new ArrayList<String>();
|
---|
80 | if( values.length==6 ) { // post vars found
|
---|
81 | for( String postVar : values[5].trim().split(" ") ) {
|
---|
82 | postedVars.add(postVar);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | if( !isRobot(agent) ) {
|
---|
86 | URI uri = new URI(uriString);
|
---|
87 |
|
---|
88 | String path = uri.getPath();
|
---|
89 | List<String> getVars = extractGetVarsFromUri(uri);
|
---|
90 |
|
---|
91 | WebEvent event = new WebEvent(path, timestamp, postedVars, getVars);
|
---|
92 |
|
---|
93 | // find session and add event
|
---|
94 | List<Integer> sessionIds = cookieSessionMap.get(cookie);
|
---|
95 | if( sessionIds==null ) {
|
---|
96 | sessionIds = new ArrayList<Integer>();
|
---|
97 | // start new session
|
---|
98 | sessionIds.add(++lastId);
|
---|
99 | cookieSessionMap.put(cookie, sessionIds);
|
---|
100 | sequences.add(new LinkedList<WebEvent>());
|
---|
101 | }
|
---|
102 | Integer lastSessionIndex = sessionIds.get(sessionIds.size()-1);
|
---|
103 | List<WebEvent> lastSession = sequences.get(lastSessionIndex);
|
---|
104 | long lastEventTime = timestamp;
|
---|
105 | if( !lastSession.isEmpty() ) {
|
---|
106 | lastEventTime = lastSession.get(lastSession.size()-1).getTimestamp();
|
---|
107 | }
|
---|
108 | if( timestamp-lastEventTime>timeout ) {
|
---|
109 | sessionIds.add(++lastId);
|
---|
110 | List<WebEvent> newSession = new LinkedList<WebEvent>();
|
---|
111 | newSession.add(event);
|
---|
112 | sequences.add(newSession);
|
---|
113 | } else {
|
---|
114 | lastSession.add(event);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | pruneShortSequences();
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void pruneShortSequences() {
|
---|
122 | Console.traceln(""+sequences.size()+ " user sequences found");
|
---|
123 | // prune sequences shorter than min-length
|
---|
124 | int i=0;
|
---|
125 | while( i<sequences.size() ) {
|
---|
126 | if( sequences.get(i).size()<minLength ) {
|
---|
127 | sequences.remove(i);
|
---|
128 | } else {
|
---|
129 | i++;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | Console.traceln(""+sequences.size()+ " remaining after pruning of sequences shorter than " + minLength);
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void loadRobotRegex() throws IOException, FileNotFoundException {
|
---|
136 | String[] lines = FileTools.getLinesFromFile(ROBOTFILTERFILE);
|
---|
137 | StringBuilder regex = new StringBuilder();
|
---|
138 | for( int i=0; i<lines.length; i++ ) {
|
---|
139 | regex.append("(.*"+lines[i]+".*)");
|
---|
140 | if( i!=lines.length-1 ) {
|
---|
141 | regex.append("|");
|
---|
142 | }
|
---|
143 | }
|
---|
144 | robotRegex = regex.toString();
|
---|
145 | }
|
---|
146 |
|
---|
147 | private boolean isRobot(String agent) {
|
---|
148 | return agent.matches(robotRegex);
|
---|
149 | }
|
---|
150 |
|
---|
151 | private List<String> extractGetVarsFromUri(URI uri) {
|
---|
152 | List<String> getVars = new ArrayList<String>();
|
---|
153 | String query = uri.getQuery();
|
---|
154 | if( query!=null ) {
|
---|
155 | String[] paramPairs = query.split("&");
|
---|
156 | for( String paramPair : paramPairs ) {
|
---|
157 | String[] paramSplit = paramPair.split("=");
|
---|
158 | getVars.add(paramSplit[0]);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return getVars;
|
---|
162 | }
|
---|
163 | }
|
---|