Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 충전기 상태 및 혼잡도를 저장하는 기능을 구현한다 #113

Merged
merged 9 commits into from
Jul 20, 2023
11 changes: 11 additions & 0 deletions backend/src/docs/asciidoc/station.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ include::{snippets}/charger-station-controller-test/findChargeStationByInvalidId

include::{snippets}/charger-station-controller-test/findChargeStationByInvalidId/http-response.adoc[]
include::{snippets}/charger-station-controller-test/findChargeStationByInvalidId/response-fields.adoc[]

== 충전소의 id 값을 기준으로 충전소의 혼잡도를 조회한다 (/stations/{stationId}/statistics)

=== Request

include::{snippets}/congestion-controller-test/statistics/http-request.adoc[]

=== Response

include::{snippets}/congestion-controller-test/statistics/http-response.adoc[]
include::{snippets}/congestion-controller-test/statistics/response-fields.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;

@Configuration
public class RestTemplateConfig {

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
DefaultUriBuilderFactory defaultUriBuilderFactory = new DefaultUriBuilderFactory();
defaultUriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(defaultUriBuilderFactory);
return restTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.carffeine.carffeine.config;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

@EnableScheduling
@Component
public class SchedulingConfig {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import java.util.List;
import java.util.Map;

public record CongestionResponse(Map<String, List<CongestionInfoResponse>> STANDARD,
Map<String, List<CongestionInfoResponse>> QUICK) {
public record CongestionResponse(Map<String, List<CongestionInfoResponse>> standard,
Map<String, List<CongestionInfoResponse>> quick) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.carffeine.carffeine.controller.congestion;

import com.carffeine.carffeine.controller.chargerStation.dto.StatisticsResponse;
import com.carffeine.carffeine.service.chargerstation.CongestionService;
import com.carffeine.carffeine.service.chargerstation.dto.StatisticsRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
public class CongestionController {

private final CongestionService congestionService;

@GetMapping("/api/stations/{stationId}/statistics")
public ResponseEntity<StatisticsResponse> showCongestionStatistics(@PathVariable String stationId) {
StatisticsResponse statisticsResponse = congestionService.calculateCongestion(new StatisticsRequest(stationId));
return ResponseEntity.ok(statisticsResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -15,13 +16,13 @@
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@EqualsAndHashCode(of = "stationId")
@Table(name = "charge_station")
public class ChargeStation {

Expand Down Expand Up @@ -63,21 +64,4 @@ public int getAvailableCount() {
.filter(Charger::isAvailable)
.count();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ChargeStation that = (ChargeStation) o;
return Objects.equals(stationId, that.stationId);
}

@Override
public int hashCode() {
return Objects.hash(stationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
@Entity
@Table(name = "charger")
public class Charger {
private static final BigDecimal OUTPUT_THRESHOLD = BigDecimal.valueOf(50);

@Id
@Column(name = "station_id")
Expand Down Expand Up @@ -60,6 +61,16 @@ public class Charger {
private ChargeStation chargeStation;

public boolean isAvailable() {
if (chargerStatus == null) {
return false;
}
return chargerStatus.isAvailable();
}

public boolean isQuick() {
if (capacity == null) {
return false;
}
return capacity.compareTo(OUTPUT_THRESHOLD) >= 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
import java.util.List;

public interface ChargerRepository extends Repository<Charger, String> {

List<Charger> findAllByStationId(String stationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public static ChargerState from(int input) {
.orElse(STATUS_UNKNOWN);
}

public static ChargerState from(String input) {
return Arrays.stream(ChargerState.values())
.filter(it -> it.value == input.charAt(0) - '0')
.findAny()
.orElse(STATUS_UNKNOWN);
}

public boolean isStandBy() {
return this == STANDBY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import java.time.LocalDateTime;

@ToString
@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@IdClass(ChargerId.class)
@Entity
@Table(name = "charger_status")
public class ChargerStatus {

@Id
Expand All @@ -30,6 +36,7 @@ public class ChargerStatus {

private LocalDateTime latestUpdateTime;

@Enumerated(EnumType.STRING)
private ChargerState chargerState;

public boolean isAvailable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.carffeine.carffeine.domain.chargestation.charger;

import org.springframework.data.repository.Repository;

import java.util.List;

public interface ChargerStatusRepository extends Repository<ChargerStatus, ChargerId> {

List<ChargerStatus> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package com.carffeine.carffeine.domain.chargestation.congestion;

import com.carffeine.carffeine.domain.chargestation.charger.Charger;

import java.time.DayOfWeek;

public class IdGenerator {

private IdGenerator() {
}

public static String generateIdWithCharger(DayOfWeek dayOfWeek, RequestPeriod startTime, Charger charger) {
return generateId(dayOfWeek, startTime, charger.getStationId(), charger.getChargerId());
}

public static String generateId(DayOfWeek dayOfWeek, RequestPeriod startTime, String stationId, String chargerId) {
return dayOfWeek.getValue() * 10000L + startTime.getSection() + stationId + chargerId;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id값이 유니크한 값을 가지면서 데이터처럼 사용되네요👍 이 방법은 추후에 문제점은 없을까요?

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
Expand All @@ -21,6 +23,7 @@
@Entity
@Table(name = "periodic_congestion")
public class PeriodicCongestion {

@Id
private String id;
private DayOfWeek dayOfWeek;
Expand All @@ -30,10 +33,10 @@ public class PeriodicCongestion {
private double congestion;

@ManyToOne
@JoinColumns({
@JoinColumns(value = {
@JoinColumn(name = "station_id", referencedColumnName = "station_id", insertable = false, updatable = false),
@JoinColumn(name = "charger_id", referencedColumnName = "charger_id", insertable = false, updatable = false)
})
}, foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
private Charger charger;

@Column(name = "station_id")
Expand All @@ -53,15 +56,6 @@ public PeriodicCongestion(String id, DayOfWeek dayOfWeek, RequestPeriod startTim
this.chargerId = chargerId;
}

public PeriodicCongestion(Charger charger, DayOfWeek dayOfWeek, RequestPeriod startTime, int useCount, int totalCount, double congestion) {
id = IdGenerator.generateIdWithCharger(dayOfWeek, startTime, charger);
this.dayOfWeek = dayOfWeek;
this.startTime = startTime;
this.useCount = useCount;
this.totalCount = totalCount;
this.congestion = congestion;
}

public static PeriodicCongestion of(DayOfWeek dayOfWeek, RequestPeriod startTime, int useCount, int totalCount, String stationId, String chargerId) {
String id = IdGenerator.generateId(dayOfWeek, startTime, stationId, chargerId);
double congestion = (double) useCount / totalCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;

public interface PeriodicCongestionRepository extends Repository<PeriodicCongestion, String> {

List<PeriodicCongestion> findAllByDayOfWeekAndStartTime(DayOfWeek dayOfWeek, RequestPeriod startTime);

PeriodicCongestion save(PeriodicCongestion periodicCongestion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

@Getter
public enum RequestPeriod {

ZERO(0),
TWELVE(1200);

private static final int UNIT = 100;
private static final List<RequestPeriod> periods = Arrays.stream(values())
.sorted(Comparator.comparingInt(RequestPeriod::getSection))
.toList();

private final int section;

RequestPeriod(int section) {
Expand All @@ -22,7 +25,7 @@ public enum RequestPeriod {

public static RequestPeriod from(int hour) {
return periods.stream()
.filter(it -> it.section <= hour * 100)
.filter(it -> it.section <= hour * UNIT)
.findFirst()
.orElseThrow();
}
Expand Down
Loading