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
15 changed files
with
349 additions
and
14 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
57 changes: 57 additions & 0 deletions
57
...va/be/informatievlaanderen/vsds/demonstrator/member/domain/member/entities/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,57 @@ | ||
package be.informatievlaanderen.vsds.demonstrator.member.domain.member.entities; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class LineChart { | ||
private final List<Member> memberList; | ||
private final long numberOfMembersOutsideFrame; | ||
private final List<String> labels; | ||
private final List<List<Integer>> values; | ||
private final LocalDateTime startDate; | ||
|
||
public LineChart(LocalDateTime startDate, long numberOfMembersOutsideFrame, List<Member> memberList) { | ||
this.startDate = startDate; | ||
this.numberOfMembersOutsideFrame = numberOfMembersOutsideFrame; | ||
this.memberList = memberList; | ||
this.labels = new ArrayList<>(); | ||
this.values = new ArrayList<>(); | ||
} | ||
|
||
private void calculatePointElements() { | ||
Map<LocalDateTime, Integer> collect = memberList | ||
.stream() | ||
.map(Member::getTimestamp) | ||
.map(localDateTime -> localDateTime.truncatedTo(ChronoUnit.HOURS)) | ||
.collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e -> 1))); | ||
LocalDateTime startTime = startDate.truncatedTo(ChronoUnit.HOURS); | ||
long memberCount = numberOfMembersOutsideFrame; | ||
values.add(new ArrayList<>()); | ||
while (startTime.isBefore(LocalDateTime.now())) { | ||
|
||
memberCount = memberCount + collect.getOrDefault(startTime, 0); | ||
if (memberCount > 0) { | ||
labels.add(startTime.toString()); | ||
values.get(0).add((int) memberCount); | ||
} | ||
startTime = startTime.plusHours(1); | ||
} | ||
} | ||
|
||
public List<String> getLabels() { | ||
if (labels.isEmpty()) | ||
calculatePointElements(); | ||
return labels; | ||
} | ||
|
||
public List<List<Integer>> getValues() { | ||
if (values.isEmpty()) | ||
calculatePointElements(); | ||
return 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
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()); | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.