Skip to content

Commit

Permalink
DEAR-132 add sprint to insights, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
smuefsmuef committed Aug 4, 2024
1 parent 520946a commit c06942d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public class InsightsController {
private InsightsService insightsService;


@GetMapping("/{userId}/team/{teamId}/sprint/{sprint}")
@GetMapping("/{userId}/team/{teamId}/sprint/{sprintId}")
public ResponseEntity<InsightDTO> getInsightsByTeamAndSprint(@PathVariable Integer userId,
@PathVariable Integer teamId,
@PathVariable String sprint) {
InsightDTO insights = insightsService.getInsightsByTeamAndSprint(userId, teamId, sprint);
@PathVariable Integer sprintId) {
InsightDTO insights = insightsService.getInsightsByTeamAndSprint(userId, teamId, sprintId);
return ResponseEntity.ok(insights);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface SprintConfigRepository extends JpaRepository<SprintConfig, Integer> {
List<SprintConfig> findAllByCreatedBy(Integer createdBy);

Optional<SprintConfig> findById(Integer id);

}
173 changes: 55 additions & 118 deletions src/main/java/ch/fhnw/deardevbackend/services/InsightsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.fhnw.deardevbackend.annotations.ValidateUserIdParam;
import ch.fhnw.deardevbackend.dto.insights.*;
import ch.fhnw.deardevbackend.entities.SprintConfig;
import ch.fhnw.deardevbackend.mapper.HappinessInsightMapper;
import ch.fhnw.deardevbackend.mapper.WorkKindInsightMapper;
import ch.fhnw.deardevbackend.repositories.*;
Expand All @@ -24,24 +25,28 @@ public class InsightsService {
private final HappinessSurveyRepository happinessSurveyRepository;
private final HappinessInsightMapper happinessInsightMapper;
private final WorkKindInsightMapper workKindInsightMapper;
private final SprintConfigRepository sprintConfigRepository;


public InsightsService(InsightsRepository insightsRepository,
HappinessSurveyRepository happinessSurveyRepository,
HappinessInsightMapper happinessInsightMapper,
WorkKindInsightMapper workKindInsightMapper) {
WorkKindInsightMapper workKindInsightMapper,
SprintConfigRepository sprintConfigRepository) {

this.insightsRepository = insightsRepository;
this.happinessSurveyRepository = happinessSurveyRepository;
this.happinessInsightMapper = happinessInsightMapper;
this.workKindInsightMapper = workKindInsightMapper;
this.sprintConfigRepository = sprintConfigRepository;
}

@Transactional(readOnly = true)
public InsightDTO getInsightsByTeamAndSprint(@ValidateUserIdParam Integer userId, Integer teamId, String sprint) {
List<HappinessInsightDTO> happinessInsights = getHappinessInsightsByTeam(userId, teamId, sprint);
List<WorkKindInsightDTO> workKindInsights = getWorkKindInsightsByUserAndTeam(userId, teamId, sprint);
List<EmotionInsightDTO> emotionInsights = getEmotionInsightsByUserAndTeam(userId, teamId, sprint);
List<WorkKindCountPerDayInsightDTO> workKindCountPerDayInsights = calculateAverageHappinessPerWorkKindCount(userId, sprint, teamId);
public InsightDTO getInsightsByTeamAndSprint(@ValidateUserIdParam Integer userId, Integer teamId, Integer sprintId) {
List<HappinessInsightDTO> happinessInsights = getHappinessInsightsByTeam(userId, teamId, sprintId);
List<WorkKindInsightDTO> workKindInsights = getWorkKindInsightsByUserAndTeam(userId, teamId, sprintId);
List<EmotionInsightDTO> emotionInsights = getEmotionInsightsByUserAndTeam(userId, teamId, sprintId);
List<WorkKindCountPerDayInsightDTO> workKindCountPerDayInsights = calculateAverageHappinessPerWorkKindCount(userId, sprintId, teamId);

double userAverageHappiness = calculateAverageHappiness(happinessInsights, true);
double teamAverageHappiness = calculateAverageHappiness(happinessInsights, false);
Expand All @@ -53,41 +58,25 @@ public InsightDTO getInsightsByTeamAndSprint(@ValidateUserIdParam Integer userId
///// Happiness insights

@Transactional(readOnly = true)
public List<HappinessInsightDTO> getHappinessInsightsByTeam(@ValidateUserIdParam Integer userId, Integer teamId, String sprint) {
// todo


LocalDateTime startDate = null;
LocalDateTime endDate = LocalDateTime.now();


switch (sprint.toLowerCase()) {
case "current":
startDate = LocalDateTime.now().minusWeeks(3);
break;
case "last":
startDate = LocalDateTime.now().minusWeeks(6);
endDate = LocalDateTime.now().minusWeeks(3);
break;
case "none":
default:
startDate = null;
endDate = null;
break;
}

public List<HappinessInsightDTO> getHappinessInsightsByTeam(@ValidateUserIdParam Integer userId, Integer teamId, Integer sprintId) {
List<Object[]> userAverages;
List<Object[]> teamAverages;

if (startDate != null) {
userAverages = happinessSurveyRepository.findDailyAveragesByUserIdAndDateRange(userId, startDate, endDate);
teamAverages = insightsRepository.findTeamDailyAveragesAndDateRange(teamId, startDate, endDate);

} else {
if (sprintId == 0) {
userAverages = happinessSurveyRepository.findDailyAveragesByUserId(userId);
teamAverages = insightsRepository.findTeamDailyAverages(teamId);
} else {
Optional<SprintConfig> sprintConfigOptional = sprintConfigRepository.findById(sprintId);
SprintConfig sprintConfig = sprintConfigOptional.get();

LocalDateTime startDate = sprintConfig.getStartDate().atStartOfDay();
LocalDateTime endDate = sprintConfig.getEndDate().atTime(23, 59, 59);

userAverages = happinessSurveyRepository.findDailyAveragesByUserIdAndDateRange(userId, startDate, endDate);
teamAverages = insightsRepository.findTeamDailyAveragesAndDateRange(teamId, startDate, endDate);
}


Map<String, Double> teamAveragesMap = teamAverages.stream()
.collect(Collectors.toMap(
row -> row[0].toString(),
Expand Down Expand Up @@ -117,40 +106,22 @@ private double calculateAverageHappiness(List<HappinessInsightDTO> insights, boo
///// Workkind insights

@Transactional(readOnly = true)
public List<WorkKindInsightDTO> getWorkKindInsightsByUserAndTeam(@ValidateUserIdParam Integer userId, Integer teamId, String sprint) {
// later todo
// LocalDateTime startDate = getSprintStartDate(sprint);
// LocalDateTime endDate = getSprintEndDate(sprint);

LocalDateTime startDate = null;
LocalDateTime endDate = LocalDateTime.now();


switch (sprint.toLowerCase()) {
case "current":
startDate = LocalDateTime.now().minusWeeks(3);
break;
case "last":
startDate = LocalDateTime.now().minusWeeks(6);
endDate = LocalDateTime.now().minusWeeks(3);
break;
case "none":
default:
startDate = null;
endDate = null;
break;
}


public List<WorkKindInsightDTO> getWorkKindInsightsByUserAndTeam(@ValidateUserIdParam Integer userId, Integer teamId, Integer sprintId) {
List<Object[]> userWorkKinds;
List<Object[]> teamWorkKinds;

if (startDate != null) {
userWorkKinds = insightsRepository.findTopWorkKindsByUserAndDateRange(userId, teamId, startDate, endDate);
teamWorkKinds = insightsRepository.findTopWorkKindsByTeamAndDateRange(teamId, startDate, endDate);
} else {
if (sprintId == 0) {
userWorkKinds = insightsRepository.findTopWorkKindsByUser(userId, teamId);
teamWorkKinds = insightsRepository.findTopWorkKindsByTeam(teamId);
} else {
Optional<SprintConfig> sprintConfigOptional = sprintConfigRepository.findById(sprintId);
SprintConfig sprintConfig = sprintConfigOptional.get();

LocalDateTime startDate = sprintConfig.getStartDate().atStartOfDay();
LocalDateTime endDate = sprintConfig.getEndDate().atTime(23, 59, 59);

userWorkKinds = insightsRepository.findTopWorkKindsByUserAndDateRange(userId, teamId, startDate, endDate);
teamWorkKinds = insightsRepository.findTopWorkKindsByTeamAndDateRange(teamId, startDate, endDate);
}

List<WorkKindInsightDTO> userWorkKindInsights = userWorkKinds.stream()
Expand Down Expand Up @@ -208,35 +179,22 @@ private List<WorkKindInsightDTO> mergeUserAndTeamWorkKindInsights(List<WorkKindI
///// Emotion insights

@Transactional(readOnly = true)
public List<EmotionInsightDTO> getEmotionInsightsByUserAndTeam(Integer userId, Integer teamId, String sprint) {
// todo temp
LocalDateTime startDate = null;
LocalDateTime endDate = LocalDateTime.now();

switch (sprint.toLowerCase()) {
case "current":
startDate = LocalDateTime.now().minusWeeks(3);
break;
case "last":
startDate = LocalDateTime.now().minusWeeks(6);
endDate = LocalDateTime.now().minusWeeks(3);
break;
case "none":
default:
startDate = null;
endDate = null;
break;
}

public List<EmotionInsightDTO> getEmotionInsightsByUserAndTeam(Integer userId, Integer teamId, Integer sprintId) {
List<Object[]> userEmotions;
List<Object[]> teamEmotions;

if (startDate != null) {
userEmotions = insightsRepository.findTopEmotionsByUserAndDateRange(userId, startDate, endDate);
teamEmotions = insightsRepository.findTopEmotionsByTeamAndDateRange(teamId, startDate, endDate);
} else {
if (sprintId == 0) {
userEmotions = insightsRepository.findTopEmotionsByUser(userId);
teamEmotions = insightsRepository.findTopEmotionsByTeam(teamId);
} else {
Optional<SprintConfig> sprintConfigOptional = sprintConfigRepository.findById(sprintId);
SprintConfig sprintConfig = sprintConfigOptional.get();

LocalDateTime startDate = sprintConfig.getStartDate().atStartOfDay();
LocalDateTime endDate = sprintConfig.getEndDate().atTime(23, 59, 59);

userEmotions = insightsRepository.findTopEmotionsByUserAndDateRange(userId, startDate, endDate);
teamEmotions = insightsRepository.findTopEmotionsByTeamAndDateRange(teamId, startDate, endDate);
}

List<EmotionInsightDTO> userEmotionInsights = userEmotions.stream()
Expand Down Expand Up @@ -283,43 +241,22 @@ private List<EmotionInsightDTO> mergeUserAndTeamEmotionInsights(List<EmotionInsi

///// Workkind count per day vs. Average happiness

public List<WorkKindCountPerDayInsightDTO> calculateAverageHappinessPerWorkKindCount(Integer userId, String sprint, Integer teamId) {
// todo
// Calculate the start and end date based on the sprint string
LocalDate startDate = null;
LocalDate endDate = LocalDate.now();

switch (sprint.toLowerCase()) {
case "current":
startDate = LocalDate.now().minusWeeks(3);
break;
case "last":
startDate = LocalDate.now().minusWeeks(6);
endDate = LocalDate.now().minusWeeks(3);
break;
case "none":
default:
startDate = null;
endDate = null;
break;
}

public List<WorkKindCountPerDayInsightDTO> calculateAverageHappinessPerWorkKindCount(Integer userId, Integer sprintId, Integer teamId) {
List<Object[]> workKindData;
List<Object[]> teamWorkKindData;

if (startDate != null) {
// Convert LocalDate to LocalDateTime for the query
LocalDateTime startDateTime = startDate.atStartOfDay();
LocalDateTime endDateTime = endDate.atStartOfDay();
// Call the query with date range
workKindData = insightsRepository.findWorkKindCountPerDayForUserWithDateRange(userId, startDateTime, endDateTime);
teamWorkKindData = insightsRepository.findTeamWorkKindCountPerDayWithDateRange(teamId, startDateTime, endDateTime);

} else {
// Call the query without date range
if (sprintId == 0) {
workKindData = insightsRepository.findWorkKindCountPerDayForUserNoDateRange(userId);
teamWorkKindData = insightsRepository.findTeamWorkKindCountPerDayNoDateRange(teamId);
} else {
Optional<SprintConfig> sprintConfigOptional = sprintConfigRepository.findById(sprintId);
SprintConfig sprintConfig = sprintConfigOptional.get();

LocalDateTime startDate = sprintConfig.getStartDate().atStartOfDay();
LocalDateTime endDate = sprintConfig.getEndDate().atTime(23, 59, 59);

workKindData = insightsRepository.findWorkKindCountPerDayForUserWithDateRange(userId, startDate, endDate);
teamWorkKindData = insightsRepository.findTeamWorkKindCountPerDayWithDateRange(teamId, startDate, endDate);
}

// Map to store workKindCount -> list of daily average happiness scores
Expand Down

0 comments on commit c06942d

Please sign in to comment.