Skip to content
Snippets Groups Projects
Commit ee664468 authored by Ruben Backx's avatar Ruben Backx :coffee:
Browse files

Merge branch '672-add-endpoint-for-tam-star-ratings-issue' into 'development'

Resolve "Add endpoint for TAM star ratings issue"

Closes #672

See merge request !732
parents 414d5ce8 78ae63bd
Branches
Tags
2 merge requests!8412425.0.0 release,!732Resolve "Add endpoint for TAM star ratings issue"
......@@ -10,7 +10,7 @@ version = "2.4.0"
val javaVersion = JavaVersion.VERSION_21
val libradorVersion = "1.5.0"
val labradoorVersion = "1.7.0"
val labradoorVersion = "1.7.1"
val chihuahUIVersion = "1.2.3"
val csvVersion = "5.8"
val guavaVersion = "32.1.3-jre"
......
......@@ -23,12 +23,13 @@ import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;
import nl.tudelft.api.TuDelftApiConfig;
import nl.tudelft.labracore.app.LabracoreAppConfig;
import nl.tudelft.labracore.lib.LabracoreApiConfig;
import nl.tudelft.librador.EnableLibrador;
@EnableScheduling
@SpringBootApplication(scanBasePackageClasses = QueueApplication.class)
@Import({ LabracoreApiConfig.class, TuDelftApiConfig.class })
@Import({ LabracoreApiConfig.class, TuDelftApiConfig.class, LabracoreAppConfig.class })
@EnableLibrador
public class QueueApplication {
public static void main(String[] args) {
......
......@@ -26,8 +26,10 @@ import java.util.*;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
......@@ -264,6 +266,22 @@ public class HomeController {
return "home/index";
}
/**
* Returns average star rating for a list of assistants.
*
* @param ids the ids of the assistants
* @return the average star rating for each assistant (that the user has access to)
*/
@GetMapping("/api/average-star-rating")
@ResponseBody
public ResponseEntity<Map<Long, Double>> averageStarRating(@RequestParam("ids") List<Long> ids) {
Map<Long, Double> result = ids.stream()
.collect(Collectors.toMap(
id -> id,
id -> fs.getAvgStarRating(fs.countRatings(fr.findByAssistant(id)))));
return ResponseEntity.ok(result);
}
/**
* Fills in the model for displaying shared editions.
*
......
......@@ -22,11 +22,11 @@ import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
import nl.tudelft.labracore.lib.security.LabradorSecurityConfig;
import nl.tudelft.labracore.app.security.LabradorSecurityConfigWithApi;
@Configuration
@Profile("!production")
public class DevSecurityConfig extends LabradorSecurityConfig {
public class DevSecurityConfig extends LabradorSecurityConfigWithApi {
@Override
protected void configure(HttpSecurity http) throws Exception {
......
......@@ -21,11 +21,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import nl.tudelft.labracore.lib.security.LabradorSecurityConfig;
import nl.tudelft.labracore.app.security.LabradorSecurityConfigWithApi;
@Configuration
@Profile("production")
public class ProductionSecurityConfig extends LabradorSecurityConfig {
public class ProductionSecurityConfig extends LabradorSecurityConfigWithApi {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
......
......@@ -109,9 +109,11 @@ public class FeedbackService {
* number of occurances of that star.
*/
private List<Integer> countRatings(Long assistantId, List<LabRequest> requests) {
List<Feedback> feedbacks = requests.stream().flatMap(rq -> rq.getFeedbacks().stream())
.filter(fb -> Objects.equals(fb.getId().getAssistantId(), assistantId)).toList();
return countRatings(feedbacks);
return countRatings(
requests.stream()
.flatMap(rq -> rq.getFeedbacks().stream())
.filter(fb -> Objects.equals(fb.getId().getAssistantId(), assistantId))
.toList());
}
/**
......@@ -139,17 +141,24 @@ public class FeedbackService {
* @return The average star rating for the assistant w.r.t those requests.
*/
public Double getAvgStarRating(Long personId, List<LabRequest> requests) {
List<Integer> ratings = countRatings(personId, requests);
double totalRatings = 0.0;
double totalCounts = 0.0;
return getAvgStarRating(countRatings(personId, requests));
}
/**
* Get the average star rating based on a list of rating counts (like the one that countRatings method
* produces)
*
* @param ratings The number of ratings (pertaining to a scale from 1 to 5) in buckets.
* @return The average star rating
*/
public Double getAvgStarRating(List<Integer> ratings) {
int totalRatings = 0, totalCount = 0;
for (int i = 0; i < ratings.size(); i++) {
totalRatings += ratings.get(i) * (i + 1);
totalCounts += ratings.get(i);
totalCount += ratings.get(i);
}
if (totalCounts == 0.0)
return 0.0;
return new BigDecimal(totalRatings / totalCounts)
return totalCount == 0 ? 0
: BigDecimal.valueOf((double) totalRatings / totalCount)
.setScale(1, RoundingMode.HALF_UP)
.doubleValue();
}
......
......@@ -81,6 +81,9 @@ queue:
# statically-served-path: /var/www/queue
labrador:
api:
url: https://queue.tudelft.nl
name: queue
sso:
login-path: /login
logout-path: /logout
......
......@@ -18,6 +18,7 @@
package nl.tudelft.queue.controller;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;
......@@ -39,9 +40,12 @@ import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import lombok.SneakyThrows;
import nl.tudelft.labracore.api.EditionControllerApi;
......@@ -53,6 +57,7 @@ import nl.tudelft.labracore.api.dto.PersonSummaryDTO;
import nl.tudelft.queue.model.Feedback;
import nl.tudelft.queue.model.LabRequest;
import nl.tudelft.queue.repository.FeedbackRepository;
import nl.tudelft.queue.service.FeedbackService;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import test.TestDatabaseLoader;
......@@ -79,6 +84,9 @@ public class HomeControllerTest {
@SpyBean
private FeedbackRepository feedbackRepository;
@MockBean
private FeedbackService feedbackService;
private PersonSummaryDTO admin;
private PersonSummaryDTO student50;
......@@ -197,6 +205,21 @@ public class HomeControllerTest {
.andExpect(view().name("home/dashboard"));
}
@Test
@WithUserDetails("admin")
void averageStarRatingTest() throws Exception {
when(feedbackRepository.findByAssistant(anyLong())).thenReturn(List.of(f1, f2, f3));
when(feedbackService.countRatings(anyList())).thenReturn(List.of(2, 1, 1));
when(feedbackService.getAvgStarRating(anyList())).thenReturn(4.5);
MvcResult result = mvc.perform(get("/api/average-star-rating")
.param("ids", "1", "2", "3")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
assertEquals("{\"1\":4.5,\"2\":4.5,\"3\":4.5}", result.getResponse().getContentAsString());
}
@Test
@WithUserDetails("student99")
void finishedRolesInOrder() throws Exception {
......
......@@ -19,6 +19,7 @@ package nl.tudelft.queue.service;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -246,6 +247,17 @@ public class FeedbackServiceTest {
}
@Test
void getAvgStarRating() {
assertEquals(3.1, fs.getAvgStarRating(List.of(3, 2, 5, 1, 4)), 0.01);
}
@Test
void getAvgStarRatingEmptyOrZerosList() {
assertEquals(0, fs.getAvgStarRating(List.of()));
assertEquals(0, fs.getAvgStarRating(List.of(0, 0, 0, 0, 0)));
}
@Test
void updateFeedbackCreatesNewFeedbackWhenNeeded() {
assertThat(fr.findByAssistant(people[4].getId())).isEmpty();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment