22
33import org .example .finalbe .domains .alert .domain .AlertHistory ;
44import org .example .finalbe .domains .common .enumdir .AlertLevel ;
5- import org .example .finalbe .domains .common .enumdir .AlertStatus ;
65import org .example .finalbe .domains .common .enumdir .MetricType ;
76import org .example .finalbe .domains .common .enumdir .TargetType ;
7+ import org .springframework .data .domain .Page ;
8+ import org .springframework .data .domain .Pageable ;
89import org .springframework .data .jpa .repository .JpaRepository ;
10+ import org .springframework .data .jpa .repository .Modifying ;
911import org .springframework .data .jpa .repository .Query ;
1012import org .springframework .data .repository .query .Param ;
1113
14+ import java .time .LocalDateTime ;
1215import java .util .List ;
1316
1417public interface AlertHistoryRepository extends JpaRepository <AlertHistory , Long > {
1518
16- // ========== 기존 메서드 ==========
19+ // ========== 기존 조회 메서드 (status 제거) ==========
1720
18- List <AlertHistory > findByEquipmentIdAndStatusOrderByTriggeredAtDesc (
19- Long equipmentId , AlertStatus status );
21+ List <AlertHistory > findByEquipmentIdOrderByTriggeredAtDesc (Long equipmentId );
2022
2123 @ Query ("SELECT a FROM AlertHistory a WHERE a.equipmentId = :equipmentId " +
2224 "AND a.metricType = :metricType AND a.metricName = :metricName " +
23- "AND a.status != 'RESOLVED' ORDER BY a.triggeredAt DESC" )
25+ "ORDER BY a.triggeredAt DESC" )
2426 List <AlertHistory > findActiveAlertsByEquipmentIdAndMetric (
2527 @ Param ("equipmentId" ) Long equipmentId ,
2628 @ Param ("metricType" ) MetricType metricType ,
2729 @ Param ("metricName" ) String metricName );
2830
29- List <AlertHistory > findByRackIdAndStatusOrderByTriggeredAtDesc (
30- Long rackId , AlertStatus status );
31+ List <AlertHistory > findByRackIdOrderByTriggeredAtDesc (Long rackId );
3132
3233 @ Query ("SELECT a FROM AlertHistory a WHERE a.rackId = :rackId " +
3334 "AND a.metricType = :metricType AND a.metricName = :metricName " +
34- "AND a.status != 'RESOLVED' ORDER BY a.triggeredAt DESC" )
35+ "ORDER BY a.triggeredAt DESC" )
3536 List <AlertHistory > findActiveAlertsByRackIdAndMetric (
3637 @ Param ("rackId" ) Long rackId ,
3738 @ Param ("metricType" ) MetricType metricType ,
3839 @ Param ("metricName" ) String metricName );
3940
40- List <AlertHistory > findByStatusOrderByTriggeredAtDesc ( AlertStatus status );
41+ List <AlertHistory > findAllByOrderByTriggeredAtDesc ( );
4142
42- // ========== 통계 조회 메서드 (신규) ==========
43+ List < AlertHistory > findByServerRoomIdOrderByTriggeredAtDesc ( Long serverRoomId );
4344
44- /**
45- * 상태별 알림 개수 조회
46- */
47- long countByStatus (AlertStatus status );
45+ List <AlertHistory > findByDataCenterIdOrderByTriggeredAtDesc (Long dataCenterId );
46+
47+ // ========== 페이지네이션 및 필터링 메서드 ==========
48+
49+ @ Query ("SELECT a FROM AlertHistory a " +
50+ "WHERE a.serverRoomId IN :serverRoomIds " +
51+ "AND a.triggeredAt >= :startTime " +
52+ "AND a.targetType != :excludeTargetType" )
53+ Page <AlertHistory > findByServerRoomIdInAndTriggeredAtAfterAndTargetTypeNot (
54+ @ Param ("serverRoomIds" ) List <Long > serverRoomIds ,
55+ @ Param ("startTime" ) LocalDateTime startTime ,
56+ @ Param ("excludeTargetType" ) TargetType excludeTargetType ,
57+ Pageable pageable );
58+
59+ @ Query ("SELECT a FROM AlertHistory a " +
60+ "WHERE a.serverRoomId IN :serverRoomIds " +
61+ "AND a.level = :level " +
62+ "AND a.triggeredAt >= :startTime " +
63+ "AND a.targetType != :excludeTargetType" )
64+ Page <AlertHistory > findByServerRoomIdInAndLevelAndTriggeredAtAfterAndTargetTypeNot (
65+ @ Param ("serverRoomIds" ) List <Long > serverRoomIds ,
66+ @ Param ("level" ) AlertLevel level ,
67+ @ Param ("startTime" ) LocalDateTime startTime ,
68+ @ Param ("excludeTargetType" ) TargetType excludeTargetType ,
69+ Pageable pageable );
70+
71+ // ========== 통계 조회 메서드 (서버실별 필터링) ==========
72+
73+ @ Query ("SELECT COUNT(a) FROM AlertHistory a WHERE a.serverRoomId IN :serverRoomIds" )
74+ long countByServerRoomIdIn (@ Param ("serverRoomIds" ) List <Long > serverRoomIds );
75+
76+ @ Query ("SELECT COUNT(a) FROM AlertHistory a WHERE a.serverRoomId IN :serverRoomIds AND a.level = :level" )
77+ long countByServerRoomIdInAndLevel (
78+ @ Param ("serverRoomIds" ) List <Long > serverRoomIds ,
79+ @ Param ("level" ) AlertLevel level );
80+
81+ @ Query ("SELECT COUNT(a) FROM AlertHistory a WHERE a.serverRoomId IN :serverRoomIds AND a.targetType = :targetType" )
82+ long countByServerRoomIdInAndTargetType (
83+ @ Param ("serverRoomIds" ) List <Long > serverRoomIds ,
84+ @ Param ("targetType" ) TargetType targetType );
85+
86+ // ========== 기존 통계 조회 메서드 (전체 기준) ==========
4887
49- /**
50- * 레벨별 알림 개수 조회
51- */
5288 long countByLevel (AlertLevel level );
5389
54- /**
55- * 타겟 타입별 알림 개수 조회
56- */
5790 long countByTargetType (TargetType targetType );
91+
92+ // ========== 읽음 처리 관련 메서드 ==========
93+
94+ @ Modifying
95+ @ Query ("UPDATE AlertHistory a SET a.isRead = true, a.readAt = :readAt, a.readBy = :readBy " +
96+ "WHERE a.serverRoomId IN :serverRoomIds AND a.isRead = false" )
97+ int markAllAsReadByServerRoomIds (
98+ @ Param ("serverRoomIds" ) List <Long > serverRoomIds ,
99+ @ Param ("readAt" ) LocalDateTime readAt ,
100+ @ Param ("readBy" ) Long readBy
101+ );
102+
103+ @ Modifying
104+ @ Query ("UPDATE AlertHistory a SET a.isRead = true, a.readAt = :readAt, a.readBy = :readBy " +
105+ "WHERE a.id IN :alertIds AND a.isRead = false" )
106+ int markAsReadByIds (
107+ @ Param ("alertIds" ) List <Long > alertIds ,
108+ @ Param ("readAt" ) LocalDateTime readAt ,
109+ @ Param ("readBy" ) Long readBy
110+ );
111+
112+ // ========== 삭제 관련 메서드 ==========
113+
114+ @ Modifying
115+ @ Query ("DELETE FROM AlertHistory a WHERE a.id IN :alertIds AND a.serverRoomId IN :serverRoomIds" )
116+ int deleteByIdsAndServerRoomIds (
117+ @ Param ("alertIds" ) List <Long > alertIds ,
118+ @ Param ("serverRoomIds" ) List <Long > serverRoomIds
119+ );
120+
121+ @ Modifying
122+ @ Query ("DELETE FROM AlertHistory a WHERE a.serverRoomId IN :serverRoomIds" )
123+ int deleteAllByServerRoomIds (@ Param ("serverRoomIds" ) List <Long > serverRoomIds );
124+
125+ @ Query ("SELECT COUNT(a) FROM AlertHistory a WHERE a.serverRoomId IN :serverRoomIds AND a.isRead = false" )
126+ long countUnreadByServerRoomIds (@ Param ("serverRoomIds" ) List <Long > serverRoomIds );
58127}
0 commit comments