Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
PierpaoloSpadafora committed Dec 12, 2024
1 parent f6159f3 commit ca8f11b
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

import java.util.List;
import java.util.Map;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

@RestController
@RequestMapping(value = "/api/v1/json", produces = "application/json")
Expand Down Expand Up @@ -109,32 +115,72 @@ public ResponseEntity<List<MachineDTO>> exportMachine() {
@Operation(summary = "Export Job data to JSON", description = "Export all Job data to JSON.",
tags = {"json-controller"})
@GetMapping(value = "/export-job-scheduled-by-priority", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<ScheduleWithMachineDTO>> exportJobScheduledPriority() {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-by-priority.json");
return ResponseEntity.ok(schedules);
public ResponseEntity<?> exportJobScheduledPriority() {
try {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-by-priority.json");
return ResponseEntity.ok(schedules);
} catch (RuntimeException e) {
String message = e.getMessage();
if (message.startsWith("FILE_NOT_FOUND:")) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(Map.of("error", "Schedule file not found", "details", message));
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of("error", "Error reading schedule file", "details", message));
}
}

@Operation(summary = "Export Job data to JSON", description = "Export all Job data to JSON.",
tags = {"json-controller"})
@GetMapping(value = "/export-job-scheduled-by-due-date", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<ScheduleWithMachineDTO>> exportJobScheduledDueDate() {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-by-due-date.json");
return ResponseEntity.ok(schedules);
public ResponseEntity<?> exportJobScheduledDueDate() {
try {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-by-due-date.json");
return ResponseEntity.ok(schedules);
} catch (RuntimeException e) {
String message = e.getMessage();
if (message.startsWith("FILE_NOT_FOUND:")) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(Map.of("error", "Schedule file not found", "details", message));
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of("error", "Error reading schedule file", "details", message));
}
}

@Operation(summary = "Export Job data to JSON", description = "Export all Job data to JSON.",
tags = {"json-controller"})
@GetMapping(value = "/export-job-scheduled-by-duration", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<ScheduleWithMachineDTO>> exportJobScheduledDuration() {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-by-duration.json");
return ResponseEntity.ok(schedules);
public ResponseEntity<?> exportJobScheduledDuration() {
try {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-by-duration.json");
return ResponseEntity.ok(schedules);
} catch (RuntimeException e) {
String message = e.getMessage();
if (message.startsWith("FILE_NOT_FOUND:")) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(Map.of("error", "Schedule file not found", "details", message));
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of("error", "Error reading schedule file", "details", message));
}
}

@Operation(summary = "Export RO scheduled jobs", description = "Export all RO scheduled jobs to JSON.")
@GetMapping(value = "/export-job-scheduled-ro", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<ScheduleWithMachineDTO>> exportJobScheduledRO() {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-ro.json");
return ResponseEntity.ok(schedules);
@GetMapping(value = "/export-job-scheduled-external", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> exportJobScheduledExternal() {
try {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-imported.json");
return ResponseEntity.ok(schedules);
} catch (RuntimeException e) {
String message = e.getMessage();
if (message.startsWith("FILE_NOT_FOUND:")) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(Map.of("error", "Schedule file not found", "details", message));
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of("error", "Error reading schedule file", "details", message));
}
}

@Operation(summary = "Download all Schedules as JSON", description = "Download all Schedules as a JSON file.",
Expand All @@ -147,18 +193,29 @@ public ResponseEntity<List<ScheduleDTO>> downloadSchedules() {

@Operation(summary = "Import Schedules from JSON", description = "Upload and import Schedules from a JSON file.",
tags = {"json-controller"})
@PostMapping(value = "/upload-schedules", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PostMapping(value = "/upload-schedules-scheduled-externally", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Map<String, String>> importSchedules(@RequestParam("file") MultipartFile file) {
try {
if (file.isEmpty()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Map.of("message", "File is empty."));
}
jsonService.importSchedules(file);
File directory = new File("./data");
if (!directory.exists()) {
directory.mkdirs();
}
Path filePath = Paths.get("./data/job-scheduled-imported.json");
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
return ResponseEntity.ok(Map.of("message", "Schedules imported successfully."));
} catch (RuntimeException e) {
} catch (IOException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of("message", "Error importing schedules: " + e.getMessage()));
}
}

@GetMapping("/export-job-scheduled-ro")
public ResponseEntity<List<ScheduleWithMachineDTO>> exportJobScheduledRO() {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-ro.json");
return ResponseEntity.ok(schedules);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class JsonServiceImpl implements IJsonService {

public List<ScheduleWithMachineDTO> readScheduleFile(String fileName) {
File file = new File(fileName);
if (!file.exists()) {
throw new RuntimeException(new IOException("File not found: " + fileName));
}
try {
return objectMapper.readValue(file,
objectMapper.getTypeFactory().constructCollectionType(List.class, ScheduleWithMachineDTO.class));
Expand Down
6 changes: 6 additions & 0 deletions Frontend/src/app/components/home/home.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@
border-left: 3px solid #3498db;
}

.error-message {
color: red;
text-align: center;
margin-bottom: 1rem;
}

@media (max-width: 768px) {
.page-container {
padding: 1rem;
Expand Down
5 changes: 4 additions & 1 deletion Frontend/src/app/components/home/home.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h1>Scheduled Jobs</h1>
<div class="schedule-selector">
<label for="scheduleType">Select scheduling type:</label>
<select id="scheduleType" [(ngModel)]="selectedScheduleType" class="theme-background theme-text" (change)="onScheduleTypeChange()">
<select id="scheduleType" [(ngModel)]="newSelectedScheduleType" class="theme-background theme-text" (change)="onScheduleTypeChange()">
<option *ngFor="let type of scheduleTypes" [value]="type.value">{{ type.label }}</option>
</select>
</div>
Expand Down Expand Up @@ -98,5 +98,8 @@ <h4 class="machine-header">{{ getMachineName(machineSchedules[0].machineId) }}</
</div>
</div>
</div>
<div *ngIf="errorMessage" class="error-message">
<p>{{ errorMessage }}</p>
</div>
</div>
</div>
146 changes: 82 additions & 64 deletions Frontend/src/app/components/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { ScheduleControllerService, JobDTO,
JsonControllerService } from '../../generated-api';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { ScheduleWithMachineDTO } from '../../generated-api/model/scheduleWithMachineDTO';
import { ScheduleWithMachineDTO } from '../../generated-api';
import Swal from 'sweetalert2';

@Component({
selector: 'app-home',
Expand All @@ -14,16 +15,18 @@ export class HomeComponent implements OnInit {
scheduleData: ScheduleWithMachineDTO[] = [];
jobsMap: Map<number, JobDTO> = new Map<number, JobDTO>();
machinesMap: Map<number, string> = new Map<number, string>();
machineNamesMap: Map<number, string> = new Map<number, string>(); // New map for machine names
machineNamesMap: Map<number, string> = new Map<number, string>();

scheduleTypes = [
{ label: 'All Jobs', value: 'ALL' },
{ label: 'Scheduled by Due Date', value: 'DUE_DATE' },
{ label: 'Scheduled by Priority', value: 'PRIORITY' },
{ label: 'Scheduled by Duration', value: 'DURATION' },
{ label: 'Scheduler di RICERCA OPERATIVA', value: 'RO' }
{ label: 'External Scheduler ', value: 'EXTERNAL' }
];
selectedScheduleType = 'ALL';
errorMessage: string = '';
newSelectedScheduleType: string = this.selectedScheduleType;

daysPerPageOptions = [1, 3, 5, 7];
selectedDaysPerPage = 3;
Expand All @@ -42,18 +45,18 @@ export class HomeComponent implements OnInit {
) { }

ngOnInit() {
this.fetchData();
this.fetchData(this.selectedScheduleType);
}

onScheduleTypeChange() {
this.fetchData();
this.fetchData(this.newSelectedScheduleType);
}

fetchData() {
fetchData(scheduleType: string) {
this.loading = true;

let scheduleObservable: Observable<ScheduleWithMachineDTO[]>;
switch (this.selectedScheduleType) {
switch (scheduleType) {
case 'PRIORITY':
scheduleObservable = this.jsonService.exportJobScheduledPriority();
break;
Expand All @@ -63,69 +66,84 @@ export class HomeComponent implements OnInit {
case 'DURATION':
scheduleObservable = this.jsonService.exportJobScheduledDuration();
break;
case 'RO':
scheduleObservable = this.jsonService.exportJobScheduledRO();
case 'EXTERNAL':
scheduleObservable = this.jsonService.exportJobScheduledExternal();
break;
default:
scheduleObservable = this.scheduleService.getAllSchedules();
}

scheduleObservable.subscribe({
next: (scheduleData: ScheduleWithMachineDTO[]) => {
this.scheduleData = scheduleData;

this.jsonService.exportJob().subscribe({
next: (jobs: JobDTO[]) => {
this.jobsMap.clear();
jobs.forEach(job => {
if (job.id !== undefined) {
this.jobsMap.set(job.id, job);
}
});

this.jsonService.exportMachineType().subscribe({
next: (machineTypes) => {
this.machinesMap.clear();
machineTypes.forEach(machineType => {
if (machineType.id !== undefined && machineType.name !== undefined) {
this.machinesMap.set(machineType.id, machineType.name);
}
});

// Fetch machine names
this.jsonService.exportMachine().subscribe({
next: (machines) => {
this.machineNamesMap.clear();
machines.forEach(machine => {
if (machine.id !== undefined && machine.name !== undefined) {
this.machineNamesMap.set(machine.id, machine.name);
}
});

this.processData();
this.loading = false;
},
error: (error: unknown) => {
console.error('Error fetching machines data', error);
this.loading = false;
}
});
},
error: (error: unknown) => {
console.error('Error fetching machine types data', error);
this.loading = false;
}
});
},
error: (error: unknown) => {
console.error('Error fetching jobs data', error);
this.loading = false;
}
});
if (scheduleData.length === 0) {
this.errorMessage = 'Nessun job pianificato trovato per il tipo selezionato.';
this.loading = false;
} else {
this.errorMessage = '';
this.selectedScheduleType = scheduleType;
this.newSelectedScheduleType = scheduleType;
this.scheduleData = scheduleData;

this.jsonService.exportJob().subscribe({
next: (jobs: JobDTO[]) => {
this.jobsMap.clear();
jobs.forEach(job => {
if (job.id !== undefined) {
this.jobsMap.set(job.id, job);
}
});

this.jsonService.exportMachineType().subscribe({
next: (machineTypes) => {
this.machinesMap.clear();
machineTypes.forEach(machineType => {
if (machineType.id !== undefined && machineType.name !== undefined) {
this.machinesMap.set(machineType.id, machineType.name);
}
});

// Fetch machine names
this.jsonService.exportMachine().subscribe({
next: (machines) => {
this.machineNamesMap.clear();
machines.forEach(machine => {
if (machine.id !== undefined && machine.name !== undefined) {
this.machineNamesMap.set(machine.id, machine.name);
}
});

this.processData();
this.loading = false;
},
error: (error: unknown) => {
console.error('Error fetching machines data', error);
this.loading = false;
}
});
},
error: (error: unknown) => {
console.error('Error fetching machine types data', error);
this.loading = false;
}
});
},
error: (error: unknown) => {
console.error('Error fetching jobs data', error);
this.loading = false;
}
});
}
},
error: (error: unknown) => {
console.error('Error fetching schedule data', error);
this.loading = false;
Swal.fire({
icon: 'error',
title: 'Errore',
text: 'Impossibile caricare i dati. Il file potrebbe non essere presente.',
});
// Ripristina il valore della select
this.newSelectedScheduleType = this.selectedScheduleType;
}
});
}
Expand Down Expand Up @@ -179,26 +197,26 @@ export class HomeComponent implements OnInit {
getUniqueMachineTypes(date: string): string[] {
const machineTypeIds = Array.from(this.schedulesByDateAndMachine.get(date)?.keys() || []);
const uniqueTypes = new Set<string>();

machineTypeIds.forEach(id => {
const machineName = this.machinesMap.get(id) || 'Unknown Machine Type';
uniqueTypes.add(machineName);
});

return Array.from(uniqueTypes);
}

getSchedulesForMachineType(date: string, machineTypeName: string): ScheduleWithMachineDTO[][] {
const machineTypeId = Array.from(this.machinesMap.entries())
.find(([_, name]) => name === machineTypeName)?.[0];

if (!machineTypeId) return [];

const schedules = this.schedulesByDateAndMachine.get(date)?.get(machineTypeId) || [];

// Group schedules by machineId
const groupedSchedules = new Map<number, ScheduleWithMachineDTO[]>();

schedules.forEach(schedule => {
const machineId = schedule.machineId || 0;
if (!groupedSchedules.has(machineId)) {
Expand Down
Loading

0 comments on commit ca8f11b

Please sign in to comment.