/src/main/java/background/jobs/DailyJob.java

https://github.com/hirokiterashima/WISE-VLE · Java · 305 lines · 167 code · 51 blank · 87 comment · 11 complexity · f83bdd73edde1e4888be6f89be066d08 MD5 · raw file

  1. package background.jobs;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.sql.Timestamp;
  8. import java.util.Date;
  9. import org.hibernate.cfg.AnnotationConfiguration;
  10. import org.json.JSONArray;
  11. import org.json.JSONException;
  12. import org.json.JSONObject;
  13. import org.quartz.Job;
  14. import org.quartz.JobExecutionContext;
  15. import org.quartz.JobExecutionException;
  16. import org.slf4j.LoggerFactory;
  17. import vle.domain.statistics.VLEStatistics;
  18. public class DailyJob implements Job {
  19. public void execute(JobExecutionContext context) throws JobExecutionException {
  20. try {
  21. //get the user name and password for the db
  22. AnnotationConfiguration configure = new AnnotationConfiguration().configure();
  23. String userName = configure.getProperty("connection.username");
  24. String password = configure.getProperty("connection.password");
  25. //create a connection to the mysql db
  26. Class.forName("com.mysql.jdbc.Driver").newInstance();
  27. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/vle_database", userName, password);
  28. //create a statement to run db queries
  29. Statement statement = conn.createStatement();
  30. //the JSONObject that we will store all the statistics in and then store in the db
  31. JSONObject vleStatistics = new JSONObject();
  32. //gather the StepWork statistics
  33. gatherStepWorkStatistics(statement, vleStatistics);
  34. //gather the Node statistics
  35. gatherNodeStatistics(statement, vleStatistics);
  36. //gather the Annotation statistics
  37. gatherAnnotationStatistics(statement, vleStatistics);
  38. //gather the Hint statistics
  39. gatherHintStatistics(statement, vleStatistics);
  40. //get the current timestamp
  41. Date date = new Date();
  42. Timestamp timestamp = new Timestamp(date.getTime());
  43. //set the timestamp in milliseconds into the JSONObject
  44. vleStatistics.put("timestamp", timestamp.getTime());
  45. //save the vle statistics row
  46. VLEStatistics vleStatisticsObject = new VLEStatistics();
  47. vleStatisticsObject.setTimestamp(timestamp);
  48. vleStatisticsObject.setData(vleStatistics.toString());
  49. vleStatisticsObject.saveOrUpdate();
  50. } catch (Exception ex) {
  51. LoggerFactory.getLogger(getClass()).error(ex.getMessage());
  52. }
  53. }
  54. /**
  55. * Gather the StepWork statistics. This includes the total number of StepWork
  56. * rows as well as how many StepWork rows for each step type.
  57. * @param statement the object to execute queries
  58. * @param vleStatistics the JSONObject to store the statistics in
  59. */
  60. private void gatherStepWorkStatistics(Statement statement, JSONObject vleStatistics) {
  61. try {
  62. //counter for total step work rows
  63. long stepWorkCount = 0;
  64. //array to hold the counts for each node type
  65. JSONArray stepWorkNodeTypeCounts = new JSONArray();
  66. /*
  67. * the query to get the total step work rows for each node type
  68. * e.g.
  69. *
  70. * nodeType | count(*)
  71. * ------------------------------
  72. * AssessmentListNode | 331053
  73. * BrainstormNode | 10936
  74. * CarGraphNode | 9
  75. * etc.
  76. *
  77. */
  78. ResultSet stepWorkNodeTypeCountQuery = statement.executeQuery("select node.nodeType, count(*) from stepwork, node where stepwork.node_id=node.id group by nodeType");
  79. //loop through all the rows from the query
  80. while(stepWorkNodeTypeCountQuery.next()) {
  81. //get the nodeType
  82. String nodeType = stepWorkNodeTypeCountQuery.getString(1);
  83. //get the count
  84. long nodeTypeCount = stepWorkNodeTypeCountQuery.getLong(2);
  85. try {
  86. if(nodeType != null && !nodeType.toLowerCase().equals("null")) {
  87. //create the object that will store the nodeType and count
  88. JSONObject stepWorkNodeTypeObject = new JSONObject();
  89. stepWorkNodeTypeObject.put("nodeType", nodeType);
  90. stepWorkNodeTypeObject.put("count", nodeTypeCount);
  91. //put the object into our array
  92. stepWorkNodeTypeCounts.put(stepWorkNodeTypeObject);
  93. //update the total count
  94. stepWorkCount += nodeTypeCount;
  95. }
  96. } catch(JSONException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. //add the step work statistics to the vleStatistics object
  101. vleStatistics.put("individualStepWorkNodeTypeCounts", stepWorkNodeTypeCounts);
  102. vleStatistics.put("totalStepWorkCount", stepWorkCount);
  103. } catch(SQLException e) {
  104. e.printStackTrace();
  105. } catch(JSONException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. /**
  110. * Gather the Annotation statistics. This includes the total number of Annotation
  111. * rows as well as how many Annotation nodes for each annotation type.
  112. * @param statement the object to execute queries
  113. * @param vleStatistics the JSONObject to store the statistics in
  114. */
  115. private void gatherAnnotationStatistics(Statement statement, JSONObject vleStatistics) {
  116. try {
  117. //get the total number of annotations
  118. ResultSet annotationCountQuery = statement.executeQuery("select count(*) from annotation");
  119. if(annotationCountQuery.first()) {
  120. long annotationCount = annotationCountQuery.getLong(1);
  121. try {
  122. //add the total annotation count to the vle statistics
  123. vleStatistics.put("totalAnnotationCount", annotationCount);
  124. } catch(JSONException e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. //the array to store the counts for each annotation type
  129. JSONArray annotationCounts = new JSONArray();
  130. //get the total number of comment annotations
  131. ResultSet annotationCommentCountQuery = statement.executeQuery("select count(*) from annotation_comment");
  132. if(annotationCommentCountQuery.first()) {
  133. long annotationCommentCount = annotationCommentCountQuery.getLong(1);
  134. try {
  135. //create an object to store the type and count in
  136. JSONObject annotationCommentObject = new JSONObject();
  137. annotationCommentObject.put("annotationType", "comment");
  138. annotationCommentObject.put("count", annotationCommentCount);
  139. annotationCounts.put(annotationCommentObject);
  140. } catch(JSONException e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. //get the total number of flag annotations
  145. ResultSet annotationFlagCountQuery = statement.executeQuery("select count(*) from annotation_flag");
  146. if(annotationFlagCountQuery.first()) {
  147. long annotationFlagCount = annotationFlagCountQuery.getLong(1);
  148. try {
  149. //create an object to store the type and count in
  150. JSONObject annotationFlagObject = new JSONObject();
  151. annotationFlagObject.put("annotationType", "flag");
  152. annotationFlagObject.put("count", annotationFlagCount);
  153. annotationCounts.put(annotationFlagObject);
  154. } catch(JSONException e) {
  155. e.printStackTrace();
  156. }
  157. }
  158. //get the total number of score annotations
  159. ResultSet annotationScoreCountQuery = statement.executeQuery("select count(*) from annotation_score");
  160. if(annotationScoreCountQuery.first()) {
  161. long annotationScoreCount = annotationScoreCountQuery.getLong(1);
  162. try {
  163. //create an object to store the type and count in
  164. JSONObject annotationScoreObject = new JSONObject();
  165. annotationScoreObject.put("annotationType", "score");
  166. annotationScoreObject.put("count", annotationScoreCount);
  167. annotationCounts.put(annotationScoreObject);
  168. } catch(JSONException e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. //add the annotation statistics to the vle statistics
  173. vleStatistics.put("individualAnnotationCounts", annotationCounts);
  174. } catch(SQLException e) {
  175. e.printStackTrace();
  176. } catch(JSONException e) {
  177. e.printStackTrace();
  178. }
  179. }
  180. /**
  181. * Get the node statistics. This includes the total number of step nodes as well
  182. * as how many step nodes for each node type.
  183. * @param statement the object to execute queries
  184. * @param vleStatistics the JSONObject to store the statistics in
  185. */
  186. private void gatherNodeStatistics(Statement statement, JSONObject vleStatistics) {
  187. try {
  188. //counter for the total number of nodes
  189. long nodeCount = 0;
  190. //array to hold all the counts for each node type
  191. JSONArray nodeTypeCounts = new JSONArray();
  192. /*
  193. * the query to get the total number of nodes for each node type
  194. * e.g.
  195. *
  196. * nodeType | count(*)
  197. * ------------------------------
  198. * AssessmentListNode | 3408
  199. * BrainstormNode | 98
  200. * CarGraphNode | 9
  201. * etc.
  202. *
  203. */
  204. ResultSet nodeTypeCountQuery = statement.executeQuery("select nodeType, count(*) from node group by nodeType");
  205. //loop through all the rows
  206. while(nodeTypeCountQuery.next()) {
  207. //get a node type and the count
  208. String nodeType = nodeTypeCountQuery.getString(1);
  209. long nodeTypeCount = nodeTypeCountQuery.getLong(2);
  210. if(nodeType != null && !nodeType.toLowerCase().equals("null")) {
  211. try {
  212. //create an object to hold the node type and count
  213. JSONObject nodeTypeObject = new JSONObject();
  214. nodeTypeObject.put("nodeType", nodeType);
  215. nodeTypeObject.put("count", nodeTypeCount);
  216. //add the object to our array
  217. nodeTypeCounts.put(nodeTypeObject);
  218. //update the total count
  219. nodeCount += nodeTypeCount;
  220. } catch(JSONException e) {
  221. e.printStackTrace();
  222. }
  223. }
  224. }
  225. //add the counts to the vle statistics
  226. vleStatistics.put("individualNodeTypeCounts", nodeTypeCounts);
  227. vleStatistics.put("totalNodeCount", nodeCount);
  228. } catch(SQLException e) {
  229. e.printStackTrace();
  230. } catch(JSONException e) {
  231. e.printStackTrace();
  232. }
  233. }
  234. /**
  235. * Get the number of times hints were viewed by a student
  236. * @param statement the object to execute queries
  237. * @param vleStatistics the JSONObject to store the statistics in
  238. */
  239. private void gatherHintStatistics(Statement statement, JSONObject vleStatistics) {
  240. try {
  241. //get the total number of times a hint was viewed by a student
  242. ResultSet hintCountQuery = statement.executeQuery("select count(*) from stepwork where data like '%hintStates\":[{%]%'");
  243. if(hintCountQuery.first()) {
  244. //add the count to the vle statistics
  245. long hintCount = hintCountQuery.getLong(1);
  246. vleStatistics.put("totalHintViewCount", hintCount);
  247. }
  248. } catch(SQLException e) {
  249. e.printStackTrace();
  250. } catch(JSONException e) {
  251. e.printStackTrace();
  252. }
  253. }
  254. }