All files / src/services incorrect-tracker-service.ts

100% Statements 61/61
87.5% Branches 7/8
100% Functions 2/2
100% Lines 61/61

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 611x 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 9x 9x 9x 1x 1x 1x 8x 9x 6x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x
import { LogData, LogEvent } from "../api/types/event";
import { trackEvent } from "../api/log-api";
 
 
/**
 * Represents a record of a user's incorrect suggestion selection.
 */
interface IncorrectUserChoice {
    suggestion: string;
    suggestionStartTime: number;
}
 
 
/**
 * A map to store incorrect suggestions selected by each user.
 * Keyed by user ID, with an array of incorrect choices per user.
 */
const incorrectUserChoices: Map<string, IncorrectUserChoice[]> = new Map();
 
/**
 * Tracks when a user selects an incorrect suggestion.
 *
 * Logs a `USER_REJECT` event for telemetry and stores the incorrect selection
 * along with the timestamp.
 *
 * @param userId - The unique identifier of the user.
 * @param incorrectSuggestion - The incorrect suggestion the user selected.
 */
export function trackIncorrectChoices(userId: string, incorrectSuggestion: string): void {
    /** Make sure that the user ID is real. */
    if(!userId) {
        console.warn("No User ID Detected.");
        return;
    }
    /** Adds an array of incorrect user choices for a user and their ID if they are not in the map yet. */
    if(!incorrectUserChoices.has(userId)){
        incorrectUserChoices.set(userId, []);
    }
    /** Gets the user's incorrect choices and adds new incorrect choice to the user's array. */
    incorrectUserChoices.get(userId)!.push({
        suggestion: incorrectSuggestion,
        suggestionStartTime: Date.now(),
    });
    /** Create log for when the user does not accept a code suggestion from the model. */
    const logData: LogData = {
        event: LogEvent.USER_REJECT,
        timeLapse: 0,
        metadata: { userId, incorrectSuggestion, incorrectAttempt: incorrectUserChoices.get(userId)?.length || 1 },
    };
    trackEvent(logData);
}
 
/**
 * Retrieves all incorrect suggestions selected by a specific user.
 *
 * @param userId - The unique identifier of the user.
 * @returns An array of `IncorrectUserChoice` entries representing the user's incorrect selections.
 */
export function getIncorrectChoices(userId: string): IncorrectUserChoice[] {
    return incorrectUserChoices.get(userId) || [];
}