Files
sdu-course-bot/src/health.ts
2021-09-14 16:25:11 +08:00

61 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {IJsonCourse} from "./types"
import {JsonCourseList} from "./poll"
import {findJsonCourse} from "./includes"
import {appConfig} from "./config"
import {scalarOptions} from "yaml"
import Str = scalarOptions.Str
let lastJsonCourseList: { bx: IJsonCourse[], xx: IJsonCourse[], rx: IJsonCourse[] }
let firstRunHealth = true
export async function health() {
let totalAcquireCount = 0
let totalExitCount = 0
let acquireNames: string[] = []
let exitNames: string[] = []
if (firstRunHealth) {
console.log('[health]程序health监控启动完毕')
firstRunHealth = false
} else {
for (let channel of appConfig.channels) {
for (let lastCourse of lastJsonCourseList[channel]) {
let currentCourse = findJsonCourse({
kch: lastCourse.kch,
kxh: parseInt(lastCourse.kxh),
channel: channel
})
if (currentCourse == undefined) {
continue
}
if (parseInt(currentCourse.syrs) > parseInt(lastCourse.syrs)) {
totalExitCount += parseInt(currentCourse.syrs) - parseInt(lastCourse.syrs)
if (!exitNames.includes(currentCourse.kcmc)) {
exitNames.push(currentCourse.kcmc)
}
} else {
let num = parseInt(lastCourse.syrs) - parseInt(currentCourse.syrs)
totalAcquireCount += num
if (!acquireNames.includes(currentCourse.kcmc) && num > 0) {
acquireNames.push(currentCourse.kcmc)
}
}
}
}
console.log('[health]实时总退课人次:' + totalExitCount + '' + shuffle(exitNames).slice(0, 3).join(',') + '等),总选课人次:'
+ totalAcquireCount + '' + shuffle(acquireNames).slice(0, 3).join(',') + '等)')
}
lastJsonCourseList = JSON.parse(JSON.stringify(JsonCourseList))
setTimeout(health, 60 * 1000)
}
function shuffle(array: any[]) {
let currentIndex = array.length, randomIndex
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]]
}
return array
}