mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-03-11 06:05:16 +08:00
renamed
This commit is contained in:
parent
88d1529706
commit
44c75552ad
2 changed files with 85 additions and 77 deletions
85
backend/worker.js
Normal file
85
backend/worker.js
Normal file
|
@ -0,0 +1,85 @@
|
|||
const { config } = require("dotenv");
|
||||
const path = require("path");
|
||||
config({ path: path.join(__dirname, ".env") });
|
||||
|
||||
const admin = require("firebase-admin");
|
||||
const serviceAccount = require("./credentials/serviceAccountKey.json");
|
||||
|
||||
const { MongoClient } = require("mongodb");
|
||||
|
||||
class DatabaseClient {
|
||||
static mongoClient = null;
|
||||
static db = null;
|
||||
static collections = {};
|
||||
static connected = false;
|
||||
static async connect() {
|
||||
const {
|
||||
DB_USERNAME,
|
||||
DB_PASSWORD,
|
||||
DB_AUTH_MECHANISM,
|
||||
DB_AUTH_SOURCE,
|
||||
DB_URI,
|
||||
DB_NAME,
|
||||
} = process.env;
|
||||
const connectionOptions = {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
connectTimeoutMS: 2000,
|
||||
serverSelectionTimeoutMS: 2000,
|
||||
};
|
||||
if (DB_USERNAME && DB_PASSWORD) {
|
||||
connectionOptions.auth = {
|
||||
username: DB_USERNAME,
|
||||
password: DB_PASSWORD,
|
||||
};
|
||||
}
|
||||
if (DB_AUTH_MECHANISM) {
|
||||
connectionOptions.authMechanism = DB_AUTH_MECHANISM;
|
||||
}
|
||||
if (DB_AUTH_SOURCE) {
|
||||
connectionOptions.authSource = DB_AUTH_SOURCE;
|
||||
}
|
||||
this.mongoClient = new MongoClient(DB_URI, connectionOptions);
|
||||
try {
|
||||
await this.mongoClient.connect();
|
||||
this.db = this.mongoClient.db(DB_NAME);
|
||||
this.connected = true;
|
||||
} catch (error) {
|
||||
console.error(error.message);
|
||||
console.error(
|
||||
"Failed to connect to database. Exiting with exit status code 1."
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
static async close() {
|
||||
if (this.connected) {
|
||||
await this.mongoClient.close();
|
||||
}
|
||||
}
|
||||
static collection(collectionName) {
|
||||
if (!this.connected) {
|
||||
return null;
|
||||
}
|
||||
if (!(collectionName in this.collections)) {
|
||||
this.collections[collectionName] = this.db.collection(collectionName);
|
||||
}
|
||||
return this.collections[collectionName];
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await DatabaseClient.connect();
|
||||
await admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
});
|
||||
console.log("Database Connected!!");
|
||||
run();
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
function run() {
|
||||
//
|
||||
console.log("test");
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
import path from "path";
|
||||
// @ts-ignore
|
||||
import serviceAccount from "./credentials/serviceAccountKey.json"; // eslint-disable-line require-path-exists/exists
|
||||
import admin, { ServiceAccount } from "firebase-admin";
|
||||
import db from "./init/db";
|
||||
import { config } from "dotenv";
|
||||
|
||||
config({ path: path.join(__dirname, ".env") });
|
||||
|
||||
async function main(): Promise<void> {
|
||||
await db.connect();
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(
|
||||
serviceAccount as unknown as ServiceAccount
|
||||
),
|
||||
});
|
||||
console.log("Database Connected!!");
|
||||
refactor();
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
async function refactor(): Promise<void> {
|
||||
console.log("getting all users");
|
||||
|
||||
const usersCollection = db.collection<any>("users");
|
||||
const users = await usersCollection.find({}).toArray();
|
||||
console.log(users.length);
|
||||
|
||||
for (const user of users) {
|
||||
const obj = user.personalBests;
|
||||
|
||||
const lbPb = {
|
||||
time: {
|
||||
15: {},
|
||||
60: {},
|
||||
},
|
||||
};
|
||||
let bestForEveryLanguage = {};
|
||||
if (obj?.time?.[15]) {
|
||||
obj.time[15].forEach((pb) => {
|
||||
if (!bestForEveryLanguage[pb.language]) {
|
||||
bestForEveryLanguage[pb.language] = pb;
|
||||
} else {
|
||||
if (bestForEveryLanguage[pb.language].wpm < pb.wpm) {
|
||||
bestForEveryLanguage[pb.language] = pb;
|
||||
}
|
||||
}
|
||||
});
|
||||
Object.keys(bestForEveryLanguage).forEach((key) => {
|
||||
lbPb.time[15][key] = bestForEveryLanguage[key];
|
||||
});
|
||||
bestForEveryLanguage = {};
|
||||
}
|
||||
if (obj?.time?.[60]) {
|
||||
obj.time[60].forEach((pb) => {
|
||||
if (!bestForEveryLanguage[pb.language]) {
|
||||
bestForEveryLanguage[pb.language] = pb;
|
||||
} else {
|
||||
if (bestForEveryLanguage[pb.language].wpm < pb.wpm) {
|
||||
bestForEveryLanguage[pb.language] = pb;
|
||||
}
|
||||
}
|
||||
});
|
||||
Object.keys(bestForEveryLanguage).forEach((key) => {
|
||||
lbPb.time[60][key] = bestForEveryLanguage[key];
|
||||
});
|
||||
}
|
||||
|
||||
await usersCollection.updateOne(
|
||||
{ _id: user._id },
|
||||
{ $set: { lbPersonalBests: lbPb } }
|
||||
);
|
||||
console.log(`updated ${user.name}`);
|
||||
}
|
||||
console.log("done");
|
||||
}
|
Loading…
Reference in a new issue