Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package export;
- import com.google.gson.Gson;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- *
- * @author cheok
- */
- public class Export {
- private static class Duration {
- public final String start;
- public final String end;
- public Date date;
- Duration(String start, String end, Date date) {
- this.start = start;
- this.end = end;
- this.date = date;
- }
- }
- /**
- * <p>Checks if the first date is before the second date ignoring time.</p>
- * @param date1 the first date, not altered, not null
- * @param date2 the second date, not altered, not null
- * @return true if the first date day is before the second date day.
- * @throws IllegalArgumentException if the date is <code>null</code>
- */
- public static boolean isBeforeDay(Date date1, Date date2) {
- if (date1 == null || date2 == null) {
- throw new IllegalArgumentException("The dates must not be null");
- }
- Calendar cal1 = Calendar.getInstance();
- cal1.setTime(date1);
- Calendar cal2 = Calendar.getInstance();
- cal2.setTime(date2);
- return isBeforeDay(cal1, cal2);
- }
- /**
- * <p>Checks if the first calendar date is before the second calendar date ignoring time.</p>
- * @param cal1 the first calendar, not altered, not null.
- * @param cal2 the second calendar, not altered, not null.
- * @return true if cal1 date is before cal2 date ignoring time.
- * @throws IllegalArgumentException if either of the calendars are <code>null</code>
- */
- public static boolean isBeforeDay(Calendar cal1, Calendar cal2) {
- if (cal1 == null || cal2 == null) {
- throw new IllegalArgumentException("The dates must not be null");
- }
- if (cal1.get(Calendar.ERA) < cal2.get(Calendar.ERA)) return true;
- if (cal1.get(Calendar.ERA) > cal2.get(Calendar.ERA)) return false;
- if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) return true;
- if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR)) return false;
- return cal1.get(Calendar.DAY_OF_YEAR) < cal2.get(Calendar.DAY_OF_YEAR);
- }
- public static HashMap<String, Object> resultSetToHashMap(ResultSet rs) throws SQLException {
- ResultSetMetaData md = rs.getMetaData();
- int columns = md.getColumnCount();
- HashMap<String, Object> row = new HashMap<>();
- for (int i = 1; i <= columns; i++) {
- row.put(md.getColumnName(i), rs.getString(i));
- }
- return row;
- }
- private static List<Duration> split(Date date, int h) {
- List<Duration> durations = new ArrayList<>();
- Date currDate = date;
- DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- Date[] dates = new Date[] {
- currDate,
- };
- for (Date d : dates) {
- String s = formatter.format(d);
- Duration duration0 = new Duration(s + " " + String.format("%02d", h) + ":00:00", s + " " + String.format("%02d", h) + ":59:59", d);
- durations.add(duration0);
- }
- return durations;
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws Exception {
- if (args.length < 2) {
- System.out.println("Usage: java -jar Export.jar yyyy-mm-dd hh");
- System.exit(0);
- }
- final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- final Date date = formatter.parse(args[0]);
- final int h = Integer.parseInt(args[1]);
- if (h < 0 || h > 23) {
- System.out.println("Hour must be 0 to 23");
- System.exit(0);
- }
- if (!isBeforeDay(date, new Date())) {
- System.out.println("Date must be older than today");
- System.exit(0);
- }
- System.out.println("...Going to generate hourly report with date \"" + date + "\". Hour is " + hour);
- final String servername = "rockman-api.sam-media.com";
- final String port = "3326";
- final String username = "rockman";
- final String password = "JjA1FAjyzhcInWti";
- final String db = "rockman";
- // This will load the MySQL driver, each DB has its own driver
- Class.forName("com.mysql.jdbc.Driver");
- // Setup the connection with the DB
- String url = "jdbc:mysql://" + servername + ":" + port + "/" + db;
- Connection connect = DriverManager.getConnection(url, username, password);
- final List<Duration> durations = split(date, h);
- System.out.println("Build output.json...");
- Gson gson = new Gson();
- BufferedWriter writer = null;
- File logFile = new File("output.json");
- writer = new BufferedWriter(new FileWriter(logFile));
- boolean firstTime = true;
- int counter = 0;
- for (Duration duration : durations) {
- String sql = "select *, DATE_FORMAT(timestamp, '%Y-%m-%dT%TZ') as time from rockman.matview_all_events where timestamp between ? and ? order by timestamp asc";
- PreparedStatement statement = connect.prepareStatement(sql);
- statement.setString(1, duration.start);
- statement.setString(2, duration.end);
- System.out.println("Going to run query (" + duration.start + ", " + duration.end + ")");
- long start = System.currentTimeMillis();
- ResultSet rs = statement.executeQuery();
- System.out.println("Time taken to run query (" + duration.start + ", " + duration.end + "): " + (System.currentTimeMillis() - start) + "ms");
- while (rs.next()){
- String event_type = rs.getString("event_type");
- if (event_type == null || event_type.isEmpty()) {
- // Currently, we have no idea how to handle empty rockman id.
- continue;
- }
- Map<String, Object> result = resultSetToHashMap(rs);
- if (event_type.equals("view")) {
- result.put("view", 1);
- } else {
- result.put("view", 0);
- }
- if (event_type.equals("lead")) {
- result.put("lead", 1);
- } else {
- result.put("lead", 0);
- }
- if (event_type.equals("subscription")) {
- result.put("subscription", 1);
- } else {
- result.put("subscription", 0);
- }
- if (event_type.equals("firstbilling")) {
- result.put("firstbilling", 1);
- } else {
- result.put("firstbilling", 0);
- }
- String s = gson.toJson(result);
- if (firstTime) {
- writer.write(s);
- firstTime = false;
- } else {
- writer.write("\n" + s);
- }
- counter++;
- }
- rs.close();
- } // for (Duration duration : durations)
- writer.close();
- System.out.println("Finish generating output.json with size " + counter);
- writer = new BufferedWriter(new FileWriter(new File("rockman-index.json")));
- FileReader fr = new FileReader("rockman-index.json.template");
- String s;
- BufferedReader br = new BufferedReader(fr);
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.DATE, 1);
- Date nextDate = cal.getTime();
- String replace = formatter.format(date) + "T" + String.format("%02d", h) + ":00:00" + "/" + formatter.format(date) + "T" + String.format("%02d", h) + ":59:59";
- // 2016-04-13T00:00:00/2016-04-13T00:59:59
- while ((s = br.readLine()) != null) {
- s = s.replaceAll("%DATE_RANGE%", replace);
- writer.write(s + "\n");
- }
- br.close();
- writer.close();
- System.out.println("Finish generating rockman-index.json");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement