Merge branch 'stable'

# Conflicts:
#	src/services/notes.js
#	src/tools/generate_document.js
This commit is contained in:
zadam
2019-12-01 10:32:30 +01:00
7 changed files with 74 additions and 51 deletions

View File

@@ -1,48 +1,32 @@
const fs = require('fs');
const dataDir = require('../services/data_dir');
fs.unlinkSync(dataDir.DOCUMENT_PATH);
/**
* Usage: node src/tools/generate_document.js 1000
* will create 1000 new notes and some clones into a current document.db
*/
require('../entities/entity_constructor');
const optionService = require('../services/options');
const sqlInit = require('../services/sql_init');
const myScryptService = require('../services/my_scrypt');
const passwordEncryptionService = require('../services/password_encryption');
const utils = require('../services/utils');
const noteService = require('../services/notes');
const attributeService = require('../services/attributes');
const cls = require('../services/cls');
const cloningService = require('../services/cloning');
const loremIpsum = require('lorem-ipsum');
async function setUserNamePassword() {
const username = "test";
const password = "test";
await optionService.setOption('username', username);
await optionService.setOption('passwordVerificationSalt', utils.randomSecureToken(32));
await optionService.setOption('passwordDerivedKeySalt', utils.randomSecureToken(32));
const passwordVerificationKey = utils.toBase64(await myScryptService.getVerificationHash(password));
await optionService.setOption('passwordVerificationHash', passwordVerificationKey);
await passwordEncryptionService.setDataKey(password, utils.randomSecureToken(16));
await sqlInit.initDbConnection();
}
const loremIpsum = require('lorem-ipsum').loremIpsum;
const noteCount = parseInt(process.argv[2]);
if (!noteCount) {
console.error(`Please enter number of notes as program parameter.`);
process.exit(1);
}
const notes = ['root'];
function getRandomParentNoteId() {
function getRandomNoteId() {
const index = Math.floor(Math.random() * notes.length);
return notes[index];
}
async function start() {
await setUserNamePassword();
for (let i = 0; i < noteCount; i++) {
const title = loremIpsum({ count: 1, units: 'sentences', sentenceLowerBound: 1, sentenceUpperBound: 10 });
@@ -51,7 +35,7 @@ async function start() {
paragraphLowerBound: 3, paragraphUpperBound: 10, format: 'html' });
const {note} = await noteService.createNewNote({
parentNoteId: getRandomParentNoteId(),
parentNoteId: getRandomNoteId(),
title,
content,
type: 'text'
@@ -62,10 +46,10 @@ async function start() {
notes.push(note.noteId);
}
// we'll create clones for 20% of notes
for (let i = 0; i < (noteCount / 50); i++) {
const noteIdToClone = getRandomParentNoteId();
const parentNoteId = getRandomParentNoteId();
// we'll create clones for 4% of notes
for (let i = 0; i < (noteCount / 25); i++) {
const noteIdToClone = getRandomNoteId();
const parentNoteId = getRandomNoteId();
const prefix = Math.random() > 0.8 ? "prefix" : null;
const result = await cloningService.cloneNoteToParent(noteIdToClone, parentNoteId, prefix);
@@ -73,6 +57,30 @@ async function start() {
console.log(`Cloning ${i}:`, result.success ? "succeeded" : "FAILED");
}
for (let i = 0; i < noteCount; i++) {
await attributeService.createAttribute({
noteId: getRandomNoteId(),
type: 'label',
name: 'label',
value: 'value',
isInheritable: Math.random() > 0.1 // 10% are inheritable
});
console.log(`Creating label ${i}`);
}
for (let i = 0; i < noteCount; i++) {
await attributeService.createAttribute({
noteId: getRandomNoteId(),
type: 'relation',
name: 'relation',
value: getRandomNoteId(),
isInheritable: Math.random() > 0.1 // 10% are inheritable
});
console.log(`Creating relation ${i}`);
}
process.exit(0);
}