Spaces:
Build error
Build error
da-policyengine-dev
/
src
/main
/java
/com
/dalab
/policyengine
/web
/rest
/EventCenterController.java
| package com.dalab.policyengine.web.rest; | |
| import java.net.URI; | |
| import java.time.Instant; | |
| import java.util.List; | |
| import java.util.UUID; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.data.domain.Page; | |
| import org.springframework.data.domain.Pageable; | |
| import org.springframework.data.web.PageableDefault; | |
| import org.springframework.http.ResponseEntity; | |
| import org.springframework.security.access.prepost.PreAuthorize; | |
| import org.springframework.web.bind.annotation.DeleteMapping; | |
| import org.springframework.web.bind.annotation.GetMapping; | |
| import org.springframework.web.bind.annotation.PathVariable; | |
| import org.springframework.web.bind.annotation.PostMapping; | |
| import org.springframework.web.bind.annotation.PutMapping; | |
| import org.springframework.web.bind.annotation.RequestBody; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RequestParam; | |
| import org.springframework.web.bind.annotation.RestController; | |
| import org.springframework.web.servlet.support.ServletUriComponentsBuilder; | |
| import com.dalab.common.security.SecurityUtils; | |
| import com.dalab.policyengine.dto.EventAnalyticsDTO; | |
| import com.dalab.policyengine.dto.EventStreamDTO; | |
| import com.dalab.policyengine.dto.EventSubscriptionInputDTO; | |
| import com.dalab.policyengine.dto.EventSubscriptionOutputDTO; | |
| import com.dalab.policyengine.model.EventSubscriptionStatus; | |
| import com.dalab.policyengine.model.EventType; | |
| import com.dalab.policyengine.service.IEventSubscriptionService; | |
| import io.swagger.v3.oas.annotations.Operation; | |
| import io.swagger.v3.oas.annotations.Parameter; | |
| 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 jakarta.validation.Valid; | |
| /** | |
| * REST controller for Event Center functionality. | |
| * Provides endpoints for managing event subscriptions, streaming events, and analytics. | |
| */ | |
| public class EventCenterController { | |
| private static final Logger log = LoggerFactory.getLogger(EventCenterController.class); | |
| private final IEventSubscriptionService eventSubscriptionService; | |
| public EventCenterController(IEventSubscriptionService eventSubscriptionService) { | |
| this.eventSubscriptionService = eventSubscriptionService; | |
| } | |
| // Endpoint 1: Subscription Management | |
| public ResponseEntity<EventSubscriptionOutputDTO> createSubscription( | |
| EventSubscriptionInputDTO inputDTO) { | |
| log.info("REST request to create Event Subscription: {}", inputDTO.getName()); | |
| UUID creatorUserId = SecurityUtils.getAuthenticatedUserId(); | |
| EventSubscriptionOutputDTO createdSubscription = eventSubscriptionService.createSubscription(inputDTO, creatorUserId); | |
| URI location = ServletUriComponentsBuilder.fromCurrentRequest() | |
| .path("/{id}") | |
| .buildAndExpand(createdSubscription.getId()) | |
| .toUri(); | |
| return ResponseEntity.created(location).body(createdSubscription); | |
| } | |
| public ResponseEntity<Page<EventSubscriptionOutputDTO>> getUserSubscriptions( | |
| Pageable pageable, | |
| String status, | |
| String nameContains) { | |
| log.info("REST request to get Event Subscriptions for user with filters: status={}, nameContains={}", status, nameContains); | |
| UUID userId = SecurityUtils.getAuthenticatedUserId(); | |
| Page<EventSubscriptionOutputDTO> subscriptionsPage = eventSubscriptionService.getSubscriptionsForUser( | |
| userId, pageable, status, nameContains); | |
| return ResponseEntity.ok(subscriptionsPage); | |
| } | |
| public ResponseEntity<EventSubscriptionOutputDTO> getSubscriptionById( UUID subscriptionId) { | |
| log.info("REST request to get Event Subscription by id: {}", subscriptionId); | |
| EventSubscriptionOutputDTO subscription = eventSubscriptionService.getSubscriptionById(subscriptionId); | |
| return ResponseEntity.ok(subscription); | |
| } | |
| public ResponseEntity<EventSubscriptionOutputDTO> updateSubscription( | |
| UUID subscriptionId, | |
| EventSubscriptionInputDTO inputDTO) { | |
| log.info("REST request to update Event Subscription: {}", subscriptionId); | |
| UUID updaterUserId = SecurityUtils.getAuthenticatedUserId(); | |
| EventSubscriptionOutputDTO updatedSubscription = eventSubscriptionService.updateSubscription( | |
| subscriptionId, inputDTO, updaterUserId); | |
| return ResponseEntity.ok(updatedSubscription); | |
| } | |
| public ResponseEntity<Void> deleteSubscription( UUID subscriptionId) { | |
| log.info("REST request to delete Event Subscription: {}", subscriptionId); | |
| eventSubscriptionService.deleteSubscription(subscriptionId); | |
| return ResponseEntity.noContent().build(); | |
| } | |
| public ResponseEntity<EventSubscriptionOutputDTO> updateSubscriptionStatus( | |
| UUID subscriptionId, | |
| EventSubscriptionStatus status) { | |
| log.info("REST request to update Event Subscription status: {} to {}", subscriptionId, status); | |
| UUID updaterUserId = SecurityUtils.getAuthenticatedUserId(); | |
| EventSubscriptionOutputDTO updatedSubscription = eventSubscriptionService.updateSubscriptionStatus( | |
| subscriptionId, status, updaterUserId); | |
| return ResponseEntity.ok(updatedSubscription); | |
| } | |
| // Endpoint 2: Event Streaming | |
| public ResponseEntity<List<EventStreamDTO>> getEventStream( | |
| Integer limit) { | |
| log.info("REST request to get Event Stream with limit: {}", limit); | |
| UUID userId = SecurityUtils.getAuthenticatedUserId(); | |
| List<EventStreamDTO> eventStream = eventSubscriptionService.getEventStreamForUser(userId, limit); | |
| return ResponseEntity.ok(eventStream); | |
| } | |
| public ResponseEntity<List<EventStreamDTO>> getAllEventStream( | |
| Integer limit) { | |
| log.info("REST request to get All Event Stream with limit: {}", limit); | |
| List<EventStreamDTO> eventStream = eventSubscriptionService.getAllEventStream(limit); | |
| return ResponseEntity.ok(eventStream); | |
| } | |
| // Endpoint 3: Historical Events | |
| public ResponseEntity<Page<EventStreamDTO>> getHistoricalEvents( | |
| UUID subscriptionId, | |
| Pageable pageable) { | |
| log.info("REST request to get Historical Events for subscription: {}", subscriptionId); | |
| Page<EventStreamDTO> historicalEvents = eventSubscriptionService.getHistoricalEventsForSubscription( | |
| subscriptionId, pageable); | |
| return ResponseEntity.ok(historicalEvents); | |
| } | |
| // Endpoint 4: Event Analytics | |
| public ResponseEntity<EventAnalyticsDTO> getEventAnalytics( | |
| String fromTime, | |
| String toTime) { | |
| log.info("REST request to get Event Analytics with time range: {} to {}", fromTime, toTime); | |
| UUID userId = SecurityUtils.getAuthenticatedUserId(); | |
| if (fromTime != null && toTime != null) { | |
| Instant from = Instant.parse(fromTime); | |
| Instant to = Instant.parse(toTime); | |
| EventAnalyticsDTO analytics = eventSubscriptionService.getEventAnalyticsForTimeRange(userId, from, to); | |
| return ResponseEntity.ok(analytics); | |
| } else { | |
| EventAnalyticsDTO analytics = eventSubscriptionService.getEventAnalyticsForUser(userId); | |
| return ResponseEntity.ok(analytics); | |
| } | |
| } | |
| public ResponseEntity<EventAnalyticsDTO> getSystemEventAnalytics() { | |
| log.info("REST request to get System Event Analytics"); | |
| EventAnalyticsDTO analytics = eventSubscriptionService.getSystemEventAnalytics(); | |
| return ResponseEntity.ok(analytics); | |
| } | |
| // Endpoint 5: Rule Testing | |
| public ResponseEntity<Boolean> testEventRule( | |
| String ruleCondition, | |
| EventStreamDTO sampleEvent) { | |
| log.info("REST request to test Event Rule condition: {}", ruleCondition); | |
| boolean ruleMatches = eventSubscriptionService.testEventRule(ruleCondition, sampleEvent); | |
| return ResponseEntity.ok(ruleMatches); | |
| } | |
| // Endpoint 6: Configuration Helpers | |
| public ResponseEntity<List<EventType>> getAvailableEventTypes() { | |
| log.info("REST request to get Available Event Types"); | |
| List<EventType> eventTypes = eventSubscriptionService.getAvailableEventTypes(); | |
| return ResponseEntity.ok(eventTypes); | |
| } | |
| public ResponseEntity<List<String>> getAvailableSourceServices() { | |
| log.info("REST request to get Available Source Services"); | |
| List<String> sourceServices = eventSubscriptionService.getAvailableSourceServices(); | |
| return ResponseEntity.ok(sourceServices); | |
| } | |
| public ResponseEntity<Void> validateSubscriptionConfiguration( | |
| EventSubscriptionInputDTO inputDTO) { | |
| log.info("REST request to validate Subscription Configuration: {}", inputDTO.getName()); | |
| eventSubscriptionService.validateSubscriptionConfiguration(inputDTO); | |
| return ResponseEntity.ok().build(); | |
| } | |
| } |