Logging fixes / imporvements, voteTodo api

This commit is contained in:
mrfry 2020-11-29 10:04:18 +01:00
parent 4e34267d44
commit 865e97a754
6 changed files with 66 additions and 38 deletions

View file

@ -3,7 +3,6 @@ module.exports = {
browser: true, browser: true,
es6: true, es6: true,
node: true, node: true,
jest: true,
}, },
parser: '@typescript-eslint/parser', parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'], plugins: ['@typescript-eslint'],
@ -29,7 +28,8 @@ module.exports = {
'@typescript-eslint/ban-types': 'off', '@typescript-eslint/ban-types': 'off',
'id-length': [ 'id-length': [
'warn', 'warn',
{ exceptions: ['i', 'j', 't', 'Q', 'A', 'C', 'q', 'a', 'b'] }, { exceptions: ['i', 'j', 't', 'Q', 'A', 'C', 'q', 'a', 'b', 'x'] },
], ],
}, },
root: true
} }

View file

@ -128,6 +128,7 @@ function jsonStats() {
-e "s,/install,${P}&${NC},g" \ -e "s,/install,${P}&${NC},g" \
-e "s,/irc,${P}&${NC},g" \ -e "s,/irc,${P}&${NC},g" \
-e "s,/postfeedback,${P}&${NC},g" \ -e "s,/postfeedback,${P}&${NC},g" \
-e "s,/voteTodo,${P}&${NC},g" \
-e "s,/quickvote,${P}&${NC},g" \ -e "s,/quickvote,${P}&${NC},g" \
-e "s,/servergit,${P}&${NC},g" \ -e "s,/servergit,${P}&${NC},g" \
-e "s,/scriptgit,${P}&${NC},g" \ -e "s,/scriptgit,${P}&${NC},g" \

View file

@ -8,6 +8,13 @@ interface Options {
exceptions: Array<string> exceptions: Array<string>
} }
const testUser = {
id: 19,
avaiblePWRequests: 999,
pwRequestCount: 19,
created: new Date(),
}
export default function(options: Options): any { export default function(options: Options): any {
const { userDB, jsonResponse, exceptions } = options const { userDB, jsonResponse, exceptions } = options
@ -34,9 +41,7 @@ export default function(options: Options): any {
if (process.env.NS_NOUSER) { if (process.env.NS_NOUSER) {
req.session = { req.session = {
user: { user: testUser,
id: 19,
},
sessionID: sessionID || 111111111111111111, sessionID: sessionID || 111111111111111111,
isException: false, isException: false,
} }

View file

@ -274,7 +274,7 @@ function GetApp(): ModuleType {
) )
}) })
app.get('/updateTodo', (req: any, res: any) => { app.get('/voteTodo', (req: any, res: any) => {
logger.LogReq(req) logger.LogReq(req)
const userId = req.session.user.id const userId = req.session.user.id
const id = req.query.id const id = req.query.id
@ -1028,28 +1028,41 @@ function GetApp(): ModuleType {
if (req.query.q && req.query.data) { if (req.query.q && req.query.data) {
const subj = req.query.subj || '' const subj = req.query.subj || ''
const question = req.query.q const question = req.query.q
let recData: any = {} const recData: any = req.query.data
try {
recData = JSON.parse(req.query.data)
} catch (error) {
logger.Log(
`Unable to parse recieved question data! '${req.query.data}'`,
logger.GetColor('redbg')
)
}
searchDatas(questionDbs, question, subj, recData) searchDatas(questionDbs, question, subj, recData)
.then((result) => { .then((result) => {
res.json({ try {
result: result, const mergedResult = result.reduce((acc, dbRes) => {
success: true, return [...acc, ...dbRes.result]
}) }, [])
logger.DebugLog( const sortedResult = mergedResult.sort((q1, q2) => {
`Question result length: ${result.length}`, if (q1.match < q2.match) {
'ask', return 1
1 } else if (q1.match > q2.match) {
) return -1
logger.DebugLog(result, 'ask', 2) } else {
return 0
}
})
res.json({
result: sortedResult,
success: true,
})
logger.DebugLog(
`Question result length: ${result.length}`,
'ask',
1
)
logger.DebugLog(result, 'ask', 2)
} catch (err) {
console.error(err)
logger.Log(
'Error while sending ask results',
logger.GetColor('redbg')
)
}
}) })
.catch((err) => { .catch((err) => {
logger.Log('Search Data error!', logger.GetColor('redbg')) logger.Log('Search Data error!', logger.GetColor('redbg'))
@ -1099,12 +1112,15 @@ function GetApp(): ModuleType {
function getDetailedRes() { function getDetailedRes() {
return questionDbs.map((qdb) => { return questionDbs.map((qdb) => {
return qdb.data.map((subj) => { return {
return { dbName: qdb.name,
name: subj.Name, subjs: qdb.data.map((subj) => {
count: subj.Questions.length, return {
} name: subj.Name,
}) count: subj.Questions.length,
}
}),
}
}) })
} }

View file

@ -152,7 +152,7 @@ const cookieSecret = uuidv4()
app.use(cookieParser(cookieSecret)) app.use(cookieParser(cookieSecret))
app.use( app.use(
reqlogger({ reqlogger({
loggableKeywords: ['stable.user.js'], loggableKeywords: [],
loggableModules: ['dataeditor'], loggableModules: ['dataeditor'],
}) })
) )

View file

@ -112,7 +112,7 @@ function Log(msg: string | object, color?: string): void {
function LogReq( function LogReq(
req: any /*express.Request*/, req: any /*express.Request*/,
toFile?: boolean, toFile?: boolean,
sc?: string statusCode?: string
): void { ): void {
try { try {
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
@ -124,7 +124,7 @@ function LogReq(
return return
} }
let logEntry = GetRandomColor(ip) + ip + C() let logEntry = logHashed(ip)
let dl = DELIM let dl = DELIM
if (req.url.includes('lred')) { if (req.url.includes('lred')) {
dl += C('red') dl += C('red')
@ -141,7 +141,13 @@ function LogReq(
) )
} }
logEntry += logEntry +=
dl + hostname + dl + req.headers['user-agent'] + dl + req.method + dl dl +
logHashed(hostname) +
dl +
req.headers['user-agent'] +
dl +
req.method +
dl
if (req.session && req.session.user) { if (req.session && req.session.user) {
logEntry += C('cyan') + req.session.user.id + C() + dl logEntry += C('cyan') + req.session.user.id + C() + dl
@ -153,8 +159,8 @@ function LogReq(
logEntry += GetRandomColor(req.url.split('?')[0]) + req.url logEntry += GetRandomColor(req.url.split('?')[0]) + req.url
if (sc !== undefined) { if (statusCode !== undefined) {
logEntry += dl + sc logEntry += dl + statusCode
} }
logEntry += C() logEntry += C()
@ -385,7 +391,7 @@ function C(color?: string): string {
if (color === 'magenta') { if (color === 'magenta') {
return '\x1b[35m' return '\x1b[35m'
} }
if (color === 'coloryan') { if (color === 'cyan') {
return '\x1b[36m' return '\x1b[36m'
} }
return '\x1b[0m' return '\x1b[0m'