juice-shop

Форк
0
/
accuracy.ts 
69 строк · 2.2 Кб
1
/*
2
 * Copyright (c) 2014-2021 Bjoern Kimminich.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import logger from './logger'
7
import colors from 'colors/safe'
8
const solves: Record<string, { 'find it': boolean, 'fix it': boolean, attempts: { 'find it': number, 'fix it': number } }> = {}
9

10
type Phase = 'find it' | 'fix it'
11

12
export const storeFindItVerdict = (challengeKey: string, verdict: boolean) => {
13
  storeVerdict(challengeKey, 'find it', verdict)
14
}
15

16
export const storeFixItVerdict = (challengeKey: string, verdict: boolean) => {
17
  storeVerdict(challengeKey, 'fix it', verdict)
18
}
19

20
export const calculateFindItAccuracy = (challengeKey: string) => {
21
  return calculateAccuracy(challengeKey, 'find it')
22
}
23

24
export const calculateFixItAccuracy = (challengeKey: string) => {
25
  return calculateAccuracy(challengeKey, 'fix it')
26
}
27

28
export const totalFindItAccuracy = () => {
29
  return totalAccuracy('find it')
30
}
31

32
export const totalFixItAccuracy = () => {
33
  return totalAccuracy('fix it')
34
}
35

36
export const getFindItAttempts = (challengeKey: string) => {
37
  return solves[challengeKey] ? solves[challengeKey].attempts['find it'] : 0
38
}
39

40
function totalAccuracy (phase: Phase) {
41
  let sumAccuracy = 0
42
  let totalSolved = 0
43
  Object.entries(solves).forEach(([key, value]) => {
44
    if (value[phase]) {
45
      sumAccuracy += 1 / value.attempts[phase]
46
      totalSolved++
47
    }
48
  })
49
  return sumAccuracy / totalSolved
50
}
51

52
function calculateAccuracy (challengeKey: string, phase: Phase) {
53
  let accuracy = 0
54
  if (solves[challengeKey][phase]) {
55
    accuracy = 1 / solves[challengeKey].attempts[phase]
56
  }
57
  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()))}`)
58
  return accuracy
59
}
60

61
function storeVerdict (challengeKey: string, phase: Phase, verdict: boolean) {
62
  if (!solves[challengeKey]) {
63
    solves[challengeKey] = { 'find it': false, 'fix it': false, attempts: { 'find it': 0, 'fix it': 0 } }
64
  }
65
  if (!solves[challengeKey][phase]) {
66
    solves[challengeKey][phase] = verdict
67
    solves[challengeKey].attempts[phase]++
68
  }
69
}
70

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.