juice-shop
69 строк · 2.2 Кб
1/*
2* Copyright (c) 2014-2021 Bjoern Kimminich.
3* SPDX-License-Identifier: MIT
4*/
5
6import logger from './logger'
7import colors from 'colors/safe'
8const solves: Record<string, { 'find it': boolean, 'fix it': boolean, attempts: { 'find it': number, 'fix it': number } }> = {}
9
10type Phase = 'find it' | 'fix it'
11
12export const storeFindItVerdict = (challengeKey: string, verdict: boolean) => {
13storeVerdict(challengeKey, 'find it', verdict)
14}
15
16export const storeFixItVerdict = (challengeKey: string, verdict: boolean) => {
17storeVerdict(challengeKey, 'fix it', verdict)
18}
19
20export const calculateFindItAccuracy = (challengeKey: string) => {
21return calculateAccuracy(challengeKey, 'find it')
22}
23
24export const calculateFixItAccuracy = (challengeKey: string) => {
25return calculateAccuracy(challengeKey, 'fix it')
26}
27
28export const totalFindItAccuracy = () => {
29return totalAccuracy('find it')
30}
31
32export const totalFixItAccuracy = () => {
33return totalAccuracy('fix it')
34}
35
36export const getFindItAttempts = (challengeKey: string) => {
37return solves[challengeKey] ? solves[challengeKey].attempts['find it'] : 0
38}
39
40function totalAccuracy (phase: Phase) {
41let sumAccuracy = 0
42let totalSolved = 0
43Object.entries(solves).forEach(([key, value]) => {
44if (value[phase]) {
45sumAccuracy += 1 / value.attempts[phase]
46totalSolved++
47}
48})
49return sumAccuracy / totalSolved
50}
51
52function calculateAccuracy (challengeKey: string, phase: Phase) {
53let accuracy = 0
54if (solves[challengeKey][phase]) {
55accuracy = 1 / solves[challengeKey].attempts[phase]
56}
57logger.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()))}`)
58return accuracy
59}
60
61function storeVerdict (challengeKey: string, phase: Phase, verdict: boolean) {
62if (!solves[challengeKey]) {
63solves[challengeKey] = { 'find it': false, 'fix it': false, attempts: { 'find it': 0, 'fix it': 0 } }
64}
65if (!solves[challengeKey][phase]) {
66solves[challengeKey][phase] = verdict
67solves[challengeKey].attempts[phase]++
68}
69}
70