Question adding fix, rmDuplicates script update/fix

This commit is contained in:
mrfry 2020-10-06 17:04:38 +02:00
parent 98451bd5db
commit c6ee4efbe4
3 changed files with 61 additions and 46 deletions

View file

@ -118,7 +118,7 @@ function ProcessIncomingRequest(recievedData, qdb, infos, dryRun) {
3 3
) )
logger.DebugLog(currentQuestion, 'actions', 3) logger.DebugLog(currentQuestion, 'actions', 3)
addQuestion(qdb, sName, currentQuestion) qdb = addQuestion(qdb, sName, currentQuestion)
}) })
currWrites++ currWrites++

View file

@ -348,7 +348,7 @@ function addQuestion(data, subj, question) {
result = [ result = [
...data, ...data,
{ {
name: subj, Name: subj,
Questions: [question], Questions: [question],
}, },
] ]

View file

@ -19,7 +19,11 @@
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
const utils = require('./utils.js') const utils = require('./utils.js')
const classes = require('./classes.js') const {
searchData,
addQuestion,
getSubjNameWithoutYear,
} = require('./classes.js')
const actions = require('./actions.js') const actions = require('./actions.js')
const logger = require('./logger.js') const logger = require('./logger.js')
@ -98,8 +102,8 @@ function LogStats(stats, oldData, newData) {
} }
function LogDataCount(data) { function LogDataCount(data) {
const subjLength = data.Subjects.length const subjLength = data.length
const qLength = data.Subjects.reduce((acc, subj) => { const qLength = data.reduce((acc, subj) => {
return acc + subj.Questions.length return acc + subj.Questions.length
}, 0) }, 0)
@ -116,9 +120,9 @@ function LogDataCount(data) {
} }
function PrintDB(data) { function PrintDB(data) {
const maxSubjNameLength = MaxLengthOf(data.Subjects, 'Name') const maxSubjNameLength = MaxLengthOf(data, 'Name')
data.Subjects.forEach((subj) => { data.forEach((subj) => {
let toLog = '' let toLog = ''
toLog += C('green') toLog += C('green')
toLog += GetExactLength(subj.Name, maxSubjNameLength) toLog += GetExactLength(subj.Name, maxSubjNameLength)
@ -136,8 +140,8 @@ function PrintDB(data) {
console.log(hr()) console.log(hr())
} }
function GetExactLength(s, length) { function GetExactLength(string, length) {
let toLog = s.toString() let toLog = string.toString()
const lengthDiff = length - toLog.length const lengthDiff = length - toLog.length
for (let i = 0; i < lengthDiff; i++) { for (let i = 0; i < lengthDiff; i++) {
toLog += ' ' toLog += ' '
@ -157,40 +161,47 @@ function MaxLengthOf(prop, key) {
function RemoveDuplicates(data) { function RemoveDuplicates(data) {
console.log(C('yellow') + 'Removing duplicates' + C()) console.log(C('yellow') + 'Removing duplicates' + C())
const res = new classes.QuestionDB() let res = []
const stats = [] const stats = []
data.Subjects.forEach((subj, i) => { data.forEach((subj, i) => {
const logFile = const logFile =
logPath + '/' + subj.Name.replace(/ /g, '_').replace(/\//g, '-') logPath + '/' + subj.Name.replace(/ /g, '_').replace(/\//g, '-')
LogSubjProgress(i, subj, data.Subjects.length)
let addedQuestions = 0 let addedQuestions = 0
let removedQuestions = 0 let removedQuestions = 0
subj.Questions.forEach((question) => { subj.Questions.forEach((question, j) => {
// Searching for same question in result database // Searching for same question in result database
let r = res.Search(question).reduce((acc, r) => { let result = searchData(res, question).reduce((acc, res) => {
if (r.match >= minMatchAmmount) { if (res.match >= minMatchAmmount) {
acc.push(r) acc.push(res)
} }
return acc return acc
}, []) }, [])
// if htere are more that one same questions in the new database // if htere are more that one same questions in the new database
if (r.length > 0) { if (result.length > 0) {
utils.AppendToFile(hr('#'), logFile) utils.AppendToFile(hr('#'), logFile)
utils.AppendToFile('QUESTION', logFile) utils.AppendToFile('QUESTION', logFile)
utils.AppendToFile(JSON.stringify(question, null, 2), logFile) utils.AppendToFile(JSON.stringify(question, null, 2), logFile)
utils.AppendToFile(hr(), logFile) utils.AppendToFile(hr(), logFile)
utils.AppendToFile('SAMES', logFile) utils.AppendToFile('SAMES', logFile)
utils.AppendToFile(JSON.stringify(r, null, 2), logFile) utils.AppendToFile(JSON.stringify(result, null, 2), logFile)
removedQuestions++ removedQuestions++
} else { } else {
// if no same questions are fount then adding it to then new db // if no same questions are fount then adding it to then new db
res.AddQuestion(subj.getSubjNameWithoutYear(), question) res = addQuestion(res, getSubjNameWithoutYear(subj.Name), question)
addedQuestions++ addedQuestions++
} }
LogResultProgress(
subj,
i,
j,
subj.Questions.length,
addedQuestions,
removedQuestions,
data.length
)
}) })
LogResultProgress(subj, addedQuestions, removedQuestions)
stats.push({ stats.push({
name: subj.Name, name: subj.Name,
prevQuestions: subj.Questions.length, prevQuestions: subj.Questions.length,
@ -201,8 +212,16 @@ function RemoveDuplicates(data) {
return { res, stats } return { res, stats }
} }
function LogSubjProgress(i, subj, subjCount) { function LogResultProgress(
log( subj,
i,
j,
length,
addedQuestions,
removedQuestions,
subjCount
) {
process.stdout.write(
'[ ' + '[ ' +
C('cyan') + C('cyan') +
(i + 1) + (i + 1) +
@ -217,12 +236,7 @@ function LogSubjProgress(i, subj, subjCount) {
C() + C() +
': ' + ': ' +
C('green') + C('green') +
subj.Questions.length subj.Questions.length +
)
}
function LogResultProgress(subj, addedQuestions, removedQuestions) {
log(
' ' + ' ' +
C('cyan') + C('cyan') +
'-> ' + '-> ' +
@ -232,22 +246,23 @@ function LogResultProgress(subj, addedQuestions, removedQuestions) {
', removed: ' + ', removed: ' +
C('red') + C('red') +
removedQuestions + removedQuestions +
C() + C()
'\n'
) )
}
function log(msg) { if (j === length - 1) {
process.stdout.write(msg) process.stdout.write('\n')
} else {
process.stdout.write('\r')
}
} }
function hr(char) { function hr(char) {
let h = '' let hr = ''
const cols = process.stdout.columns || 20 const cols = process.stdout.columns || 20
for (let i = 0; i < cols; i++) { for (let i = 0; i < cols; i++) {
h += char || '-' hr += char || '-'
} }
return h return hr
} }
function C(color) { function C(color) {
@ -259,18 +274,18 @@ function GetParams() {
} }
function GetDateString() { function GetDateString() {
const m = new Date() const date = new Date()
const d = const dateString =
m.getFullYear() + date.getFullYear() +
'-' + '-' +
('0' + (m.getMonth() + 1)).slice(-2) + ('0' + (date.getMonth() + 1)).slice(-2) +
'-' + '-' +
('0' + m.getDate()).slice(-2) + ('0' + date.getDate()).slice(-2) +
' ' + ' ' +
('0' + m.getHours()).slice(-2) + ('0' + date.getHours()).slice(-2) +
':' + ':' +
('0' + m.getMinutes()).slice(-2) + ('0' + date.getMinutes()).slice(-2) +
':' + ':' +
('0' + m.getSeconds()).slice(-2) ('0' + date.getSeconds()).slice(-2)
return d return dateString
} }