Skip to content

Commit

Permalink
Update StudentIdSys api
Browse files Browse the repository at this point in the history
  • Loading branch information
WavJaby committed Dec 1, 2023
1 parent 301c011 commit 3889f57
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 137 deletions.
10 changes: 6 additions & 4 deletions res/pages/stuIdSysGrades.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import {div, h1, mountableStylesheet, ShowIf, Signal, span, State, text, button} from '../minjs_v000/domHelper.min.js';
import {button, div, h1, mountableStylesheet, ShowIf, Signal, span, text} from '../minjs_v000/domHelper.min.js';
import {fetchApi} from '../lib/lib.js';
import PopupWindow from '../popupWindow.js';

Expand All @@ -10,6 +10,7 @@ import PopupWindow from '../popupWindow.js';
* @typedef CourseGrade
* @property {string} serialNumber
* @property {string} systemNumber
* @property {string | null} classCode
* @property {string} courseName
* @property {string} remark
* @property {float} credits
Expand Down Expand Up @@ -199,7 +200,8 @@ function MyGrades(router) {
// span(null, 'info', span('Remark'), text(': ' + course.remark)),
// span(null, 'info', span('Require'), text(': ' + course.require)),
span(null, 'info', span('課程序號', null, {title: 'Serial Number'}), text(': ' + course.serialNumber)),
span(null, 'info', span('課程碼', null, {title: 'System Number'}), text(': ' + course.systemNumber)),
span(null, 'info', span('課程碼', null, {title: 'System Number'}),
text(': ' + course.systemNumber + (course.classCode ? '-' + course.classCode : ''))),
span(null, 'info', span('學分'), text(': ' + course.credits)),
course.gpa === null ? null :
span(null, 'info', span('Gpa', null, {title: course.gpa}), text(': ' + gpaPointCalculate(course.gpa))),
Expand Down Expand Up @@ -239,7 +241,7 @@ function MyGrades(router) {
bar.style.left = barWidth * i + '%';
bars.push(bar);

const line = div(i * 10);
const line = div();
line.style.left = barWidth * i + '%';
labels.push(line);

Expand Down Expand Up @@ -289,7 +291,7 @@ function MyGrades(router) {
const courseInfo = this && this.courseInfo || inCourseInfo;
if (!courseInfo.imgQuery)
return;
fetchApi('/stuIdSys?mode=courseNormalDist&imgQuery=' + courseInfo.imgQuery).then(response => {
fetchApi('/stuIdSys?mode=courseGradesDistribution&imgQuery=' + courseInfo.imgQuery).then(response => {
if (response.success)
createDistWindow(courseInfo, response.data);
else {
Expand Down
79 changes: 61 additions & 18 deletions src/main/java/com/wavjaby/CourseEnrollmentTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.wavjaby.json.JsonArrayStringBuilder;
import com.wavjaby.json.JsonObject;
import com.wavjaby.json.JsonObjectStringBuilder;
import com.wavjaby.lib.PropertiesReader;
import com.wavjaby.lib.ThreadFactory;
import com.wavjaby.logger.Logger;

Expand All @@ -18,7 +19,10 @@
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
Expand All @@ -31,7 +35,8 @@ public class CourseEnrollmentTracker implements Runnable, Module {
private static final String TAG = "EnrollmentTrack";
private static final Logger logger = new Logger(TAG);
private static final String ENROLLMENT_TRACKER_FOLDER = "./api_file/CourseEnrollmentTracker";
private static final String CACHE_FILE_NAME = "cache.json";
private static final String ALL_COURSE_FILE_NAME = "allCourse.json";
private final boolean courseEnrollmentTracker;
private final Search search;
private CookieStore baseCookieStore;
private ScheduledExecutorService scheduler;
Expand All @@ -48,8 +53,20 @@ public class CourseEnrollmentTracker implements Runnable, Module {
private List<CourseData> lastCourseDataList;
// Serial, index
private Map<String, Integer> tableIndex;
private Map<String, SharedCourse> systemNumberMap;

public CourseEnrollmentTracker(Search search, Properties serverSettings) {
public static class SharedCourse {
public final String[] serialIds;
public final CourseData courseData;

public SharedCourse(String[] serialIds, CourseData courseData) {
this.serialIds = serialIds;
this.courseData = courseData;
}
}

public CourseEnrollmentTracker(Search search, PropertiesReader serverSettings) {
courseEnrollmentTracker = serverSettings.getPropertyBoolean("courseEnrollmentTracker", false);
this.search = search;
}

Expand All @@ -71,30 +88,56 @@ public void start() {
this.folder = folder;

lastCourseDataList = new ArrayList<>();
// Read cache
// Read all course
if (folder != null) {
File cacheFile = new File(folder, CACHE_FILE_NAME);
File allCourseFile = new File(folder, ALL_COURSE_FILE_NAME);
JsonObject cache = null;
try {
if (!cacheFile.exists()) {
if (cacheFile.createNewFile())
setFilePermission(cacheFile, Main.userPrincipal, Main.groupPrincipal, Main.filePermission);
if (!allCourseFile.exists()) {
if (allCourseFile.createNewFile())
setFilePermission(allCourseFile, Main.userPrincipal, Main.groupPrincipal, Main.filePermission);
else
logger.err("EnrollmentTracker cache file failed to create");
} else {
JsonObject cache = new JsonObject(Files.newInputStream(cacheFile.toPath()));
for (Object o : cache.getArray("data"))
lastCourseDataList.add(new CourseData((JsonObject) o));
}
} else
cache = new JsonObject(Files.newInputStream(allCourseFile.toPath()));
} catch (IOException e) {
logger.errTrace(e);
}

// systemNumberMap = new HashMap<>();
// // Parse all course
// if (cache != null) {
// Map<String, List<String>> systemNumberMapBuilder = new HashMap<>();
// Map<String, CourseData> systemNumberMapBuilderCourse = new HashMap<>();
// for (Object o : cache.getArray("data")) {
// CourseData courseData = new CourseData((JsonObject) o);
// lastCourseDataList.add(courseData);
// // Add course, check shared system number
// systemNumberMapBuilder.computeIfAbsent(courseData.getSystemNumber(), (i) -> new ArrayList<>())
// .add(courseData.getSerialNumber());
// systemNumberMapBuilderCourse.putIfAbsent(courseData.getSystemNumber(), courseData);
// }
// // Add to systemNumberMap
// for (Map.Entry<String, List<String>> entry : systemNumberMapBuilder.entrySet()) {
// systemNumberMap.put(entry.getKey(), new SharedCourse(
// entry.getValue().toArray(new String[0]),
// systemNumberMapBuilderCourse.get(entry.getKey())
// ));
// }
// }
}

messageSendPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(4, new ThreadFactory(TAG + "-Msg"));
scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory(TAG + "-Schedule"));
scheduler.scheduleAtFixedRate(this, 10000, updateInterval, TimeUnit.MILLISECONDS);
if (courseEnrollmentTracker) {
messageSendPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(4, new ThreadFactory(TAG + "-Msg"));
scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory(TAG + "-Schedule"));
scheduler.scheduleAtFixedRate(this, 10000, updateInterval, TimeUnit.MILLISECONDS);
}
}

// public SharedCourse getCourseDataBySystemNumber(String systemNumber) {
// return systemNumberMap.get(systemNumber);
// }

@Override
public void stop() {
executorShutdown(scheduler, 5000, TAG);
Expand Down Expand Up @@ -181,15 +224,15 @@ public void run() {
entry.setValue(i++);
}
// Create file
File cacheFile = new File(folder, CACHE_FILE_NAME);
File cacheFile = new File(folder, ALL_COURSE_FILE_NAME);
JsonObjectStringBuilder out = new JsonObjectStringBuilder();
out.append("time", now);
JsonArrayStringBuilder courseJsonArray = new JsonArrayStringBuilder();
int[] selected = new int[tableIndex.size()];
for (CourseData courseData : newCourseDataList) {
if (courseData.getSerialNumber() == null || courseData.getSelected() == null)
continue;
courseJsonArray.appendRaw(courseData.toStringShort());
courseJsonArray.appendRaw(courseData.toString());

Integer index = tableIndex.get(courseData.getSerialNumber());
if (index == null) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/wavjaby/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public class Main {
addModule(login);
DeptWatchdog watchDog = new DeptWatchdog(login, sqLite);
addModule(watchDog);
CourseEnrollmentTracker enrollmentTracker = new CourseEnrollmentTracker(search, serverSettings);
addModule(enrollmentTracker);
addModule(new AllDept(search));
addModule(new Logout(proxyManager));
addModule(new A9Registered(proxyManager));
Expand All @@ -133,13 +135,11 @@ public class Main {
addModule(new HomeInfo(proxyManager));
addModule(new UsefulWebsite());
addModule(new ClientDebugLog());
addModule(new StudentIdSys(sqLite));
addModule(new StudentIdSys(sqLite, enrollmentTracker));

if (serverSettings.getPropertyBoolean("courseWatcher", false))
addModule(new CourseWatcher(search, watchDog, serverSettings));

if (serverSettings.getPropertyBoolean("courseEnrollmentTracker", false))
addModule(new CourseEnrollmentTracker(search, serverSettings));

// logger.log("Server started, " + server.hostname + ':' + server.port);

Expand Down
Loading

0 comments on commit 3889f57

Please sign in to comment.