From 6d06da66f93ba1640ce46ad511c3e280aba8e37d Mon Sep 17 00:00:00 2001 From: Nenad Jakic Date: Fri, 27 Sep 2024 21:08:07 +0200 Subject: [PATCH] Added anaylitics controller, service and dto. --- .../studio/controller/AnalyticsController.kt | 42 +++++++++++++++++++ .../nenadjakic/ocr/studio/dto/StatusCount.kt | 8 ++++ .../ocr/studio/service/AnalyticsService.kt | 14 +++++++ 3 files changed, 64 insertions(+) create mode 100644 src/main/kotlin/com/github/nenadjakic/ocr/studio/controller/AnalyticsController.kt create mode 100644 src/main/kotlin/com/github/nenadjakic/ocr/studio/dto/StatusCount.kt create mode 100644 src/main/kotlin/com/github/nenadjakic/ocr/studio/service/AnalyticsService.kt diff --git a/src/main/kotlin/com/github/nenadjakic/ocr/studio/controller/AnalyticsController.kt b/src/main/kotlin/com/github/nenadjakic/ocr/studio/controller/AnalyticsController.kt new file mode 100644 index 0000000..63db8e5 --- /dev/null +++ b/src/main/kotlin/com/github/nenadjakic/ocr/studio/controller/AnalyticsController.kt @@ -0,0 +1,42 @@ +package com.github.nenadjakic.ocr.studio.controller + +import com.github.nenadjakic.ocr.studio.dto.StatusCount +import com.github.nenadjakic.ocr.studio.service.AnalyticsService +import io.swagger.v3.oas.annotations.Operation +import io.swagger.v3.oas.annotations.responses.ApiResponse +import io.swagger.v3.oas.annotations.responses.ApiResponses +import io.swagger.v3.oas.annotations.tags.Tag +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@Tag(name = "Analytics controller", description = "API endpoints for analytics.") +@RestController +@RequestMapping("/analytics") +class AnalyticsController(private val analyticsService: AnalyticsService) { + + @Operation( + operationId = "getCountByStatus", + summary = "Get count by status.", + description = "Returns count by status information.") + @ApiResponses( + value = [ + ApiResponse(responseCode = "200", description = "Successfully retrieved count by status.") + ] + ) + @GetMapping(value = ["/count-by-status"], produces = [org.springframework.http.MediaType.APPLICATION_JSON_VALUE]) + fun getCountByStatus(): ResponseEntity> = ResponseEntity.ok(analyticsService.getCountByStatus()) + + @Operation( + operationId = "getAverageInDocuments", + summary = "Get average count across inDocuments.", + description = "Returns average count across inDocuments.") + @ApiResponses( + value = [ + ApiResponse(responseCode = "200", description = "Successfully retrieved average count across inDocuments.") + ] + ) + @GetMapping(value = ["/average-in-documents"], produces = [org.springframework.http.MediaType.APPLICATION_JSON_VALUE]) + fun getAverageInDocuments(): ResponseEntity = ResponseEntity.ok(analyticsService.getAverageInDocuments()) +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/nenadjakic/ocr/studio/dto/StatusCount.kt b/src/main/kotlin/com/github/nenadjakic/ocr/studio/dto/StatusCount.kt new file mode 100644 index 0000000..00da0fa --- /dev/null +++ b/src/main/kotlin/com/github/nenadjakic/ocr/studio/dto/StatusCount.kt @@ -0,0 +1,8 @@ +package com.github.nenadjakic.ocr.studio.dto + +import com.github.nenadjakic.ocr.studio.entity.Status + +data class StatusCount( + val status: Status, + val count: Long +) diff --git a/src/main/kotlin/com/github/nenadjakic/ocr/studio/service/AnalyticsService.kt b/src/main/kotlin/com/github/nenadjakic/ocr/studio/service/AnalyticsService.kt new file mode 100644 index 0000000..85c182f --- /dev/null +++ b/src/main/kotlin/com/github/nenadjakic/ocr/studio/service/AnalyticsService.kt @@ -0,0 +1,14 @@ +package com.github.nenadjakic.ocr.studio.service + +import com.github.nenadjakic.ocr.studio.dto.StatusCount +import com.github.nenadjakic.ocr.studio.repository.TaskRepository +import org.springframework.stereotype.Service + +@Service +class AnalyticsService( + private val taskRepository: TaskRepository +) { + fun getCountByStatus(): List = taskRepository.countTasksByStatus() + + fun getAverageInDocuments(): Long = taskRepository.averageInDocuments() +} \ No newline at end of file