Advertisement
Guest User

Untitled

a guest
Apr 21st, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package export;
  7.  
  8. import com.google.gson.Gson;
  9. import java.io.BufferedReader;
  10. import java.io.BufferedWriter;
  11. import java.io.File;
  12. import java.io.FileReader;
  13. import java.io.FileWriter;
  14. import java.sql.Connection;
  15. import java.sql.DriverManager;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.ResultSetMetaData;
  19. import java.sql.SQLException;
  20. import java.text.DateFormat;
  21. import java.text.SimpleDateFormat;
  22. import java.util.ArrayList;
  23. import java.util.Calendar;
  24. import java.util.Date;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28.  
  29. /**
  30. *
  31. * @author cheok
  32. */
  33. public class Export {
  34.  
  35. private static class Duration {
  36. public final String start;
  37. public final String end;
  38. public Date date;
  39.  
  40. Duration(String start, String end, Date date) {
  41. this.start = start;
  42. this.end = end;
  43. this.date = date;
  44. }
  45. }
  46.  
  47. /**
  48. * <p>Checks if the first date is before the second date ignoring time.</p>
  49. * @param date1 the first date, not altered, not null
  50. * @param date2 the second date, not altered, not null
  51. * @return true if the first date day is before the second date day.
  52. * @throws IllegalArgumentException if the date is <code>null</code>
  53. */
  54. public static boolean isBeforeDay(Date date1, Date date2) {
  55. if (date1 == null || date2 == null) {
  56. throw new IllegalArgumentException("The dates must not be null");
  57. }
  58. Calendar cal1 = Calendar.getInstance();
  59. cal1.setTime(date1);
  60. Calendar cal2 = Calendar.getInstance();
  61. cal2.setTime(date2);
  62. return isBeforeDay(cal1, cal2);
  63. }
  64.  
  65. /**
  66. * <p>Checks if the first calendar date is before the second calendar date ignoring time.</p>
  67. * @param cal1 the first calendar, not altered, not null.
  68. * @param cal2 the second calendar, not altered, not null.
  69. * @return true if cal1 date is before cal2 date ignoring time.
  70. * @throws IllegalArgumentException if either of the calendars are <code>null</code>
  71. */
  72. public static boolean isBeforeDay(Calendar cal1, Calendar cal2) {
  73. if (cal1 == null || cal2 == null) {
  74. throw new IllegalArgumentException("The dates must not be null");
  75. }
  76. if (cal1.get(Calendar.ERA) < cal2.get(Calendar.ERA)) return true;
  77. if (cal1.get(Calendar.ERA) > cal2.get(Calendar.ERA)) return false;
  78. if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) return true;
  79. if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR)) return false;
  80. return cal1.get(Calendar.DAY_OF_YEAR) < cal2.get(Calendar.DAY_OF_YEAR);
  81. }
  82.  
  83. public static HashMap<String, Object> resultSetToHashMap(ResultSet rs) throws SQLException {
  84. ResultSetMetaData md = rs.getMetaData();
  85. int columns = md.getColumnCount();
  86. HashMap<String, Object> row = new HashMap<>();
  87. for (int i = 1; i <= columns; i++) {
  88. row.put(md.getColumnName(i), rs.getString(i));
  89. }
  90. return row;
  91. }
  92.  
  93. private static List<Duration> split(Date date, int h) {
  94. List<Duration> durations = new ArrayList<>();
  95.  
  96. Date currDate = date;
  97.  
  98. DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  99. Date[] dates = new Date[] {
  100. currDate,
  101. };
  102.  
  103. for (Date d : dates) {
  104. String s = formatter.format(d);
  105.  
  106. Duration duration0 = new Duration(s + " " + String.format("%02d", h) + ":00:00", s + " " + String.format("%02d", h) + ":59:59", d);
  107.  
  108. durations.add(duration0);
  109. }
  110.  
  111. return durations;
  112. }
  113.  
  114. /**
  115. * @param args the command line arguments
  116. */
  117. public static void main(String[] args) throws Exception {
  118. if (args.length < 2) {
  119. System.out.println("Usage: java -jar Export.jar yyyy-mm-dd hh");
  120. System.exit(0);
  121. }
  122.  
  123. final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  124. final Date date = formatter.parse(args[0]);
  125.  
  126. final int h = Integer.parseInt(args[1]);
  127.  
  128. if (h < 0 || h > 23) {
  129. System.out.println("Hour must be 0 to 23");
  130. System.exit(0);
  131. }
  132.  
  133. if (!isBeforeDay(date, new Date())) {
  134. System.out.println("Date must be older than today");
  135. System.exit(0);
  136. }
  137.  
  138. System.out.println("...Going to generate hourly report with date \"" + date + "\". Hour is " + hour);
  139.  
  140. final String servername = "rockman-api.sam-media.com";
  141. final String port = "3326";
  142. final String username = "rockman";
  143. final String password = "JjA1FAjyzhcInWti";
  144. final String db = "rockman";
  145.  
  146. // This will load the MySQL driver, each DB has its own driver
  147. Class.forName("com.mysql.jdbc.Driver");
  148. // Setup the connection with the DB
  149. String url = "jdbc:mysql://" + servername + ":" + port + "/" + db;
  150. Connection connect = DriverManager.getConnection(url, username, password);
  151.  
  152. final List<Duration> durations = split(date, h);
  153.  
  154. System.out.println("Build output.json...");
  155.  
  156. Gson gson = new Gson();
  157. BufferedWriter writer = null;
  158. File logFile = new File("output.json");
  159. writer = new BufferedWriter(new FileWriter(logFile));
  160. boolean firstTime = true;
  161.  
  162. int counter = 0;
  163.  
  164. for (Duration duration : durations) {
  165. 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";
  166. PreparedStatement statement = connect.prepareStatement(sql);
  167.  
  168. statement.setString(1, duration.start);
  169. statement.setString(2, duration.end);
  170.  
  171. System.out.println("Going to run query (" + duration.start + ", " + duration.end + ")");
  172. long start = System.currentTimeMillis();
  173. ResultSet rs = statement.executeQuery();
  174. System.out.println("Time taken to run query (" + duration.start + ", " + duration.end + "): " + (System.currentTimeMillis() - start) + "ms");
  175.  
  176. while (rs.next()){
  177. String event_type = rs.getString("event_type");
  178.  
  179. if (event_type == null || event_type.isEmpty()) {
  180. // Currently, we have no idea how to handle empty rockman id.
  181. continue;
  182. }
  183.  
  184. Map<String, Object> result = resultSetToHashMap(rs);
  185.  
  186. if (event_type.equals("view")) {
  187. result.put("view", 1);
  188. } else {
  189. result.put("view", 0);
  190. }
  191.  
  192. if (event_type.equals("lead")) {
  193. result.put("lead", 1);
  194. } else {
  195. result.put("lead", 0);
  196. }
  197.  
  198. if (event_type.equals("subscription")) {
  199. result.put("subscription", 1);
  200. } else {
  201. result.put("subscription", 0);
  202. }
  203.  
  204. if (event_type.equals("firstbilling")) {
  205. result.put("firstbilling", 1);
  206. } else {
  207. result.put("firstbilling", 0);
  208. }
  209.  
  210. String s = gson.toJson(result);
  211. if (firstTime) {
  212. writer.write(s);
  213. firstTime = false;
  214. } else {
  215. writer.write("\n" + s);
  216. }
  217.  
  218. counter++;
  219. }
  220.  
  221. rs.close();
  222. } // for (Duration duration : durations)
  223.  
  224. writer.close();
  225.  
  226. System.out.println("Finish generating output.json with size " + counter);
  227.  
  228.  
  229. writer = new BufferedWriter(new FileWriter(new File("rockman-index.json")));
  230.  
  231. FileReader fr = new FileReader("rockman-index.json.template");
  232. String s;
  233. BufferedReader br = new BufferedReader(fr);
  234.  
  235. Calendar cal = Calendar.getInstance();
  236. cal.setTime(date);
  237. cal.add(Calendar.DATE, 1);
  238. Date nextDate = cal.getTime();
  239. String replace = formatter.format(date) + "T" + String.format("%02d", h) + ":00:00" + "/" + formatter.format(date) + "T" + String.format("%02d", h) + ":59:59";
  240.  
  241. // 2016-04-13T00:00:00/2016-04-13T00:59:59
  242. while ((s = br.readLine()) != null) {
  243. s = s.replaceAll("%DATE_RANGE%", replace);
  244. writer.write(s + "\n");
  245. }
  246.  
  247. br.close();
  248. writer.close();
  249.  
  250. System.out.println("Finish generating rockman-index.json");
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement