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] 버스 위치 정보 수집 스케줄러 구현 #96

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions data/mysql-files/add_constraints.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
-- bus_location 테이블의 외래 키 추가
ALTER TABLE talkka_db.bus_location
ADD CONSTRAINT FKohe5hrtdo44wqkqfc96kpb30m
FOREIGN KEY (route_id) REFERENCES talkka_db.bus_route (route_id);

-- bus_route_station 테이블의 외래 키 추가
ALTER TABLE talkka_db.bus_route_station
ADD CONSTRAINT FKf66j4cjj3igamxvlxvbvm3shp
Expand Down Expand Up @@ -35,3 +30,10 @@ ALTER TABLE talkka_db.subway_review
FOREIGN KEY (station_id) REFERENCES talkka_db.subway_station (station_id),
ADD CONSTRAINT FKt1tn431cfkx0p8qvx8k6hd6i5
FOREIGN KEY (user_id) REFERENCES talkka_db.users (user_id);

-- bookmark 테이블의 외래 키 추가
ALTER TABLE bookmark
ADD CONSTRAINT FK_BOOKMARK_ON_USER FOREIGN KEY (user_id) REFERENCES users (user_id);

ALTER TABLE bookmark_detail
ADD CONSTRAINT FK_BOOKMARK_DETAIL_ON_BOOKMARK FOREIGN KEY (bookmark_id) REFERENCES bookmark (bookmark_id);
67 changes: 58 additions & 9 deletions data/mysql-files/init_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ create table bus_location
remain_seat_count smallint not null,
station_seq smallint not null,
created_at datetime(6) not null,
route_id bigint null,
station_id bigint not null,
api_route_id varchar(20) not null,
api_station_id varchar(20) not null,
api_call_no int not null,
plate_no varchar(32) not null
);

Expand Down Expand Up @@ -100,7 +101,22 @@ create table bus_review
updated_at datetime(6) not null,
user_id bigint null,
content varchar(400) null,
time_slot varchar(3) not null
time_slot varchar(20) not null
);

drop table if exists subway_review;
create table subway_review
(
subway_review_id bigint auto_increment primary key,
user_id bigint null,
station_id bigint null,
line_code VARCHAR(4) not null,
updown VARCHAR(1) not null,
content varchar(400) null,
time_slot varchar(20) not null,
rating int not null,
created_at datetime(6) not null,
updated_at datetime(6) not null
);

drop table if exists subway_confusion;
Expand All @@ -113,7 +129,7 @@ CREATE TABLE subway_confusion
line_code VARCHAR(4) NOT NULL,
day_type VARCHAR(3) NOT NULL,
updown VARCHAR(1) NOT NULL,
time_slot VARCHAR(2) NOT NULL,
time_slot VARCHAR(20) NOT NULL,
confusion DOUBLE NULL
);

Expand Down Expand Up @@ -144,6 +160,29 @@ CREATE TABLE subway_timetable
end_station_name VARCHAR(50) NOT NULL
);

DROP TABLE IF EXISTS bookmark;
CREATE TABLE bookmark
(
bookmark_id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
user_id BIGINT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
);

DROP TABLE IF EXISTS bookmark_detail;
CREATE TABLE bookmark_detail
(
bookmark_detail_id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
seq INT NOT NULL,
type VARCHAR(10) NOT NULL,
bookmark_id BIGINT NULL,
subway_station_id BIGINT NULL,
subway_updown VARCHAR(255) NULL,
bus_route_station_id BIGINT NULL,
created_at datetime NOT NULL
);


-- BULK INSERT (STATIC DATA)
# SOURCE /var/lib/mysql-files/bulk_insert_routes.sql;
Expand Down Expand Up @@ -306,11 +345,6 @@ DROP TABLE temp_bus_route_station;

-- BULK INSERT 이후 DB 의 FK CONSTRAINT 를 잡아줌
# SOURCE /var/lib/mysql-files/add_constraints.sql;
-- bus_location 테이블의 외래 키 추가
ALTER TABLE talkka_db.bus_location
ADD CONSTRAINT FKohe5hrtdo44wqkqfc96kpb30m
FOREIGN KEY (route_id) REFERENCES talkka_db.bus_route (route_id);

-- bus_route_station 테이블의 외래 키 추가
ALTER TABLE talkka_db.bus_route_station
ADD CONSTRAINT FKf66j4cjj3igamxvlxvbvm3shp
Expand All @@ -336,3 +370,18 @@ ALTER TABLE talkka_db.bus_review
FOREIGN KEY (route_id) REFERENCES talkka_db.bus_route (route_id),
ADD CONSTRAINT FKojkl7pwovnccu0txrbvxk97jd
FOREIGN KEY (user_id) REFERENCES talkka_db.users (user_id);

-- subway_review 테이블의 외래 키 추가
ALTER TABLE talkka_db.subway_review
ADD CONSTRAINT FKjbnhvh0i4ey6y3bt2ipouhw41
FOREIGN KEY (station_id) REFERENCES talkka_db.subway_station (station_id),
ADD CONSTRAINT FKt1tn431cfkx0p8qvx8k6hd6i5
FOREIGN KEY (user_id) REFERENCES talkka_db.users (user_id);

-- bookmark 테이블의 외래 키 추가
ALTER TABLE bookmark
ADD CONSTRAINT FK_BOOKMARK_ON_USER FOREIGN KEY (user_id) REFERENCES users (user_id);

-- bookmark_detail 테이블의 외래 키 추가
ALTER TABLE bookmark_detail
ADD CONSTRAINT FK_BOOKMARK_DETAIL_ON_BOOKMARK FOREIGN KEY (bookmark_id) REFERENCES bookmark (bookmark_id);
70 changes: 32 additions & 38 deletions data/mysql-files/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ create table bus_location
remain_seat_count smallint not null,
station_seq smallint not null,
created_at datetime(6) not null,
route_id bigint null,
station_id bigint not null,
api_route_id varchar(20) not null,
api_station_id varchar(20) not null,
api_call_no int not null,
plate_no varchar(32) not null
);

Expand Down Expand Up @@ -99,16 +100,16 @@ create table bus_review
drop table if exists subway_review;
create table subway_review
(
subway_review_id bigint auto_increment primary key,
user_id bigint null,
station_id bigint null,
line_code VARCHAR(4) not null,
updown VARCHAR(1) not null,
content varchar(400) null,
time_slot varchar(20) not null,
rating int not null,
created_at datetime(6) not null,
updated_at datetime(6) not null
subway_review_id bigint auto_increment primary key,
user_id bigint null,
station_id bigint null,
line_code VARCHAR(4) not null,
updown VARCHAR(1) not null,
content varchar(400) null,
time_slot varchar(20) not null,
rating int not null,
created_at datetime(6) not null,
updated_at datetime(6) not null
);

drop table if exists subway_confusion;
Expand Down Expand Up @@ -152,32 +153,25 @@ CREATE TABLE subway_timetable
end_station_name VARCHAR(50) NOT NULL
);

DROP TABLE IF EXISTS bookmark;
CREATE TABLE bookmark
(
bookmark_id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
user_id BIGINT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
);

