|
@@ -0,0 +1,125 @@
|
|
|
+package cn.com.qmth.examcloud.task.base.online;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+public class ActiveDataUtil {
|
|
|
+ private final static String spe="_";
|
|
|
+ private final static Long defTimeOut=5*60*1000L;
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ 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.setOrgId(Long.valueOf(ks[1]));
|
|
|
+ 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.setOrgId(Long.valueOf(ks[1]));
|
|
|
+ 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.setOrgId(Long.valueOf(ks[1]));
|
|
|
+ oc.setExamId(Long.valueOf(ks[2]));
|
|
|
+ oc.setOnlineCount(examStudentActiveData.get(key).size());
|
|
|
+ ret.add(oc);
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+}
|