Skip to content

Commit

Permalink
feat: cluster dist 별 정렬
Browse files Browse the repository at this point in the history
  • Loading branch information
KangmoonSeo committed Nov 15, 2023
1 parent 5060553 commit 81fa6ec
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.hellokicktty.server.domain.Cluster;
import org.hellokicktty.server.domain.Coordinate;
import org.hellokicktty.server.domain.Kickboard;
import org.hellokicktty.server.dto.KickboardListResponseDto;
import org.hellokicktty.server.dto.KickboardResponseDto;
Expand All @@ -10,7 +11,9 @@
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static org.hellokicktty.server.service.ClusterService.*;

Expand All @@ -26,7 +29,9 @@ public class KickboardController {
KickboardListResponseDto findKickboards(Double lat, Double lng) {

List<Kickboard> kickboardList = kickboardService.findKickboardsInOrder(lat, lng);
List<Cluster> clusters = clusterKickboards(kickboardList);
List<Cluster> clusters = clusterKickboards(kickboardList, lat, lng);


Double distance = getShortestDistance(clusters);

return new KickboardListResponseDto(distance, clusters, kickboardList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class RecommendController {

Logger log = LoggerFactory.getLogger(Logger.class);
private final KickboardService kickboardService;
private final int LEAST_CLUSTER_NUMBER = 4;
private final int MAX_CLUSTER_NUMBER = 4;

@GetMapping("/recommend")
public RecommendResponseDto recommendClustersByKickboardId(Long id) {
Expand All @@ -28,8 +28,8 @@ public RecommendResponseDto recommendClustersByKickboardId(Long id) {
if (kickboard == null) return new RecommendResponseDto();

List<Kickboard> kickboardList = kickboardService.findKickboardsInOrder(kickboard.getLat(), kickboard.getLng());
List<Cluster> clusters = ClusterService.clusterKickboards(kickboardList);
clusters.subList(0, Math.min(LEAST_CLUSTER_NUMBER, clusters.size()));
List<Cluster> clusters = ClusterService.clusterKickboards(kickboardList, kickboard.getLat(), kickboard.getLng());
clusters = clusters.subList(0, Math.min(MAX_CLUSTER_NUMBER, clusters.size()));

return new RecommendResponseDto(clusters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
Expand All @@ -19,7 +20,8 @@ public class UpdateController {
private final KickboardService kickboardService;

@PostMapping("/update")
public void updateKickboards(UpdateRequestDto dto) {
public void updateKickboards(@RequestBody UpdateRequestDto dto) {

List<Kickboard> kickboardList = dto.getKickboards();
for (Kickboard k : kickboardList) {
kickboardService.updateKickboard(k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public Long save(Kickboard kickboard) {
return kickboard.getId();
}

@Transactional
public Long update(Kickboard k) {
Kickboard kickboard = em.find(Kickboard.class, k.getId());
kickboard.update(k.getCluster_id(), k.getDanger(), k.getBorder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import org.hellokicktty.server.domain.Kickboard;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -18,9 +16,9 @@ public class ClusterService {

// == utils ==

public static List<Cluster> clusterKickboards(List<Kickboard> kickboardList) {
public static List<Cluster> clusterKickboards(List<Kickboard> kickboardList, Double lat, Double lng) {

Map<Long, Cluster> clusterMap = new TreeMap<>();
Map<Long, Cluster> clusterMap = new HashMap<>();
for (Kickboard kickboard : kickboardList) {
if (kickboard.getBorder()) {
Long cluster_id = kickboard.getCluster_id();
Expand All @@ -38,8 +36,27 @@ public static List<Cluster> clusterKickboards(List<Kickboard> kickboardList) {
List<Coordinate> borders = cluster.getBorders();
Coordinate center = getCenter(borders);
cluster.setCenter(center);

if (lat == null || lng == null) {
cluster.setDistance(-1d);
continue;
}
double dist = 1e9d;

for (Coordinate border : borders) {
Double delta = KickboardService.getCoordinateDelta(new Coordinate(lat, lng), border);
delta = KickboardService.coordinateToMeter(delta);
dist = Math.min(dist, delta);
}
cluster.setDistance(dist);
}
return new ArrayList<>(clusterMap.values());
if (lat == null || lng == null) return clusterList;

clusterList = clusterList.stream()
.sorted(Comparator.comparingDouble(Cluster::getDistance))
.collect(Collectors.toList());

return clusterList;
}

public static Coordinate getCenter(List<Coordinate> borders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class KickboardService {

public static double CLUSTER_COORDINATE_RANGE = 0.00001 * CLUSTER_METER_RANGE; // degree

private final String URL = "http://localhost:8081/cluster"; // AI Server Request End-Point
private final String URL = "http://localhost:8001/cluster"; // AI Server Request End-Point

@PostConstruct
public void init() {
Expand Down Expand Up @@ -150,9 +150,8 @@ public static Double getCoordinateDelta(Coordinate u, Coordinate v) {
return Math.sqrt(Math.pow(dLat, 2) + Math.pow(dLng, 2));
}

public static Double convertCtoM(Double coordinateDelta) {
return coordinateDelta * 0.00001;
public static Double coordinateToMeter(Double coordinateDelta) {
return coordinateDelta * 100000;
}


}

0 comments on commit 81fa6ec

Please sign in to comment.