Skip to content

Commit 53818ef

Browse files
committed
added new controller
1 parent 54ca343 commit 53818ef

File tree

4 files changed

+83
-31
lines changed

4 files changed

+83
-31
lines changed

src/main/java/org/springframework/samples/petclinic/clinicactivity/ClinicActivityController.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,42 +69,16 @@ public ResponseEntity<String> populateData(@RequestParam(name = "count", default
6969
}
7070
}
7171

72-
@GetMapping("/query-logs")
73-
public ResponseEntity<String> getLogs(
74-
@RequestParam(name = "repetitions", defaultValue = "100") int repetitions) {
75-
76-
if (repetitions <= 0) {
77-
return ResponseEntity.badRequest().body("Repetitions must be a positive integer.");
78-
}
79-
72+
@GetMapping(value = "/query-logs", produces = "application/json")
73+
public List<Map<String, Object>> getLogs(
74+
@RequestParam(name = "repetitions", defaultValue = "1") int repetitions) {
8075
int numericValueToTest = 50000;
8176
String sql = "SELECT id, activity_type, numeric_value, event_timestamp, status_flag, payload FROM clinic_activity_logs WHERE numeric_value = ?";
82-
83-
logger.info("Executing direct JDBC query for numeric_value = {}, {} times.", numericValueToTest, repetitions);
84-
85-
long totalTimeForAllRepetitionsNanos = 0;
86-
int rowsFoundLastCall = 0;
87-
List<Map<String, Object>> lastResults; // To store results from the last call
88-
77+
List<Map<String, Object>> lastResults = null;
8978
for (int i = 0; i < repetitions; i++) {
90-
long startTimeNanos = System.nanoTime();
9179
lastResults = jdbcTemplate.queryForList(sql, numericValueToTest);
92-
long endTimeNanos = System.nanoTime();
93-
totalTimeForAllRepetitionsNanos += (endTimeNanos - startTimeNanos);
94-
if (i == repetitions - 1) { // Get row count from the last execution
95-
rowsFoundLastCall = lastResults.size();
96-
}
9780
}
98-
99-
long totalDurationMillis = totalTimeForAllRepetitionsNanos / 1_000_000;
100-
101-
String message = String.format(
102-
"Executed JDBC query for numeric_value = %d, %d time(s). Last call found %d rows. Total execution time: %d ms.",
103-
numericValueToTest, repetitions, rowsFoundLastCall, totalDurationMillis
104-
);
105-
logger.info(message);
106-
107-
return ResponseEntity.ok(message);
81+
return lastResults;
10882
}
10983

11084
@DeleteMapping("/cleanup-logs")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.springframework.samples.petclinic.clinicactivity;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.jdbc.core.JdbcTemplate;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
@Controller
15+
@RequestMapping("/clinic-activity")
16+
public class ClinicActivityUiController {
17+
18+
private final JdbcTemplate jdbcTemplate;
19+
20+
@Autowired
21+
public ClinicActivityUiController(JdbcTemplate jdbcTemplate) {
22+
this.jdbcTemplate = jdbcTemplate;
23+
}
24+
25+
@GetMapping("/query-logs")
26+
public String showQueryLogsPage() {
27+
return "clinicactivity/query-logs";
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'activity')}">
3+
<body>
4+
<h1>Clinic Activity Logs</h1>
5+
<div id="loader" style="display:none;">Loading...</div>
6+
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.5/css/jquery.dataTables.min.css" />
7+
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
8+
<script src="https://cdn.datatables.net/1.13.5/js/jquery.dataTables.min.js"></script>
9+
<table id="logsTable" class="display" style="width:100%">
10+
<thead>
11+
<tr>
12+
<th>ID</th>
13+
<th>Activity Type</th>
14+
<!-- Numeric Value column hidden -->
15+
<th>Event Timestamp</th>
16+
<th>Status Flag</th>
17+
<!-- Payload column hidden -->
18+
</tr>
19+
</thead>
20+
<tbody></tbody>
21+
</table>
22+
<script th:inline="javascript">
23+
function fetchData() {
24+
$('#loader').show();
25+
$.getJSON('/api/clinic-activity/query-logs', function(data) {
26+
$('#loader').hide();
27+
$('#logsTable').DataTable({
28+
data: data,
29+
destroy: true,
30+
columns: [
31+
{ data: 'id' },
32+
{ data: 'activity_type' },
33+
{ data: 'event_timestamp' },
34+
{ data: 'status_flag' }
35+
]
36+
});
37+
});
38+
}
39+
$(document).ready(function() {
40+
fetchData();
41+
});
42+
</script>
43+
</body>
44+
</html>

src/main/resources/templates/fragments/layout.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
<span>Error</span>
6767
</li>
6868

69+
<li th:replace="~{::menuItem ('/clinic-activity/query-logs','activity','Clinic Activity Logs','bar-chart','Activity')}">
70+
<span class="fa fa-bar-chart" aria-hidden="true"></span>
71+
<span>Activity</span>
72+
</li>
73+
6974
</ul>
7075
</div>
7176
</div>

0 commit comments

Comments
 (0)