-- BULK INSERT (STATIC DATA)
# SOURCE /var/lib/mysql-files/bulk_insert_routes.sql;
-- 1. 임시 테이블 생성
DROP TABLE IF EXISTS temp_bus_route;
CREATE TABLE temp_bus_route
DROP TABLE IF EXISTS bookmark_detail;
CREATE TABLE bookmark_detail
(
route_id BIGINT,
route_name VARCHAR(100),
route_type_cd VARCHAR(10),
route_type_name VARCHAR(100),
start_station_id BIGINT,
start_station_name VARCHAR(100),
start_mobile_no VARCHAR(50),
end_station_id BIGINT,
end_station_name VARCHAR(100),
end_mobile_no VARCHAR(50),
region_name VARCHAR(100),
district_cd VARCHAR(10),
up_first_time VARCHAR(20),
up_last_time VARCHAR(20),
down_first_time VARCHAR(20),
down_last_time VARCHAR(20),
peek_alloc INT,
n_peek_alloc INT,
company_id BIGINT,
company_name VARCHAR(100),
company_tel VARCHAR(50)
bookmark_detail_id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
seq INT NOT NULL,
type VARCHAR(10) NOT NULL,
bookmark_id BIGINT NULL,
subway_station_id BIGINT NULL,
subway_updown VARCHAR(255) NULL,
bus_route_station_id BIGINT NULL,
created_at datetime NOT NULL
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
package com.talkka.server.api.datagg.dto;

import java.time.LocalDateTime;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.talkka.server.bus.dao.BusLocationEntity;
import com.talkka.server.bus.enums.EndBus;
import com.talkka.server.bus.enums.LowPlate;
import com.talkka.server.bus.enums.PlateType;
import com.talkka.server.common.util.EnumCodeConverterUtils;

@JacksonXmlRootElement(localName = "busLocationList")
public record BusLocationBodyDto(String endBus, String lowPlate, String plateNo, String plateType,
Integer remainSeatCnt, Long routeId, Long stationId, Integer stationSeq) {
Integer remainSeatCnt, String routeId, String stationId, Integer stationSeq) {

public BusLocationEntity toEntity(int apiCallNo, LocalDateTime createdAt) {
EndBus endBusEnum = EnumCodeConverterUtils.fromCode(EndBus.class, endBus);
LowPlate lowPlateEnum = EnumCodeConverterUtils.fromCode(LowPlate.class, lowPlate);
PlateType plateTypeEnum = EnumCodeConverterUtils.fromCode(PlateType.class, plateType);

return BusLocationEntity.builder()
.apiRouteId(routeId)
.apiStationId(stationId)
.stationSeq(stationSeq.shortValue())
.endBus(endBusEnum)
.lowPlate(lowPlateEnum)
.plateNo(plateNo)
.plateType(plateTypeEnum)
.remainSeatCount(remainSeatCnt.shortValue())
.apiCallNo(apiCallNo)
.createdAt(createdAt)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.net.URI;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
Expand All @@ -25,7 +27,8 @@

@Service
@RequiredArgsConstructor
public class BusApiServiceImpl implements BusApiService {
public class SimpleBusApiService implements BusApiService {
private static final Logger log = LoggerFactory.getLogger(SimpleBusApiService.class);
private final BusApiKeyProperty busApiKeyProperty;
private final RestTemplate restTemplate = new RestTemplate();
private static final String host = "apis.data.go.kr";
Expand All @@ -38,7 +41,11 @@ public List<BusRouteSearchBodyDto> getSearchedRouteInfo(String keyword) throws A
try {
URI uri = this.getOpenApiUri(path, params);
ResponseEntity<BusRouteSearchRespDto> resp = restTemplate.getForEntity(uri, BusRouteSearchRespDto.class);
return resp.getBody().msgBody();
var body = resp.getBody().msgBody();
if (body == null) {
throw new ApiClientException("결과가 없습니다.");
}
return body;
} catch (Exception exception) {
throw new ApiClientException(exception.getMessage());
}
Expand All @@ -52,7 +59,11 @@ public List<BusRouteInfoBodyDto> getRouteInfo(String apiRouteId) throws ApiClien
try {
URI uri = this.getOpenApiUri(path, params);
ResponseEntity<BusRouteInfoRespDto> resp = restTemplate.getForEntity(uri, BusRouteInfoRespDto.class);
return resp.getBody().msgBody();
var body = resp.getBody().msgBody();
if (body == null) {
throw new ApiClientException("결과가 없습니다.");
}
return body;
} catch (Exception exception) {
throw new ApiClientException(exception.getMessage());
}
Expand All @@ -66,7 +77,11 @@ public List<BusRouteStationBodyDto> getRouteStationInfo(String apiRouteId) throw
try {
URI uri = this.getOpenApiUri(path, params);
ResponseEntity<BusRouteStationRespDto> resp = restTemplate.getForEntity(uri, BusRouteStationRespDto.class);
return resp.getBody().msgBody();
var body = resp.getBody().msgBody();
if (body == null) {
throw new ApiClientException("결과가 없습니다.");
}
return body;
} catch (Exception exception) {
throw new ApiClientException(exception.getMessage());
}
Expand All @@ -80,7 +95,11 @@ public List<BusLocationBodyDto> getBusLocationInfo(String apiRouteId) throws Api
try {
URI uri = this.getOpenApiUri(path, params);
ResponseEntity<BusLocationRespDto> resp = restTemplate.getForEntity(uri, BusLocationRespDto.class);
return resp.getBody().msgBody();
var body = resp.getBody().msgBody();
if (body == null) {
throw new ApiClientException("결과가 없습니다.");
}
return body;
} catch (Exception exception) {
throw new ApiClientException(exception.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.LocalDateTime;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.talkka.server.bus.enums.EndBus;
Expand All @@ -19,8 +18,6 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -38,12 +35,11 @@ public class BusLocationEntity {
@Column(name = "bus_location_id", nullable = false)
private Long busLocationId;

@ManyToOne
@JoinColumn(name = "route_id")
private BusRouteEntity route;
@Column(name = "api_route_id", nullable = false, length = 20)
private String apiRouteId;

@Column(name = "station_id", nullable = false)
private Long stationId;
@Column(name = "api_station_id", nullable = false, length = 20)
private String apiStationId;

@Column(name = "station_seq", nullable = false)
private Short stationSeq;
Expand All @@ -66,8 +62,10 @@ public class BusLocationEntity {
@Column(name = "remain_seat_count", nullable = false)
private Short remainSeatCount;

@Column(name = "api_call_no", nullable = false)
private Integer apiCallNo;

@Column(name = "created_at", nullable = false)
@CreatedDate
private LocalDateTime createdAt;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ public class BusRouteEntity {
@LastModifiedDate
private LocalDateTime updatedAt;

// 추후 논의 필요
@OneToMany(mappedBy = "route")
@Builder.Default
private List<BusLocationEntity> routeLocations = new ArrayList<>();

@OneToMany(mappedBy = "route")
@Builder.Default
private List<BusRouteStationEntity> stations = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Getter
public enum EndBus implements EnumCodeInterface {
// "0 = RUNNING" or "1 = END"
RUNNING("0"), END("1");
RUNNING("0"), END("1"), LENT_BUS_END("4");

private final String code;

Expand Down
Loading
Loading