Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 4x 4x 4x 4x 4x 5x 5x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 3x 3x 3x 3x 3x 3x 5x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 7x 1x 1x 1x 1x 1x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x | import { trackEvent, getLogsByUser } from "../api/log-api"; import { LogData, LogEvent } from "../api/types/event"; import { calculateUserProgress } from "./user-service"; import { getUserSection, getUserStatus, updateUserSection, updateUserStatus, } from "../api/user-api"; import { getAuthContext } from "./auth-service"; import { User, UserSectionStatus, UserStatus } from "../api/types/user"; import { errorNotification, notifyUser } from "../views/notifications"; import { getSelectedClass } from "../utils/userClass"; /** * This module handles tracking and evaluation of user interactions with AI-generated suggestions. * It logs acceptance/rejection events, monitors user progress, and updates user or section statuses accordingly. */ let suggestionContext = { suggestionId: "", hasBug: false, startTime: 0, }; /** * Logs a user interaction event (accept or reject) for a suggestion. * * Tracks elapsed time, suggestion metadata, and user state. * If the user is suspended, the behavior for logging changes. * * @param accepted - Whether the user accepted (`true`) or rejected (`false`) the suggestion. * @param context - The suggestion context containing suggestion ID, bug presence, and start time. */ export const logSuggestionEvent = async ( accepted: boolean, context: typeof suggestionContext ) => { const { suggestionId, hasBug, startTime } = context; const elapsedTime = Date.now() - startTime; const { context: user, error } = await getAuthContext(); if (error || user === undefined) { await errorNotification(`Failed to get user context: ${error}`); return; } const userId = user.id; const selectedClass = getSelectedClass(); let userSection; try { userSection = await getUserSection(user.id, selectedClass?.id); } catch (error) { await errorNotification(`Failed to get user section: ${error}`); return; } let finalHasBug = hasBug; let logEventType = accepted ? LogEvent.USER_ACCEPT : LogEvent.USER_REJECT; const response = await getUserStatus(user.id, selectedClass?.id); const userClassStatus = response.data || null; const status = userClassStatus ?? user.userStatus; if (status === UserStatus.SUSPENDED) { logEventType = LogEvent.USER_ACCEPT; finalHasBug = !accepted; } const logData: LogData = { event: logEventType, timeLapse: elapsedTime, metadata: { user_id: userId, suggestion_id: suggestionId, has_bug: finalHasBug, user_section_id: userSection.userSectionId, user_class_id: selectedClass?.id, }, }; trackEvent(logData); const { logs, error: getLogsError } = await getLogsByUser( user.id, userSection.userSectionId as string ); if (getLogsError || logs === null || logs === undefined || logs.length < 2) { return; } await evaluateUserProgress( user, logs, userSection.userSectionId, selectedClass?.id ); }; /** * Evaluates a user's progress based on their logged interactions with suggestions. * * Compares the user's acceptance rate and bug rate against configured thresholds * to determine whether to award a badge, suspend, or lock their account. * * @param user - The authenticated user object. * @param logs - Array of user event logs (`LogData`). * @param userSectionId - (Optional) ID of the user's active section. * @param userClassId - (Optional) ID of the user's current class. */ export async function evaluateUserProgress( user: User, logs: LogData[], userSectionId?: string, userClassId?: string ) { const { id, userStatus, settings: { active_threshold, enable_quiz, suspend_threshold, pass_rate, suspend_rate, }, } = user; if (!enable_quiz) { return; } const ACTIVE_THRESHOLD = active_threshold; const SUSPEND_THRESHOLD = suspend_threshold; const PASS_RATE = pass_rate; const SUSPEND_RATE = suspend_rate; const { totalAccepted, totalWithBugs, percentageWithBugs } = calculateUserProgress(logs); const bugFreeRate = 100 - percentageWithBugs; const response = await getUserStatus(user.id, userClassId); const userClassStatus = response.data || null; const status = userClassStatus ?? userStatus; const threshold = status === UserStatus.SUSPENDED ? SUSPEND_THRESHOLD : ACTIVE_THRESHOLD; console.log( `User ${id} - Total Accepted: ${totalAccepted}, Total With Bugs: ${totalWithBugs}, Bug Free Rate: ${bugFreeRate}, Threshold: ${threshold}, Status: ${status}, Pass Rate: ${PASS_RATE}, Suspend Rate: ${SUSPEND_RATE}` ); if (totalAccepted < threshold) { return; } if (bugFreeRate >= PASS_RATE) { await updateUserSection(id, UserSectionStatus.COMPLETE, userSectionId); await notifyUser( "Congrats! You've earned a badge!", "https://clover.nickrucinski.com/" ); } else if (bugFreeRate >= SUSPEND_RATE) { if (status === UserStatus.ACTIVE) { console.log("Update user status to suspended"); await updateUserStatus(id, UserStatus.SUSPENDED, userClassId); await notifyUser( "We're currently slowing you down with suggestions. Please review the next 10 suggestions carefully to improve your progress." ); return; } await updateUserSection(id, UserSectionStatus.NEED_REVIEW, userSectionId); await updateUserStatus(id, UserStatus.LOCKED, userClassId); } else { await updateUserSection(id, UserSectionStatus.NEED_REVIEW, userSectionId); console.log("Update user status to locked"); await updateUserStatus(id, UserStatus.LOCKED, userClassId); } } |