| 2528 |
vikas |
1 |
package in.shop2020.utils;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.datalogger.EventType;
|
| 2777 |
varun.gupt |
4 |
import in.shop2020.datalogger.SocialEventType;
|
| 2528 |
vikas |
5 |
import in.shop2020.datalogger.event.Event;
|
| 2777 |
varun.gupt |
6 |
import in.shop2020.datalogger.socialevent.SocialEvent;
|
| 2528 |
vikas |
7 |
|
|
|
8 |
import java.io.BufferedReader;
|
| 4963 |
varun.gupt |
9 |
import java.io.IOException;
|
| 2528 |
vikas |
10 |
import java.io.InputStreamReader;
|
|
|
11 |
import java.io.OutputStreamWriter;
|
| 4963 |
varun.gupt |
12 |
import java.io.UnsupportedEncodingException;
|
|
|
13 |
import java.net.MalformedURLException;
|
| 2528 |
vikas |
14 |
import java.net.URL;
|
|
|
15 |
import java.net.URLConnection;
|
|
|
16 |
import java.net.URLEncoder;
|
|
|
17 |
import java.util.Date;
|
|
|
18 |
import java.util.ResourceBundle;
|
|
|
19 |
import java.util.concurrent.ExecutorService;
|
|
|
20 |
import java.util.concurrent.Executors;
|
|
|
21 |
|
|
|
22 |
import org.apache.commons.lang.StringUtils;
|
|
|
23 |
import org.apache.log4j.Logger;
|
|
|
24 |
import org.json.JSONObject;
|
|
|
25 |
|
|
|
26 |
public class DataLogger {
|
| 3073 |
vikas |
27 |
/**
|
|
|
28 |
* If it fails to log the data to google app engine, we log the event locally to data.log using log4j.
|
|
|
29 |
*/
|
| 2528 |
vikas |
30 |
private static Logger log = Logger.getLogger(DataLogger.class);
|
| 3073 |
vikas |
31 |
|
|
|
32 |
/**
|
|
|
33 |
* RootLogger to be used internal logging of this class.
|
|
|
34 |
*/
|
| 2965 |
chandransh |
35 |
private static Logger rootLogger = Logger.getRootLogger();
|
| 3073 |
vikas |
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Logger to post data to google app engine.
|
|
|
39 |
*/
|
| 2528 |
vikas |
40 |
private static ExecutorService asyncDataLogger = Executors.newSingleThreadExecutor();
|
| 2777 |
varun.gupt |
41 |
private static ExecutorService asyncSocialDataLogger = Executors.newSingleThreadExecutor();
|
| 2528 |
vikas |
42 |
private static ResourceBundle properties = ResourceBundle.getBundle(DataLogger.class.getName());
|
|
|
43 |
private static String googleAppUrl = properties.getString("googleappurl");
|
| 2777 |
varun.gupt |
44 |
|
| 2528 |
vikas |
45 |
public static void logData(final EventType eType, final String sessionId, final long userId, final String email, final String... logData) {
|
|
|
46 |
final String strToWrite = StringUtils.join(logData, ", ");
|
|
|
47 |
final Long time = (new Date()).getTime();
|
|
|
48 |
asyncDataLogger.execute(new Runnable() {
|
|
|
49 |
@Override
|
|
|
50 |
public void run() {
|
|
|
51 |
Event event = Event.createEvent(eType, sessionId, userId, email, logData);
|
|
|
52 |
JSONObject logDataInJson = event.getLogDataInJson();
|
|
|
53 |
try {
|
|
|
54 |
// Construct data
|
|
|
55 |
String data = URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(time.toString(), "UTF-8");
|
|
|
56 |
data += "&" + URLEncoder.encode("eventType", "UTF-8") + "=" + URLEncoder.encode(eType.name(), "UTF-8");
|
|
|
57 |
data += "&" + URLEncoder.encode("sessionId", "UTF-8") + "=" + URLEncoder.encode(sessionId, "UTF-8");
|
|
|
58 |
data += "&" + URLEncoder.encode("userId", "UTF-8") + "=" + URLEncoder.encode(Long.toString(userId), "UTF-8");
|
|
|
59 |
data += "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8");
|
|
|
60 |
if (logDataInJson != null) {
|
|
|
61 |
data += "&" + URLEncoder.encode("jsonLogData", "UTF-8")
|
|
|
62 |
+ "=" + URLEncoder.encode(logDataInJson.toString(), "UTF-8");
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// Send data
|
|
|
66 |
URL url = new URL("http://" + googleAppUrl + "/add-data-log");
|
|
|
67 |
URLConnection conn = url.openConnection();
|
|
|
68 |
conn.setDoOutput(true);
|
|
|
69 |
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
|
|
|
70 |
wr.write(data);
|
|
|
71 |
wr.close();
|
|
|
72 |
|
| 3073 |
vikas |
73 |
conn.getInputStream().close();
|
| 4963 |
varun.gupt |
74 |
|
|
|
75 |
} catch (UnsupportedEncodingException e) {
|
|
|
76 |
rootLogger.error("Unsupported Encoding Exception: " + eType.name() + " " + sessionId + " " + userId + " " + email);
|
|
|
77 |
log.error(eType.name() + ", " + sessionId + ", " + Long.toString(userId) + ", " + email + ", " + strToWrite);
|
|
|
78 |
|
|
|
79 |
} catch (MalformedURLException e) {
|
|
|
80 |
rootLogger.error("Malformed URL Exception: " + eType.name() + " " + sessionId + " " + userId + " " + email);
|
|
|
81 |
log.error(eType.name() + ", " + sessionId + ", " + Long.toString(userId) + ", " + email + ", " + strToWrite);
|
|
|
82 |
|
|
|
83 |
} catch (IOException e) {
|
|
|
84 |
rootLogger.error("IO Exception: " + eType.name() + " " + sessionId + " " + userId + " " + email);
|
|
|
85 |
log.error(eType.name() + ", " + sessionId + ", " + Long.toString(userId) + ", " + email + ", " + strToWrite);
|
|
|
86 |
}
|
| 2777 |
varun.gupt |
87 |
}
|
|
|
88 |
});
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public static void logSocialData(final SocialEventType eType, final String userId, final String... logData) {
|
|
|
92 |
final String strToWrite = StringUtils.join(logData, ", ");
|
|
|
93 |
final Long time = (new Date()).getTime();
|
|
|
94 |
asyncSocialDataLogger.execute(new Runnable() {
|
|
|
95 |
@Override
|
|
|
96 |
public void run() {
|
|
|
97 |
log.info(eType.name() + ", " + userId + ", " + strToWrite);
|
|
|
98 |
SocialEvent event = SocialEvent.createEvent(eType, userId, logData);
|
|
|
99 |
JSONObject logDataInJson = event.getLogDataInJson();
|
|
|
100 |
|
|
|
101 |
try {
|
|
|
102 |
String data = URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(time.toString(), "UTF-8");
|
|
|
103 |
data += "&" + URLEncoder.encode("eventType", "UTF-8") + "=" + URLEncoder.encode(eType.name(), "UTF-8");
|
|
|
104 |
data += "&" + URLEncoder.encode("userId", "UTF-8") + "=" + URLEncoder.encode(userId, "UTF-8");
|
|
|
105 |
|
|
|
106 |
if (logDataInJson != null) {
|
|
|
107 |
data += "&" + URLEncoder.encode("jsonLogData", "UTF-8") + "=" + URLEncoder.encode(logDataInJson.toString(), "UTF-8");
|
|
|
108 |
}
|
| 2528 |
vikas |
109 |
|
| 2777 |
varun.gupt |
110 |
// Send data
|
|
|
111 |
URL url = new URL("http://" + googleAppUrl + "/add-social-data-log");
|
|
|
112 |
URLConnection conn = url.openConnection();
|
|
|
113 |
conn.setDoOutput(true);
|
|
|
114 |
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
|
|
|
115 |
wr.write(data);
|
|
|
116 |
wr.close();
|
|
|
117 |
|
|
|
118 |
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
119 |
while (in.readLine() != null);
|
|
|
120 |
in.close();
|
|
|
121 |
|
|
|
122 |
} catch (Exception e) {
|
|
|
123 |
Logger.getRootLogger().warn(e);
|
|
|
124 |
}
|
| 2528 |
vikas |
125 |
}
|
|
|
126 |
});
|
|
|
127 |
}
|
| 3073 |
vikas |
128 |
|
|
|
129 |
public static void main(String[] args) {
|
|
|
130 |
DataLogger.logData(EventType.NEW_SESSION, "FDTWSYTW", 12, "vmalik@gmail.com", "Test");
|
|
|
131 |
}
|
| 2777 |
varun.gupt |
132 |
}
|