refactor score calculations for performance

master
Matthew 2021-10-09 20:18:27 -04:00
parent ad6f05409f
commit 5e8a6c8837
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
2 changed files with 206 additions and 208 deletions

View File

@ -183,6 +183,9 @@ export default class Queue {
return this.queues.score.add('score::inquiry', { inqID, userID, name, type, reason }); return this.queues.score.add('score::inquiry', { inqID, userID, name, type, reason });
} }
/**
* @deprecated
*/
public updateScore(score: ScoreInterface, total: number, activity: number, roles: number, moderation: number, cloudServices: number, other: number, staff: number) { public updateScore(score: ScoreInterface, total: number, activity: number, roles: number, moderation: number, cloudServices: number, other: number, staff: number) {
return this.queues.score.add('score::update', { score, total, activity, roles, moderation, cloudServices, other, staff }); return this.queues.score.add('score::update', { score, total, activity, roles, moderation, cloudServices, other, staff });
} }

View File

@ -2,19 +2,9 @@
/* eslint-disable no-continue */ /* eslint-disable no-continue */
/* eslint-disable one-var-declaration-per-line */ /* eslint-disable one-var-declaration-per-line */
/* eslint-disable no-await-in-loop */ /* eslint-disable no-await-in-loop */
import axios from 'axios';
import { Message, TextChannel } from 'eris'; import { Message, TextChannel } from 'eris';
import { Client } from '../class'; import { Client } from '../class';
import { CloudServicesUtil } from '../util';
interface CSResponse {
found: boolean,
tier: number,
totalReferrals?: number,
createdAt?: Date,
warns?: Date[],
locks?: Date[],
deletes?: Date[],
}
let interval: NodeJS.Timeout; let interval: NodeJS.Timeout;
@ -116,8 +106,8 @@ export default async function calculateScore(client: Client): Promise<NodeJS.Tim
if (activity > (Math.log1p(getTotalMessageCount(client)) * 12)) activity = Math.floor((Math.log1p(getTotalMessageCount(client)) * 12)); if (activity > (Math.log1p(getTotalMessageCount(client)) * 12)) activity = Math.floor((Math.log1p(getTotalMessageCount(client)) * 12));
if (member.roles.includes('446104438969466890') || member.roles.includes('701481967149121627')) staff = 20; if (member.roles.includes('446104438969466890') || member.roles.includes('701481967149121627')) staff = 20;
const response = <CSResponse> (await axios.get(`https://api.cloud.libraryofcode.org/wh/score?id=${member.user.id}&authorization=${client.config.internalKey}`)).data; const response = await CloudServicesUtil.fetchAccountStatus(member.user.id, client.config.internalKey);
if (response.found === true) { if (response || response.found === true) {
let negatives = 0; let negatives = 0;
let positives = 0; let positives = 0;
if (response.createdAt) { if (response.createdAt) {
@ -197,12 +187,17 @@ export default async function calculateScore(client: Client): Promise<NodeJS.Tim
total = Math.floor(((total + activity + roles + moderation + cloudServices + staff + other) * 5.13) * 1.87); total = Math.floor(((total + activity + roles + moderation + cloudServices + staff + other) * 5.13) * 1.87);
client.queue.updateScore(score, total, activity, roles, moderation, cloudServices, other, staff); // client.queue.updateScore(score, total, activity, roles, moderation, cloudServices, other, staff);
client.db.Score.updateOne({ userID: score.userID }, { $set: { total, activity, roles, moderation, cloudServices, other, staff, lastUpdate: new Date() } }).exec();
if (!score.pin || score.pin?.length < 1) {
client.db.Score.updateOne({ userID: score.userID }, { $set: { pin: [this.client.util.randomNumber(100, 999), this.client.util.randomNumber(10, 99), this.client.util.randomNumber(1000, 9999)] } }).exec();
}
} }
}; };
await start(); await start();
interval = setInterval(async () => { interval = setInterval(async () => {
await start(); await start();
}, 1800000); }, 3600000);
return interval; return interval;
} }