mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
prettier 4 tabwidth
This commit is contained in:
parent
00ec614f1d
commit
96b413a365
42 changed files with 7034 additions and 6905 deletions
|
@ -3,36 +3,36 @@ const fs = require('fs')
|
|||
const params = process.argv
|
||||
const file = params[2]
|
||||
|
||||
const data = fs.readFileSync(file,'utf8').split('\n')
|
||||
const data = fs.readFileSync(file, 'utf8').split('\n')
|
||||
console.log(data)
|
||||
|
||||
console.log("TODO: remove 'Q: ' and 'A: '")
|
||||
|
||||
let currVal = {}
|
||||
const res = data.reduce((acc, val) => {
|
||||
const formattedVal = val.replace(/\r/g, '').trim()
|
||||
const formattedVal = val.replace(/\r/g, '').trim()
|
||||
|
||||
if (formattedVal.startsWith('#')) return acc
|
||||
if (formattedVal.startsWith('Q')) {
|
||||
currVal = {
|
||||
Q: formattedVal
|
||||
}
|
||||
return acc
|
||||
}
|
||||
if (formattedVal.startsWith('A')) {
|
||||
currVal.A = formattedVal
|
||||
return [
|
||||
...acc,
|
||||
{
|
||||
...currVal,
|
||||
data: {
|
||||
type: 'simple'
|
||||
if (formattedVal.startsWith('#')) return acc
|
||||
if (formattedVal.startsWith('Q')) {
|
||||
currVal = {
|
||||
Q: formattedVal,
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
return acc
|
||||
}
|
||||
if (formattedVal.startsWith('A')) {
|
||||
currVal.A = formattedVal
|
||||
return [
|
||||
...acc,
|
||||
{
|
||||
...currVal,
|
||||
data: {
|
||||
type: 'simple',
|
||||
},
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
return acc
|
||||
return acc
|
||||
}, [])
|
||||
|
||||
console.log(res)
|
||||
|
|
|
@ -7,23 +7,23 @@ const data = JSON.parse(fs.readFileSync(file, 'utf8'))
|
|||
const res = []
|
||||
|
||||
data.forEach((subj) => {
|
||||
const questions = []
|
||||
subj.Questions.forEach((question) => {
|
||||
const res = {}
|
||||
if (question.Q) {
|
||||
res.Q = simplifyString(question.Q)
|
||||
}
|
||||
if (question.A) {
|
||||
res.A = simplifyString(question.A)
|
||||
}
|
||||
res.data = question.data
|
||||
const questions = []
|
||||
subj.Questions.forEach((question) => {
|
||||
const res = {}
|
||||
if (question.Q) {
|
||||
res.Q = simplifyString(question.Q)
|
||||
}
|
||||
if (question.A) {
|
||||
res.A = simplifyString(question.A)
|
||||
}
|
||||
res.data = question.data
|
||||
|
||||
questions.push(res)
|
||||
})
|
||||
res.push({
|
||||
Name: subj.Name,
|
||||
Questions: questions,
|
||||
})
|
||||
questions.push(res)
|
||||
})
|
||||
res.push({
|
||||
Name: subj.Name,
|
||||
Questions: questions,
|
||||
})
|
||||
})
|
||||
|
||||
fs.writeFileSync(file + '.res', JSON.stringify(res))
|
||||
|
|
|
@ -4,38 +4,38 @@ const dbtools = require('../../dist/utils/dbtools.js').default // eslint-disable
|
|||
const { v4: uuidv4 } = require('uuid') // eslint-disable-line
|
||||
|
||||
const dbStructPaths = [
|
||||
{ structPath: '../modules/api/usersDBStruct.json', name: 'users.db' },
|
||||
{ structPath: '../modules/api/msgsDbStruct.json', name: 'msgs.db' },
|
||||
{ structPath: '../modules/api/usersDBStruct.json', name: 'users.db' },
|
||||
{ structPath: '../modules/api/msgsDbStruct.json', name: 'msgs.db' },
|
||||
]
|
||||
|
||||
dbStructPaths.forEach((data) => {
|
||||
const { structPath, name } = data
|
||||
createDB(structPath, name)
|
||||
const { structPath, name } = data
|
||||
createDB(structPath, name)
|
||||
})
|
||||
|
||||
function createDB(path, name) {
|
||||
const dbStruct = utils.ReadJSON(path)
|
||||
const db = dbtools.GetDB(`./${name}`)
|
||||
db.pragma('synchronous = OFF')
|
||||
const dbStruct = utils.ReadJSON(path)
|
||||
const db = dbtools.GetDB(`./${name}`)
|
||||
db.pragma('synchronous = OFF')
|
||||
|
||||
Object.keys(dbStruct).forEach((tableName) => {
|
||||
const tableData = dbStruct[tableName]
|
||||
dbtools.CreateTable(
|
||||
db,
|
||||
tableName,
|
||||
tableData.tableStruct,
|
||||
tableData.foreignKey
|
||||
)
|
||||
})
|
||||
printDb(db, dbStruct)
|
||||
db.close()
|
||||
Object.keys(dbStruct).forEach((tableName) => {
|
||||
const tableData = dbStruct[tableName]
|
||||
dbtools.CreateTable(
|
||||
db,
|
||||
tableName,
|
||||
tableData.tableStruct,
|
||||
tableData.foreignKey
|
||||
)
|
||||
})
|
||||
printDb(db, dbStruct)
|
||||
db.close()
|
||||
|
||||
logger.Log('Done')
|
||||
logger.Log('Done')
|
||||
}
|
||||
|
||||
function printDb(db, dbStruct) {
|
||||
Object.keys(dbStruct).forEach((key) => {
|
||||
console.log(dbtools.TableInfo(db, key))
|
||||
console.log(dbtools.SelectAll(db, key))
|
||||
})
|
||||
Object.keys(dbStruct).forEach((key) => {
|
||||
console.log(dbtools.TableInfo(db, key))
|
||||
console.log(dbtools.SelectAll(db, key))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
const fs = require('fs')
|
||||
|
||||
function GetParams() {
|
||||
return process.argv.splice(2)
|
||||
return process.argv.splice(2)
|
||||
}
|
||||
const params = GetParams()
|
||||
console.log(params)
|
||||
if (params.length === 0) {
|
||||
console.error('No params! Need a path to a question database!')
|
||||
process.exit()
|
||||
console.error('No params! Need a path to a question database!')
|
||||
process.exit()
|
||||
}
|
||||
const file = params[0]
|
||||
|
||||
|
@ -16,36 +16,36 @@ const res = []
|
|||
let invalidQuestionCount = 0
|
||||
|
||||
data.forEach((subj) => {
|
||||
const questions = []
|
||||
subj.Questions.forEach((question) => {
|
||||
if (isInvalidQuestion(question)) {
|
||||
console.log(`invalid question in ${subj.Name}:`)
|
||||
console.log(question)
|
||||
invalidQuestionCount++
|
||||
} else {
|
||||
questions.push(question)
|
||||
}
|
||||
})
|
||||
res.push({
|
||||
Name: subj.Name,
|
||||
Questions: questions,
|
||||
})
|
||||
const questions = []
|
||||
subj.Questions.forEach((question) => {
|
||||
if (isInvalidQuestion(question)) {
|
||||
console.log(`invalid question in ${subj.Name}:`)
|
||||
console.log(question)
|
||||
invalidQuestionCount++
|
||||
} else {
|
||||
questions.push(question)
|
||||
}
|
||||
})
|
||||
res.push({
|
||||
Name: subj.Name,
|
||||
Questions: questions,
|
||||
})
|
||||
})
|
||||
|
||||
function isInvalidQuestion(q) {
|
||||
if (q.Q === 'Ugrás...' || q.A === 'Ugrás...') {
|
||||
return true
|
||||
}
|
||||
if (q.Q === 'Ugrás...' || q.A === 'Ugrás...') {
|
||||
return true
|
||||
}
|
||||
|
||||
if (!q.Q && !q.A) {
|
||||
return true
|
||||
}
|
||||
if (!q.Q && !q.A) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (!q.Q && q.data.type === 'simple') {
|
||||
return true
|
||||
}
|
||||
if (!q.Q && q.data.type === 'simple') {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
return false
|
||||
}
|
||||
|
||||
console.log(`${invalidQuestionCount} invalid questions, writing results...`)
|
||||
|
|
|
@ -21,16 +21,16 @@ const answer = params[2]
|
|||
|
||||
console.time('SEARCH')
|
||||
const searchRes = search({
|
||||
qdb: loadData(path),
|
||||
subjName: 'Elektronika',
|
||||
question: {
|
||||
Q: question,
|
||||
A: answer,
|
||||
data: {
|
||||
type: 'simple',
|
||||
qdb: loadData(path),
|
||||
subjName: 'Elektronika',
|
||||
question: {
|
||||
Q: question,
|
||||
A: answer,
|
||||
data: {
|
||||
type: 'simple',
|
||||
},
|
||||
},
|
||||
},
|
||||
searchTillMatchPercent: 80,
|
||||
searchTillMatchPercent: 80,
|
||||
})
|
||||
hr()
|
||||
console.log('Search result')
|
||||
|
@ -39,9 +39,9 @@ showSearchResult(searchRes)
|
|||
hr()
|
||||
console.timeEnd('SEARCH')
|
||||
log(
|
||||
`Searched for question: "${C('green')}${question}${C()}" answer: "${C(
|
||||
'green'
|
||||
)}${answer || ''}${C()}" in "${C('cyan')}${path}${C()}"`
|
||||
`Searched for question: "${C('green')}${question}${C()}" answer: "${C(
|
||||
'green'
|
||||
)}${answer || ''}${C()}" in "${C('cyan')}${path}${C()}"`
|
||||
)
|
||||
hr()
|
||||
|
||||
|
@ -50,44 +50,44 @@ hr()
|
|||
// ---------------------------------------------------------------------------------
|
||||
|
||||
function showSearchResult(res) {
|
||||
res.forEach((x) => {
|
||||
console.log(`${C('green')}Q:${C()}`, x.q.Q)
|
||||
console.log(`${C('green')}A:${C()}`, x.q.A)
|
||||
console.log(`${C('green')}match:${C()}`, x.match)
|
||||
console.log()
|
||||
})
|
||||
console.log(`Result length: ${C('green')}${res.length}${C()}`)
|
||||
res.forEach((x) => {
|
||||
console.log(`${C('green')}Q:${C()}`, x.q.Q)
|
||||
console.log(`${C('green')}A:${C()}`, x.q.A)
|
||||
console.log(`${C('green')}match:${C()}`, x.match)
|
||||
console.log()
|
||||
})
|
||||
console.log(`Result length: ${C('green')}${res.length}${C()}`)
|
||||
}
|
||||
|
||||
function search({ qdb, subjName, question, searchInAllIfNoResult }) {
|
||||
return doSearch(
|
||||
qdb,
|
||||
subjName,
|
||||
question,
|
||||
null,
|
||||
minpercent,
|
||||
searchInAllIfNoResult
|
||||
)
|
||||
return doSearch(
|
||||
qdb,
|
||||
subjName,
|
||||
question,
|
||||
null,
|
||||
minpercent,
|
||||
searchInAllIfNoResult
|
||||
)
|
||||
}
|
||||
|
||||
function hr() {
|
||||
let res = ''
|
||||
for (let i = 0; i < process.stdout.columns; i++) {
|
||||
res += '='
|
||||
}
|
||||
log(`${C('cyan')}${res}${C()}`)
|
||||
let res = ''
|
||||
for (let i = 0; i < process.stdout.columns; i++) {
|
||||
res += '='
|
||||
}
|
||||
log(`${C('cyan')}${res}${C()}`)
|
||||
}
|
||||
|
||||
function log(text) {
|
||||
utils.AppendToFile(text, globalLog)
|
||||
if (process.stdout.isTTY) {
|
||||
process.stdout.clearLine()
|
||||
process.stdout.cursorTo(0)
|
||||
}
|
||||
utils.AppendToFile(text, globalLog)
|
||||
if (process.stdout.isTTY) {
|
||||
process.stdout.clearLine()
|
||||
process.stdout.cursorTo(0)
|
||||
}
|
||||
|
||||
console.log(text)
|
||||
console.log(text)
|
||||
}
|
||||
|
||||
function C(color) {
|
||||
return logger.C(color)
|
||||
return logger.C(color)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
const utils = require('../../dist/utils/utils.js').default // eslint-disable-line
|
||||
const logger = require('../../dist/utils/logger.js').default // eslint-disable-line
|
||||
const {
|
||||
addQuestion,
|
||||
doSearch,
|
||||
compareQuestionObj,
|
||||
createQuestion,
|
||||
addQuestion,
|
||||
doSearch,
|
||||
compareQuestionObj,
|
||||
createQuestion,
|
||||
} = require('../../dist/utils/classes.js') // eslint-disable-line
|
||||
const { loadData, writeData } = require('../../dist/utils/actions.js') // eslint-disable-line
|
||||
const fs = require('fs') // eslint-disable-line
|
||||
|
@ -35,7 +35,7 @@ const fs = require('fs') // eslint-disable-line
|
|||
|
||||
const minpercent = 95
|
||||
const line =
|
||||
'===================================================================='
|
||||
'===================================================================='
|
||||
const logPath = './duplicateRemovingLog/'
|
||||
const globalLog = './duplicateRemovingLog/log'
|
||||
utils.CreatePath(logPath)
|
||||
|
@ -45,25 +45,25 @@ utils.WriteFile('', globalLog)
|
|||
let currentMaxIndex = -1
|
||||
let currentIndex = -1
|
||||
process.on('message', function () {
|
||||
process.send({
|
||||
currentMaxIndex: currentMaxIndex,
|
||||
currentIndex: currentIndex,
|
||||
})
|
||||
process.send({
|
||||
currentMaxIndex: currentMaxIndex,
|
||||
currentIndex: currentIndex,
|
||||
})
|
||||
})
|
||||
// ----------------------------------------------
|
||||
|
||||
let params = process.argv.splice(2)
|
||||
let silenced = false
|
||||
if (params.includes('-s')) {
|
||||
silenced = true
|
||||
silenced = true
|
||||
}
|
||||
params = params.filter((x) => {
|
||||
return !x.startsWith('-')
|
||||
return !x.startsWith('-')
|
||||
})
|
||||
console.log(params)
|
||||
if (params.length === 0) {
|
||||
console.log('At least 1 parameter required (path to DB)')
|
||||
process.exit(1)
|
||||
console.log('At least 1 parameter required (path to DB)')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const pathA = params[0]
|
||||
|
@ -71,61 +71,67 @@ const pathB = params[1]
|
|||
|
||||
const stat = fs.lstatSync(pathA)
|
||||
if (stat.isDirectory()) {
|
||||
if (pathB) {
|
||||
log(
|
||||
`Clearing possible questions from ${C(
|
||||
'green'
|
||||
)}${pathA}${C()} based on ${C('green')}${pathB}${C()} db`
|
||||
)
|
||||
const db = pathB ? loadData(pathB) : null
|
||||
if (pathB) {
|
||||
log(
|
||||
`Clearing possible questions from ${C(
|
||||
'green'
|
||||
)}${pathA}${C()} based on ${C('green')}${pathB}${C()} db`
|
||||
)
|
||||
const db = pathB ? loadData(pathB) : null
|
||||
|
||||
clearPossibleAnswers(pathA, db)
|
||||
clearPossibleAnswers(pathA, db)
|
||||
|
||||
log(
|
||||
`Cleared possible questions from ${C('green')}${pathA}${C()} based on ${C(
|
||||
'green'
|
||||
)}${pathB}${C()} db`
|
||||
)
|
||||
} else {
|
||||
log(
|
||||
`Removing possible question duplicates from ${C('green')}${pathA}${C()}`
|
||||
)
|
||||
removePossibleAnswersDuplicates(pathA)
|
||||
log(`Removed possible question duplicates from ${C('green')}${pathA}${C()}`)
|
||||
}
|
||||
log(
|
||||
`Cleared possible questions from ${C(
|
||||
'green'
|
||||
)}${pathA}${C()} based on ${C('green')}${pathB}${C()} db`
|
||||
)
|
||||
} else {
|
||||
log(
|
||||
`Removing possible question duplicates from ${C(
|
||||
'green'
|
||||
)}${pathA}${C()}`
|
||||
)
|
||||
removePossibleAnswersDuplicates(pathA)
|
||||
log(
|
||||
`Removed possible question duplicates from ${C(
|
||||
'green'
|
||||
)}${pathA}${C()}`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
console.time('load')
|
||||
const dbA = loadData(pathA)
|
||||
const dbB = pathB ? loadData(pathB) : null
|
||||
console.timeEnd('load')
|
||||
console.time('load')
|
||||
const dbA = loadData(pathA)
|
||||
const dbB = pathB ? loadData(pathB) : null
|
||||
console.timeEnd('load')
|
||||
|
||||
console.time('rmduplicates')
|
||||
console.time('rmduplicates')
|
||||
|
||||
if (!dbB) {
|
||||
log(`Removing duplicate questions from ${C('green')}${pathA}${C()}`)
|
||||
const resultDbFileName = pathA.split('/')[pathA.split('/').length - 1]
|
||||
const res = rmDuplicates(dbA)
|
||||
console.timeEnd('rmduplicates')
|
||||
writeData(res, resultDbFileName + '.res')
|
||||
log('File written')
|
||||
log(`Removed duplicate questions from ${C('green')}${pathA}${C()}`)
|
||||
} else {
|
||||
log(
|
||||
`Removing questions found in ${C('green')}${pathB}${C()} from ${C(
|
||||
'green'
|
||||
)}${pathA}${C()}`
|
||||
)
|
||||
const res = difference({ dbA: dbA, dbB: dbB })
|
||||
console.timeEnd('rmduplicates')
|
||||
const resultDbFileName = pathA.split('/')[pathA.split('/').length - 1]
|
||||
writeData(res, resultDbFileName + '.res')
|
||||
log('File written')
|
||||
log(
|
||||
`Removed questions found in ${C('green')}${pathB}${C()} from ${C(
|
||||
'green'
|
||||
)}${pathA}${C()}`
|
||||
)
|
||||
}
|
||||
if (!dbB) {
|
||||
log(`Removing duplicate questions from ${C('green')}${pathA}${C()}`)
|
||||
const resultDbFileName = pathA.split('/')[pathA.split('/').length - 1]
|
||||
const res = rmDuplicates(dbA)
|
||||
console.timeEnd('rmduplicates')
|
||||
writeData(res, resultDbFileName + '.res')
|
||||
log('File written')
|
||||
log(`Removed duplicate questions from ${C('green')}${pathA}${C()}`)
|
||||
} else {
|
||||
log(
|
||||
`Removing questions found in ${C('green')}${pathB}${C()} from ${C(
|
||||
'green'
|
||||
)}${pathA}${C()}`
|
||||
)
|
||||
const res = difference({ dbA: dbA, dbB: dbB })
|
||||
console.timeEnd('rmduplicates')
|
||||
const resultDbFileName = pathA.split('/')[pathA.split('/').length - 1]
|
||||
writeData(res, resultDbFileName + '.res')
|
||||
log('File written')
|
||||
log(
|
||||
`Removed questions found in ${C('green')}${pathB}${C()} from ${C(
|
||||
'green'
|
||||
)}${pathA}${C()}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------
|
||||
|
@ -135,141 +141,141 @@ if (stat.isDirectory()) {
|
|||
// TODO: dont check every file, only check per directorires
|
||||
// only compare questions of same subjects
|
||||
function removePossibleAnswersDuplicates(path) {
|
||||
const dirs = fs.readdirSync(path)
|
||||
let count = 0
|
||||
let currIndex = 1
|
||||
let delets = 0
|
||||
const dirs = fs.readdirSync(path)
|
||||
let count = 0
|
||||
let currIndex = 1
|
||||
let delets = 0
|
||||
|
||||
iterateDir(path, () => {
|
||||
count++
|
||||
})
|
||||
|
||||
dirs.forEach((currDir) => {
|
||||
const contents = fs.readdirSync(path + '/' + currDir)
|
||||
|
||||
contents.forEach((currFile) => {
|
||||
const currPath = path + '/' + currDir + '/' + currFile
|
||||
if (currPath.includes('savedQuestions.json')) {
|
||||
return
|
||||
}
|
||||
if (!utils.FileExists(currPath)) {
|
||||
return
|
||||
}
|
||||
const dataA = utils.ReadJSON(currPath)
|
||||
|
||||
currIndex++
|
||||
printProgressBar(currIndex, count - 1)
|
||||
|
||||
contents.forEach((currFile2) => {
|
||||
const currPath2 = path + '/' + currDir + '/' + currFile2
|
||||
if (currPath2.includes('savedQuestions.json')) {
|
||||
return
|
||||
}
|
||||
if (!utils.FileExists(currPath2)) {
|
||||
return
|
||||
}
|
||||
if (currPath === currPath2) {
|
||||
return
|
||||
}
|
||||
const dataB = utils.ReadJSON(currPath2)
|
||||
|
||||
dataA.questions.forEach((q1) => {
|
||||
dataB.questions.some((q2) => {
|
||||
const percent = compareQuestionObj(
|
||||
createQuestion(q1),
|
||||
'',
|
||||
createQuestion(q2),
|
||||
''
|
||||
)
|
||||
if (percent.avg === 100) {
|
||||
utils.deleteFile(currPath2)
|
||||
count--
|
||||
delets++
|
||||
return true
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
iterateDir(path, () => {
|
||||
count++
|
||||
})
|
||||
})
|
||||
|
||||
log(`${C('green')}Deleting empty directories ...${C()}`)
|
||||
count = dirs.length
|
||||
currIndex = 0
|
||||
let deletedDirCount = 0
|
||||
dirs.forEach((dir) => {
|
||||
currIndex++
|
||||
const currDirContent = fs.readdirSync(path + '/' + dir)
|
||||
if (currDirContent.length === 0) {
|
||||
fs.rmdirSync(path + '/' + dir)
|
||||
deletedDirCount++
|
||||
}
|
||||
printProgressBar(currIndex, count)
|
||||
})
|
||||
dirs.forEach((currDir) => {
|
||||
const contents = fs.readdirSync(path + '/' + currDir)
|
||||
|
||||
log(`${C('green')}Updating savedQuestions.json ...${C()}`)
|
||||
count = dirs.length
|
||||
currIndex = 0
|
||||
dirs.forEach((dir) => {
|
||||
currIndex++
|
||||
updateSavedQuestionsFile(path + '/' + dir)
|
||||
printProgressBar(currIndex, count)
|
||||
})
|
||||
contents.forEach((currFile) => {
|
||||
const currPath = path + '/' + currDir + '/' + currFile
|
||||
if (currPath.includes('savedQuestions.json')) {
|
||||
return
|
||||
}
|
||||
if (!utils.FileExists(currPath)) {
|
||||
return
|
||||
}
|
||||
const dataA = utils.ReadJSON(currPath)
|
||||
|
||||
log(
|
||||
`Deleted ${C('green')}${delets}${C()} files, and ${C(
|
||||
'green'
|
||||
)}${deletedDirCount}${C()} directories`
|
||||
)
|
||||
currIndex++
|
||||
printProgressBar(currIndex, count - 1)
|
||||
|
||||
contents.forEach((currFile2) => {
|
||||
const currPath2 = path + '/' + currDir + '/' + currFile2
|
||||
if (currPath2.includes('savedQuestions.json')) {
|
||||
return
|
||||
}
|
||||
if (!utils.FileExists(currPath2)) {
|
||||
return
|
||||
}
|
||||
if (currPath === currPath2) {
|
||||
return
|
||||
}
|
||||
const dataB = utils.ReadJSON(currPath2)
|
||||
|
||||
dataA.questions.forEach((q1) => {
|
||||
dataB.questions.some((q2) => {
|
||||
const percent = compareQuestionObj(
|
||||
createQuestion(q1),
|
||||
'',
|
||||
createQuestion(q2),
|
||||
''
|
||||
)
|
||||
if (percent.avg === 100) {
|
||||
utils.deleteFile(currPath2)
|
||||
count--
|
||||
delets++
|
||||
return true
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
log(`${C('green')}Deleting empty directories ...${C()}`)
|
||||
count = dirs.length
|
||||
currIndex = 0
|
||||
let deletedDirCount = 0
|
||||
dirs.forEach((dir) => {
|
||||
currIndex++
|
||||
const currDirContent = fs.readdirSync(path + '/' + dir)
|
||||
if (currDirContent.length === 0) {
|
||||
fs.rmdirSync(path + '/' + dir)
|
||||
deletedDirCount++
|
||||
}
|
||||
printProgressBar(currIndex, count)
|
||||
})
|
||||
|
||||
log(`${C('green')}Updating savedQuestions.json ...${C()}`)
|
||||
count = dirs.length
|
||||
currIndex = 0
|
||||
dirs.forEach((dir) => {
|
||||
currIndex++
|
||||
updateSavedQuestionsFile(path + '/' + dir)
|
||||
printProgressBar(currIndex, count)
|
||||
})
|
||||
|
||||
log(
|
||||
`Deleted ${C('green')}${delets}${C()} files, and ${C(
|
||||
'green'
|
||||
)}${deletedDirCount}${C()} directories`
|
||||
)
|
||||
}
|
||||
|
||||
function clearPossibleAnswers(path, db) {
|
||||
let count = 0
|
||||
let currIndex = 1
|
||||
let delets = 0
|
||||
iterateDir(path, () => {
|
||||
count++
|
||||
})
|
||||
|
||||
iterateDir(path, (currPath) => {
|
||||
currIndex++
|
||||
if (currPath.includes('savedQuestions.json')) {
|
||||
return
|
||||
}
|
||||
const { subj, questions } = utils.ReadJSON(currPath)
|
||||
|
||||
questions.forEach((question) => {
|
||||
const searchRes = search({
|
||||
qdb: db,
|
||||
subjName: subj,
|
||||
question: question,
|
||||
searchTillMatchPercent: 80,
|
||||
})
|
||||
if (searchRes.length > 0) {
|
||||
utils.deleteFile(currPath)
|
||||
delets++
|
||||
}
|
||||
let count = 0
|
||||
let currIndex = 1
|
||||
let delets = 0
|
||||
iterateDir(path, () => {
|
||||
count++
|
||||
})
|
||||
printProgressBar(currIndex, count)
|
||||
})
|
||||
log(`Deleted ${C('green')}${delets}${C()} files`)
|
||||
|
||||
iterateDir(path, (currPath) => {
|
||||
currIndex++
|
||||
if (currPath.includes('savedQuestions.json')) {
|
||||
return
|
||||
}
|
||||
const { subj, questions } = utils.ReadJSON(currPath)
|
||||
|
||||
questions.forEach((question) => {
|
||||
const searchRes = search({
|
||||
qdb: db,
|
||||
subjName: subj,
|
||||
question: question,
|
||||
searchTillMatchPercent: 80,
|
||||
})
|
||||
if (searchRes.length > 0) {
|
||||
utils.deleteFile(currPath)
|
||||
delets++
|
||||
}
|
||||
})
|
||||
printProgressBar(currIndex, count)
|
||||
})
|
||||
log(`Deleted ${C('green')}${delets}${C()} files`)
|
||||
}
|
||||
|
||||
function updateSavedQuestionsFile(path) {
|
||||
const filePath = path + '/' + 'savedQuestions.json'
|
||||
if (!utils.FileExists(filePath)) {
|
||||
log(`${filePath} does not exists!`)
|
||||
return
|
||||
}
|
||||
const filePath = path + '/' + 'savedQuestions.json'
|
||||
if (!utils.FileExists(filePath)) {
|
||||
log(`${filePath} does not exists!`)
|
||||
return
|
||||
}
|
||||
|
||||
const savedQuestions = utils.ReadJSON(filePath)
|
||||
const filtered = savedQuestions.filter((sq) => {
|
||||
return utils.FileExists(path + '/' + sq.fname)
|
||||
})
|
||||
const savedQuestions = utils.ReadJSON(filePath)
|
||||
const filtered = savedQuestions.filter((sq) => {
|
||||
return utils.FileExists(path + '/' + sq.fname)
|
||||
})
|
||||
|
||||
if (savedQuestions.length !== filtered.length) {
|
||||
utils.WriteFile(JSON.stringify(filtered), filePath)
|
||||
}
|
||||
if (savedQuestions.length !== filtered.length) {
|
||||
utils.WriteFile(JSON.stringify(filtered), filePath)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------
|
||||
|
@ -277,103 +283,105 @@ function updateSavedQuestionsFile(path) {
|
|||
// ---------------------------------------------------------------------------------
|
||||
|
||||
function rmDuplicates(db) {
|
||||
return difference({ dbA: db })
|
||||
return difference({ dbA: db })
|
||||
}
|
||||
|
||||
function difference({ dbA, dbB }) {
|
||||
const doingDifference = !!dbB
|
||||
// Stuff only from A
|
||||
const resultDb = []
|
||||
let dbLength = 0
|
||||
let removedTotal = 0
|
||||
let processedQuestions = 0
|
||||
const doingDifference = !!dbB
|
||||
// Stuff only from A
|
||||
const resultDb = []
|
||||
let dbLength = 0
|
||||
let removedTotal = 0
|
||||
let processedQuestions = 0
|
||||
|
||||
iterateSubjects(dbA, () => {
|
||||
dbLength++
|
||||
})
|
||||
currentMaxIndex = dbLength
|
||||
|
||||
const getResultDbLength = () => {
|
||||
let resultDbLength = 0
|
||||
iterateSubjects(resultDb, () => {
|
||||
resultDbLength++
|
||||
iterateSubjects(dbA, () => {
|
||||
dbLength++
|
||||
})
|
||||
return resultDbLength
|
||||
}
|
||||
currentMaxIndex = dbLength
|
||||
|
||||
for (let i = 0; i < dbA.length; i++) {
|
||||
const subj = dbA[i]
|
||||
const subjLogPath = logPath + subj.Name
|
||||
utils.WriteFile('', subjLogPath)
|
||||
let removedCount = 0
|
||||
const getResultDbLength = () => {
|
||||
let resultDbLength = 0
|
||||
iterateSubjects(resultDb, () => {
|
||||
resultDbLength++
|
||||
})
|
||||
return resultDbLength
|
||||
}
|
||||
|
||||
for (let i = 0; i < dbA.length; i++) {
|
||||
const subj = dbA[i]
|
||||
const subjLogPath = logPath + subj.Name
|
||||
utils.WriteFile('', subjLogPath)
|
||||
let removedCount = 0
|
||||
|
||||
hr()
|
||||
log(
|
||||
`${C('blue')}${i + 1} / ${dbA.length}: ${C('green')}${
|
||||
subj.Name
|
||||
}, ${C('blue')}${subj.Questions.length}${C(
|
||||
'green'
|
||||
)} questions${C()}`
|
||||
)
|
||||
|
||||
printProgressBar(i + 1, dbA.length)
|
||||
for (let j = 0; j < subj.Questions.length; j++) {
|
||||
const question = subj.Questions[j]
|
||||
const searchRes = search({
|
||||
qdb: doingDifference ? dbB : resultDb,
|
||||
subjName: subj.Name,
|
||||
question: question,
|
||||
searchInAllIfNoResult: doingDifference,
|
||||
searchTillMatchPercent: minpercent,
|
||||
})
|
||||
|
||||
printProgressBar(processedQuestions, dbLength)
|
||||
processedQuestions++
|
||||
currentIndex = processedQuestions
|
||||
|
||||
const res = hasRequiredPercent(searchRes, minpercent)
|
||||
|
||||
// no result: adding to difference
|
||||
if (res.length === 0) {
|
||||
// no result: adding to difference
|
||||
addQuestion(resultDb, subj.Name, question)
|
||||
} else {
|
||||
// has result, not adding to difference
|
||||
utils.AppendToFile(
|
||||
line +
|
||||
'\n' +
|
||||
line +
|
||||
'\n' +
|
||||
JSON.stringify(question, null, 2) +
|
||||
'\n' +
|
||||
line +
|
||||
JSON.stringify(res, null, 2) +
|
||||
'\n',
|
||||
subjLogPath
|
||||
)
|
||||
removedCount++
|
||||
removedTotal++
|
||||
}
|
||||
}
|
||||
log(
|
||||
`${C('yellow')}Removed ${C('red')}${removedCount}${C(
|
||||
'yellow'
|
||||
)} questions${C()}`
|
||||
)
|
||||
}
|
||||
|
||||
hr()
|
||||
log(
|
||||
`${C('blue')}${i + 1} / ${dbA.length}: ${C('green')}${subj.Name}, ${C(
|
||||
'blue'
|
||||
)}${subj.Questions.length}${C('green')} questions${C()}`
|
||||
`Result length: ${getResultDbLength()}, original length: ${dbLength}, removed ${removedTotal} questions`
|
||||
)
|
||||
|
||||
printProgressBar(i + 1, dbA.length)
|
||||
for (let j = 0; j < subj.Questions.length; j++) {
|
||||
const question = subj.Questions[j]
|
||||
const searchRes = search({
|
||||
qdb: doingDifference ? dbB : resultDb,
|
||||
subjName: subj.Name,
|
||||
question: question,
|
||||
searchInAllIfNoResult: doingDifference,
|
||||
searchTillMatchPercent: minpercent,
|
||||
})
|
||||
|
||||
printProgressBar(processedQuestions, dbLength)
|
||||
processedQuestions++
|
||||
currentIndex = processedQuestions
|
||||
|
||||
const res = hasRequiredPercent(searchRes, minpercent)
|
||||
|
||||
// no result: adding to difference
|
||||
if (res.length === 0) {
|
||||
// no result: adding to difference
|
||||
addQuestion(resultDb, subj.Name, question)
|
||||
} else {
|
||||
// has result, not adding to difference
|
||||
utils.AppendToFile(
|
||||
line +
|
||||
'\n' +
|
||||
line +
|
||||
'\n' +
|
||||
JSON.stringify(question, null, 2) +
|
||||
'\n' +
|
||||
line +
|
||||
JSON.stringify(res, null, 2) +
|
||||
'\n',
|
||||
subjLogPath
|
||||
)
|
||||
removedCount++
|
||||
removedTotal++
|
||||
}
|
||||
}
|
||||
log(
|
||||
`${C('yellow')}Removed ${C('red')}${removedCount}${C(
|
||||
'yellow'
|
||||
)} questions${C()}`
|
||||
)
|
||||
}
|
||||
|
||||
hr()
|
||||
log(
|
||||
`Result length: ${getResultDbLength()}, original length: ${dbLength}, removed ${removedTotal} questions`
|
||||
)
|
||||
return resultDb
|
||||
return resultDb
|
||||
}
|
||||
|
||||
function hasRequiredPercent(result, minpercent) {
|
||||
return result.reduce((acc, res) => {
|
||||
if (res.match >= minpercent) {
|
||||
acc.push(res)
|
||||
}
|
||||
return acc
|
||||
}, [])
|
||||
return result.reduce((acc, res) => {
|
||||
if (res.match >= minpercent) {
|
||||
acc.push(res)
|
||||
}
|
||||
return acc
|
||||
}, [])
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------
|
||||
|
@ -381,22 +389,22 @@ function hasRequiredPercent(result, minpercent) {
|
|||
// ---------------------------------------------------------------------------------
|
||||
|
||||
function search({ qdb, subjName, question, searchInAllIfNoResult }) {
|
||||
return doSearch(
|
||||
qdb,
|
||||
subjName,
|
||||
question,
|
||||
null,
|
||||
minpercent,
|
||||
searchInAllIfNoResult
|
||||
)
|
||||
return doSearch(
|
||||
qdb,
|
||||
subjName,
|
||||
question,
|
||||
null,
|
||||
minpercent,
|
||||
searchInAllIfNoResult
|
||||
)
|
||||
}
|
||||
|
||||
function iterateSubjects(db, fn) {
|
||||
db.forEach((subj) => {
|
||||
subj.Questions.forEach((question) => {
|
||||
fn(subj, question)
|
||||
db.forEach((subj) => {
|
||||
subj.Questions.forEach((question) => {
|
||||
fn(subj, question)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------
|
||||
|
@ -404,19 +412,19 @@ function iterateSubjects(db, fn) {
|
|||
// ---------------------------------------------------------------------------------
|
||||
|
||||
function iterateDir(path, action) {
|
||||
if (!utils.FileExists(path)) {
|
||||
return
|
||||
}
|
||||
if (!utils.FileExists(path)) {
|
||||
return
|
||||
}
|
||||
|
||||
const stat = fs.lstatSync(path)
|
||||
if (stat.isDirectory()) {
|
||||
const content = fs.readdirSync(path)
|
||||
content.forEach((currContent) => {
|
||||
iterateDir(`${path}/${currContent}`, action)
|
||||
})
|
||||
} else {
|
||||
action(path)
|
||||
}
|
||||
const stat = fs.lstatSync(path)
|
||||
if (stat.isDirectory()) {
|
||||
const content = fs.readdirSync(path)
|
||||
content.forEach((currContent) => {
|
||||
iterateDir(`${path}/${currContent}`, action)
|
||||
})
|
||||
} else {
|
||||
action(path)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------
|
||||
|
@ -424,69 +432,69 @@ function iterateDir(path, action) {
|
|||
// ---------------------------------------------------------------------------------
|
||||
|
||||
function hr() {
|
||||
let res = ''
|
||||
for (let i = 0; i < process.stdout.columns; i++) {
|
||||
res += '='
|
||||
}
|
||||
log(`${C('cyan')}${res}${C()}`)
|
||||
let res = ''
|
||||
for (let i = 0; i < process.stdout.columns; i++) {
|
||||
res += '='
|
||||
}
|
||||
log(`${C('cyan')}${res}${C()}`)
|
||||
}
|
||||
|
||||
function log(text) {
|
||||
utils.AppendToFile(text, globalLog)
|
||||
if (silenced) return
|
||||
if (process.stdout.isTTY) {
|
||||
process.stdout.clearLine()
|
||||
process.stdout.cursorTo(0)
|
||||
}
|
||||
utils.AppendToFile(text, globalLog)
|
||||
if (silenced) return
|
||||
if (process.stdout.isTTY) {
|
||||
process.stdout.clearLine()
|
||||
process.stdout.cursorTo(0)
|
||||
}
|
||||
|
||||
console.log(text)
|
||||
console.log(text)
|
||||
}
|
||||
|
||||
function writeInSameLine(text, returnToLineStart) {
|
||||
if (!process.stdout.isTTY) {
|
||||
return
|
||||
}
|
||||
process.stdout.clearLine()
|
||||
process.stdout.cursorTo(0)
|
||||
process.stdout.write(text)
|
||||
if (returnToLineStart) {
|
||||
process.stdout.write('\r')
|
||||
} else {
|
||||
process.stdout.write('\n')
|
||||
}
|
||||
if (!process.stdout.isTTY) {
|
||||
return
|
||||
}
|
||||
process.stdout.clearLine()
|
||||
process.stdout.cursorTo(0)
|
||||
process.stdout.write(text)
|
||||
if (returnToLineStart) {
|
||||
process.stdout.write('\r')
|
||||
} else {
|
||||
process.stdout.write('\n')
|
||||
}
|
||||
}
|
||||
|
||||
function printProgressBar(current, total) {
|
||||
if (!process.stdout.isTTY || silenced) {
|
||||
return
|
||||
}
|
||||
const width = process.stdout.columns - 30
|
||||
if (!process.stdout.isTTY || silenced) {
|
||||
return
|
||||
}
|
||||
const width = process.stdout.columns - 30
|
||||
|
||||
if (width <= 0) {
|
||||
return
|
||||
}
|
||||
if (width <= 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const x = width / total
|
||||
const xCurrent = Math.floor(current * x)
|
||||
const xTotal = Math.floor(total * x)
|
||||
const x = width / total
|
||||
const xCurrent = Math.floor(current * x)
|
||||
const xTotal = Math.floor(total * x)
|
||||
|
||||
let line = ''
|
||||
for (let i = 0; i < xCurrent; i++) {
|
||||
line += '='
|
||||
}
|
||||
let line = ''
|
||||
for (let i = 0; i < xCurrent; i++) {
|
||||
line += '='
|
||||
}
|
||||
|
||||
for (let i = 0; i < xTotal - xCurrent; i++) {
|
||||
line += ' '
|
||||
}
|
||||
const numbers = `${current} / ${total}`
|
||||
writeInSameLine(
|
||||
`${C('magenta')} [${line}]${C('green')} ${numbers}${C()}`,
|
||||
current !== total
|
||||
)
|
||||
for (let i = 0; i < xTotal - xCurrent; i++) {
|
||||
line += ' '
|
||||
}
|
||||
const numbers = `${current} / ${total}`
|
||||
writeInSameLine(
|
||||
`${C('magenta')} [${line}]${C('green')} ${numbers}${C()}`,
|
||||
current !== total
|
||||
)
|
||||
}
|
||||
|
||||
function C(color) {
|
||||
return logger.C(color)
|
||||
return logger.C(color)
|
||||
}
|
||||
|
||||
process.exit()
|
||||
|
|
|
@ -6,73 +6,73 @@ const dbtools = require('../utils/dbtools.js')
|
|||
Main()
|
||||
|
||||
function Main() {
|
||||
const cols = {
|
||||
uname: {
|
||||
type: 'text',
|
||||
},
|
||||
pw: {
|
||||
type: 'text',
|
||||
},
|
||||
notes: {
|
||||
type: 'text',
|
||||
},
|
||||
}
|
||||
const dbName = 'test'
|
||||
const cols = {
|
||||
uname: {
|
||||
type: 'text',
|
||||
},
|
||||
pw: {
|
||||
type: 'text',
|
||||
},
|
||||
notes: {
|
||||
type: 'text',
|
||||
},
|
||||
}
|
||||
const dbName = 'test'
|
||||
|
||||
const db = dbtools.GetDB('./testdb.db')
|
||||
const db = dbtools.GetDB('./testdb.db')
|
||||
|
||||
// Creating table
|
||||
dbtools.CreateTable(db, dbName, cols)
|
||||
console.log(dbtools.TableInfo(db, dbName))
|
||||
dbtools.SelectAll(db, dbName)
|
||||
// inserting test val to table
|
||||
dbtools.Insert(db, dbName, {
|
||||
uname: 'mrfry',
|
||||
pw: 'dsads',
|
||||
})
|
||||
// Selecting a record
|
||||
console.log(
|
||||
dbtools.Select(db, dbName, {
|
||||
uname: 'mrfry',
|
||||
// Creating table
|
||||
dbtools.CreateTable(db, dbName, cols)
|
||||
console.log(dbtools.TableInfo(db, dbName))
|
||||
dbtools.SelectAll(db, dbName)
|
||||
// inserting test val to table
|
||||
dbtools.Insert(db, dbName, {
|
||||
uname: 'mrfry',
|
||||
pw: 'dsads',
|
||||
})
|
||||
)
|
||||
console.log(dbtools.TableInfo(db, dbName))
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
// Updating record
|
||||
dbtools.Update(
|
||||
db,
|
||||
dbName,
|
||||
{
|
||||
pw: 'sspw',
|
||||
},
|
||||
{
|
||||
uname: 'mrfry',
|
||||
}
|
||||
)
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
// Updating record again
|
||||
dbtools.Update(
|
||||
db,
|
||||
dbName,
|
||||
{
|
||||
notes: 'new note!',
|
||||
},
|
||||
{
|
||||
uname: 'mrfry',
|
||||
}
|
||||
)
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
// Adding new column and
|
||||
dbtools.AddColumn(db, dbName, {
|
||||
test: 'text',
|
||||
})
|
||||
console.log(dbtools.TableInfo(db, dbName))
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
// Deleting stuff
|
||||
dbtools.Delete(db, dbName, {
|
||||
uname: 'mrfry',
|
||||
})
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
// Selecting a record
|
||||
console.log(
|
||||
dbtools.Select(db, dbName, {
|
||||
uname: 'mrfry',
|
||||
})
|
||||
)
|
||||
console.log(dbtools.TableInfo(db, dbName))
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
// Updating record
|
||||
dbtools.Update(
|
||||
db,
|
||||
dbName,
|
||||
{
|
||||
pw: 'sspw',
|
||||
},
|
||||
{
|
||||
uname: 'mrfry',
|
||||
}
|
||||
)
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
// Updating record again
|
||||
dbtools.Update(
|
||||
db,
|
||||
dbName,
|
||||
{
|
||||
notes: 'new note!',
|
||||
},
|
||||
{
|
||||
uname: 'mrfry',
|
||||
}
|
||||
)
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
// Adding new column and
|
||||
dbtools.AddColumn(db, dbName, {
|
||||
test: 'text',
|
||||
})
|
||||
console.log(dbtools.TableInfo(db, dbName))
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
// Deleting stuff
|
||||
dbtools.Delete(db, dbName, {
|
||||
uname: 'mrfry',
|
||||
})
|
||||
console.log(dbtools.SelectAll(db, dbName))
|
||||
|
||||
dbtools.CloseDB(db)
|
||||
dbtools.CloseDB(db)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue