Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5687 amar.kumar 1
package in.shop2020.utils;
2
 
3
import java.io.File;
4
import java.io.IOException;
5
import java.io.OutputStreamWriter;
6
import java.io.RandomAccessFile;
7
import java.io.UnsupportedEncodingException;
8
import java.net.MalformedURLException;
9
import java.net.URL;
10
import java.net.URLConnection;
11
import java.net.URLEncoder;
12
import java.text.SimpleDateFormat;
13
import java.util.EventObject;
14
import java.util.ResourceBundle;
15
 
16
import in.shop2020.datalogger.EventType;
17
import in.shop2020.datalogger.event.Event;
18
 
19
import org.apache.commons.lang.StringUtils;
20
import org.apache.log4j.Logger;
21
import org.json.JSONObject;
22
 
23
public class GAEFailedEventUploader {
24
 
25
	private static String filePath;
26
	private static File file = null;
27
	private static RandomAccessFile reader = null;
28
	private static ResourceBundle properties = ResourceBundle.getBundle(DataLogger.class.getName());
29
    private static String googleAppUrl = properties.getString("googleappurl");
30
 
31
	/**
32
	 * @param args
33
	 */
34
	public static void main(String[] args) {
35
		if(args.length!=1) {
36
			System.out.println("Use only one argument containing log-file path");
37
			System.exit(0);
38
		}
39
		Logger rootLogger = Logger.getRootLogger();
40
		long uploadedEventCount = 0;
41
		long totalEventCount = 0;
42
		try {
43
			filePath = args[0];
44
			file = new File(filePath);
45
			reader = new RandomAccessFile(file,"r");
46
			reader.seek(0);
47
 
48
			String line = null;
49
			while (!((line = reader.readLine()) == null)) {
50
				totalEventCount++;
51
				try {
52
					String[] eventTokens = line.split(", ");
53
					String[] logData = new String[eventTokens.length-5];
54
					Long time = new SimpleDateFormat("yyyy-MM-dd/hh:mm:ss.SSS").parse(eventTokens[0].substring(0,eventTokens[0].length()-4)).getTime();
55
					for(int tokenIndex = 5,logDataIndex=0;tokenIndex<eventTokens.length;tokenIndex++,logDataIndex++) {
56
						logData[logDataIndex] = eventTokens[tokenIndex];
57
					}
58
					EventType eType = EventType.valueOf(eventTokens[1]);
59
		            Event event = Event.createEvent(eType, eventTokens[2], Long.parseLong(eventTokens[3]), eventTokens[4], logData);
60
		            JSONObject logDataInJson = event.getLogDataInJson();
61
 
62
	                String data = URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(time.toString(), "UTF-8");
63
	                data += "&" + URLEncoder.encode("eventType", "UTF-8") + "=" + URLEncoder.encode(eventTokens[1], "UTF-8");
64
	                data += "&" + URLEncoder.encode("sessionId", "UTF-8") + "=" + URLEncoder.encode(eventTokens[2], "UTF-8");
65
	                data += "&" + URLEncoder.encode("userId", "UTF-8") + "=" + URLEncoder.encode(eventTokens[3], "UTF-8");
66
	                data += "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(eventTokens[4], "UTF-8");
67
 
68
	                if (logDataInJson != null) {
69
	                    data += "&" + URLEncoder.encode("jsonLogData", "UTF-8")
70
	                            + "=" + URLEncoder.encode(logDataInJson.toString(), "UTF-8");
71
	                }
72
 
73
	                // Send data
74
	                URL url = new URL("http://" + googleAppUrl + "/add-data-log");
75
	                URLConnection conn = url.openConnection();
76
	                conn.setDoOutput(true);
77
	                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
78
	                wr.write(data);
79
	                wr.close();
80
 
81
	                conn.getInputStream().close();
82
	                uploadedEventCount++;
83
				} catch(Exception e) {
84
					rootLogger.error(e);
85
					continue;
86
				}
87
			}
88
		} catch(IOException ioEx) {
89
			System.exit(0);
90
		}
91
		System.out.println(uploadedEventCount + " Events uploaded out of " + totalEventCount);
92
	}
93
 
94
}