From c827b9dab975150c601e9902fdc1c8e98c3153be Mon Sep 17 00:00:00 2001 From: juzeon <812312770@qq.com> Date: Mon, 13 Sep 2021 18:34:24 +0800 Subject: [PATCH] =?UTF-8?q?perf=EF=BC=9Ahealth=20logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/health.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/health.ts b/src/health.ts index a2795d0..15e85e0 100644 --- a/src/health.ts +++ b/src/health.ts @@ -2,6 +2,8 @@ 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 @@ -9,6 +11,8 @@ 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 @@ -25,13 +29,31 @@ export async function health() { } if (parseInt(currentCourse.syrs) > parseInt(lastCourse.syrs)) { totalExitCount += parseInt(currentCourse.syrs) - parseInt(lastCourse.syrs) + if (!exitNames.includes(currentCourse.kcmc)) { + exitNames.push(currentCourse.kcmc) + } } else { totalAcquireCount += parseInt(lastCourse.syrs) - parseInt(currentCourse.syrs) + if (!acquireNames.includes(currentCourse.kcmc)) { + acquireNames.push(currentCourse.kcmc) + } } } } - console.log('[health]实时总退课人次:' + totalExitCount + ',总选课人次:' + totalAcquireCount) + 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 +}