This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
422 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...e/informatievlaanderen/vsds/demonstrator/member/domain/member/valueobjects/HourCount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.member.domain.member.valueobjects; | ||
|
||
import be.informatievlaanderen.vsds.demonstrator.member.domain.member.entities.Member; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class HourCount { | ||
private final List<Member> memberList; | ||
|
||
public HourCount(List<Member> memberList) { | ||
this.memberList = memberList; | ||
} | ||
|
||
public Map<LocalDateTime, Integer> getMemberCountByHour(){ | ||
return memberList | ||
.stream() | ||
.map(Member::getTimestamp) | ||
.map(localDateTime -> localDateTime.truncatedTo(ChronoUnit.HOURS)) | ||
.collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e -> 1))); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...e/informatievlaanderen/vsds/demonstrator/member/domain/member/valueobjects/LineChart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.member.domain.member.valueobjects; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class LineChart { | ||
private final Map<LocalDateTime, Integer> memberCountByHour; | ||
private final long numberOfMembersOutsideFrame; | ||
private final List<String> labels; | ||
private final List<Integer> values; | ||
private final LocalDateTime startDate; | ||
|
||
public LineChart(LocalDateTime startDate, long numberOfMembersOutsideFrame, Map<LocalDateTime, Integer> memberCountByHour) { | ||
this.startDate = startDate; | ||
this.numberOfMembersOutsideFrame = numberOfMembersOutsideFrame; | ||
this.memberCountByHour = memberCountByHour; | ||
this.labels = new ArrayList<>(); | ||
this.values = new ArrayList<>(); | ||
} | ||
|
||
private void calculatePointElements() { | ||
LocalDateTime startTime = startDate.truncatedTo(ChronoUnit.HOURS); | ||
long memberCount = numberOfMembersOutsideFrame; | ||
while (startTime.isBefore(LocalDateTime.now())) { | ||
memberCount = memberCount + memberCountByHour.getOrDefault(startTime, 0); | ||
if (memberCount > 0) { | ||
labels.add(startTime.toString()); | ||
values.add((int) memberCount); | ||
} | ||
startTime = startTime.plusHours(1); | ||
} | ||
} | ||
|
||
public List<String> getLabels() { | ||
if (labels.isEmpty()) | ||
calculatePointElements(); | ||
return labels; | ||
} | ||
|
||
//TODO multiple streams | ||
public List<List<Integer>> getValues() { | ||
if (values.isEmpty()) | ||
calculatePointElements(); | ||
return List.of(values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...rc/main/java/be/informatievlaanderen/vsds/demonstrator/member/rest/dtos/LineChartDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.member.rest.dtos; | ||
|
||
import java.util.List; | ||
|
||
public class LineChartDto { | ||
private final List<String> labels; | ||
private final List<List<Integer>> values; | ||
|
||
public LineChartDto(List<String> labels, List<List<Integer>> values) { | ||
this.labels = labels; | ||
this.values = values; | ||
} | ||
|
||
public List<String> getLabels() { | ||
return labels; | ||
} | ||
|
||
public List<List<Integer>> getValues() { | ||
return values; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.../be/informatievlaanderen/vsds/demonstrator/member/rest/websocket/LineChartController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.member.rest.websocket; | ||
|
||
import be.informatievlaanderen.vsds.demonstrator.member.application.services.MemberService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Controller; | ||
|
||
@Controller | ||
public class LineChartController { | ||
private final SimpMessagingTemplate template; | ||
private final MemberService memberService; | ||
|
||
@Autowired | ||
public LineChartController(SimpMessagingTemplate template, MemberService memberService) { | ||
this.template = template; | ||
this.memberService = memberService; | ||
} | ||
|
||
@Scheduled(fixedDelay = 1000) | ||
public void send() { | ||
this.template.convertAndSend("/broker/linechart", memberService.getLineChartDto()); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...formatievlaanderen/vsds/demonstrator/member/domain/member/valueobjects/HourCountTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.member.domain.member.valueobjects; | ||
|
||
import be.informatievlaanderen.vsds.demonstrator.member.domain.member.entities.Member; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class HourCountTest { | ||
|
||
@Test | ||
void test_HourCount(){ | ||
Member id1 = new Member("id1", null, LocalDateTime.of(2023, 1, 5, 1, 5)); | ||
Member id2 = new Member("id1", null, LocalDateTime.of(2023, 1, 5, 1, 5)); | ||
Member id3 = new Member("id1", null, LocalDateTime.of(2023, 1, 5, 1, 15)); | ||
Member id4 = new Member("id1", null, LocalDateTime.of(2023, 1, 5, 2, 5)); | ||
Member id5 = new Member("id1", null, LocalDateTime.of(2023, 1, 5, 2, 25)); | ||
Member id6 = new Member("id1", null, LocalDateTime.of(2023, 1, 6, 1, 5)); | ||
Member id7 = new Member("id1", null, LocalDateTime.of(2023, 1, 6, 1, 2)); | ||
Member id8 = new Member("id1", null, LocalDateTime.of(2023, 1, 6, 2, 5)); | ||
HourCount hourCount = new HourCount(List.of(id1, id2, id3, id4, id5, id6, id7, id8)); | ||
|
||
Map<LocalDateTime, Integer> memberCountByHour = hourCount.getMemberCountByHour(); | ||
|
||
assertEquals(4, hourCount.getMemberCountByHour().size()); | ||
assertEquals(3,memberCountByHour.get(LocalDateTime.of(2023,1,5,1,0))); | ||
assertEquals(2,memberCountByHour.get(LocalDateTime.of(2023,1,5,2,0))); | ||
assertEquals(2,memberCountByHour.get(LocalDateTime.of(2023,1,6,1,0))); | ||
assertEquals(1,memberCountByHour.get(LocalDateTime.of(2023,1,6,2,0))); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...informatievlaanderen/vsds/demonstrator/member/rest/websocket/LineChartControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.member.rest.websocket; | ||
|
||
import be.informatievlaanderen.vsds.demonstrator.member.application.services.MemberService; | ||
import be.informatievlaanderen.vsds.demonstrator.member.rest.dtos.LineChartDto; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
|
||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class LineChartControllerTest { | ||
|
||
@Mock | ||
private MemberService memberService; | ||
@Mock | ||
private SimpMessagingTemplate simpMessagingTemplate; | ||
@InjectMocks | ||
private LineChartController lineChartController; | ||
|
||
@Test | ||
void test_lineChartIsTransferredToWebsocket(){ | ||
LineChartDto lineChartDto = new LineChartDto(List.of(), List.of()); | ||
when(memberService.getLineChartDto()).thenReturn(lineChartDto); | ||
lineChartController.send(); | ||
|
||
verify(memberService).getLineChartDto(); | ||
verify(simpMessagingTemplate).convertAndSend("/broker/linechart",lineChartDto); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.