mirror of
https://github.com/KeygraphHQ/juice-shop.git
synced 2026-11-07 11:06:06 -10:00
71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
/*
|
|
* Copyright (c) 2014-2021 Bjoern Kimminich.
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
import { type ChallengeKey } from 'models/challenge'
|
|
import logger from './logger'
|
|
import colors from 'colors/safe'
|
|
const solves: Record<string, { 'find it': boolean, 'fix it': boolean, attempts: { 'find it': number, 'fix it': number } }> = {}
|
|
|
|
type Phase = 'find it' | 'fix it'
|
|
|
|
export const storeFindItVerdict = (challengeKey: ChallengeKey, verdict: boolean) => {
|
|
storeVerdict(challengeKey, 'find it', verdict)
|
|
}
|
|
|
|
export const storeFixItVerdict = (challengeKey: ChallengeKey, verdict: boolean) => {
|
|
storeVerdict(challengeKey, 'fix it', verdict)
|
|
}
|
|
|
|
export const calculateFindItAccuracy = (challengeKey: ChallengeKey) => {
|
|
return calculateAccuracy(challengeKey, 'find it')
|
|
}
|
|
|
|
export const calculateFixItAccuracy = (challengeKey: ChallengeKey) => {
|
|
return calculateAccuracy(challengeKey, 'fix it')
|
|
}
|
|
|
|
export const totalFindItAccuracy = () => {
|
|
return totalAccuracy('find it')
|
|
}
|
|
|
|
export const totalFixItAccuracy = () => {
|
|
return totalAccuracy('fix it')
|
|
}
|
|
|
|
export const getFindItAttempts = (challengeKey: ChallengeKey) => {
|
|
return solves[challengeKey] ? solves[challengeKey].attempts['find it'] : 0
|
|
}
|
|
|
|
function totalAccuracy (phase: Phase) {
|
|
let sumAccuracy = 0
|
|
let totalSolved = 0
|
|
Object.entries(solves).forEach(([key, value]) => {
|
|
if (value[phase]) {
|
|
sumAccuracy += 1 / value.attempts[phase]
|
|
totalSolved++
|
|
}
|
|
})
|
|
return sumAccuracy / totalSolved
|
|
}
|
|
|
|
function calculateAccuracy (challengeKey: ChallengeKey, phase: Phase) {
|
|
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.toString()) : (accuracy > 0.25 ? colors.yellow(accuracy.toString()) : colors.red(accuracy.toString()))}`)
|
|
return accuracy
|
|
}
|
|
|
|
function storeVerdict (challengeKey: ChallengeKey, phase: Phase, 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]++
|
|
}
|
|
}
|