From fa7d48a3bdf60e75b8b0c048f851549ecee0cbf7 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 10 Jan 2023 18:14:48 +0000 Subject: [PATCH] Chore: Desktop: Add more ways to automatically populate the database --- packages/app-desktop/app.ts | 7 +++- .../lib/services/debug/populateDatabase.ts | 34 +++++++++++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/packages/app-desktop/app.ts b/packages/app-desktop/app.ts index 600da6c06..25dd7bae0 100644 --- a/packages/app-desktop/app.ts +++ b/packages/app-desktop/app.ts @@ -559,7 +559,12 @@ class Application extends BaseApplication { await SpellCheckerService.instance().initialize(new SpellCheckerServiceDriverNative()); - // await populateDatabase(reg.db()); + // await populateDatabase(reg.db(), { + // clearDatabase: true, + // folderCount: 1000, + // rootFolderCount: 1, + // subFolderDepth: 1, + // }); // setTimeout(() => { // console.info(CommandService.instance().commandsToMarkdownTable(this.store().getState())); diff --git a/packages/lib/services/debug/populateDatabase.ts b/packages/lib/services/debug/populateDatabase.ts index 9045e541b..ec1710132 100644 --- a/packages/lib/services/debug/populateDatabase.ts +++ b/packages/lib/services/debug/populateDatabase.ts @@ -9,6 +9,8 @@ export interface Options { tagsPerNote?: number; silent?: number; clearDatabase?: boolean; + rootFolderCount?: number; + subFolderDepth?: number; } function randomIndex(array: any[]): number { @@ -47,6 +49,8 @@ export default async function populateDatabase(db: any, options: Options = null) tagCount: 0, tagsPerNote: 0, clearDatabase: false, + rootFolderCount: 0, + subFolderDepth: 0, ...options, }; @@ -55,21 +59,45 @@ export default async function populateDatabase(db: any, options: Options = null) const createdFolderIds: string[] = []; const createdNoteIds: string[] = []; const createdTagIds: string[] = []; + const createdFolderDepths: Record = {}; + const folderDepthToId: Record = {}; + let rootFolderCount: number = 0; for (let i = 0; i < options.folderCount; i++) { const folder: any = { title: `folder${i}`, }; - const isRoot = Math.random() <= 0.1 || i === 0; + let isRoot = Math.random() <= 0.1 || i === 0; + + if (options.rootFolderCount && rootFolderCount >= options.rootFolderCount) isRoot = false; + + let depth: number = 0; if (!isRoot) { - const parentIndex = randomIndex(createdFolderIds); - folder.parent_id = createdFolderIds[parentIndex]; + let possibleFolderIds: string[] = []; + if (options.subFolderDepth) { + for (let i = 0; i < options.subFolderDepth; i++) { + if (folderDepthToId[i]) possibleFolderIds = possibleFolderIds.concat(folderDepthToId[i]); + } + } else { + possibleFolderIds = createdFolderIds; + } + + const parentIndex = randomIndex(possibleFolderIds); + const parentId = possibleFolderIds[parentIndex]; + folder.parent_id = parentId; + depth = createdFolderDepths[parentId] + 1; + } else { + rootFolderCount++; } const savedFolder = await Folder.save(folder); createdFolderIds.push(savedFolder.id); + createdFolderDepths[savedFolder.id] = depth; + + if (!folderDepthToId[depth]) folderDepthToId[depth] = []; + folderDepthToId[depth].push(savedFolder.id); if (!options.silent) console.info(`Folders: ${i} / ${options.folderCount}`); }