Elffuss BraidHub

kikocis/dataspace-portal public

fix: uptime calculation (#265)

Co-authored-by: Kamil Czaja <kamil.czaja@sovity.de>
Co-authored-by: Richard Treier <richard.treier@sovity.de>
Changesetc39ccfa2d12fc3616a2e8dbcfd66d0c97e76478f5c43c850503f7b70b56360d4
AuthorChristophe Loiseau <uuh116@gmail.com> · 2024-08-12 16:07 +02:00
CommitterGitHub <noreply@github.com> · 2024-08-12 14:07 UTC
Parents371742f0f792

Browse the files at this changeset

4 files changed

modified CHANGELOG.md +9 −7
@@ -13,18 +13,20 @@
1313
1414 #### Minor
1515
16+- Copyable contact email and subject fields on data offer detail dialogs
17+- Catalog: Organization filter is no longer split into ID and name
18+- Catalog: Connector filter is no longer split into ID and endpoint
19+- Catalog: Removed dataspace filter when only one dataspace is known
20+- Added a message when the CaaS request feature is not available
21+
1622 #### Patch
1723
18-- Copyable contact email and subject fields on data offer detail dialogs
19-- Fixed the close button on the self-hosted/CaaS connector choice page [#258](https://github.com/sovity/authority-portal/issues/258)
24+- Fixed Dashboard showing uptimes of over 100%
25+- Organization list: Data offer and connector counts now show the correct numbers according to the active environment
2026 - Fixed provider organization ID not showing up on CaaS connectors [#206](https://github.com/sovity/authority-portal/issues/206)
2127 - Keep in mind that sovity needs to be registered in the portal for the ID to show up.
2228 - Already registered connectors will be updated automatically, this process can take up to 24 hours
23-- Added a message when the CaaS request feature is not available
24-- Catalog: Removed dataspace filter when only one dataspace is known
25-- Catalog: Organization filter is no longer split into ID and name
26-- Catalog: Connector filter is no longer split into ID and endpoint
27-- Organization list: Data offer and connector counts now show the correct numbers according to the active environment
29+- Fixed the close button on the self-hosted/CaaS connector choice page [#258](https://github.com/sovity/authority-portal/issues/258)
2830
2931 ### Known issues
3032
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/pages/ComponentStatusApiService.kt +26 −50
@@ -15,7 +15,7 @@
1515
1616 import de.sovity.authorityportal.api.model.ComponentStatusOverview
1717 import de.sovity.authorityportal.api.model.UptimeStatusDto
18-import de.sovity.authorityportal.db.jooq.enums.ComponentOnlineStatus
18+import de.sovity.authorityportal.db.jooq.enums.ComponentOnlineStatus.UP
1919 import de.sovity.authorityportal.db.jooq.enums.ComponentType
2020 import de.sovity.authorityportal.db.jooq.enums.ConnectorOnlineStatus
2121 import de.sovity.authorityportal.db.jooq.tables.records.ComponentDowntimesRecord
@@ -25,11 +25,9 @@
2525 import de.sovity.authorityportal.web.utils.TimeUtils
2626 import jakarta.enterprise.context.ApplicationScoped
2727 import org.jooq.DSLContext
28-import java.text.DecimalFormat
29-import java.text.DecimalFormatSymbols
3028 import java.time.Duration
29+import java.time.Duration.between
3130 import java.time.OffsetDateTime
32-import java.util.Locale
3331
3432 @ApplicationScoped
3533 class ComponentStatusApiService(
@@ -47,7 +45,8 @@
4745 }
4846
4947 fun getComponentsStatusForOrganizationId(environmentId: String, organizationId: String): ComponentStatusOverview {
50- val connectorStatuses = connectorStatusQuery.getConnectorStatusInfoByOrganizationIdAndEnvironment(organizationId, environmentId)
48+ val connectorStatuses =
49+ connectorStatusQuery.getConnectorStatusInfoByOrganizationIdAndEnvironment(organizationId, environmentId)
5150 val statusCount = countConnectorStatuses(connectorStatuses)
5251
5352 return buildComponenStatusOverview(statusCount, environmentId)
@@ -60,7 +59,8 @@
6059 val latestDapsStatus = componentStatusService.getLatestComponentStatus(ComponentType.DAPS, environmentId)
6160 val latestLoggingHouseStatus =
6261 componentStatusService.getLatestComponentStatus(ComponentType.LOGGING_HOUSE, environmentId)
63- val latestBrokerCrawlerStatus = componentStatusService.getLatestComponentStatus(ComponentType.CATALOG_CRAWLER, environmentId)
62+ val latestBrokerCrawlerStatus =
63+ componentStatusService.getLatestComponentStatus(ComponentType.CATALOG_CRAWLER, environmentId)
6464 val now = timeUtils.now()
6565
6666 return ComponentStatusOverview(
@@ -82,12 +82,12 @@
8282 return null
8383 }
8484
85- val upSince = Duration.between(latestStatus.timeStamp.toInstant(), now.toInstant()).abs()
85+ val upSince = between(latestStatus.timeStamp.toInstant(), now.toInstant()).abs()
8686 val timeSpan = Duration.ofDays(30)
8787
8888 return UptimeStatusDto(
8989 componentStatus = latestStatus.status.toDto(),
90- upSinceSeconds = upSince.toSeconds().takeIf { latestStatus.status == ComponentOnlineStatus.UP } ?: 0,
90+ upSinceSeconds = upSince.toSeconds().takeIf { latestStatus.status == UP } ?: 0,
9191 timeSpanSeconds = timeSpan.toSeconds(),
9292 uptimePercentage = calculateUptimePercentage(latestStatus.component, timeSpan, environmentId, now)
9393 )
@@ -100,55 +100,31 @@
100100 now: OffsetDateTime
101101 ): Double {
102102 val limit = now.minus(timeSpan)
103- var statusHistoryAsc = componentStatusService.getStatusHistoryAscSince(component, limit, environmentId)
104- // If no status was found before the limit, the first record in the history is used as base for the calculation
105- val initialRecord =
106- componentStatusService.getFirstRecordBefore(component, limit, environmentId) ?: statusHistoryAsc.first()
107-
108- // If no "UP" status was found, return 0.00
109- // Also, drop all entries before first "UP" status, to avoid wrong uptime calculation
110- var tmpLastUpStatus = if (initialRecord.status == ComponentOnlineStatus.UP) initialRecord else {
111- statusHistoryAsc = statusHistoryAsc.dropWhile { it.status != ComponentOnlineStatus.UP }
112- statusHistoryAsc.firstOrNull()
113- }
114103
115- if (tmpLastUpStatus == null) {
116- return 0.00
117- }
104+ val statusHistoryAsc = componentStatusService.getStatusHistoryAscSince(component, limit, environmentId)
118105
119- // Sum up the total duration of "UP" statuses
120- var totalUptimeDuration = Duration.ZERO
121- for (componentRecord in statusHistoryAsc) {
122- if (componentRecord.status == ComponentOnlineStatus.UP) {
123- tmpLastUpStatus = componentRecord
124- } else {
125- totalUptimeDuration += Duration.between(
126- tmpLastUpStatus!!.timeStamp.toInstant(),
127- componentRecord.timeStamp.toInstant()
128- ).abs()
129- }
130- }
106+ val first = statusHistoryAsc.first()
131107
132- // Add time if last status is "UP"
133- val lastRecord = statusHistoryAsc.lastOrNull() ?: tmpLastUpStatus
134- if (lastRecord!!.status == ComponentOnlineStatus.UP) {
135- totalUptimeDuration += Duration.between(lastRecord.timeStamp.toInstant(), now.toInstant()).abs()
108+ val head = when {
109+ first.timeStamp.isBefore(limit) -> ComponentDowntimesRecord(component, first.status, environmentId, limit)
110+ else -> first
136111 }
137112
138- // Subtract potential uptime before the limit
139- if (initialRecord.status == ComponentOnlineStatus.UP && initialRecord.timeStamp.isBefore(limit)) {
140- totalUptimeDuration -= Duration.between(initialRecord.timeStamp.toInstant(), limit.toInstant()).abs()
113+ val body = statusHistoryAsc.drop(1)
114+ val tail = ComponentDowntimesRecord(component, statusHistoryAsc.last().status, environmentId, now)
115+
116+ val whole = listOf(head) + body + listOf(tail)
117+
118+ val duration = whole.zipWithNext().fold(Duration.ZERO) { acc, (start, end) ->
119+ when (start.status) {
120+ UP -> acc + between(start.timeStamp, end.timeStamp)
121+ else -> acc
122+ }
141123 }
142124
143- // Calculate uptime percentage
144- val totalDuration =
145- Duration.between(initialRecord.timeStamp.toInstant(), now.toInstant()).coerceAtMost(timeSpan).abs()
146- val uptimePercentage = totalUptimeDuration.toMillis().toDouble() / totalDuration.toMillis().toDouble() * 100
147-
148- // Round value to two decimal places
149- val symbols = DecimalFormatSymbols(Locale.US)
150- val formatter = DecimalFormat("#.##", symbols)
151- return formatter.format(uptimePercentage).toDouble()
125+ val uptime = 100.0 * duration.toMillis() / between(head.timeStamp, now).toMillis()
126+
127+ return uptime
152128 }
153129
154130 private fun countConnectorStatuses(
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/services/ComponentStatusService.kt +16 −16
@@ -49,21 +49,7 @@
4949 val c = Tables.COMPONENT_DOWNTIMES
5050
5151 return dsl.selectFrom(c)
52- .where(c.COMPONENT.eq(component)).and(c.ENVIRONMENT.eq(environment))
53- .orderBy(c.TIME_STAMP.desc())
54- .limit(1)
55- .fetchOne()
56- }
57-
58- fun getFirstRecordBefore(
59- component: ComponentType,
60- limit: OffsetDateTime,
61- environment: String
62- ): ComponentDowntimesRecord? {
63- val c = Tables.COMPONENT_DOWNTIMES
64-
65- return dsl.selectFrom(c)
66- .where(c.COMPONENT.eq(component)).and(c.ENVIRONMENT.eq(environment)).and(c.TIME_STAMP.lessThan(limit))
52+ .where(c.COMPONENT.eq(component), c.ENVIRONMENT.eq(environment))
6753 .orderBy(c.TIME_STAMP.desc())
6854 .limit(1)
6955 .fetchOne()
@@ -77,7 +63,21 @@
7763 val c = Tables.COMPONENT_DOWNTIMES
7864
7965 return dsl.selectFrom(c)
80- .where(c.COMPONENT.eq(component)).and(c.ENVIRONMENT.eq(environment)).and(c.TIME_STAMP.greaterOrEqual(limit))
66+ .where(
67+ c.COMPONENT.eq(component),
68+ c.ENVIRONMENT.eq(environment),
69+ c.TIME_STAMP.lessThan(limit)
70+ )
71+ .orderBy(c.TIME_STAMP.desc())
72+ .limit(1)
73+ .union(
74+ dsl.selectFrom(c)
75+ .where(
76+ c.COMPONENT.eq(component),
77+ c.ENVIRONMENT.eq(environment),
78+ c.TIME_STAMP.greaterOrEqual(limit)
79+ )
80+ )
8181 .orderBy(c.TIME_STAMP.asc())
8282 .fetch()
8383 }
modified authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/ComponentStatusApiServiceTest.kt +21 −2
@@ -54,7 +54,8 @@
5454 val now = OffsetDateTime.now()
5555 val env1 = "test"
5656 val env2 = "dev"
57- setupStatusHistory(now, env1, env2)
57+ val env3 = "env3"
58+ setupStatusHistory(now, env1, env2, env3)
5859
5960 useMockNow(now)
6061
@@ -96,6 +97,7 @@
9697 // act
9798 val resultEnv1 = componentStatusApiService.getComponentsStatus(env1)
9899 val resultEnv2 = componentStatusApiService.getComponentsStatus(env2)
100+ val resultEnv3 = componentStatusApiService.getComponentsStatus(env3)
99101
100102 // assert
101103 assertThat(resultEnv1.dapsStatus?.componentStatus).isEqualTo(ComponentOnlineStatus.MAINTENANCE.toDto())
@@ -115,9 +117,19 @@
115117 assertThat(resultEnv2.dapsStatus?.timeSpanSeconds).isEqualTo(Duration.ofDays(30).toSeconds())
116118 assertThat(resultEnv2.dapsStatus?.upSinceSeconds).isEqualTo(Duration.ZERO.toSeconds())
117119 assertThat(resultEnv2.loggingHouseStatus).isNull()
120+
121+ assertThat(resultEnv3.loggingHouseStatus?.componentStatus).isEqualTo(ComponentOnlineStatus.DOWN.toDto())
122+ assertThat(resultEnv3.loggingHouseStatus?.uptimePercentage).isCloseTo(50.0, Offset.offset(0.1))
123+ assertThat(resultEnv3.loggingHouseStatus?.timeSpanSeconds).isEqualTo(Duration.ofDays(30).toSeconds())
124+ assertThat(resultEnv3.loggingHouseStatus?.upSinceSeconds).isEqualTo(Duration.ZERO.toSeconds())
125+
126+ assertThat(resultEnv3.dapsStatus?.componentStatus).isEqualTo(ComponentOnlineStatus.UP.toDto())
127+ assertThat(resultEnv3.dapsStatus?.uptimePercentage).isCloseTo(100.0, Offset.offset(0.1))
128+ assertThat(resultEnv3.dapsStatus?.timeSpanSeconds).isEqualTo(Duration.ofDays(30).toSeconds())
129+ assertThat(resultEnv3.dapsStatus?.upSinceSeconds).isCloseTo(Duration.ofDays(15).toSeconds(), Offset.offset(1L))
118130 }
119131
120- private fun setupStatusHistory(now: OffsetDateTime, environment1: String, environment2: String) {
132+ private fun setupStatusHistory(now: OffsetDateTime, environment1: String, environment2: String, environment3: String) {
121133 val c = Tables.COMPONENT_DOWNTIMES
122134
123135 dsl.insertInto(c)
@@ -137,6 +149,13 @@
137149 // DAPS: Only "DOWN" status, older than 30 days
138150 .values(ComponentType.DAPS, ComponentOnlineStatus.DOWN, environment2, now.minus(Duration.ofDays(31)))
139151 // LH: Empty
152+ // Environment 3
153+ // LH: Up
154+ .values(ComponentType.LOGGING_HOUSE, ComponentOnlineStatus.UP, environment3, now.minus(Duration.ofDays(10)))
155+ .values(ComponentType.LOGGING_HOUSE, ComponentOnlineStatus.PENDING, environment3, now.minus(Duration.ofDays(5)))
156+ .values(ComponentType.LOGGING_HOUSE, ComponentOnlineStatus.DOWN, environment3, now.minus(Duration.ofSeconds(1)))
157+ // DAPS: Only "UP" status
158+ .values(ComponentType.DAPS, ComponentOnlineStatus.UP, environment3, now.minus(Duration.ofDays(15)))
140159 .execute()
141160 }
142161 }