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,50 +121,64 @@ function removePossibleAnswersDuplicates(path) {
count++ count++
}) })
iterateDir(path, (currPath) => { dirs.forEach((currDir) => {
currIndex++ const contents = fs.readdirSync(path + '/' + currDir)
if (currPath.includes('savedQuestions.json')) { let removed = 0
return log(
} `Processing ${C('green')}${currDir}${C()} (${contents.length} files) ...`
if (!utils.FileExists(currPath)) { )
return
} contents.forEach((currFile) => {
const currData = utils.ReadJSON(currPath) const currPath = path + '/' + currDir + '/' + currFile
currData.questions.forEach((q1) => { if (currPath.includes('savedQuestions.json')) {
iterateDir(path, (currPath2) => { return
if (currPath === currPath2) { }
return if (!utils.FileExists(currPath)) {
} return
}
const dataA = utils.ReadJSON(currPath)
currIndex++
printProgressBar(currIndex, count)
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)
dataB.questions.some((q2) => { dataA.questions.forEach((q1) => {
const percent = compareQuestionObj( dataB.questions.some((q2) => {
createQuestion(q1), const percent = compareQuestionObj(
'', createQuestion(q1),
createQuestion(q2), '',
'' createQuestion(q2),
) ''
if (percent.avg === 100) { )
utils.deleteFile(currPath2) if (percent.avg === 100) {
count-- utils.deleteFile(currPath2)
delets++ count--
return true delets++
} removed++
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,7 +252,9 @@ function updateSavedQuestionsFile(path) {
return utils.FileExists(path + '/' + sq.fname) return utils.FileExists(path + '/' + sq.fname)
}) })
utils.WriteFile(JSON.stringify(filtered), filePath) if (savedQuestions.length !== filtered.length) {
utils.WriteFile(JSON.stringify(filtered), filePath)
}
} }
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------