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