-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HeartBeatService and HeartBeatController (#244)
- Loading branch information
1 parent
6d75454
commit ddb8e38
Showing
4 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/de/app/fivegla/business/HeartBeatService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package de.app.fivegla.business; | ||
|
||
import io.micrometer.core.instrument.Gauge; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* The HeartBeatService class provides functionality to register heartbeats. | ||
*/ | ||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class HeartBeatService { | ||
|
||
private final MeterRegistry meterRegistry; | ||
private final ConcurrentHashMap<String, AtomicLong> lastHeartbeatTimestamps = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Register a heartbeat using Micrometer. | ||
*/ | ||
public void registerHeartBeat(String id) { | ||
log.info("Registering heartbeat using Micrometer for sensor id: {}", id); | ||
var counter = meterRegistry.counter("heartbeat", "id", id); | ||
counter.increment(); | ||
|
||
lastHeartbeatTimestamps.computeIfAbsent(id, key -> { | ||
AtomicLong timestamp = new AtomicLong(); | ||
Gauge.builder("heartbeat.lastTimestamp", timestamp, AtomicLong::get) | ||
.description("The timestamp of the last heartbeat for sensor id: " + id) | ||
.tags("sensorId", id) | ||
.register(meterRegistry); | ||
return timestamp; | ||
}).set(System.currentTimeMillis()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/de/app/fivegla/controller/global/HeartBeatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package de.app.fivegla.controller.global; | ||
|
||
import de.app.fivegla.business.HeartBeatService; | ||
import de.app.fivegla.config.security.marker.ApiKeyApiAccess; | ||
import de.app.fivegla.controller.api.BaseMappings; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* The HeartBeatController class handles heartbeat-related operations. | ||
* It provides a method to register a heartbeat. | ||
* <p> | ||
* This controller is mapped to the /heartbeat endpoint. | ||
*/ | ||
@Slf4j | ||
@RestController | ||
@RequestMapping(BaseMappings.HEARTBEAT) | ||
@RequiredArgsConstructor | ||
public class HeartBeatController implements ApiKeyApiAccess { | ||
|
||
private final HeartBeatService heartBeatService; | ||
|
||
@Operation( | ||
operationId = "heartbeat", | ||
description = "Heartbeat endpoint.", | ||
tags = BaseMappings.HEARTBEAT | ||
) | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "The heartbeat was successful." | ||
) | ||
@PostMapping("/{id}") | ||
public ResponseEntity<Void> heartBeat(@PathVariable @Schema(description = "The ID") String id) { | ||
heartBeatService.registerHeartBeat(id); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
} |