experimental: initial autofix blast

This commit is contained in:
zkldi
2022-05-06 03:39:03 +01:00
parent 61ea7a4bf7
commit 0adc39b5cb
275 changed files with 2207 additions and 1877 deletions
+44 -27
View File
@@ -4,14 +4,19 @@
import db, { monkDB } from "external/mongo/db";
import fjsh from "fast-json-stable-hash";
import { PullDatabaseSeeds } from "lib/database-seeds/repo";
import CreateLogCtx, { KtLogger } from "lib/logger/logger";
import CreateLogCtx from "lib/logger/logger";
import UpdateIsPrimaryStatus from "lib/score-mutation/update-isprimary";
import { TachiConfig } from "lib/setup/config";
import { RemoveStaleFolderShowcaseStats } from "lib/showcase/showcase";
import { UpdateMilestoneSubscriptions } from "lib/targets/milestones";
import { BulkWriteOperation } from "mongodb";
import { ICollection } from "monk";
import {
import { RecalcAllScores } from "utils/calculations/recalc-scores";
import { UpdateGameSongIDCounter } from "utils/db";
import { InitaliseFolderChartLookup } from "utils/folder";
import { ArrayDiff, IsSupported } from "utils/misc";
import type { KtLogger } from "lib/logger/logger";
import type { BulkWriteOperation } from "mongodb";
import type { ICollection } from "monk";
import type {
BMSCourseDocument,
ChartDocument,
FolderDocument,
@@ -22,31 +27,28 @@ import {
SongDocument,
TableDocument,
} from "tachi-common";
import { RecalcAllScores } from "utils/calculations/recalc-scores";
import { UpdateGameSongIDCounter } from "utils/db";
import { InitaliseFolderChartLookup } from "utils/folder";
import { ArrayDiff, IsSupported } from "utils/misc";
interface SyncInstructions {
pattern: RegExp;
handler: (
// These 'any's are necessary because generifying here kinda sucks.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
c: any[],
c: Array<any>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
collection: ICollection<any>,
collection: ICollection,
logger: KtLogger,
collectionName: string
) => Promise<unknown>;
}
async function RemoveNotPresent<T>(
documents: T[],
documents: Array<T>,
collection: ICollection<T>,
field: keyof T,
logger: KtLogger
) {
logger.verbose(`Removing all documents that are no longer present.`);
// Remove anything no longer present.
// Note that $nin is incredibly slow.
// @ts-expect-error generic system failing
@@ -62,7 +64,7 @@ async function RemoveNotPresent<T>(
}
async function GenericUpsert<T>(
documents: T[],
documents: Array<T>,
collection: ICollection<T>,
field: keyof T,
logger: KtLogger,
@@ -71,11 +73,12 @@ async function GenericUpsert<T>(
) {
logger.verbose(`Running bulkwrite.`);
const bwriteOps: BulkWriteOperation<T>[] = [];
const bwriteOps: Array<BulkWriteOperation<T>> = [];
const allExistingDocs = await collection.find({});
const map = new Map();
for (const doc of allExistingDocs) {
map.set(doc[field], doc);
}
@@ -83,6 +86,7 @@ async function GenericUpsert<T>(
const changedFields = [];
let i = 0;
for (const document of documents) {
i++;
if (i % 10_000 === 0) {
@@ -139,11 +143,11 @@ async function GenericUpsert<T>(
};
}
const syncInstructions: SyncInstructions[] = [
const syncInstructions: Array<SyncInstructions> = [
{
pattern: /^charts-(b|p)ms/u,
handler: async (
charts: ChartDocument[],
charts: Array<ChartDocument>,
collection: ICollection<ChartDocument>,
logger,
collectionName
@@ -165,7 +169,7 @@ const syncInstructions: SyncInstructions[] = [
{
pattern: /^charts-/u,
handler: async (
charts: ChartDocument[],
charts: Array<ChartDocument>,
collection: ICollection<ChartDocument>,
logger
) => {
@@ -183,7 +187,11 @@ const syncInstructions: SyncInstructions[] = [
},
{
pattern: /^songs-(b|p)ms/u,
handler: async (songs: SongDocument[], collection: ICollection<SongDocument>, logger) => {
handler: async (
songs: Array<SongDocument>,
collection: ICollection<SongDocument>,
logger
) => {
const r = await GenericUpsert(songs, collection, "id", logger, false);
if (r.thingsChanged) {
@@ -195,7 +203,11 @@ const syncInstructions: SyncInstructions[] = [
},
{
pattern: /^songs-/u,
handler: async (songs: SongDocument[], collection: ICollection<SongDocument>, logger) => {
handler: async (
songs: Array<SongDocument>,
collection: ICollection<SongDocument>,
logger
) => {
const r = await GenericUpsert(songs, collection, "id", logger, true);
if (r.thingsChanged) {
@@ -208,7 +220,7 @@ const syncInstructions: SyncInstructions[] = [
{
pattern: /^folders/u,
handler: async (
folders: FolderDocument[],
folders: Array<FolderDocument>,
collection: ICollection<FolderDocument>,
logger
) => {
@@ -223,7 +235,7 @@ const syncInstructions: SyncInstructions[] = [
if (r) {
await InitaliseFolderChartLookup();
const allModifiedFolderIDs = r.changedFields as string[];
const allModifiedFolderIDs = r.changedFields as Array<string>;
const keptFolderIDs = await db.folders.find(
{
@@ -245,7 +257,7 @@ const syncInstructions: SyncInstructions[] = [
},
{
pattern: /^tables/u,
handler: (tables: TableDocument[], collection: ICollection<TableDocument>, logger) =>
handler: (tables: Array<TableDocument>, collection: ICollection<TableDocument>, logger) =>
GenericUpsert(
tables.filter((e) => TachiConfig.GAMES.includes(e.game)),
collection,
@@ -257,7 +269,7 @@ const syncInstructions: SyncInstructions[] = [
{
pattern: /^bms-course-lookup/u,
handler: async (
bmsCourseDocuments: BMSCourseDocument[],
bmsCourseDocuments: Array<BMSCourseDocument>,
collection: ICollection<BMSCourseDocument>,
logger
) => {
@@ -270,7 +282,11 @@ const syncInstructions: SyncInstructions[] = [
},
{
pattern: /^goals/u,
handler: async (goals: GoalDocument[], collection: ICollection<GoalDocument>, logger) => {
handler: async (
goals: Array<GoalDocument>,
collection: ICollection<GoalDocument>,
logger
) => {
// never remove goals. Never update goals either. Only insert new ones as
// they come in.
await GenericUpsert(
@@ -286,7 +302,7 @@ const syncInstructions: SyncInstructions[] = [
{
pattern: /^milestone-sets/u,
handler: async (
milestoneSets: MilestoneSetDocument[],
milestoneSets: Array<MilestoneSetDocument>,
collection: ICollection<MilestoneSetDocument>,
logger
) => {
@@ -302,7 +318,7 @@ const syncInstructions: SyncInstructions[] = [
{
pattern: /^milestones/u,
handler: async (
milestones: MilestoneDocument[],
milestones: Array<MilestoneDocument>,
collection: ICollection<MilestoneDocument>,
logger
) => {
@@ -315,7 +331,7 @@ const syncInstructions: SyncInstructions[] = [
);
if (r.thingsChanged) {
const affectedMilestoneIDs = r.changedFields as string[];
const affectedMilestoneIDs = r.changedFields as Array<string>;
await Promise.all(affectedMilestoneIDs.map((e) => UpdateMilestoneSubscriptions(e)));
}
@@ -348,8 +364,9 @@ async function SynchroniseDBWithSeeds() {
spawnLogger.verbose(`Found ${data.length} documents.`);
let matchedSomething = false;
for (const syncInst of syncInstructions) {
if (collectionName.match(syncInst.pattern)) {
if (syncInst.pattern.exec(collectionName)) {
spawnLogger.info(`Starting handler...`);
await syncInst.handler(
data,