Skip to content

Commit

Permalink
[HOTFIX] redis geo error
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Jan 16, 2024
1 parent 24224ae commit 09de186
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,10 @@ public boolean hasMember(final Member member) {
public void removeMember(final GroupMember groupMember) {
this.groupMembers.remove(groupMember);
}

public String[] getMemberIds() {
return this.groupMembers.stream()
.map(groupMember -> groupMember.getMember().getId().toString())
.toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
import com.twtw.backend.domain.location.dto.request.LocationRequest;
import com.twtw.backend.domain.location.dto.response.AverageCoordinate;
import com.twtw.backend.domain.member.entity.Member;

import lombok.RequiredArgsConstructor;

import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class GeoService {

private static final int CURRENT_LOCATION_INDEX = 0;
private static final Distance DEFAULT_DISTANCE = new Distance(0, Metrics.KILOMETERS);
private final RedisTemplate<String, String> redisTemplate;

public AverageCoordinate saveLocation(
Expand All @@ -34,37 +33,35 @@ public AverageCoordinate saveLocation(
new Point(locationRequest.getLongitude(), locationRequest.getLatitude()),
memberId);

return calculateAverage(collectMemberDistances(planId), planId, memberId);
return calculateAverage(collectMemberDistances(planId, group), planId, memberId);
}

private MemberDistances collectMemberDistances(final String planId) {
return redisTemplate.opsForSet().members(planId).stream()
.map(
member ->
redisTemplate
.opsForGeo()
.position(planId, member)
.get(CURRENT_LOCATION_INDEX))
.collect(Collectors.collectingAndThen(Collectors.toList(), MemberDistances::new));
private MemberDistances collectMemberDistances(final String groupId, final Group group) {
final List<Point> points = redisTemplate.opsForGeo().position(groupId, group.getMemberIds());
return new MemberDistances(points);
}

private AverageCoordinate calculateAverage(
final MemberDistances memberDistances, final String planId, final String memberId) {
final MemberDistances memberDistances, final String groupId, final String memberId) {

final double averageLongitude = memberDistances.averageLongitude();
final double averageLatitude = memberDistances.averageLatitude();

redisTemplate.opsForGeo().add(planId, new Point(averageLongitude, averageLatitude), planId);
redisTemplate.opsForGeo().add(groupId, new Point(averageLongitude, averageLatitude), groupId);

final Double distance = distance(planId, memberId);
final Distance distance = distance(groupId, memberId);

return new AverageCoordinate(averageLongitude, averageLatitude, distance);
return new AverageCoordinate(averageLongitude, averageLatitude, distance.getValue());
}

private double distance(final String planId, final String memberId) {
return redisTemplate
private Distance distance(final String groupId, final String memberId) {
final Distance distance = redisTemplate
.opsForGeo()
.distance(planId, memberId, planId, Metrics.KILOMETERS)
.getValue();
.distance(groupId, memberId, groupId, Metrics.KILOMETERS);

if (distance == null) {
return DEFAULT_DISTANCE;
}
return distance;
}
}

0 comments on commit 09de186

Please sign in to comment.