source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/web/commands/CMDloadSessionsFromClickstream.java @ 53

Last change on this file since 53 was 53, checked in by sherbold, 13 years ago
  • experimental parsing of web usage logs
  • Property svn:mime-type set to text/plain
File size: 3.1 KB
Line 
1package de.ugoe.cs.eventbench.web.commands;
2
3import java.io.File;
4import java.io.FileReader;
5import java.security.InvalidParameterException;
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.console.Command;
15import de.ugoe.cs.util.console.Console;
16
17public class CMDloadSessionsFromClickstream implements Command {
18
19        @Override
20        public void run(List<Object> parameters) {
21                // TODO Auto-generated method stub
22                if( parameters.size() < 1 ) {
23                        throw new InvalidParameterException();
24                }
25                String source = (String) parameters.get(0);
26               
27                // TODO needs to be variable
28                long timeout = 3600000; // 1 hour session-timeout
29               
30                try { // TODO dummy try/catch block
31                        File f = new File(source);
32                        FileReader reader = new FileReader(f);
33                        char[] buffer = new char[(int) f.length()];
34                        reader.read(buffer);
35                        reader.close();
36                        String[] lines = (new String(buffer)).split("\n");
37                       
38                        Map<String, List<Integer>> cookieSessionMap = new HashMap<String, List<Integer>>();
39                        int lastId = -1;
40                       
41                        List<List<WebEvent>> sessions = new ArrayList<List<WebEvent>>();
42                       
43                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
44                       
45                        for( String line : lines ) {
46                                String[] values = line.trim().split(" ");
47                               
48                                // use cookie as session identifier
49                                int cookieStart = values[0].lastIndexOf('.');
50                                String cookie = values[0].substring(cookieStart+1);
51                                String dateString = values[1].substring(1)+" "+values[2].substring(0, values[2].length()-1);
52                                long timestamp = dateFormat.parse(dateString).getTime();
53                                String uri = values[3];
54                                String ref = values[4];
55                                List<String> postedVars = new ArrayList<String>();
56                                for( int i=5 ; i<values.length ; i++ ) {
57                                        postedVars.add(values[i]);
58                                }
59                               
60                                       
61                                WebEvent event = new WebEvent(uri, timestamp, postedVars);
62                               
63                               
64                                // find session and add event
65                                List<Integer> sessionIds = cookieSessionMap.get(cookie);
66                                if( sessionIds==null ) {
67                                        sessionIds = new ArrayList<Integer>();
68                                        // start new session
69                                        sessionIds.add(++lastId);
70                                        cookieSessionMap.put(cookie, sessionIds);
71                                        sessions.add(new LinkedList<WebEvent>());
72                                }
73                                Integer lastSessionIndex = sessionIds.get(sessionIds.size()-1);
74                                List<WebEvent> lastSession = sessions.get(lastSessionIndex);
75                                long lastEventTime = timestamp;
76                                if( !lastSession.isEmpty() ) {
77                                        lastEventTime = lastSession.get(lastSession.size()-1).getTimestamp();
78                                }
79                                if( timestamp-lastEventTime>timeout ) {
80                                        sessionIds.add(++lastId);
81                                        List<WebEvent> newSession = new LinkedList<WebEvent>();
82                                        newSession.add(event);
83                                        sessions.add(newSession);
84                                } else {
85                                        lastSession.add(event);
86                                }
87                        }
88                        Console.traceln(sessions.toString());
89                } catch(Exception e) {
90                        e.printStackTrace();
91                }
92               
93        }
94       
95        @Override
96        public void help() {
97                // TODO Auto-generated method stub
98               
99        }
100
101}
Note: See TracBrowser for help on using the repository browser.