diff --git a/src/main/java/org/springframework/samples/petclinic/clinicactivity/ClinicActivityController.java b/src/main/java/org/springframework/samples/petclinic/clinicactivity/ClinicActivityController.java index 96b8c5e80de..5ded783ee97 100644 --- a/src/main/java/org/springframework/samples/petclinic/clinicactivity/ClinicActivityController.java +++ b/src/main/java/org/springframework/samples/petclinic/clinicactivity/ClinicActivityController.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.Map; +import java.util.HashMap; import java.util.concurrent.ThreadLocalRandom; @RestController @@ -48,7 +49,6 @@ public void afterPropertiesSet() throws Exception { this.otelTracer = openTelemetry.getTracer("ClinicActivityController"); } - // This ep is here to throw error @GetMapping("active-errors-ratio") public int getActiveErrorsRatio() { return dataService.getActiveLogsRatio("errors"); @@ -72,106 +72,22 @@ public ResponseEntity populateData(@RequestParam(name = "count", default @GetMapping(value = "/query-logs", produces = "application/json") public List> getLogs( @RequestParam(name = "repetitions", defaultValue = "1") int repetitions) { - int numericValueToTest = 50000; - String sql = "SELECT id, activity_type, numeric_value, event_timestamp, status_flag, payload FROM clinic_activity_logs WHERE numeric_value = ?"; - List> lastResults = null; - for (int i = 0; i < repetitions; i++) { - lastResults = jdbcTemplate.queryForList(sql, numericValueToTest); - } - return lastResults; - } - - @DeleteMapping("/cleanup-logs") - public ResponseEntity cleanupLogs() { - logger.info("Received request to cleanup all clinic activity logs."); - try { - dataService.cleanupActivityLogs(); - return ResponseEntity.ok("Successfully cleaned up all clinic activity logs."); - } catch (Exception e) { - logger.error("Error during clinic activity log cleanup", e); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error during cleanup: " + e.getMessage()); - } - } - - @GetMapping("/run-simulated-queries") - public ResponseEntity runSimulatedQueries( - @RequestParam(name = "uniqueQueriesCount", defaultValue = "3") int uniqueQueriesCount, - @RequestParam(name = "repetitions", defaultValue = "100") int repetitions - ) { - long startTime = System.currentTimeMillis(); - int totalOperations = 0; - - for (int queryTypeIndex = 0; queryTypeIndex < uniqueQueriesCount; queryTypeIndex++) { - char queryTypeChar = (char) ('A' + queryTypeIndex); - String parentSpanName = "Batch_Type" + queryTypeChar; - Span typeParentSpan = otelTracer.spanBuilder(parentSpanName).startSpan(); - - try (Scope scope = typeParentSpan.makeCurrent()) { - for (int execution = 1; execution <= repetitions; execution++) { - String operationName = "SimulatedClinicQuery_Type" + queryTypeChar; - performObservableOperation(operationName); - totalOperations++; - } - } finally { - typeParentSpan.end(); - } - } - - long endTime = System.currentTimeMillis(); - String message = String.format("Executed %d simulated clinic query operations in %d ms.", totalOperations, (endTime - startTime)); - logger.info(message); - return ResponseEntity.ok(message); - } - - @PostMapping("/recreate-and-populate-logs") - public ResponseEntity recreateAndPopulateLogs(@RequestParam(name = "count", defaultValue = "6000000") int count) { - logger.info("Received request to recreate and populate {} clinic activity logs.", count); - if (count <= 0) { - return ResponseEntity.badRequest().body("Count must be a positive integer."); - } - try { - // Drop the table - jdbcTemplate.execute("DROP TABLE IF EXISTS clinic_activity_logs"); - logger.info("Table 'clinic_activity_logs' dropped successfully."); - - // Recreate the table - String createTableSql = "CREATE TABLE clinic_activity_logs (" + - "id SERIAL PRIMARY KEY," + - "activity_type VARCHAR(255)," + - "numeric_value INTEGER," + - "event_timestamp TIMESTAMP," + - "status_flag BOOLEAN," + - "payload TEXT" + - ")"; - jdbcTemplate.execute(createTableSql); - logger.info("Table 'clinic_activity_logs' created successfully."); - - // Populate data - dataService.populateData(count); - return ResponseEntity.ok("Successfully recreated and initiated population of " + count + " clinic activity logs."); - } catch (Exception e) { - logger.error("Error during clinic activity log recreation and population", e); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error during data recreation and population: " + e.getMessage()); - } - } - - private void performObservableOperation(String operationName) { - Span span = otelTracer.spanBuilder(operationName) - .setSpanKind(SpanKind.CLIENT) - .setAttribute("db.system", "postgresql") - .setAttribute("db.name", "petclinic") - .setAttribute("db.statement", "SELECT * FROM some_table" + operationName) - .setAttribute("db.operation", "SELECT") + final int BATCH_SIZE = 1000; + final int numericValueToTest = 50000; + + String sql = "SELECT id, activity_type, numeric_value, event_timestamp, status_flag, payload " + + "FROM clinic_activity_logs " + + "WHERE numeric_value = ? " + + "LIMIT ?"; + + Span span = otelTracer.spanBuilder("BatchQueryClinicLogs") + .setAttribute("db.batch_size", BATCH_SIZE) + .setAttribute("db.repetitions", repetitions) .startSpan(); - try { - Thread.sleep(ThreadLocalRandom.current().nextInt(1, 6)); - logger.debug("Executing simulated operation: {}", operationName); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - logger.error("Simulated operation {} interrupted", operationName, e); - span.recordException(e); - } finally { - span.end(); - } - } -} + + try (Scope scope = span.makeCurrent()) { + List> lastResults = null; + for (int i = 0; i < repetitions; i++) { + lastResults = jdbcTemplate.query( + sql, + new Object[]{numericValueToTest, BATCH_ \ No newline at end of file