core: update detailed status

This commit is contained in:
undefined committed 2021-04-03 12:17:27 +08:00
1 parent 51ce9f80d5
commit b9e08c7e93
15 files changed
+142 -99

No files matched your search

-37
View File
@@ -2,45 +2,10 @@
import { BadRequestError } from '../error';
import { PRIV } from '../model/builtin';
import user from '../model/user';
import db from '../service/db';
import {
Route, Handler, Types, param,
} from '../service/server';
const coll = db.collection('status');
class StatusHandler extends Handler {
async get() {
const stats = await coll.find().sort({ type: 1, updateAt: -1 }).toArray();
for (const i in stats) {
let desc = '';
const online = new Date(stats[i].updateAt).getTime() > new Date().getTime() - 300000;
if (!online) desc = 'Offline';
desc = desc || 'Online';
stats[i].isOnline = online;
stats[i].status = desc;
}
const path = [
['Hydro', 'homepage'],
['status', null],
];
this.response.body = { stats, path };
this.response.template = 'status.html';
}
}
class StatusUpdateHandler extends Handler {
async post(args) {
this.checkPriv(PRIV.PRIV_JUDGE);
args.type = 'judge';
return coll.updateOne(
{ mid: args.mid, type: 'judge' },
{ $set: args },
{ upsert: true },
);
}
}
class CheckInHandler extends Handler {
async prepare() {
const { checkincnt, lastcheckin } = this.user;
@@ -82,8 +47,6 @@ class SwitchLanguageHandler extends Handler {
}
export async function apply() {
Route('status', '/status', StatusHandler);
Route('status_update', '/status/update', StatusUpdateHandler);
Route('check_in', '/checkin', CheckInHandler, PRIV.PRIV_USER_PROFILE);
Route('switch_language', '/language/:lang', SwitchLanguageHandler);
}
+1 -8
View File
@@ -12,14 +12,7 @@ class NotFoundHandler extends Handler {
}
export async function apply() {
Route('notfound', '/:a', NotFoundHandler);
Route('notfound', '/:a/:b', NotFoundHandler);
Route('notfound', '/:a/:b/:c', NotFoundHandler);
Route('notfound', '/:a/:b/:c/:d', NotFoundHandler);
Route('notfound', '/:a/:b/:c/:d/:e', NotFoundHandler);
Route('notfound', '/:a/:b/:c/:d/:e/:f', NotFoundHandler);
Route('notfound', '/:a/:b/:c/:d/:e/:f/:g', NotFoundHandler);
Route('notfound', '/:a/:b/:c/:d/:e/:f/:g/:h', NotFoundHandler);
Route('notfound', '(/.*)+', NotFoundHandler);
}
global.Hydro.handler.notfound = apply;
+81
View File
@@ -0,0 +1,81 @@
import { PRIV } from '../model/builtin';
import RecordModel from '../model/record';
import DomainModel from '../model/domain';
import * as DocumentModel from '../model/document';
import db from '../service/db';
import { Route, Handler } from '../service/server';
import UserModel from '../model/user';
const coll = db.collection('status');
async function getStatus() {
const stats = await coll.find().sort({ type: 1, updateAt: -1 }).toArray();
for (const stat of stats) {
let desc = '';
const online = new Date(stat.updateAt).getTime() > new Date().getTime() - 300000;
if (!online) desc = 'Offline';
desc = desc || 'Online';
stat.isOnline = online;
stat.status = desc;
}
return stats;
}
class StatusHandler extends Handler {
async get() {
const path = [
['Hydro', 'homepage'],
['status', null],
];
const stats = await getStatus();
for (const stat of stats) {
if (!stat.battery.hasBattery) stat.battery = 'No battery';
else stat.battery = `${stat.battery.type} ${stat.battery.model} ${stat.battery.percent}%${stat.battery.isCharging ? ' Charging' : ''}`;
}
this.response.body = { stats, path };
this.response.template = 'status.html';
}
}
class AdminStatusHandler extends Handler {
async get() {
const record = await RecordModel.stat();
const status = await getStatus();
const [domainCount, userCount, problemCount, discussionCount, recordCount] = await Promise.all([
DomainModel.getMulti().count(),
UserModel.getMulti().count(),
DocumentModel.coll.find({ docType: DocumentModel.TYPE_PROBLEM }).count(),
DocumentModel.coll.find({ docType: DocumentModel.TYPE_DISCUSSION }).count(),
RecordModel.coll.find().count(),
]);
this.response.body = {
record,
status,
domainCount,
userCount,
problemCount,
discussionCount,
recordCount,
};
}
}
class StatusUpdateHandler extends Handler {
async post(args) {
this.checkPriv(PRIV.PRIV_JUDGE);
args.type = 'judge';
return coll.updateOne(
{ mid: args.mid, type: 'judge' },
{ $set: args },
{ upsert: true },
);
}
}
export async function apply() {
Route('status', '/status', StatusHandler);
Route('status_admin', '/.status', AdminStatusHandler);
Route('status_update', '/status/update', StatusUpdateHandler);
}
global.Hydro.handler.status = apply;