Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

@RestController
@RequestMapping("/api/v1/stats")
Expand All @@ -22,33 +21,26 @@ public class CaseStatsController {
// 개요 조회
@GetMapping("/overview")
public ResponseEntity<?> getOverview(HttpSession session) {
try {
CaseStatsOverviewResponse response = caseStatsService.getOverview(session);
return ResponseEntity.ok(response);
} catch (IllegalStateException e) {
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
} catch (NoSuchElementException e) {
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류 발생"));
// 결과가 없으면 404, 있으면 200
CaseStatsOverviewResponse response = caseStatsService.getOverview(session);
if (response == null) {
return ResponseEntity.status(404)
.body(Collections.singletonMap("message", "개요 정보가 없습니다."));
}
return ResponseEntity.ok(response);
}

// 시간대별 사건 수 조회 (0시~23시 모두 반환)
@GetMapping("/hour")
public ResponseEntity<?> getHourlyCaseStats(@RequestParam("date") String date,
@RequestParam(value = "category", required = false) String category,
HttpSession session) {
try {
List<HourlyCaseStatsResponse> stats = caseStatsService.getHourlyCaseStats(date, category, session);
return ResponseEntity.ok(stats);
} catch (IllegalStateException e) {
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
} catch (NoSuchElementException e) {
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
List<HourlyCaseStatsResponse> stats = caseStatsService.getHourlyCaseStats(date, category, session);
if (stats.isEmpty()) {
return ResponseEntity.status(404)
.body(Collections.singletonMap("message", "시간대별 사건 정보가 없습니다."));
}
return ResponseEntity.ok(stats);
}

// 월별/일별 사건 수 조회 (month 파라미터 존재 여부에 따라 분기; 전체 범위를 0으로 채워 반환)
Expand All @@ -57,70 +49,36 @@ public ResponseEntity<?> getCaseStats(@RequestParam("year") int year,
@RequestParam(value = "month", required = false) Integer month,
@RequestParam(value = "category", required = false) String category,
HttpSession session) {
try {
if (month == null) {
List<MonthlyCaseStatsResponse> monthlyStats = caseStatsService.getMonthlyCaseStats(year, category, session);
return ResponseEntity.ok(monthlyStats);
}
List<DailyCaseStatsResponse> dailyStats = caseStatsService.getDailyCaseStats(year, month, category, session);
return ResponseEntity.ok(dailyStats);
} catch (IllegalStateException e) {
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
} catch (NoSuchElementException e) {
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
// month가 없으면 월별 데이터
if (month == null) {
List<MonthlyCaseStatsResponse> monthlyStats = caseStatsService.getMonthlyCaseStats(year, category, session);
return ResponseEntity.ok(monthlyStats);
}

// month가 있으면 일별 데이터
List<DailyCaseStatsResponse> dailyStats = caseStatsService.getDailyCaseStats(year, month, category, session);
return ResponseEntity.ok(dailyStats);
}

// 유형별 사건 수 조회 (기본 카테고리 0 포함)
@GetMapping("/category")
public ResponseEntity<?> getCategoryCaseStats(@RequestParam("period") String period, HttpSession session) {
try {
Map<String, Integer> stats = caseStatsService.getCategoryCaseStats(period, session);
return ResponseEntity.ok(stats);
} catch (IllegalStateException e) {
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
} catch (IllegalArgumentException e) {
return ResponseEntity.status(400).body(Collections.singletonMap("message", e.getMessage()));
} catch (NoSuchElementException e) {
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
}
Map<String, Integer> stats = caseStatsService.getCategoryCaseStats(period, session);
return ResponseEntity.ok(stats);
}

// 장소별 사건 수 조회
@GetMapping("/location")
public ResponseEntity<?> getLocationCaseStats(@RequestParam("period") String period, HttpSession session) {
try {
List<LocationCaseStatsResponse> stats = caseStatsService.getLocationCaseStats(period, session);
return ResponseEntity.ok(stats);
} catch (IllegalStateException e) {
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
} catch (IllegalArgumentException e) {
return ResponseEntity.status(400).body(Collections.singletonMap("message", e.getMessage()));
} catch (NoSuchElementException e) {
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
}
List<LocationCaseStatsResponse> stats = caseStatsService.getLocationCaseStats(period, session);
return ResponseEntity.ok(stats);
}

// 지도용 장소별 사건 수 조회
@GetMapping("/map")
public ResponseEntity<?> getMapCaseStats(@RequestParam("period") String period, HttpSession session) {
try {
List<MapCaseStatsResponse> stats = caseStatsService.getMapCaseStats(period, session);
return ResponseEntity.ok(stats);
} catch (IllegalStateException e) {
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
} catch (IllegalArgumentException e) {
return ResponseEntity.status(400).body(Collections.singletonMap("message", e.getMessage()));
} catch (NoSuchElementException e) {
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
}
List<MapCaseStatsResponse> stats = caseStatsService.getMapCaseStats(period, session);
return ResponseEntity.ok(stats);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package com.example.backend.analysis.dto;

import com.example.backend.common.domain.CaseEntity;
import com.example.backend.common.domain.CaseStatsOverviewEntity;
import lombok.*;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CaseStatsOverviewResponse {
private int recentCase;
private int todayCase;
private String mostCase;
private CaseEntity.CaseCategory mostCase;
private String patrolRegion;

public static CaseStatsOverviewResponse fromEntity(CaseStatsOverviewEntity entity, String patrolRegionAddress) {
return CaseStatsOverviewResponse.builder()
.recentCase(entity.getRecentCaseCount())
.todayCase(entity.getTodayCaseCount())
.mostCase(entity.getMostCase())
.patrolRegion(patrolRegionAddress)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DailyCaseStatsResponse {
private int day;
private int fireCount;
private int assaultCount;
private int crowdCongestionCount;
private int weaponCount;
private int swoonCount;

public static DailyCaseStatsResponse fromRow(Object[] row) {
return DailyCaseStatsResponse.builder()
.day(((Number) row[0]).intValue())
.fireCount(((Number) row[1]).intValue())
.assaultCount(((Number) row[2]).intValue())
.crowdCongestionCount(((Number) row[3]).intValue())
.weaponCount(((Number) row[4]).intValue())
.swoonCount(((Number) row[5]).intValue())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.example.backend.analysis.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.*;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class HourlyCaseStatsResponse {
private int hour;
Expand All @@ -12,4 +13,15 @@ public class HourlyCaseStatsResponse {
private int crowdCongestionCount;
private int weaponCount;
private int swoonCount;

public static HourlyCaseStatsResponse fromRow(Object[] row) {
return HourlyCaseStatsResponse.builder()
.hour(((Number) row[0]).intValue())
.fireCount(((Number) row[1]).intValue())
.assaultCount(((Number) row[2]).intValue())
.crowdCongestionCount(((Number) row[3]).intValue())
.weaponCount(((Number) row[4]).intValue())
.swoonCount(((Number) row[5]).intValue())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
import lombok.*;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class LocationCaseStatsResponse {
String address;
double latitude;
double longitude;
int count;
private String address;
private double latitude;
private double longitude;
private int count;

public static LocationCaseStatsResponse fromRow(Object[] row) {
return LocationCaseStatsResponse.builder()
.address((String) row[0])
.latitude((Double) row[1])
.longitude((Double) row[2])
.count(((Number) row[3]).intValue())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package com.example.backend.analysis.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.*;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MapCaseStatsResponse {
String address;
double latitude;
double longitude;
int fire_count;
int assault_count;
int crowd_congestion_count;
int weapon_count;
int swoon_count;
private String address;
private double latitude;
private double longitude;
private int fireCount;
private int assaultCount;
private int crowdCongestionCount;
private int weaponCount;
private int swoonCount;

public static MapCaseStatsResponse fromRow(Object[] row) {
return MapCaseStatsResponse.builder()
.address((String) row[0])
.latitude((Double) row[1])
.longitude((Double) row[2])
.fireCount(((Number) row[3]).intValue())
.assaultCount(((Number) row[4]).intValue())
.crowdCongestionCount(((Number) row[5]).intValue())
.weaponCount(((Number) row[6]).intValue())
.swoonCount(((Number) row[7]).intValue())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MonthlyCaseStatsResponse {
private int month;
private int fireCount;
private int assaultCount;
private int crowdCongestionCount;
private int weaponCount;
private int swoonCount;

public static MonthlyCaseStatsResponse fromRow(Object[] row) {
return MonthlyCaseStatsResponse.builder()
.month(((Number) row[0]).intValue())
.fireCount(((Number) row[1]).intValue())
.assaultCount(((Number) row[2]).intValue())
.crowdCongestionCount(((Number) row[3]).intValue())
.weaponCount(((Number) row[4]).intValue())
.swoonCount(((Number) row[5]).intValue())
.build();
}
}
Loading
Loading