|
@@ -0,0 +1,167 @@
|
|
|
+package cn.com.qmth.examcloud.core.reports.base.util.online;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.util.DateUtil;
|
|
|
+
|
|
|
+public class ActiveDataUtil {
|
|
|
+ private final static String spe = "_";
|
|
|
+ private final static Long defTimeOut = 5 * 60 * 1000L;
|
|
|
+ private static String studentLoginDataReportDay=DateUtil.format(new Date(), DateUtil.DatePatterns.YYYY_MM_DD);
|
|
|
+ private final static Map<String, Map<Long, Long>> studentLoginData = new ConcurrentHashMap<String, Map<Long, Long>>();
|
|
|
+ private final static Map<String, Map<Long, Long>> userActiveData = new ConcurrentHashMap<String, Map<Long, Long>>();
|
|
|
+ private final static Map<String, Map<Long, Long>> studentActiveData = new ConcurrentHashMap<String, Map<Long, Long>>();
|
|
|
+ private final static Map<String, Map<Long, Long>> examStudentActiveData = new ConcurrentHashMap<String, Map<Long, Long>>();
|
|
|
+
|
|
|
+ public static void updateUserActive(UserActive ac) {
|
|
|
+ String key = ac.getKey(spe);
|
|
|
+ Map<Long, Long> map = userActiveData.get(key);
|
|
|
+ if (map == null) {
|
|
|
+ map = new ConcurrentHashMap<Long, Long>();
|
|
|
+ userActiveData.put(key, map);
|
|
|
+ }
|
|
|
+ map.put(ac.getUserId(), ac.getActiveTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void updateStudentActive(StudentActive ac) {
|
|
|
+ String key = ac.getKey(spe);
|
|
|
+ Map<Long, Long> map = studentActiveData.get(key);
|
|
|
+ if (map == null) {
|
|
|
+ map = new ConcurrentHashMap<Long, Long>();
|
|
|
+ studentActiveData.put(key, map);
|
|
|
+ }
|
|
|
+ map.put(ac.getStudentId(), ac.getActiveTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void updateExamStudentActive(ExamStudentActive ac) {
|
|
|
+ String key = ac.getKey(spe);
|
|
|
+ Map<Long, Long> map = examStudentActiveData.get(key);
|
|
|
+ if (map == null) {
|
|
|
+ map = new ConcurrentHashMap<Long, Long>();
|
|
|
+ examStudentActiveData.put(key, map);
|
|
|
+ }
|
|
|
+ map.put(ac.getExamStudentId(), ac.getActiveTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void updateStudentlogin(StudentLogin sl) {
|
|
|
+ String key = sl.getKey(spe);
|
|
|
+ Map<Long, Long> map = studentLoginData.get(key);
|
|
|
+ if (map == null) {
|
|
|
+ map = new ConcurrentHashMap<Long, Long>();
|
|
|
+ studentLoginData.put(key, map);
|
|
|
+ }
|
|
|
+ map.put(sl.getStudentId(), sl.getActiveTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void clearTimeOutUserActive(Long timeOut) {
|
|
|
+ if (timeOut == null) {
|
|
|
+ timeOut = defTimeOut;
|
|
|
+ }
|
|
|
+ Date d = new Date();
|
|
|
+ Long now = d.getTime();
|
|
|
+ for (Map<Long, Long> map : userActiveData.values()) {
|
|
|
+ for (Long k : map.keySet()) {
|
|
|
+ if (now - map.get(k) > timeOut) {
|
|
|
+ map.remove(k);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void clearTimeOutStudentActive(Long timeOut) {
|
|
|
+ if (timeOut == null) {
|
|
|
+ timeOut = defTimeOut;
|
|
|
+ }
|
|
|
+ Date d = new Date();
|
|
|
+ Long now = d.getTime();
|
|
|
+ for (Map<Long, Long> map : studentActiveData.values()) {
|
|
|
+ for (Long k : map.keySet()) {
|
|
|
+ if (now - map.get(k) > timeOut) {
|
|
|
+ map.remove(k);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void clearTimeOutExamStudentActive(Long timeOut) {
|
|
|
+ if (timeOut == null) {
|
|
|
+ timeOut = defTimeOut;
|
|
|
+ }
|
|
|
+ Date d = new Date();
|
|
|
+ Long now = d.getTime();
|
|
|
+ for (Map<Long, Long> map : examStudentActiveData.values()) {
|
|
|
+ for (Long k : map.keySet()) {
|
|
|
+ if (now - map.get(k) > timeOut) {
|
|
|
+ map.remove(k);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<OnlineCount> getUserCount(Long timeOut) {
|
|
|
+ clearTimeOutUserActive(timeOut);
|
|
|
+ List<OnlineCount> ret = new ArrayList<OnlineCount>();
|
|
|
+ for (String key : userActiveData.keySet()) {
|
|
|
+ OnlineCount oc = new OnlineCount();
|
|
|
+ String[] ks = key.split(spe);
|
|
|
+ oc.setRootOrgId(Long.valueOf(ks[0]));
|
|
|
+ oc.setOnlineCount(userActiveData.get(key).size());
|
|
|
+ ret.add(oc);
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<OnlineCount> getStudentCount(Long timeOut) {
|
|
|
+ clearTimeOutStudentActive(timeOut);
|
|
|
+ List<OnlineCount> ret = new ArrayList<OnlineCount>();
|
|
|
+ for (String key : studentActiveData.keySet()) {
|
|
|
+ OnlineCount oc = new OnlineCount();
|
|
|
+ String[] ks = key.split(spe);
|
|
|
+ oc.setRootOrgId(Long.valueOf(ks[0]));
|
|
|
+ oc.setOnlineCount(studentActiveData.get(key).size());
|
|
|
+ ret.add(oc);
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<OnlineCount> getExamStudentCount(Long timeOut) {
|
|
|
+ clearTimeOutExamStudentActive(timeOut);
|
|
|
+ List<OnlineCount> ret = new ArrayList<OnlineCount>();
|
|
|
+ for (String key : examStudentActiveData.keySet()) {
|
|
|
+ OnlineCount oc = new OnlineCount();
|
|
|
+ String[] ks = key.split(spe);
|
|
|
+ oc.setRootOrgId(Long.valueOf(ks[0]));
|
|
|
+ oc.setExamId(Long.valueOf(ks[1]));
|
|
|
+ oc.setOnlineCount(examStudentActiveData.get(key).size());
|
|
|
+ ret.add(oc);
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<OnlineCount> getStudentLoginCount() {
|
|
|
+ List<OnlineCount> ret = new ArrayList<OnlineCount>();
|
|
|
+ for (String key : studentLoginData.keySet()) {
|
|
|
+ OnlineCount oc = new OnlineCount();
|
|
|
+ String[] ks = key.split(spe);
|
|
|
+ oc.setRootOrgId(Long.valueOf(ks[0]));
|
|
|
+ oc.setOnlineCount(studentLoginData.get(key).size());
|
|
|
+ ret.add(oc);
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void clearStudentLoginCount() {
|
|
|
+ studentLoginDataReportDay=DateUtil.format(new Date(), DateUtil.DatePatterns.YYYY_MM_DD);
|
|
|
+ studentLoginData.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getStudentLoginDataReportDay() {
|
|
|
+ return studentLoginDataReportDay;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|