Skip to content

Commit

Permalink
refactor: use entities to find schedules instead of dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
dlg1206 committed May 14, 2024
1 parent 01b9433 commit 567b660
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 674 deletions.
70 changes: 11 additions & 59 deletions src/main/java/com/uh/rainbow/controller/CampusController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.uh.rainbow.controller;

import com.uh.rainbow.dto.course.CourseDTO;
import com.uh.rainbow.dto.identifier.IdentifierDTO;
import com.uh.rainbow.dto.response.*;
import com.uh.rainbow.entities.Section;
import com.uh.rainbow.service.HTMLParserService;
import com.uh.rainbow.services.DTOMapperService;
import com.uh.rainbow.util.SourceURL;
import com.uh.rainbow.util.filter.CourseFilter;
import com.uh.rainbow.util.logging.Logger;
Expand All @@ -14,26 +15,23 @@
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
* <b>File:</b> CampusController.java
* <p>
* <b>Description:</b> Controller that handles request for course and subject information at UH campuses
* <b>Description:</b> Controller that handles parsing campus and course information
*
* @author Derek Garcia
*/

@RequestMapping("/v1/campuses")
@RestController(value = "CampusController")
@RestController(value = "campusController")
public class CampusController {

private final static Logger LOGGER = new Logger(CampusController.class);
private final HTMLParserService htmlParserService = new HTMLParserService();
private final DTOMapperService dtoMapperService = new DTOMapperService();

/**
* Util logging method for reporting HTTP failures
Expand Down Expand Up @@ -176,7 +174,8 @@ public ResponseEntity<ResponseDTO> getCourses(
.setKeywords(keyword)
.build();
// Get all courses for subject
List<CourseDTO> courseDTOs = this.htmlParserService.parseCourses(cf, instID, termID, subjectID);
List<Section> sections = this.htmlParserService.parseSections(cf, instID, termID, subjectID);
List<CourseDTO> courseDTOs = this.dtoMapperService.toCourseDTOs(sections);
return new ResponseEntity<>(
new CourseResponseDTO(courseDTOs),
HttpStatus.OK
Expand Down Expand Up @@ -226,10 +225,6 @@ public ResponseEntity<ResponseDTO> getCourses(
@RequestParam(required = false) List<String> instructor,
@RequestParam(required = false) List<String> keyword) {
try {
// Get all available subjects
Instant start = Instant.now();
List<IdentifierDTO> subjects = this.htmlParserService.parseSubjects(instID, termID);

// Build filter
CourseFilter cf = new CourseFilter.Builder()
.setCRNs(crn)
Expand All @@ -244,54 +239,11 @@ public ResponseEntity<ResponseDTO> getCourses(
.setKeywords(keyword)
.build();

// Parse each subject for courses
List<String> failedSources = new ArrayList<>();
List<CompletableFuture<List<CourseDTO>>> futures = new ArrayList<>();
for (IdentifierDTO s : subjects) {

// skip if not in filter
if (!cf.validSubject(s.id()))
continue;

// Add async job to queue
SourceURL source = new SourceURL(instID, termID, s.id());
futures.add(CompletableFuture
.supplyAsync(() -> {
try {
// Attempt to parse
return this.htmlParserService.parseCourses(cf, instID, termID, s.id());
} catch (HttpStatusException e) {
// Report html access failure, add to failed sources and continue
reportHTTPAccessError(MessageBuilder.Type.COURSE, e);
LOGGER.warn(new MessageBuilder(MessageBuilder.Type.COURSE).addDetails("Skipping %s".formatted(source)));
failedSources.add(source.toString());
} catch (IOException e) {
// Internal server error, add to failed sources and continue
LOGGER.error(new MessageBuilder(MessageBuilder.Type.COURSE).addDetails(e));
failedSources.add(source.toString());
}
return new ArrayList<>(); // empty results
}));
}
// Join each thread / wait for each to finish
futures.forEach(CompletableFuture::join);

// Get all results
List<CourseDTO> courseDTOs = new ArrayList<>();
for (CompletableFuture<List<CourseDTO>> result : futures) {
try {
courseDTOs.addAll(result.get());
} catch (ExecutionException | InterruptedException e) {
LOGGER.error(new MessageBuilder(MessageBuilder.Type.COURSE).addDetails(e));
}
}
// Parse Sections
List<Section> sections = this.htmlParserService.parseSections(cf, instID, termID);
List<CourseDTO> courseDTOs = this.dtoMapperService.toCourseDTOs(sections);

// Report Success and return results
int numSites = futures.size();
LOGGER.info(new MessageBuilder(MessageBuilder.Type.COURSE)
.addDetails("Parsed %s site%s".formatted(numSites, numSites == 1 ? "" : "s"))
.setDuration(start));
return new ResponseEntity<>(new CourseResponseDTO(courseDTOs, failedSources), HttpStatus.OK);
return new ResponseEntity<>(new CourseResponseDTO(courseDTOs), HttpStatus.OK);
} catch (HttpStatusException e) {
// Report and return html access failure
reportHTTPAccessError(MessageBuilder.Type.COURSE, e);
Expand Down
262 changes: 0 additions & 262 deletions src/main/java/com/uh/rainbow/controller/ParserController.java

This file was deleted.

Loading

0 comments on commit 567b660

Please sign in to comment.