Skip to content
Snippets Groups Projects
Commit ff2b69cf authored by michael.minelli's avatar michael.minelli
Browse files

Exercise[Result|Summary]Command => Disable commands (not working)

parent e8b09ae3
No related branches found
No related tags found
No related merge requests found
Pipeline #38756 passed
......@@ -5,7 +5,6 @@ import ExerciseCorrectionCommand from './subcommands/ExerciseCorrectionCommand.j
import ExerciseDeleteCommand from './subcommands/ExerciseDeleteCommand';
import ExerciseSearchCommand from './subcommands/ExerciseSearchCommand';
import ExerciseInfoCommand from './subcommands/ExerciseInfoCommand';
import ExerciseSummaryCommand from "./subcommands/ExerciseSummaryCommand";
class ExerciseCommand extends CommanderCommand {
......@@ -21,10 +20,10 @@ class ExerciseCommand extends CommanderCommand {
ExerciseRunCommand.registerOnCommand(this.command);
ExerciseDeleteCommand.registerOnCommand(this.command);
ExerciseCorrectionCommand.registerOnCommand(this.command);
ExerciseListCommand.registerOnCommand(this.command);
ExerciseSearchCommand.registerOnCommand(this.command);
ExerciseInfoCommand.registerOnCommand(this.command);
ExerciseSummaryCommand.registerOnCommand(this.command);
// ExerciseResultCommand.registerOnCommand(this.command);
// ExerciseSummaryCommand.registerOnCommand(this.command);
}
protected async commandAction(): Promise<void> {
......
import CommanderCommand from '../../CommanderCommand';
import chalk from 'chalk';
import ora from 'ora';
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';
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';
......@@ -22,45 +24,43 @@ class ExerciseResultCommand extends CommanderCommand {
try {
const exerciseId = this.extractExerciseId(idOrLink);
// console.log('Exercise ID:', exerciseId);
spinner.info(`Fetching results for exercise with ID: ${exerciseId}`);
spinner.info(`Fetching results for exercise with ID: ${ exerciseId }`);
const results = await DojoBackendManager.getExerciseResults(exerciseId);
if (!results) {
if ( !results ) {
spinner.info('No results found for this exercise.');
spinner.succeed('Exercise results fetched successfully.');
return;
}
if (results.length === 0) {
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 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);
this.displayResults(results, testType as string);
spinner.succeed('Exercise results fetched successfully.');
}
} catch (error) {
} catch ( error ) {
spinner.fail('Error fetching exercise results.');
console.error(error);
}
}
private extractExerciseId(idOrLink: string): string {
if (idOrLink.length <= 36) {
if ( idOrLink.length <= 36 ) {
return idOrLink;
} else {
const lastUnderscoreIndex = idOrLink.lastIndexOf('_');
// console.log('Last underscore index:', lastUnderscoreIndex);
if (lastUnderscoreIndex !== -1) { // -1 = pas de underscore trouvé
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 '';
......@@ -69,48 +69,48 @@ class ExerciseResultCommand extends CommanderCommand {
}
private displayResults(results: Result[], testType: string): void {
if (!results || results.length === 0) {
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') {
if ( testType === 'Tests réussis' ) {
return result.success;
} else if (testType === 'Tests échoués') {
} else if ( testType === 'Tests échoués' ) {
return !result.success;
}
return true; // 'Les deux' ou autre
});
if (filteredResults.length === 0) {
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(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}`);
console.log(` - Tests réussis : ${ result.results.successfulTests }`);
console.log(` - Tests échoués : ${ result.results.failedTests }`);
if (testType === 'Tests réussis' || testType === 'Les deux') {
if ( testType === 'Tests réussis' || testType === 'Les deux' ) {
console.log(' - Liste des tests réussis :');
if (Array.isArray(result.results.successfulTestsList)) {
if ( Array.isArray(result.results.successfulTestsList) ) {
result.results.successfulTestsList.forEach((test: string) => {
console.log(` - ${test} ${chalk.green('\u2713')}`);
console.log(` - ${ test } ${ chalk.green('\u2713') }`);
});
}
}
if (testType === 'Tests échoués' || testType === 'Les deux') {
if ( testType === 'Tests échoués' || testType === 'Les deux' ) {
console.log(' - Liste des tests échoués :');
if (Array.isArray(result.results.failedTestsList)) {
if ( Array.isArray(result.results.failedTestsList) ) {
result.results.failedTestsList.forEach((test: string) => {
console.log(` - ${test} ${chalk.red('\u2717')}`);
console.log(` - ${ test } ${ chalk.red('\u2717') }`);
});
}
}
......@@ -119,4 +119,6 @@ class ExerciseResultCommand extends CommanderCommand {
});
}
}
export default new ExerciseResultCommand();
import CommanderCommand from '../../CommanderCommand';
import ora from 'ora';
import CommanderCommand from '../../CommanderCommand';
import ora from 'ora';
import DojoBackendManager from '../../../managers/DojoBackendManager';
import Exercise from "../../../sharedByClients/models/Exercise";
import Result from '../../../sharedByClients/models/Result';
import Table from 'cli-table3';
import Exercise from '../../../sharedByClients/models/Exercise';
import Result from '../../../sharedByClients/models/Result';
import Table from 'cli-table3';
// THIS COMMAND IS NOT WORKING - NEEDS TO BE REWRITTEN AND THINK OF HIS INTEREST
class ExerciseSummaryCommand extends CommanderCommand {
protected commandName: string = 'summary';
......@@ -21,7 +24,7 @@ class ExerciseSummaryCommand extends CommanderCommand {
const exercises = await DojoBackendManager.getUserExercises();
const exerciseResults = await this.fetchExerciseResults(exercises);
if (exerciseResults.length === 0) {
if ( exerciseResults.length === 0 ) {
spinner.info('No exercise results found.');
spinner.succeed('Exercise summary fetched successfully.');
return;
......@@ -32,7 +35,7 @@ class ExerciseSummaryCommand extends CommanderCommand {
this.displayExerciseSummary(sortedExercises);
spinner.succeed('Exercise summary fetched successfully.');
} catch (error) {
} catch ( error ) {
spinner.fail('Error fetching exercise summary.');
console.error(error);
}
......@@ -41,18 +44,21 @@ class ExerciseSummaryCommand extends CommanderCommand {
private async fetchExerciseResults(exercises: Exercise[] | undefined): Promise<{ exercise: Exercise; successfulTests: number; dateTime: string }[]> {
const results: { exercise: Exercise, successfulTests: number, dateTime: string }[] = [];
// @ts-ignore
for (const exercise of exercises) {
for ( const exercise of exercises ?? [] ) {
try {
const exerciseId = exercise.id;
const exerciseResults = await DojoBackendManager.getExerciseResults(exerciseId);
if (exerciseResults) {
if ( exerciseResults ) {
const successfulTests = this.countSuccessfulTests(exerciseResults);
results.push({ exercise, successfulTests, dateTime: exerciseResults[0]?.dateTime || '' });
results.push({
exercise,
successfulTests,
dateTime: exerciseResults[0]?.dateTime || ''
});
}
} catch (error) {
console.error(`Error fetching results for exercise ${exercise.id}:`, error);
} catch ( error ) {
console.error(`Error fetching results for exercise ${ exercise.id }:`, error);
}
}
......@@ -60,7 +66,7 @@ class ExerciseSummaryCommand extends CommanderCommand {
}
private countSuccessfulTests(results: Result[]): number {
return results.reduce((count, result) => count + (result.success ? result.results.successfulTestsList.length : 0), 0);
return results.reduce((count, result) => count + (result.success ? (result.results.successfulTestsList ?? []).length : 0), 0);
}
private sortExercisesBySuccessfulTests(exerciseResults: { exercise: Exercise, successfulTests: number, dateTime: string }[]): { exercise: Exercise, successfulTests: number, dateTime: string }[] {
......@@ -69,7 +75,7 @@ class ExerciseSummaryCommand extends CommanderCommand {
private displayExerciseSummary(sortedExercises: { exercise: Exercise, successfulTests: number, dateTime: string }[]): void {
// Calculate the maximum width for each column
const headers = ['#', 'Exercise', 'Nb de tests réussis', 'Date'];
const headers = [ '#', 'Exercise', 'Nb de tests réussis', 'Date' ];
const maxWidths = headers.map(header => header.length);
sortedExercises.forEach((exercise, index) => {
......@@ -80,24 +86,21 @@ class ExerciseSummaryCommand extends CommanderCommand {
});
// Define colWidths based on maxWidths
const colWidths = maxWidths.map(width => ({ width }));
// const colWidths = maxWidths.map(width => ({ width }));
// Create the table
const table = new Table({
head: headers,
});
head: headers
});
// Populate the table with data
sortedExercises.forEach((exercise, index) => {
table.push([
index + 1,
exercise.exercise.name,
exercise.successfulTests,
exercise.dateTime
]);
table.push([ index + 1, exercise.exercise.name, exercise.successfulTests, exercise.dateTime ]);
});
console.log(table.toString(),'\n');
console.log(table.toString(), '\n');
}
}
export default new ExerciseSummaryCommand();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment