rm duplicates: possible questions removing improvements

This commit is contained in:
mrfry 2021-04-25 10:59:43 +02:00
parent 0aa9e209c1
commit 68447ba0c8

View file

@ -64,7 +64,11 @@ if (stat.isDirectory()) {
)}${pathB}${C()} db` )}${pathB}${C()} db`
) )
} else { } else {
log(
`Removing possible question duplicates from ${C('green')}${pathA}${C()}`
)
removePossibleAnswersDuplicates(pathA) removePossibleAnswersDuplicates(pathA)
log(`Removed possible question duplicates from ${C('green')}${pathA}${C()}`)
} }
} else { } else {
console.time('load') console.time('load')
@ -108,6 +112,7 @@ if (stat.isDirectory()) {
// TODO: dont check every file, only check per directorires // TODO: dont check every file, only check per directorires
// only compare questions of same subjects // only compare questions of same subjects
function removePossibleAnswersDuplicates(path) { function removePossibleAnswersDuplicates(path) {
const dirs = fs.readdirSync(path)
let count = 0 let count = 0
let currIndex = 1 let currIndex = 1
let delets = 0 let delets = 0
@ -116,28 +121,40 @@ function removePossibleAnswersDuplicates(path) {
count++ count++
}) })
iterateDir(path, (currPath) => { dirs.forEach((currDir) => {
currIndex++ const contents = fs.readdirSync(path + '/' + currDir)
let removed = 0
log(
`Processing ${C('green')}${currDir}${C()} (${contents.length} files) ...`
)
contents.forEach((currFile) => {
const currPath = path + '/' + currDir + '/' + currFile
if (currPath.includes('savedQuestions.json')) { if (currPath.includes('savedQuestions.json')) {
return return
} }
if (!utils.FileExists(currPath)) { if (!utils.FileExists(currPath)) {
return return
} }
const currData = utils.ReadJSON(currPath) const dataA = utils.ReadJSON(currPath)
currData.questions.forEach((q1) => {
iterateDir(path, (currPath2) => { currIndex++
if (currPath === currPath2) { printProgressBar(currIndex, count)
return
} contents.forEach((currFile2) => {
const currPath2 = path + '/' + currDir + '/' + currFile2
if (currPath2.includes('savedQuestions.json')) { if (currPath2.includes('savedQuestions.json')) {
return return
} }
if (!utils.FileExists(currPath)) { if (!utils.FileExists(currPath2)) {
return
}
if (currPath === currPath2) {
return return
} }
const dataB = utils.ReadJSON(currPath2) const dataB = utils.ReadJSON(currPath2)
dataA.questions.forEach((q1) => {
dataB.questions.some((q2) => { dataB.questions.some((q2) => {
const percent = compareQuestionObj( const percent = compareQuestionObj(
createQuestion(q1), createQuestion(q1),
@ -149,17 +166,19 @@ function removePossibleAnswersDuplicates(path) {
utils.deleteFile(currPath2) utils.deleteFile(currPath2)
count-- count--
delets++ delets++
removed++
return true return true
} }
}) })
}) })
}) })
})
printProgressBar(currIndex, count) log(`Removed ${C('red')}${removed}${C()} files`)
hr()
}) })
log(`${C('green')}Deleting empty directories ...${C()}`) log(`${C('green')}Deleting empty directories ...${C()}`)
const dirs = fs.readdirSync(path)
count = dirs.length count = dirs.length
currIndex = 0 currIndex = 0
let deletedDirCount = 0 let deletedDirCount = 0
@ -233,8 +252,10 @@ function updateSavedQuestionsFile(path) {
return utils.FileExists(path + '/' + sq.fname) return utils.FileExists(path + '/' + sq.fname)
}) })
if (savedQuestions.length !== filtered.length) {
utils.WriteFile(JSON.stringify(filtered), filePath) utils.WriteFile(JSON.stringify(filtered), filePath)
} }
}
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
// difference // difference