Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.utils;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.EventObject;
import java.util.ResourceBundle;

import in.shop2020.datalogger.EventType;
import in.shop2020.datalogger.event.Event;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONObject;

public class GAEFailedEventUploader {

        private static String filePath;
        private static File file = null;
        private static RandomAccessFile reader = null;
        private static ResourceBundle properties = ResourceBundle.getBundle(DataLogger.class.getName());
    private static String googleAppUrl = properties.getString("googleappurl");
        
        /**
         * @param args
         */
        public static void main(String[] args) {
                if(args.length!=1) {
                        System.out.println("Use only one argument containing log-file path");
                        System.exit(0);
                }
                Logger rootLogger = Logger.getRootLogger();
                long uploadedEventCount = 0;
                long totalEventCount = 0;
                try {
                        filePath = args[0];
                        file = new File(filePath);
                        reader = new RandomAccessFile(file,"r");
                        reader.seek(0);
                
                        String line = null;
                        while (!((line = reader.readLine()) == null)) {
                                totalEventCount++;
                                try {
                                        String[] eventTokens = line.split(", ");
                                        String[] logData = new String[eventTokens.length-5];
                                        Long time = new SimpleDateFormat("yyyy-MM-dd/hh:mm:ss.SSS").parse(eventTokens[0].substring(0,eventTokens[0].length()-4)).getTime();
                                        for(int tokenIndex = 5,logDataIndex=0;tokenIndex<eventTokens.length;tokenIndex++,logDataIndex++) {
                                                logData[logDataIndex] = eventTokens[tokenIndex];
                                        }
                                        EventType eType = EventType.valueOf(eventTokens[1]);
                            Event event = Event.createEvent(eType, eventTokens[2], Long.parseLong(eventTokens[3]), eventTokens[4], logData);
                            JSONObject logDataInJson = event.getLogDataInJson();
                            
                        String data = URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(time.toString(), "UTF-8");
                        data += "&" + URLEncoder.encode("eventType", "UTF-8") + "=" + URLEncoder.encode(eventTokens[1], "UTF-8");
                        data += "&" + URLEncoder.encode("sessionId", "UTF-8") + "=" + URLEncoder.encode(eventTokens[2], "UTF-8");
                        data += "&" + URLEncoder.encode("userId", "UTF-8") + "=" + URLEncoder.encode(eventTokens[3], "UTF-8");
                        data += "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(eventTokens[4], "UTF-8");
                        
                        if (logDataInJson != null) {
                            data += "&" + URLEncoder.encode("jsonLogData", "UTF-8")
                                    + "=" + URLEncoder.encode(logDataInJson.toString(), "UTF-8");
                        }
                        
                        // Send data
                        URL url = new URL("http://" + googleAppUrl + "/add-data-log");
                        URLConnection conn = url.openConnection();
                        conn.setDoOutput(true);
                        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                        wr.write(data);
                        wr.close();
                        
                        conn.getInputStream().close();
                        uploadedEventCount++;
                                } catch(Exception e) {
                                        rootLogger.error(e);
                                        continue;
                                }
                        }
                } catch(IOException ioEx) {
                        System.exit(0);
                }
                System.out.println(uploadedEventCount + " Events uploaded out of " + totalEventCount);
        }

}