Skip to content

Commit

Permalink
The "last" endpoint was taking more than 6 seconds to return. The
Browse files Browse the repository at this point in the history
problem was solved.
  • Loading branch information
leandro committed Nov 10, 2021
1 parent cb99a1a commit 8174a79
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
*.tmp
/.mvn/wrapper/maven-wrapper.jar
/.mvn/wrapper/maven-wrapper.properties
/.metadata/
33 changes: 33 additions & 0 deletions beacon-engine/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
33 changes: 33 additions & 0 deletions beacon-input/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
31 changes: 31 additions & 0 deletions beacon-interface/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/target/
!.mvn/wrapper/maven-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

*.log
*.gz
*.tmp
/.mvn/wrapper/maven-wrapper.jar
/.mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.example.beacon.interfac.api;

import com.example.beacon.interfac.api.dto.PulseDto;
import com.example.beacon.interfac.domain.service.QuerySinglePulsesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.beacon.interfac.api.dto.PulseDto;
import com.example.beacon.interfac.domain.service.QuerySinglePulsesService;

@CrossOrigin(origins = "*", allowedHeaders = "*")
@Controller
Expand Down Expand Up @@ -53,7 +58,7 @@ public ResponseEntity last(@PathVariable Long chainIndex){
@ResponseBody
public ResponseEntity chainAndPulse(@PathVariable Long chainIndex, @PathVariable Long pulseIndex){
try {
PulseDto pulseDto = singlePulsesService.findByChainAndPulseIndex(chainIndex, pulseIndex);
PulseDto pulseDto = singlePulsesService.findByChainAndPulseId(chainIndex, pulseIndex);

if (pulseDto==null){
return new ResponseEntity("Pulse Not Available.", HttpStatus.NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.example.beacon.interfac.domain.repository;

import com.example.beacon.interfac.infra.PulseEntity;

import java.time.ZonedDateTime;

import com.example.beacon.interfac.infra.PulseEntity;

public interface PulsesQueries {
PulseEntity last(Long chain);
PulseEntity first(Long chain);
PulseEntity findByChainAndPulseIndex(Long chainIndex, Long pulseIndex);
PulseEntity findByChainAndPulseId(Long chainIndex, Long pulseIndex);
PulseEntity findByTimestamp(ZonedDateTime timeStamp);
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
package com.example.beacon.interfac.domain.repository;

import com.example.beacon.interfac.infra.PulseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.beacon.interfac.infra.PulseEntity;

@Repository
public class PulsesRepositoryImpl implements PulsesQueries {

@PersistenceContext
private EntityManager manager;

@Transactional
@Transactional(readOnly = true)
public PulseEntity last(Long chainIndex){
Long lastPulseIndex = (Long) manager.createQuery(
"select max(p.pulseIndex) from PulseEntity p where p.chainIndex = :chainIndex")
Long lastPulseId = (Long) manager.createQuery(
"select max(p.id) from PulseEntity p where p.chainIndex = :chainIndex")
.setParameter("chainIndex", chainIndex)
.getSingleResult();

if (lastPulseIndex==null){
if (lastPulseId==null){
return null;
} else {
return findByChainAndPulseIndex(chainIndex, lastPulseIndex);
return findByChainAndPulseId(chainIndex, lastPulseId);
}
}

@Transactional
@Transactional(readOnly = true)
public PulseEntity first(Long chainIndex){
Long firstPulseIndex = (Long) manager.createQuery(
"select min(p.pulseIndex) from PulseEntity p where p.chainIndex = :chainIndex")
Expand All @@ -41,7 +43,7 @@ public PulseEntity first(Long chainIndex){
if (firstPulseIndex==null){
return null;
} else {
return findByChainAndPulseIndex(chainIndex, firstPulseIndex);
return findByChainAndPulseId(chainIndex, firstPulseIndex);
}
}

Expand All @@ -55,20 +57,24 @@ public List<PulseEntity> obterTodos(Integer chainIndex){
}

@Transactional(readOnly = true)
public PulseEntity findByChainAndPulseIndex(Long chainIndex, Long pulseIndex){
public PulseEntity findByChainAndPulseId(Long chainIndex, Long pulseId){
try {
PulseEntity recordEntity = (PulseEntity) manager
.createQuery("from PulseEntity p " +
"join fetch p.listValueEntities lve " +
"where p.chainIndex = :chainIndex and p.pulseIndex = :pulseIndex")
"where p.chainIndex = :chainIndex and p.id = :pulseId")
.setParameter("chainIndex", chainIndex)
.setParameter("pulseIndex", pulseIndex)
.setParameter("pulseId", pulseId)
.getSingleResult();

return recordEntity;
} catch (NoResultException e){
return null;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}

// @Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.example.beacon.interfac.domain.service;

import com.example.beacon.interfac.domain.repository.PulsesRepository;
import com.example.beacon.interfac.api.dto.PulseDto;
import com.example.beacon.interfac.infra.PulseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.beacon.interfac.api.dto.PulseDto;
import com.example.beacon.interfac.domain.repository.PulsesRepository;
import com.example.beacon.interfac.infra.PulseEntity;

@Service
public class QuerySinglePulsesService {

Expand Down Expand Up @@ -116,8 +117,8 @@ public PulseDto findLast() {

}

public PulseDto findByChainAndPulseIndex(Long chainIndex, Long pulseIndex){
PulseEntity last = pulsesRepository.findByChainAndPulseIndex(chainIndex, pulseIndex);
public PulseDto findByChainAndPulseId(Long chainIndex, Long pulseId){
PulseEntity last = pulsesRepository.findByChainAndPulseId(chainIndex, pulseId);

if (last == null){
return null;
Expand Down
33 changes: 33 additions & 0 deletions beacon-libs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

0 comments on commit 8174a79

Please sign in to comment.