diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-07-11 18:44:09 +0200 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-07-11 18:44:09 +0200 |
commit | d2a020837267bf024f508b402f596f8e242672ed (patch) | |
tree | 7e371015fa1451bda993e44aa591b3cc24c3209d /src/tasks/cache | |
parent | 7d1dede862339c439830a0306e2b7f3a273f967f (diff) | |
download | tanzanite-d2a020837267bf024f508b402f596f8e242672ed.tar.gz tanzanite-d2a020837267bf024f508b402f596f8e242672ed.tar.bz2 tanzanite-d2a020837267bf024f508b402f596f8e242672ed.zip |
create task categories
Diffstat (limited to 'src/tasks/cache')
-rw-r--r-- | src/tasks/cache/cpuUsage.ts | 62 | ||||
-rw-r--r-- | src/tasks/cache/updateCache.ts | 60 | ||||
-rw-r--r-- | src/tasks/cache/updateHighlightCache.ts | 15 | ||||
-rw-r--r-- | src/tasks/cache/updatePriceItemCache.ts | 27 |
4 files changed, 164 insertions, 0 deletions
diff --git a/src/tasks/cache/cpuUsage.ts b/src/tasks/cache/cpuUsage.ts new file mode 100644 index 0000000..61e7a54 --- /dev/null +++ b/src/tasks/cache/cpuUsage.ts @@ -0,0 +1,62 @@ +import { BushTask, Time } from '#lib'; +import os from 'node:os'; + +export default class CpuUsageTask extends BushTask { + public constructor() { + super('cpuUsage', { + delay: Time.Minute, + runOnStart: true + }); + } + + public async exec() { + const cpuStats = await cpu.usage(this.client.stats.cpu === undefined ? 100 * Time.Millisecond : Time.Minute); + this.client.stats.cpu = cpuStats; + } +} + +/* Everything inside the cpu namespace is adapted from the "node-os-utils" npm package which is licensed under a MIT license by Sunil Wang */ +namespace cpu { + export function usage(interval = Time.Second): Promise<number> { + return new Promise((resolve) => { + const startMeasure = average(); + + setTimeout(() => { + const endMeasure = average(); + const idleDifference = endMeasure.avgIdle - startMeasure.avgIdle; + const totalDifference = endMeasure.avgTotal - startMeasure.avgTotal; + const cpuPercentage = (10000 - Math.round((10000 * idleDifference) / totalDifference)) / 100; + + return resolve(cpuPercentage); + }, interval); + }); + } + + export function average(): CpuAverageInfo { + let totalIdle = 0; + let totalTick = 0; + const cpus = os.cpus(); + + for (let i = 0, len = cpus.length; i < len; i++) { + const cpu = cpus[i]; + for (const type in cpu.times) { + totalTick += cpu.times[type as keyof typeof cpu.times]; + } + totalIdle += cpu.times.idle; + } + + return { + totalIdle: totalIdle, + totalTick: totalTick, + avgIdle: totalIdle / cpus.length, + avgTotal: totalTick / cpus.length + }; + } + + export interface CpuAverageInfo { + totalIdle: number; + totalTick: number; + avgIdle: number; + avgTotal: number; + } +} diff --git a/src/tasks/cache/updateCache.ts b/src/tasks/cache/updateCache.ts new file mode 100644 index 0000000..87636e8 --- /dev/null +++ b/src/tasks/cache/updateCache.ts @@ -0,0 +1,60 @@ +import { Time } from '#constants'; +import { Global, Guild, Shared, type BushClient } from '#lib'; +import type { Client } from 'discord.js'; +import config from '../../../config/options.js'; +import { BushTask } from '../../lib/extensions/discord-akairo/BushTask.js'; + +export default class UpdateCacheTask extends BushTask { + public constructor() { + super('updateCache', { + delay: 5 * Time.Minute, + runOnStart: false // done in preinit task + }); + } + + public async exec() { + await Promise.all([ + UpdateCacheTask.#updateGlobalCache(this.client), + UpdateCacheTask.#updateSharedCache(this.client), + UpdateCacheTask.#updateGuildCache(this.client) + ]); + void this.client.logger.verbose(`UpdateCache`, `Updated cache.`); + } + + public static async init(client: BushClient) { + await Promise.all([ + UpdateCacheTask.#updateGlobalCache(client), + UpdateCacheTask.#updateSharedCache(client), + UpdateCacheTask.#updateGuildCache(client) + ]); + } + + static async #updateGlobalCache(client: Client) { + const environment = config.environment; + const row: { [x: string]: any } = ((await Global.findByPk(environment)) ?? (await Global.create({ environment }))).toJSON(); + + for (const option in row) { + if (Object.keys(client.cache.global).includes(option)) { + client.cache.global[option as keyof typeof client.cache.global] = row[option]; + } + } + } + + static async #updateSharedCache(client: Client) { + const row: { [x: string]: any } = ((await Shared.findByPk(0)) ?? (await Shared.create())).toJSON(); + + for (const option in row) { + if (Object.keys(client.cache.shared).includes(option)) { + client.cache.shared[option as keyof typeof client.cache.shared] = row[option]; + if (option === 'superUsers') client.superUserID = row[option]; + } + } + } + + static async #updateGuildCache(client: Client) { + const rows = await Guild.findAll(); + for (const row of rows) { + client.cache.guilds.set(row.id, row.toJSON() as Guild); + } + } +} diff --git a/src/tasks/cache/updateHighlightCache.ts b/src/tasks/cache/updateHighlightCache.ts new file mode 100644 index 0000000..44ddd90 --- /dev/null +++ b/src/tasks/cache/updateHighlightCache.ts @@ -0,0 +1,15 @@ +import { Time } from '#constants'; +import { BushTask } from '../../lib/extensions/discord-akairo/BushTask.js'; + +export default class UpdateHighlightCacheTask extends BushTask { + public constructor() { + super('updateHighlightCache', { + delay: 5 * Time.Minute, + runOnStart: false + }); + } + + public async exec() { + return this.client.highlightManager.syncCache(); + } +} diff --git a/src/tasks/cache/updatePriceItemCache.ts b/src/tasks/cache/updatePriceItemCache.ts new file mode 100644 index 0000000..9809cbd --- /dev/null +++ b/src/tasks/cache/updatePriceItemCache.ts @@ -0,0 +1,27 @@ +import { BushTask, Time } from '#lib'; +import got from 'got'; +import PriceCommand, { AuctionAverages, Bazaar, LowestBIN } from '../../commands/utilities/price.js'; + +export default class UpdatePriceItemCache extends BushTask { + public constructor() { + super('updatePriceItemCache', { + delay: 10 * Time.Minute, + runOnStart: true + }); + } + + public async exec() { + const [bazaar, currentLowestBIN, averageLowestBIN, auctionAverages] = (await Promise.all( + PriceCommand.urls.map(({ url }) => got.get(url).json().catch(undefined)) + )) as [Bazaar?, LowestBIN?, LowestBIN?, AuctionAverages?]; + + const itemNames = new Set([ + ...Object.keys(averageLowestBIN ?? {}), + ...Object.keys(currentLowestBIN ?? {}), + ...Object.keys(auctionAverages ?? {}), + ...Object.keys(bazaar?.products ?? {}) + ]); + + PriceCommand.cachedItemList = [...itemNames]; + } +} |