Skip to content
Snippets Groups Projects
Select Git revision
  • f1d3f7bf1d072c0b6355e4d6b4acacc8edddf5ae
  • main default protected
  • jw_sonar
  • v6.0.0 protected
  • interactive-mode-preference
  • bedran_exercise-list
  • add_route_user
  • Jw_sonar_backup
  • exercise_list_filter
  • assignment_filter
  • add_route_assignments
  • move-to-esm-only
  • 6.0.0-dev
  • Pre-alpha
  • 5.0.0
  • Latest
  • 4.2.0
  • 4.1.1
  • 4.1.0
  • 4.0.1
  • 4.0.0
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.3.0
  • 3.2.3
  • 3.2.2
  • 3.2.0
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
32 results

ExerciseResultCommand.ts

Blame
  • ExerciseResultCommand.ts 4.82 KiB
    import CommanderCommand   from '../../CommanderCommand';
    import chalk              from 'chalk';
    import ora                from 'ora';
    import DojoBackendManager from '../../../managers/DojoBackendManager';
    import Result             from '../../../sharedByClients/models/Result';
    import inquirer           from 'inquirer';
    
    
    // THIS COMMAND IS NOT WORKING YET - NEEDS TO BE REWRITTEN
    
    class ExerciseResultCommand extends CommanderCommand {
        protected commandName: string = 'result';
    
        protected defineCommand(): void {
            this.command
                .description('results of an exercise')
                .argument('<idOrLink>', 'display results of a specific exercise by Id or Gitlab Link')
                .action(this.commandAction.bind(this));
        }
    
        protected async commandAction(idOrLink: string): Promise<void> {
            const spinner = ora('Fetching exercise results...').start();
    
            try {
                const exerciseId = this.extractExerciseId(idOrLink);
    
                spinner.info(`Fetching results for exercise with ID: ${ exerciseId }`);
                const results = await DojoBackendManager.getExerciseResults(exerciseId);
    
                if ( !results ) {
                    spinner.info('No results found for this exercise.');
                    spinner.succeed('Exercise results fetched successfully.');
                    return;
                }
    
                if ( results.length === 0 ) {
                    spinner.info('No results found for this exercise.');
                } else {
                    const answer = await inquirer.prompt([ {
                        type   : 'list',
                        name   : 'testType',
                        message: 'Choisissez le type de tests à afficher:',
                        choices: [ 'Tests réussis', 'Tests échoués', 'Les deux' ]
                    } ]);
    
                    const { testType } = answer;
    
                    this.displayResults(results, testType as string);
                    spinner.succeed('Exercise results fetched successfully.');
                }
            } catch ( error ) {
                spinner.fail('Error fetching exercise results.');
                console.error(error);
            }
        }
    
        private extractExerciseId(idOrLink: string): string {
            if ( idOrLink.length <= 36 ) {
                return idOrLink;
            } else {
                const lastUnderscoreIndex = idOrLink.lastIndexOf('_');
    
                if ( lastUnderscoreIndex !== -1 ) { // -1 = pas de underscore trouvé
                    return idOrLink.substring(lastUnderscoreIndex + 1); // Extrait la sous-chaîne après le dernier underscore dans idOrLink
                } else {
                    return '';
                }
            }
        }
    
        private displayResults(results: Result[], testType: string): void {
            if ( !results || results.length === 0 ) {
                console.log('No results to display.');
                return;
            }
    
            // Filtrer les résultats en fonction du type de test choisi
            const filteredResults = results.filter(result => {
                if ( testType === 'Tests réussis' ) {
                    return result.success;
                } else if ( testType === 'Tests échoués' ) {
                    return !result.success;
                }
                return true; // 'Les deux' ou autre
            });
    
            if ( filteredResults.length === 0 ) {
                console.log('No results for this test type.');
                return;
            }
    
            filteredResults.forEach(result => {
                console.log(chalk.magenta(`Résultats de l\`exercice : ${ result.exerciseId }`));
                console.log(`  - Date et heure : ${ result.dateTime }`);
                console.log(`  - Succès : ${ result.success ? chalk.green('Oui') : chalk.red('Non') }`);
                console.log('  - Détails des résultats :');
                console.log(`    - Tests réussis : ${ result.results.successfulTests }`);
                console.log(`    - Tests échoués : ${ result.results.failedTests }`);
    
                if ( testType === 'Tests réussis' || testType === 'Les deux' ) {
                    console.log('    - Liste des tests réussis :');
                    if ( Array.isArray(result.results.successfulTestsList) ) {
                        result.results.successfulTestsList.forEach((test: string) => {
                            console.log(`      - ${ test } ${ chalk.green('\u2713') }`);
                        });
                    }
                }
    
                if ( testType === 'Tests échoués' || testType === 'Les deux' ) {
                    console.log('    - Liste des tests échoués :');
                    if ( Array.isArray(result.results.failedTestsList) ) {
                        result.results.failedTestsList.forEach((test: string) => {
                            console.log(`      - ${ test } ${ chalk.red('\u2717') }`);
                        });
                    }
                }
    
                console.log('-----------------------------------');
            });
        }
    }
    
    
    export default new ExerciseResultCommand();