diff --git a/CHANGELOG.md b/CHANGELOG.md index af0c247..a8f318a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# 1.2.8 + +* 增加状态配置文件`config/state.yaml` +* 增加网址测试超时时间配置 +* 增加网址测试配置 +* 将`whole.yaml`状态相关配置移入`state.yaml` +* 优化状态异步任务使用`Promise.all` + # 1.2.7 * 删除依赖`puppeteer-extra``puppeteer-extra-plugin-stealth` diff --git a/apps/admin.js b/apps/admin.js index f0fe719..f584429 100644 --- a/apps/admin.js +++ b/apps/admin.js @@ -8,9 +8,7 @@ import { setu, puppeteer } from '../model/index.js' const OtherCfgType = { 全部通知: 'notificationsAll', 状态: 'state', - 陌生人点赞: 'Strangers_love', - // 给有问题的用户关闭定时器 - 状态任务: 'statusTask' + 陌生人点赞: 'Strangers_love' } const SeSeCfgType = { 涩涩: 'sese', diff --git a/apps/state.js b/apps/state.js index 98d8b0d..7e31f5e 100644 --- a/apps/state.js +++ b/apps/state.js @@ -44,18 +44,42 @@ export class NewState extends plugin { // 防止多次触发 if (interval) { return false } else interval = true // 系统 - let osinfo = await State.si.osInfo() + let FastFetch; let HardDisk + let otherInfo = [] + // 其他信息 + otherInfo.push({ + first: '系统', + tail: State.osInfo?.distro + }) + // 网络 + otherInfo.push(State.getnetwork) + // 插件数量 + otherInfo.push(State.getPluginNum) + let promiseTaskList = [ + State.getFastFetch(e).then(res => { FastFetch = res }), + State.getFsSize().then(res => { HardDisk = res }) + ] + + // 网络测试 + let psTest = [] + let { psTestSites, psTestTimeout } = Config.state + psTestSites && promiseTaskList.push(...psTestSites?.map(i => State.getNetworkLatency(i.url, psTestTimeout).then(res => psTest.push({ + first: i.name, + tail: res + })))) + // 执行promise任务 + await Promise.all(promiseTaskList) // 可视化数据 - let visualData = _.compact([ + let visualData = _.compact(await Promise.all([ // CPU板块 - await State.getCpuInfo(osinfo.arch), + State.getCpuInfo(), // 内存板块 - await State.getMemUsage(), + State.getMemUsage(), // GPU板块 - await State.getGPU(), + State.getGPU(), // Node板块 - await State.getNodeInfo() - ]) + State.getNodeInfo() + ])) // 渲染数据 let data = { chartData: JSON.stringify(common.checkIfEmpty(State.chartData, ['echarts_theme', 'cpu', 'ram']) ? undefined : State.chartData), @@ -76,30 +100,30 @@ export class NewState extends plugin { // 图片 screenshot: await redis.get('Yz:count:screenshot:total') || 0, // nodejs版本 - nodeversion: process.version, + nodeVersion: process.version, // 群数 - group_quantity: Array.from(this.Bot.gl.values()).length, + groupQuantity: Array.from(this.Bot.gl.values()).length, // 好友数 - friend_quantity: Array.from(this.Bot.fl.values()).length, + friendQuantity: Array.from(this.Bot.fl.values()).length, // 登陆设备 platform: platform[this.Bot.config?.platform], // 在线状态 status: status[this.Bot.status], // 硬盘内存 - HardDisk: await State.getFsSize(), + HardDisk, // FastFetch - FastFetch: await State.getFastFetch(e), - // 取插件 - plugin: State.numberOfPlugIns, + FastFetch, // 硬盘速率 fsStats: State.DiskSpeed, - // 网络 - network: State.getnetwork, // 可视化数据 visualData, - // 系统信息 - osinfo + // 系统平台 + osPlatform: State.osInfo?.platform, + // 其他数据 + otherInfo: _.compact(otherInfo), + psTest: _.isEmpty(psTest) ? false : psTest } + // 渲染图片 await puppeteer.render('state/state', { ...data @@ -107,6 +131,7 @@ export class NewState extends plugin { e, scale: 1.4 }) + interval = false } } diff --git a/components/Config.js b/components/Config.js index 1927c93..c44b0b2 100644 --- a/components/Config.js +++ b/components/Config.js @@ -96,6 +96,11 @@ class Config { return this.getDefOrConfig('setu') } + /** 状态 */ + get state () { + return this.getDefOrConfig('state') + } + /** 默认配置和用户配置 */ getDefOrConfig (name) { let def = this.getdefSet(name) diff --git a/config/default_config/state.yaml b/config/default_config/state.yaml new file mode 100644 index 0000000..e12785d --- /dev/null +++ b/config/default_config/state.yaml @@ -0,0 +1,18 @@ +#网址测试超时时间 +psTestTimeout: 5000 + +# 测试访问的网址列表(可空) +# name: 显示名称 +# url: 要访问的网址 +psTestSites: false +# psTestSites: +# - name: Baidu +# url: https://baidu.com +# - name: Google +# url: https://google.com + +#监控任务 +statusTask: true + +#如果出现内存异常的情况可将此配置项开启,如果打开后报错请将监控任务关闭 +statusPowerShellStart: false diff --git a/config/default_config/whole.yaml b/config/default_config/whole.yaml index 40dd093..908061a 100644 --- a/config/default_config/whole.yaml +++ b/config/default_config/whole.yaml @@ -23,8 +23,5 @@ sese: false #涩涩 state: false #状态 deltime: 600 #删除缓存 sesepro: false #涩涩增强 -statusTask: true #状态网速定时器 renderScale: 100 #渲染精度 anonymous: false #匿名 -#如果出现内存异常的情况可将此配置项开启,如果打开后报错请将状态定时器关闭 -statusPowerShellStart: false diff --git a/guoba.support.js b/guoba.support.js index 3b39f1e..898540e 100644 --- a/guoba.support.js +++ b/guoba.support.js @@ -152,13 +152,6 @@ export function supportGuoba () { bottomHelpMessage: '是否将椰奶状态作为默认状态', component: 'Switch' }, - { - field: 'whole.statusTask', - label: '状态任务', - helpMessage: '关闭后不再读取网速和硬盘速率', - bottomHelpMessage: '如果出现许多cmd进程占用或控制台输出乱码可尝试关闭此选项', - component: 'Switch' - }, { field: 'whole.notificationsAll', label: '通知全部管理', diff --git a/lib/request/request.js b/lib/request/request.js index 1f98d28..a0b9839 100644 --- a/lib/request/request.js +++ b/lib/request/request.js @@ -76,10 +76,10 @@ export default new class { * @async * @function * @param {string} url - 请求的URL - * @param {import('node-fetch').RequestInit} [options={}] - 请求的配置项 - * @param {Object} [options.params] - 请求的参数 - * @param {Object} [options.headers] - 请求的HTTP头部 - * @param {Object} [options.data] - 请求的数据 + * @param {object} [options={}] - 请求的配置项 + * @param {object} [options.params] - 请求的参数 + * @param {object} [options.headers] - 请求的HTTP头部 + * @param {object} [options.data] - 请求的数据 * @param {boolean} [options.closeCheckStatus=false] - 是否关闭状态检查 * @param {'buffer'|'json'|'text'|'arrayBuffer'|'formData'|'blob'} [options.statusCode] - 期望的返回数据,如果设置了该值,则返回响应数据的特定的方法(如json()、text()等) * @returns {Promise} - HTTP响应或响应数据 diff --git a/model/State.js b/model/State.js index 2fd57ea..413c57c 100644 --- a/model/State.js +++ b/model/State.js @@ -3,10 +3,12 @@ import _ from 'lodash' import fs from 'fs' import { common } from './index.js' import { Config, Data } from '../components/index.js' +import request from '../lib/request/request.js' export default new class { constructor () { this.si = null + this.osInfo = null // 是否可以获取gpu this.isGPU = false // 网络 @@ -72,6 +74,7 @@ export default new class { async initDependence () { try { this.si = await import('systeminformation') + this.osInfo = await this.si.osInfo() return this.si } catch (error) { if (error.stack?.includes('Cannot find package')) { @@ -97,33 +100,42 @@ export default new class { this.isGPU = true } // 给有问题的用户关闭定时器 - if (!Config.whole.statusTask) return - - if (Config.whole.statusPowerShellStart) this.si.powerShellStart() + if (!Config.state.statusTask) return + if (Config.state.statusPowerShellStart) this.si.powerShellStart() + this.getData() // 网速 const Timer = setInterval(async () => { - let data = await this.si.get(this.valueObject) - _.forIn(data, (value, key) => { - if (_.isEmpty(value)) { - logger.debug(`获取${key}数据失败,停止获取对应数据`) - delete this.valueObject[key] - } - }) - + let data = await this.getData() if (_.isEmpty(data)) clearInterval(Timer) - let { fsStats, networkStats, mem: { active }, currentLoad: { currentLoad } } = data - this.fsStats = fsStats - this.network = networkStats - if (_.isNumber(active)) { - this.addData(this.chartData.ram, [Date.now(), active]) - } - if (_.isNumber(currentLoad)) { - this.addData(this.chartData.cpu, [Date.now(), currentLoad]) - } }, 60000) } + async getData () { + let data = await this.si.get(this.valueObject) + _.forIn(data, (value, key) => { + if (_.isEmpty(value)) { + logger.debug(`获取${key}数据失败,停止获取对应数据`) + delete this.valueObject[key] + } + }) + let { + fsStats, + networkStats, + mem: { active }, + currentLoad: { currentLoad } + } = data + this.fsStats = fsStats + this.network = networkStats + if (_.isNumber(active)) { + this.addData(this.chartData.ram, [Date.now(), active]) + } + if (_.isNumber(currentLoad)) { + this.addData(this.chartData.cpu, [Date.now(), currentLoad]) + } + return data + } + /** * 向数组中添加数据,如果数组长度超过允许的最大值,则删除最早添加的数据 * @@ -266,7 +278,7 @@ export default new class { } /** 获取CPU占用 */ - async getCpuInfo (arch) { + async getCpuInfo () { let { currentLoad: { currentLoad }, cpuCurrentSpeed } = await this.si.get({ currentLoad: 'currentLoad', cpuCurrentSpeed: 'max,avg' @@ -281,7 +293,7 @@ export default new class { inner: Math.round(currentLoad) + '%', title: 'CPU', info: [ - `${cpuModel} ${cores.length}核 ${arch}`, + `${cpuModel} ${cores.length}核 ${this.osInfo?.arch}`, `平均${cpuCurrentSpeed.avg}GHz`, `最大${cpuCurrentSpeed.max}GHz` ] @@ -294,11 +306,13 @@ export default new class { if (!this.isGPU) return false try { const { controllers } = await this.si.graphics() - logger.debug(controllers) let graphics = controllers?.find(item => item.memoryUsed && item.memoryFree && item.utilizationGpu ) - if (!graphics) return false + if (!graphics) { + logger.warn('[Yenai-plugin][state]状态GPU数据异常:\n', controllers) + return false + } let { vendor, temperatureGpu, utilizationGpu, memoryTotal, memoryUsed, powerDraw @@ -385,14 +399,18 @@ export default new class { } network.rx_sec = this.getFileSize(network.rx_sec, false, false) network.tx_sec = this.getFileSize(network.tx_sec, false, false) - return network + // return network + return { + first: network.iface, + tail: `↑${network.tx_sec}/s | ↓${network.rx_sec}/s` + } } /** * @description: 取插件包 * @return {*} 插件包数量 */ - get numberOfPlugIns () { + get getPluginNum () { let str = './plugins' let arr = fs.readdirSync(str) let plugin = [] @@ -404,10 +422,54 @@ export default new class { }) let del = ['example', 'genshin', 'other', 'system', 'bin'] plugin = plugin.filter(item => !del.includes(item)) - + const plugins = plugin?.length || 0 + const js = fs.readdirSync('./plugins/example')?.filter(item => item.includes('.js'))?.length || 0 + // return { + // plugins: plugin?.length || 0, + // js: fs.readdirSync('./plugins/example')?.filter(item => item.includes('.js'))?.length || 0 + // } return { - plugins: plugin?.length || 0, - js: fs.readdirSync('./plugins/example')?.filter(item => item.includes('.js'))?.length || 0 + first: '插件', + tail: `${plugins} plugin | ${js} js` + } + } + + async getNetworkLatency (url, timeoutTime = 5000) { + const AbortController = globalThis.AbortController || await import('abort-controller') + + const controller = new AbortController() + const timeout = setTimeout(() => { + controller.abort() + }, timeoutTime) + try { + const startTime = Date.now() + let { status } = await request.get(url, { signal: controller.signal }) + const endTime = Date.now() + let delay = endTime - startTime + let color = ''; let statusColor = '' + if (delay > 2000) { + color = '#F44336' + } else if (delay > 500) { + color = '#d68100' + } else { + color = '#188038' + } + if (status >= 500) { + statusColor = '#9C27B0' + } else if (status >= 400) { + statusColor = '#F44336' + } else if (status >= 300) { + statusColor = '#FF9800' + } else if (status >= 200) { + statusColor = '#188038' + } else if (status >= 100) { + statusColor = '#03A9F4' + } + return `${status} | ${delay}ms` + } catch { + return "timeout" + } finally { + clearTimeout(timeout) } } }() diff --git a/resources/admin/index.html b/resources/admin/index.html index 884dbd3..aed14e8 100644 --- a/resources/admin/index.html +++ b/resources/admin/index.html @@ -187,14 +187,6 @@
开启后将使用椰奶版状态作为yunzai的默认状态
-
  • -
    - 椰奶状态定时器 - #椰奶设置状态任务 + 开启/关闭 - {{@statusTask}} -
    -
    如果出现开启很多cmd任务的情况下可将其关闭,关闭后将不再获取网速和硬盘速率
    -
  • 设置删除缓存时间 diff --git a/resources/help/index.css b/resources/help/index.css index cb2a388..43ca243 100644 --- a/resources/help/index.css +++ b/resources/help/index.css @@ -1,7 +1,7 @@ .help-title, .help-group { background-image: linear-gradient(180deg, #d51a2f, #0cacaa); -webkit-background-clip: text; - background-clip: text; +dd background-clip: text; color: transparent; } @@ -16,7 +16,7 @@ body { background-position: right center; background-repeat: no-repeat; background-size: cover; - padding: 20px 35px 10px 25px; + padding: 20px 25px 10px 25px; width: 1130px; } diff --git a/resources/help/index.scss b/resources/help/index.scss index 07a5b31..c30004a 100644 --- a/resources/help/index.scss +++ b/resources/help/index.scss @@ -18,7 +18,7 @@ body { size: cover; } - padding: 20px 35px 10px 25px; + padding: 20px 25px 10px 25px; width: 1130px; } diff --git a/resources/state/state.html b/resources/state/state.html index 5deb4e9..c044c4c 100644 --- a/resources/state/state.html +++ b/resources/state/state.html @@ -14,11 +14,11 @@

    {{nickname}}


    -

    {{status}}({{platform}}) | 收{{recv}} | 发{{sent}} | 图片{{screenshot}} | 好友{{friend_quantity}} | - 群{{group_quantity}} +

    {{status}}({{platform}}) | 收{{recv}} | 发{{sent}} | 图片{{screenshot}} | 好友{{friendQuantity}} | + 群{{groupQuantity}}

    Yunzai-Bot 已运行 {{runTime}} | 系统运行 {{systime}}

    -

    {{calendar}} | Nodejs {{nodeversion}} | {{osinfo.platform}}

    +

    {{calendar}} | Nodejs {{nodeVersion}} | {{osPlatform}}

    @@ -179,22 +179,30 @@ chart.setOption(option) + {{/if}} +{{if otherInfo}}
    + {{each otherInfo}}
    -

    系统

    -

    {{osinfo.distro}}

    -
    - {{if network}} -
    -

    {{network.iface}}

    -

    ↑{{network.tx_sec}}/s | ↓{{network.rx_sec}}/s

    -
    - {{/if}} -
    -

    插件

    -

    {{plugin.plugins}} plugin | {{plugin.js}} js

    +

    {{$value.first}}

    +

    {{@$value.tail}}

    + {{/each}}
    +{{/if}} + +{{if psTest}} +
    + {{each psTest}} +
    +

    {{$value.first}}

    +

    {{@$value.tail}}

    +
    + {{/each}} +
    +{{/if}} + {{@FastFetch}} + {{/block}} \ No newline at end of file