Skip to content

Commit

Permalink
Add accuracy rating for both phases of coding challenges
Browse files Browse the repository at this point in the history
(currently only logged to server console)
  • Loading branch information
bkimminich committed Oct 2, 2021
1 parent a5a302f commit 7a8c880
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
43 changes: 43 additions & 0 deletions lib/accuracy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2014-2021 Bjoern Kimminich.
* SPDX-License-Identifier: MIT
*/

const logger = require("./logger");
const colors = require("colors/safe");
const solves = {}

exports.storeFindItVerdict = (challengeKey, verdict: boolean) => {
storeVerdict(challengeKey, 'find it', verdict)
}

exports.storeFixItVerdict = (challengeKey, verdict: boolean) => {
storeVerdict(challengeKey, 'fix it', verdict)
}

exports.calculateFindItAccuracy = (challengeKey) => {
return calculateAccuracy(challengeKey, 'find it')
}

exports.calculateFixItAccuracy = (challengeKey) => {
return calculateAccuracy(challengeKey, 'fix it')
}

function calculateAccuracy (challengeKey, phase: string) {
let accuracy = 0
if (solves[challengeKey][phase]) {
accuracy = 1 / solves[challengeKey].attempts[phase]
}
logger.info(`Accuracy for '${phase === 'fix it' ? 'Fix It' : 'Find It'}' phase of coding challenge ${colors.cyan(challengeKey)}: ${accuracy > 0.5 ? colors.green(accuracy) : (accuracy > 0.25 ? colors.yellow(accuracy) : colors.red(accuracy))}`)
return accuracy
}

function storeVerdict (challengeKey, phase: string, verdict: boolean) {
if (!solves[challengeKey]) {
solves[challengeKey] = { 'find it': false, 'fix it': false, attempts: { 'find it': 0, 'fix it': 0 } }
}
if (!solves[challengeKey][phase]) {
solves[challengeKey][phase] = verdict
solves[challengeKey].attempts[phase]++
}
}
9 changes: 7 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const isWindows = require('is-windows')
const logger = require('./logger')
const webhook = require('./webhook')
const antiCheat = require('./antiCheat')
const accuracy = require('./accuracy')
const models = require('../models')

const months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
Expand Down Expand Up @@ -277,17 +278,21 @@ exports.thaw = (frozenObject) => {
exports.solveFindIt = async function (key: string, isRestore: boolean) {
const solvedChallenge = challenges[key]
await models.Challenge.update({ codingChallengeStatus: 1 }, { where: { key, codingChallengeStatus: { [Op.lt]: 2 } } })
logger.info(`${isRestore ? colors.grey('Restored') : colors.green('Solved')} 'Find It'-part of coding challenge ${colors.cyan(solvedChallenge.key)} (${solvedChallenge.name})`)
logger.info(`${isRestore ? colors.grey('Restored') : colors.green('Solved')} 'Find It' phase of coding challenge ${colors.cyan(solvedChallenge.key)} (${solvedChallenge.name})`)
if (!isRestore) {
accuracy.storeFindItVerdict(solvedChallenge.key, true)
accuracy.calculateFindItAccuracy(solvedChallenge.key)
antiCheat.calculateFindItCheatScore(solvedChallenge)
}
}

exports.solveFixIt = async function (key: string, isRestore: boolean) {
const solvedChallenge = challenges[key]
await models.Challenge.update({ codingChallengeStatus: 2 }, { where: { key } })
logger.info(`${isRestore ? colors.grey('Restored') : colors.green('Solved')} 'Fix It'-part of coding challenge ${colors.cyan(solvedChallenge.key)} (${solvedChallenge.name})`)
logger.info(`${isRestore ? colors.grey('Restored') : colors.green('Solved')} 'Fix It' phase of coding challenge ${colors.cyan(solvedChallenge.key)} (${solvedChallenge.name})`)
if (!isRestore) {
accuracy.storeFixItVerdict(solvedChallenge.key, true)
accuracy.calculateFixItAccuracy(solvedChallenge.key)
antiCheat.calculateFixItCheatScore(solvedChallenge)
}
}
4 changes: 3 additions & 1 deletion routes/vulnCodeFixes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Request, Response, NextFunction } from 'express'
const accuracy = require('../lib/accuracy')
const utils = require('../lib/utils')
const fs = require('fs')

Expand Down Expand Up @@ -77,11 +78,12 @@ export const checkCorrectFix = () => async (req: Request<{}, {}, VerdictRequestB
}

if (selectedFix === fixData.correct) {
await utils.solveFixIt(req.body.key)
await utils.solveFixIt(key)
res.status(200).json({
verdict: true
})
} else {
accuracy.storeFixItVerdict(key, false)
res.status(200).json({
verdict: false
})
Expand Down
7 changes: 5 additions & 2 deletions routes/vulnCodeSnippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import actualFs from 'fs'
const utils = require('../lib/utils')
const challenges = require('../data/datacache').challenges
const path = require('path')
const accuracy = require('../lib/accuracy')

fs.gracefulify(actualFs)

Expand Down Expand Up @@ -174,9 +175,10 @@ export const getVerdict = (vulnLines: number[], selectedLines: number[]) => {
}

exports.checkVulnLines = () => async (req: Request<{}, {}, VerdictRequestBody>, res: Response, next: NextFunction) => {
const key = req.body.key
let snippetData
try {
snippetData = await retrieveCodeSnippet(req.body.key)
snippetData = await retrieveCodeSnippet(key)
} catch (error) {
const statusCode = setStatusCode(error)
res.status(statusCode).json({ status: 'error', error: error.message })
Expand All @@ -186,11 +188,12 @@ exports.checkVulnLines = () => async (req: Request<{}, {}, VerdictRequestBody>,
const selectedLines: number[] = req.body.selectedLines
const verdict = getVerdict(vulnLines, selectedLines)
if (verdict) {
await utils.solveFindIt(req.body.key)
await utils.solveFindIt(key)
res.status(200).json({
verdict: true
})
} else {
accuracy.storeFindItVerdict(key, false)
res.status(200).json({
verdict: false
})
Expand Down

0 comments on commit 7a8c880

Please sign in to comment.