This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit f33ea2ff authored by Michael Breu's avatar Michael Breu :speech_balloon:
Browse files

Umstellung Statistics auf Collections (und ExerciseId as String)

parent 82501327
Branches
Tags
1 merge request!55June Release
package at.ac.uibk.gitsearch.domain;
import java.text.ParseException;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* represents an exercise id.
* Also provides some utility methods to parse and unparse exercise Ids
* @author Michael Breu
*
*/
public class ExerciseId {
private String projectId; // should be numeric
private String path; // a path with slashes (root slash omitted)
public ExerciseId() {
}
public ExerciseId(String projectId, String path) {
super();
this.projectId = projectId;
this.path = path;
}
/**
* @return the projectId
*/
public String getProjectId() {
return projectId;
}
/**
* @return the path
*/
public String getPath() {
return path;
}
protected final static Pattern ExerciseIdPattern = Pattern.compile("(\\d+)(:(.*))?");
public String toString() {
if(path==null)
return projectId;
if (path.equals(""))
return projectId;
return projectId + ":" + path;
}
/**
* parses the external representation of an ExerciseId.
* @param externalRepresentation
* @return
* @throws ParseException
*/
public static ExerciseId fromString(String externalRepresentation) throws ParseException {
final Matcher ma = ExerciseIdPattern.matcher(externalRepresentation);
if(!ma.matches()) throw new ParseException("Cannot parse " + externalRepresentation, 0);
final MatchResult matchResult = ma.toMatchResult();
String projectId = matchResult.group(1);
String path = matchResult.group(3);
while (path!=null && path.startsWith("/")) {
path = path.substring(1);
}
return new ExerciseId(projectId, path);
}
}
......@@ -2,9 +2,13 @@ package at.ac.uibk.gitsearch.web.rest;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.slf4j.Logger;
......@@ -15,6 +19,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -24,8 +29,10 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import at.ac.uibk.gitsearch.domain.ExerciseId;
import at.ac.uibk.gitsearch.service.GitlabService;
import at.ac.uibk.gitsearch.service.StatisticsService;
import at.ac.uibk.gitsearch.service.dto.StatisticsDTO;
......@@ -126,53 +133,65 @@ public class StatisticsResource {
/**
* {@code GET /statistics/:id} : get the "id" statistics.
*
* @param id the id of the statisticsDTO to retrieve.
* @param statisticId the id of the statisticsDTO to retrieve.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body
* the statisticsDTO, or with status {@code 404 (Not Found)}.
*/
@GetMapping("/statistics/{id}")
@GetMapping("/statistics/{statisticId}")
@PreAuthorize("hasAnyRole('ADMIN')")
public ResponseEntity<StatisticsDTO> getStatistics(@PathVariable Long id) {
log.debug("REST request to get Statistics : {}", id);
Optional<StatisticsDTO> statisticsDTO = statisticsService.findOne(id);
public ResponseEntity<StatisticsDTO> getStatistics(@PathVariable Long statisticId) {
log.debug("REST request to get Statistics : {}", statisticId);
Optional<StatisticsDTO> statisticsDTO = statisticsService.findOne(statisticId);
return ResponseUtil.wrapOrNotFound(statisticsDTO);
}
protected final static Pattern ExerciseIdPattern = Pattern.compile("(\\d+):(.*)");
/*
getStatisticsByExerciseId is used to match the exerciseId of a Gitlab project with the database object for the statistics
If a rest request for an exerciseId comes from the client and there is no object with a fitting exerciseId in the DB the server will create a new db entry with number of views
as 1 and downloads as 0
*/
@GetMapping("/statistics/exercise/{id}")
public ResponseEntity<StatisticsDTO> getStatisticsByExerciseId(@PathVariable String id) {
log.debug("REST request to get Statistics for ExerciseID : {}", id);
Optional<StatisticsDTO> statisticsDTO = statisticsService.findOneByExerciseID(id);
Boolean repoExists = gitlabService.repositoryExists(id.toString());
if (repoExists) {
if (statisticsDTO.isPresent()) {
StatisticsDTO newStats = statisticsDTO.get();
newStats.setViews(newStats.getViews() + 1);
statisticsService.save(newStats);
}
if (!statisticsDTO.isPresent()) {
StatisticsDTO newStats = new StatisticsDTO();
newStats.setDownloads(0);
newStats.setViews(1);
newStats.setExerciseID(id);
statisticsService.save(newStats);
log.debug("Created new statistics entry for exerciseID: {}", id);
statisticsDTO = Optional.of(newStats);
}
return ResponseUtil.wrapOrNotFound(statisticsDTO);
}
else{
return ResponseUtil.wrapOrNotFound(statisticsDTO);
}
}
@GetMapping("/statistics/exercise/**")
public ResponseEntity<StatisticsDTO> getStatisticsByExerciseId(HttpServletRequest request) {
String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
String exerciseId = new AntPathMatcher().extractPathWithinPattern(pattern, request.getServletPath());
log.debug("REST request to get Statistics for ExerciseID : {}", exerciseId);
Optional<StatisticsDTO> statisticsDTO = statisticsService.findOneByExerciseID(exerciseId);
ExerciseId parsedId;
try {
parsedId = ExerciseId.fromString(exerciseId);
} catch (ParseException e) {
return ResponseUtil.wrapOrNotFound(statisticsDTO);
}
Boolean repoExists = gitlabService.repositoryExists(parsedId.getProjectId());
if (repoExists) {
if (statisticsDTO.isPresent()) {
StatisticsDTO newStats = statisticsDTO.get();
newStats.setViews(newStats.getViews() + 1);
statisticsService.save(newStats);
}
if (!statisticsDTO.isPresent()) {
StatisticsDTO newStats = new StatisticsDTO();
newStats.setDownloads(0);
newStats.setViews(1);
newStats.setExerciseID(exerciseId);
statisticsService.save(newStats);
log.debug("Created new statistics entry for exerciseID: {}", exerciseId);
statisticsDTO = Optional.of(newStats);
}
return ResponseUtil.wrapOrNotFound(statisticsDTO);
} else {
return ResponseUtil.wrapOrNotFound(statisticsDTO);
}
}
/**
* {@code DELETE /statistics/:id} : delete the "id" statistics.
......
package at.ac.uibk.gitsearch.domain;
import java.text.ParseException;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public class ExerciseIdTest {
@ParameterizedTest
@ValueSource(strings = {"3", "3:path", "3:path1/path2"} )
public void parseVariousIds(String externalRepresentation) throws ParseException {
Assert.assertEquals(externalRepresentation, ExerciseId.fromString(externalRepresentation).toString());
}
@Test
public void parseIdsWithTrailingSlashes() throws ParseException {
Assert.assertEquals("30:abc", ExerciseId.fromString("30:/abc").toString());
Assert.assertEquals("30:abc", ExerciseId.fromString("30://abc").toString());
Assert.assertEquals("30", ExerciseId.fromString("30://").toString());
Assert.assertEquals("30:abc/def", ExerciseId.fromString("30:/abc/def").toString());
}
@ParameterizedTest
@ValueSource(strings = {"=", ":path", "3x2", "3x2:/asd", "asdf=asd:30"} )
public void parseCorruptIds(String corruptIds) throws ParseException {
Assertions.assertThrows(ParseException.class, () -> ExerciseId.fromString(corruptIds));
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment