Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.ThreadLocalRandom;

@RestController
Expand Down Expand Up @@ -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");
Expand All @@ -72,106 +72,22 @@ public ResponseEntity<String> populateData(@RequestParam(name = "count", default
@GetMapping(value = "/query-logs", produces = "application/json")
public List<Map<String, Object>> 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<Map<String, Object>> lastResults = null;
for (int i = 0; i < repetitions; i++) {
lastResults = jdbcTemplate.queryForList(sql, numericValueToTest);
}
return lastResults;
}

@DeleteMapping("/cleanup-logs")
public ResponseEntity<String> 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<String> 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<String> 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<Map<String, Object>> lastResults = null;
for (int i = 0; i < repetitions; i++) {
lastResults = jdbcTemplate.query(
sql,
new Object[]{numericValueToTest, BATCH_
Loading