@@ -14,7 +14,6 @@ import org.jooq.DSLContext
14
14
import org.jooq.impl.DSL
15
15
import org.jooq.kotlin.and
16
16
import java.time.Duration
17
- import java.time.OffsetDateTime
18
17
19
18
@Singleton
20
19
class IncidentRepository (private val dslContext : DSLContext ) {
@@ -30,73 +29,117 @@ class IncidentRepository(private val dslContext: DSLContext) {
30
29
*
31
30
* @return List of [IncidentDto] matching the criteria.
32
31
*/
33
- @Suppress(" IgnoredReturnValue" )
34
32
fun getIncidents (
35
33
monitorId : Long? = null,
36
34
period : Duration ? = null,
37
35
includeResolved : Boolean ,
38
36
): List <IncidentDto > {
39
- val periodStart: OffsetDateTime ? = period?.let { getCurrentTimestamp().minus(it) }
40
37
val orderFieldName = DSL .name(IncidentDto ::updatedAt.name)
38
+
41
39
return dslContext
42
40
// HTTP incidents
43
- .select(
44
- HTTP_MONITOR .ID .`as `(IncidentDto ::monitorId.name),
45
- HTTP_MONITOR .NAME .`as `(IncidentDto ::monitorName.name),
46
- HTTP_MONITOR .ENABLED .`as `(IncidentDto ::isMonitorEnabled.name),
47
- DSL .inline(IncidentType .HTTP .name).`as `(IncidentDto ::incidentType.name),
48
- DSL .`when `(HTTP_UPTIME_EVENT .ENDED_AT .isNull, IncidentStatus .ONGOING .name)
49
- .otherwise(IncidentStatus .RESOLVED .name).`as `(IncidentDto ::status.name),
50
- HTTP_UPTIME_EVENT .ERROR .`as `(IncidentDto ::details.name),
51
- HTTP_UPTIME_EVENT .STARTED_AT .`as `(IncidentDto ::startedAt.name),
52
- HTTP_UPTIME_EVENT .ENDED_AT .`as `(IncidentDto ::endedAt.name),
53
- HTTP_UPTIME_EVENT .UPDATED_AT .`as `(IncidentDto ::updatedAt.name),
54
- )
55
- .from(HTTP_UPTIME_EVENT )
56
- .join(HTTP_MONITOR ).on(HTTP_UPTIME_EVENT .MONITOR_ID .eq(HTTP_MONITOR .ID ))
57
- .where(HTTP_UPTIME_EVENT .STATUS .eq(UptimeStatus .DOWN ))
58
- .and (HTTP_MONITOR .ENABLED .isTrue)
59
- .apply {
60
- // Filter for monitors
61
- monitorId?.let { and (HTTP_MONITOR .ID .eq(it)) }
62
- // Filter for events that were open at any point during the specified period
63
- periodStart?.let { and (DSL .coalesce(HTTP_UPTIME_EVENT .ENDED_AT , DSL .now()).greaterThan(it)) }
64
- // Filter out resolved incidents if not requested
65
- if (! includeResolved) {
66
- and (HTTP_UPTIME_EVENT .ENDED_AT .isNull)
67
- }
68
- }
41
+ .httpUptimeIncidentSelect(monitorId, period, includeResolved)
69
42
// SSL incidents
70
- .unionAll(
71
- dslContext
72
- .select(
73
- HTTP_MONITOR .ID .`as `(IncidentDto ::monitorId.name),
74
- HTTP_MONITOR .NAME .`as `(IncidentDto ::monitorName.name),
75
- DSL .field(HTTP_MONITOR .ENABLED .and (HTTP_MONITOR .SSL_CHECK_ENABLED ))
76
- .`as `(IncidentDto ::isMonitorEnabled.name),
77
- DSL .inline(IncidentType .SSL .name).`as `(IncidentDto ::incidentType.name),
78
- DSL .`when `(SSL_EVENT .ENDED_AT .isNull, IncidentStatus .ONGOING .name)
79
- .otherwise(IncidentStatus .RESOLVED .name).`as `(IncidentDto ::status.name),
80
- SSL_EVENT .ERROR .`as `(IncidentDto ::details.name),
81
- SSL_EVENT .STARTED_AT .`as `(IncidentDto ::startedAt.name),
82
- SSL_EVENT .ENDED_AT .`as `(IncidentDto ::endedAt.name),
83
- SSL_EVENT .UPDATED_AT .`as `(IncidentDto ::updatedAt.name),
84
- )
85
- .from(SSL_EVENT )
86
- .join(HTTP_MONITOR ).on(SSL_EVENT .MONITOR_ID .eq(HTTP_MONITOR .ID ))
87
- .where(SSL_EVENT .STATUS .eq(SslStatus .INVALID ))
88
- .and (HTTP_MONITOR .ENABLED .isTrue)
89
- .apply {
90
- // Filter for monitors
91
- monitorId?.let { and (HTTP_MONITOR .ID .eq(it)) }
92
- // Filter for events that were open at any point during the specified period
93
- periodStart?.let { and (DSL .coalesce(SSL_EVENT .ENDED_AT , DSL .now()).greaterThan(it)) }
94
- // Filter out resolved incidents if not requested
95
- if (! includeResolved) {
96
- and (SSL_EVENT .ENDED_AT .isNull)
97
- }
98
- }
99
- )
43
+ .unionAll(dslContext.sslIncidentsSelect(monitorId, period, includeResolved))
44
+ .orderBy(DSL .field(orderFieldName).desc())
45
+ .fetchInto(IncidentDto ::class .java)
46
+ }
47
+
48
+ @Suppress(" IgnoredReturnValue" )
49
+ private fun DSLContext.httpUptimeIncidentSelect (
50
+ monitorId : Long? = null,
51
+ period : Duration ? = null,
52
+ includeResolved : Boolean
53
+ ) = this
54
+ .select(
55
+ HTTP_MONITOR .ID .`as `(IncidentDto ::monitorId.name),
56
+ HTTP_MONITOR .NAME .`as `(IncidentDto ::monitorName.name),
57
+ HTTP_MONITOR .ENABLED .`as `(IncidentDto ::isMonitorEnabled.name),
58
+ DSL .inline(IncidentType .HTTP .name).`as `(IncidentDto ::incidentType.name),
59
+ DSL .`when `(HTTP_UPTIME_EVENT .ENDED_AT .isNull, IncidentStatus .ONGOING .name)
60
+ .otherwise(IncidentStatus .RESOLVED .name).`as `(IncidentDto ::status.name),
61
+ HTTP_UPTIME_EVENT .ERROR .`as `(IncidentDto ::details.name),
62
+ HTTP_UPTIME_EVENT .STARTED_AT .`as `(IncidentDto ::startedAt.name),
63
+ HTTP_UPTIME_EVENT .ENDED_AT .`as `(IncidentDto ::endedAt.name),
64
+ HTTP_UPTIME_EVENT .UPDATED_AT .`as `(IncidentDto ::updatedAt.name),
65
+ )
66
+ .from(HTTP_UPTIME_EVENT )
67
+ .join(HTTP_MONITOR ).on(HTTP_UPTIME_EVENT .MONITOR_ID .eq(HTTP_MONITOR .ID ))
68
+ .where(HTTP_UPTIME_EVENT .STATUS .eq(UptimeStatus .DOWN ))
69
+ .and (HTTP_MONITOR .ENABLED .isTrue)
70
+ .apply {
71
+ // Filter for monitors
72
+ monitorId?.let { and (HTTP_MONITOR .ID .eq(it)) }
73
+ // Filter for events that were open at any point during the specified period
74
+ period?.let {
75
+ val periodStart = getCurrentTimestamp().minus(period)
76
+ and (DSL .coalesce(HTTP_UPTIME_EVENT .ENDED_AT , DSL .now()).greaterThan(periodStart))
77
+ }
78
+ // Filter out resolved incidents if not requested
79
+ if (! includeResolved) {
80
+ and (HTTP_UPTIME_EVENT .ENDED_AT .isNull)
81
+ }
82
+ }
83
+
84
+ @Suppress(" IgnoredReturnValue" )
85
+ private fun DSLContext.sslIncidentsSelect (
86
+ monitorId : Long? = null,
87
+ period : Duration ? = null,
88
+ includeResolved : Boolean ,
89
+ ) = this
90
+ .select(
91
+ HTTP_MONITOR .ID .`as `(IncidentDto ::monitorId.name),
92
+ HTTP_MONITOR .NAME .`as `(IncidentDto ::monitorName.name),
93
+ DSL .field(HTTP_MONITOR .ENABLED .and (HTTP_MONITOR .SSL_CHECK_ENABLED ))
94
+ .`as `(IncidentDto ::isMonitorEnabled.name),
95
+ DSL .inline(IncidentType .SSL .name).`as `(IncidentDto ::incidentType.name),
96
+ DSL .`when `(SSL_EVENT .ENDED_AT .isNull, IncidentStatus .ONGOING .name)
97
+ .otherwise(IncidentStatus .RESOLVED .name).`as `(IncidentDto ::status.name),
98
+ SSL_EVENT .ERROR .`as `(IncidentDto ::details.name),
99
+ SSL_EVENT .STARTED_AT .`as `(IncidentDto ::startedAt.name),
100
+ SSL_EVENT .ENDED_AT .`as `(IncidentDto ::endedAt.name),
101
+ SSL_EVENT .UPDATED_AT .`as `(IncidentDto ::updatedAt.name),
102
+ )
103
+ .from(SSL_EVENT )
104
+ .join(HTTP_MONITOR ).on(SSL_EVENT .MONITOR_ID .eq(HTTP_MONITOR .ID ))
105
+ .where(SSL_EVENT .STATUS .eq(SslStatus .INVALID ))
106
+ .and (HTTP_MONITOR .ENABLED .isTrue)
107
+ .apply {
108
+ // Filter for monitors
109
+ monitorId?.let { and (HTTP_MONITOR .ID .eq(it)) }
110
+ // Filter for events that were open at any point during the specified period
111
+ period?.let {
112
+ val periodStart = getCurrentTimestamp().minus(period)
113
+ and (DSL .coalesce(SSL_EVENT .ENDED_AT , DSL .now()).greaterThan(periodStart))
114
+ }
115
+ // Filter out resolved incidents if not requested
116
+ if (! includeResolved) {
117
+ and (SSL_EVENT .ENDED_AT .isNull)
118
+ }
119
+ }
120
+
121
+ fun getHttpUptimeIncidents (
122
+ monitorId : Long? = null,
123
+ period : Duration ? = null,
124
+ includeResolved : Boolean ,
125
+ ): List <IncidentDto > {
126
+ val orderFieldName = DSL .name(IncidentDto ::updatedAt.name)
127
+
128
+ return dslContext
129
+ .httpUptimeIncidentSelect(monitorId, period, includeResolved)
130
+ .orderBy(DSL .field(orderFieldName).desc())
131
+ .fetchInto(IncidentDto ::class .java)
132
+ }
133
+
134
+ fun getSslIncidents (
135
+ monitorId : Long? = null,
136
+ period : Duration ? = null,
137
+ includeResolved : Boolean ,
138
+ ): List <IncidentDto > {
139
+ val orderFieldName = DSL .name(IncidentDto ::updatedAt.name)
140
+
141
+ return dslContext
142
+ .sslIncidentsSelect(monitorId, period, includeResolved)
100
143
.orderBy(DSL .field(orderFieldName).desc())
101
144
.fetchInto(IncidentDto ::class .java)
102
145
}
0 commit comments