mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Question adding fix, rmDuplicates script update/fix
This commit is contained in:
parent
98451bd5db
commit
c6ee4efbe4
3 changed files with 61 additions and 46 deletions
|
@ -118,7 +118,7 @@ function ProcessIncomingRequest(recievedData, qdb, infos, dryRun) {
|
|||
3
|
||||
)
|
||||
logger.DebugLog(currentQuestion, 'actions', 3)
|
||||
addQuestion(qdb, sName, currentQuestion)
|
||||
qdb = addQuestion(qdb, sName, currentQuestion)
|
||||
})
|
||||
|
||||
currWrites++
|
||||
|
|
|
@ -348,7 +348,7 @@ function addQuestion(data, subj, question) {
|
|||
result = [
|
||||
...data,
|
||||
{
|
||||
name: subj,
|
||||
Name: subj,
|
||||
Questions: [question],
|
||||
},
|
||||
]
|
||||
|
|
|
@ -19,7 +19,11 @@
|
|||
------------------------------------------------------------------------- */
|
||||
|
||||
const utils = require('./utils.js')
|
||||
const classes = require('./classes.js')
|
||||
const {
|
||||
searchData,
|
||||
addQuestion,
|
||||
getSubjNameWithoutYear,
|
||||
} = require('./classes.js')
|
||||
const actions = require('./actions.js')
|
||||
const logger = require('./logger.js')
|
||||
|
||||
|
@ -98,8 +102,8 @@ function LogStats(stats, oldData, newData) {
|
|||
}
|
||||
|
||||
function LogDataCount(data) {
|
||||
const subjLength = data.Subjects.length
|
||||
const qLength = data.Subjects.reduce((acc, subj) => {
|
||||
const subjLength = data.length
|
||||
const qLength = data.reduce((acc, subj) => {
|
||||
return acc + subj.Questions.length
|
||||
}, 0)
|
||||
|
||||
|
@ -116,9 +120,9 @@ function LogDataCount(data) {
|
|||
}
|
||||
|
||||
function PrintDB(data) {
|
||||
const maxSubjNameLength = MaxLengthOf(data.Subjects, 'Name')
|
||||
const maxSubjNameLength = MaxLengthOf(data, 'Name')
|
||||
|
||||
data.Subjects.forEach((subj) => {
|
||||
data.forEach((subj) => {
|
||||
let toLog = ''
|
||||
toLog += C('green')
|
||||
toLog += GetExactLength(subj.Name, maxSubjNameLength)
|
||||
|
@ -136,8 +140,8 @@ function PrintDB(data) {
|
|||
console.log(hr())
|
||||
}
|
||||
|
||||
function GetExactLength(s, length) {
|
||||
let toLog = s.toString()
|
||||
function GetExactLength(string, length) {
|
||||
let toLog = string.toString()
|
||||
const lengthDiff = length - toLog.length
|
||||
for (let i = 0; i < lengthDiff; i++) {
|
||||
toLog += ' '
|
||||
|
@ -157,40 +161,47 @@ function MaxLengthOf(prop, key) {
|
|||
|
||||
function RemoveDuplicates(data) {
|
||||
console.log(C('yellow') + 'Removing duplicates' + C())
|
||||
const res = new classes.QuestionDB()
|
||||
let res = []
|
||||
const stats = []
|
||||
|
||||
data.Subjects.forEach((subj, i) => {
|
||||
data.forEach((subj, i) => {
|
||||
const logFile =
|
||||
logPath + '/' + subj.Name.replace(/ /g, '_').replace(/\//g, '-')
|
||||
LogSubjProgress(i, subj, data.Subjects.length)
|
||||
let addedQuestions = 0
|
||||
let removedQuestions = 0
|
||||
subj.Questions.forEach((question) => {
|
||||
subj.Questions.forEach((question, j) => {
|
||||
// Searching for same question in result database
|
||||
let r = res.Search(question).reduce((acc, r) => {
|
||||
if (r.match >= minMatchAmmount) {
|
||||
acc.push(r)
|
||||
let result = searchData(res, question).reduce((acc, res) => {
|
||||
if (res.match >= minMatchAmmount) {
|
||||
acc.push(res)
|
||||
}
|
||||
return acc
|
||||
}, [])
|
||||
|
||||
// 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('QUESTION', logFile)
|
||||
utils.AppendToFile(JSON.stringify(question, null, 2), logFile)
|
||||
utils.AppendToFile(hr(), logFile)
|
||||
utils.AppendToFile('SAMES', logFile)
|
||||
utils.AppendToFile(JSON.stringify(r, null, 2), logFile)
|
||||
utils.AppendToFile(JSON.stringify(result, null, 2), logFile)
|
||||
removedQuestions++
|
||||
} else {
|
||||
// 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++
|
||||
}
|
||||
LogResultProgress(
|
||||
subj,
|
||||
i,
|
||||
j,
|
||||
subj.Questions.length,
|
||||
addedQuestions,
|
||||
removedQuestions,
|
||||
data.length
|
||||
)
|
||||
})
|
||||
LogResultProgress(subj, addedQuestions, removedQuestions)
|
||||
stats.push({
|
||||
name: subj.Name,
|
||||
prevQuestions: subj.Questions.length,
|
||||
|
@ -201,8 +212,16 @@ function RemoveDuplicates(data) {
|
|||
return { res, stats }
|
||||
}
|
||||
|
||||
function LogSubjProgress(i, subj, subjCount) {
|
||||
log(
|
||||
function LogResultProgress(
|
||||
subj,
|
||||
i,
|
||||
j,
|
||||
length,
|
||||
addedQuestions,
|
||||
removedQuestions,
|
||||
subjCount
|
||||
) {
|
||||
process.stdout.write(
|
||||
'[ ' +
|
||||
C('cyan') +
|
||||
(i + 1) +
|
||||
|
@ -217,12 +236,7 @@ function LogSubjProgress(i, subj, subjCount) {
|
|||
C() +
|
||||
': ' +
|
||||
C('green') +
|
||||
subj.Questions.length
|
||||
)
|
||||
}
|
||||
|
||||
function LogResultProgress(subj, addedQuestions, removedQuestions) {
|
||||
log(
|
||||
subj.Questions.length +
|
||||
' ' +
|
||||
C('cyan') +
|
||||
'-> ' +
|
||||
|
@ -232,22 +246,23 @@ function LogResultProgress(subj, addedQuestions, removedQuestions) {
|
|||
', removed: ' +
|
||||
C('red') +
|
||||
removedQuestions +
|
||||
C() +
|
||||
'\n'
|
||||
C()
|
||||
)
|
||||
}
|
||||
|
||||
function log(msg) {
|
||||
process.stdout.write(msg)
|
||||
if (j === length - 1) {
|
||||
process.stdout.write('\n')
|
||||
} else {
|
||||
process.stdout.write('\r')
|
||||
}
|
||||
}
|
||||
|
||||
function hr(char) {
|
||||
let h = ''
|
||||
let hr = ''
|
||||
const cols = process.stdout.columns || 20
|
||||
for (let i = 0; i < cols; i++) {
|
||||
h += char || '-'
|
||||
hr += char || '-'
|
||||
}
|
||||
return h
|
||||
return hr
|
||||
}
|
||||
|
||||
function C(color) {
|
||||
|
@ -259,18 +274,18 @@ function GetParams() {
|
|||
}
|
||||
|
||||
function GetDateString() {
|
||||
const m = new Date()
|
||||
const d =
|
||||
m.getFullYear() +
|
||||
const date = new Date()
|
||||
const dateString =
|
||||
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)
|
||||
return d
|
||||
('0' + date.getSeconds()).slice(-2)
|
||||
return dateString
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue