Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
Feat: Add linechart
Browse files Browse the repository at this point in the history
  • Loading branch information
Lefeverw committed Oct 3, 2023
1 parent d416494 commit 496131e
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class Application {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import be.informatievlaanderen.vsds.demonstrator.member.application.valueobjects.IngestedMemberDto;
import be.informatievlaanderen.vsds.demonstrator.member.application.valueobjects.MemberDto;
import be.informatievlaanderen.vsds.demonstrator.member.rest.dtos.LineChartDto;
import org.locationtech.jts.geom.Geometry;

import java.time.LocalDateTime;
Expand All @@ -15,4 +16,6 @@ public interface MemberService {
MemberDto getMemberById(String memberId);

long getNumberOfMembers();

LineChartDto getLineChartDto();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import be.informatievlaanderen.vsds.demonstrator.member.application.exceptions.ResourceNotFoundException;
import be.informatievlaanderen.vsds.demonstrator.member.application.valueobjects.IngestedMemberDto;
import be.informatievlaanderen.vsds.demonstrator.member.application.valueobjects.MemberDto;
import be.informatievlaanderen.vsds.demonstrator.member.domain.member.entities.LineChart;
import be.informatievlaanderen.vsds.demonstrator.member.domain.member.entities.Member;
import be.informatievlaanderen.vsds.demonstrator.member.domain.member.repositories.MemberRepository;
import be.informatievlaanderen.vsds.demonstrator.member.rest.dtos.LineChartDto;
import be.informatievlaanderen.vsds.demonstrator.member.rest.websocket.MessageController;
import org.locationtech.jts.geom.Geometry;
import org.opengis.referencing.operation.TransformException;
Expand All @@ -19,6 +21,7 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService {
Expand Down Expand Up @@ -68,4 +71,13 @@ public MemberDto getMemberById(String memberId) {
public long getNumberOfMembers() {
return repository.getNumberOfMembers();
}

@Override
public LineChartDto getLineChartDto() {
long numberOfMembers = getNumberOfMembers();
LocalDateTime startDate = LocalDateTime.now().minusDays(7);
List<Member> membersAfterLocalDateTime = repository.findMembersAfterLocalDateTime(startDate);
LineChart lineChart = new LineChart(startDate,numberOfMembers-membersAfterLocalDateTime.size(),membersAfterLocalDateTime);
return new LineChartDto(lineChart.getLabels(), lineChart.getValues());
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public interface MemberRepository {
void saveMember(Member geometry);
List<Member> getMembersByGeometry(Geometry geometry, LocalDateTime startTime, LocalDateTime endTime);
Optional<Member> findByMemberId(String memberId);

long getNumberOfMembers();
List<Member> findMembersAfterLocalDateTime(LocalDateTime localDateTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Stream;

public interface MemberEntityJpaRepository extends JpaRepository<MemberEntity, String> {

@Query(value = "select l from member_entity l where intersects(l.geometry, :geometry) = true and l.timestamp >= :startTime and l.timestamp <= :endTime")
List<MemberEntity> getMemberGeometryEntitiesCoveredByGeometryInTimePeriod(@Param("geometry") Geometry geometry, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

List<MemberEntity> findByTimestampAfter(LocalDateTime localDateTime);

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ public Optional<Member> findByMemberId(String memberId) {
public long getNumberOfMembers() {
return memberGeometryJpaRepo.count();
}

@Override
public List<Member> findMembersAfterLocalDateTime(LocalDateTime localDateTime) {
return memberGeometryJpaRepo
.findByTimestampAfter(localDateTime)
.stream()
.map(entity->new Member(entity.getMemberId(), entity.getGeometry(), entity.getTimestamp()))
.toList();
}
}
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;
}
}
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());
}
}
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MemberCounterControllerTest {
private MemberCounterController memberCounterController;

@Test
void test(){
void test_numberOfMembersIsTransferredToWebsocket(){
when(memberService.getNumberOfMembers()).thenReturn(76L);
memberCounterController.send();

Expand Down
64 changes: 55 additions & 9 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
"@livereader/graphly-d3-vue": "^1.1.2",
"@terraformer/wkt": "^2.2.0",
"axios": "^1.5.0",
"chart.js": "^4.1.1",
"d3": "^7.8.5",
"proj4": "^2.9.0",
"proj4leaflet": "^1.0.2",
"vue": "^3.3.4",
"vue-chartjs": "^5.2.0",
"vue-router": "^4.2.4",
"webstomp-client": "^1.2.6"
},
"devDependencies": {
"@types/chart.js": "^2.9.38",
"@vitejs/plugin-vue": "^4.3.1",
"leaflet": "^1.9.4",
"vite": "^4.4.9"
Expand Down
Loading

0 comments on commit 496131e

Please sign in to comment.