feat!: move query/logs/routes/diagram/agent-view endpoints under /environments/{envSlug}/
P3C — the last data/query wave of the taxonomy migration. Every user-
facing read endpoint that was keyed on env-as-query-param is now under
the env-scoped URL, making env impossible to omit and unambiguous in
server-side tenant+env filtering.
Server:
- SearchController: /api/v1/search/** → /api/v1/environments/{envSlug}/...
Endpoints: /executions (GET), /executions/search (POST), /stats,
/stats/timeseries, /stats/timeseries/by-app, /stats/timeseries/by-route,
/stats/punchcard, /attributes/keys, /errors/top. Env comes from path.
- LogQueryController: /api/v1/logs → /api/v1/environments/{envSlug}/logs.
- RouteCatalogController: /api/v1/routes/catalog → /api/v1/environments/
{envSlug}/routes. Env filter unconditional (path).
- RouteMetricsController: /api/v1/routes/metrics →
/api/v1/environments/{envSlug}/routes/metrics (and /metrics/processors).
- DiagramRenderController: /{contentHash}/render stays flat (hashes are
globally unique). Find-by-route moved to /api/v1/environments/{envSlug}/
apps/{appSlug}/routes/{routeId}/diagram — the old GET /diagrams?...
handler is removed.
- Agent views split cleanly:
- AgentListController (new): /api/v1/environments/{envSlug}/agents
- AgentEventsController: /api/v1/environments/{envSlug}/agents/events
- AgentMetricsController: /api/v1/environments/{envSlug}/agents/
{agentId}/metrics — now also rejects cross-env agents (404) as a
defense-in-depth check, fulfilling B3.
Agent self-service endpoints (register/refresh/heartbeat/deregister)
remain flat at /api/v1/agents/** — JWT-authoritative.
SPA:
- queries/agents.ts, agent-metrics.ts, logs.ts, catalog.ts (route
metrics only; /catalog stays flat), processor-metrics.ts,
executions.ts (attributes/keys, stats, timeseries, search),
dashboard.ts (all stats/errors/punchcard), correlation.ts,
diagrams.ts (by-route) — all rewritten to env-scoped URLs.
- Hooks now either read env from useEnvironmentStore internally or
require it as an argument. Query keys include env so switching env
invalidates caches.
- useAgents/useAgentEvents signature simplified — env is no longer a
parameter; it's read from the store. Callers (LayoutShell,
AgentHealth, AgentInstance) updated accordingly.
- LogTab and useStartupLogs thread env through to useLogs.
- envFetch helper introduced in executions.ts for env-prefixed raw
fetch until schema.d.ts is regenerated against the new backend.
BREAKING CHANGE: All these flat paths are removed:
/api/v1/search/**, /api/v1/logs, /api/v1/routes/catalog,
/api/v1/routes/metrics (and /processors), /api/v1/diagrams
(lookup), /api/v1/agents (list), /api/v1/agents/events-log,
/api/v1/agents/{id}/metrics, /api/v1/agent-events.
Clients must use the /api/v1/environments/{envSlug}/... equivalents.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package com.cameleer.server.app.controller;
|
||||
|
||||
import com.cameleer.server.app.dto.AgentEventResponse;
|
||||
import com.cameleer.server.app.web.EnvPath;
|
||||
import com.cameleer.server.core.agent.AgentEventService;
|
||||
import com.cameleer.server.core.runtime.Environment;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -15,8 +17,8 @@ import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/agents/events-log")
|
||||
@Tag(name = "Agent Events", description = "Agent lifecycle event log")
|
||||
@RequestMapping("/api/v1/environments/{envSlug}/agents/events")
|
||||
@Tag(name = "Agent Events", description = "Agent lifecycle event log (env-scoped)")
|
||||
public class AgentEventsController {
|
||||
|
||||
private final AgentEventService agentEventService;
|
||||
@@ -26,13 +28,13 @@ public class AgentEventsController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "Query agent events",
|
||||
@Operation(summary = "Query agent events in this environment",
|
||||
description = "Returns agent lifecycle events, optionally filtered by app and/or agent ID")
|
||||
@ApiResponse(responseCode = "200", description = "Events returned")
|
||||
public ResponseEntity<List<AgentEventResponse>> getEvents(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam(required = false) String appId,
|
||||
@RequestParam(required = false) String agentId,
|
||||
@RequestParam(required = false) String environment,
|
||||
@RequestParam(required = false) String from,
|
||||
@RequestParam(required = false) String to,
|
||||
@RequestParam(defaultValue = "50") int limit) {
|
||||
@@ -40,7 +42,7 @@ public class AgentEventsController {
|
||||
Instant fromInstant = from != null ? Instant.parse(from) : null;
|
||||
Instant toInstant = to != null ? Instant.parse(to) : null;
|
||||
|
||||
var events = agentEventService.queryEvents(appId, agentId, environment, fromInstant, toInstant, limit)
|
||||
var events = agentEventService.queryEvents(appId, agentId, env.slug(), fromInstant, toInstant, limit)
|
||||
.stream()
|
||||
.map(AgentEventResponse::from)
|
||||
.toList();
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
package com.cameleer.server.app.controller;
|
||||
|
||||
import com.cameleer.server.app.dto.AgentInstanceResponse;
|
||||
import com.cameleer.server.app.dto.ErrorResponse;
|
||||
import com.cameleer.server.app.web.EnvPath;
|
||||
import com.cameleer.server.core.agent.AgentInfo;
|
||||
import com.cameleer.server.core.agent.AgentRegistryService;
|
||||
import com.cameleer.server.core.agent.AgentState;
|
||||
import com.cameleer.server.core.runtime.Environment;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Read-only user-facing list of agents in an environment. Agent self-service
|
||||
* endpoints (register/heartbeat/refresh/deregister/events/commands) remain
|
||||
* flat at /api/v1/agents/... — those are JWT-authoritative and env is
|
||||
* derived from the token.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/environments/{envSlug}/agents")
|
||||
@Tag(name = "Agent List", description = "List registered agents in an environment")
|
||||
public class AgentListController {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AgentListController.class);
|
||||
|
||||
private final AgentRegistryService registryService;
|
||||
private final JdbcTemplate jdbc;
|
||||
|
||||
public AgentListController(AgentRegistryService registryService,
|
||||
@org.springframework.beans.factory.annotation.Qualifier("clickHouseJdbcTemplate") JdbcTemplate jdbc) {
|
||||
this.registryService = registryService;
|
||||
this.jdbc = jdbc;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "List all agents in this environment",
|
||||
description = "Returns registered agents with runtime metrics, optionally filtered by status and/or application")
|
||||
@ApiResponse(responseCode = "200", description = "Agent list returned")
|
||||
@ApiResponse(responseCode = "400", description = "Invalid status filter",
|
||||
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
|
||||
public ResponseEntity<List<AgentInstanceResponse>> listAgents(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam(required = false) String status,
|
||||
@RequestParam(required = false) String application) {
|
||||
List<AgentInfo> agents;
|
||||
|
||||
if (status != null) {
|
||||
try {
|
||||
AgentState stateFilter = AgentState.valueOf(status.toUpperCase());
|
||||
agents = registryService.findByState(stateFilter);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
} else {
|
||||
agents = registryService.findAll();
|
||||
}
|
||||
|
||||
// Filter by env (from path — always applied)
|
||||
agents = agents.stream()
|
||||
.filter(a -> env.slug().equals(a.environmentId()))
|
||||
.toList();
|
||||
|
||||
if (application != null && !application.isBlank()) {
|
||||
agents = agents.stream()
|
||||
.filter(a -> application.equals(a.applicationId()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Map<String, double[]> agentMetrics = queryAgentMetrics();
|
||||
Map<String, Double> cpuByInstance = queryAgentCpuUsage();
|
||||
final List<AgentInfo> finalAgents = agents;
|
||||
|
||||
List<AgentInstanceResponse> response = finalAgents.stream()
|
||||
.map(a -> {
|
||||
AgentInstanceResponse dto = AgentInstanceResponse.from(a);
|
||||
double[] m = agentMetrics.get(a.applicationId());
|
||||
if (m != null) {
|
||||
long appAgentCount = finalAgents.stream()
|
||||
.filter(ag -> ag.applicationId().equals(a.applicationId())).count();
|
||||
double agentTps = appAgentCount > 0 ? m[0] / appAgentCount : 0;
|
||||
double errorRate = m[1];
|
||||
int activeRoutes = (int) m[2];
|
||||
dto = dto.withMetrics(agentTps, errorRate, activeRoutes);
|
||||
}
|
||||
Double cpu = cpuByInstance.get(a.instanceId());
|
||||
if (cpu != null) {
|
||||
dto = dto.withCpuUsage(cpu);
|
||||
}
|
||||
return dto;
|
||||
})
|
||||
.toList();
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
private Map<String, double[]> queryAgentMetrics() {
|
||||
Map<String, double[]> result = new HashMap<>();
|
||||
Instant now = Instant.now();
|
||||
Instant from1m = now.minus(1, ChronoUnit.MINUTES);
|
||||
try {
|
||||
jdbc.query(
|
||||
"SELECT application_id, " +
|
||||
"uniqMerge(total_count) AS total, " +
|
||||
"uniqIfMerge(failed_count) AS failed, " +
|
||||
"COUNT(DISTINCT route_id) AS active_routes " +
|
||||
"FROM stats_1m_route WHERE bucket >= " + lit(from1m) + " AND bucket < " + lit(now) +
|
||||
" GROUP BY application_id",
|
||||
rs -> {
|
||||
long total = rs.getLong("total");
|
||||
long failed = rs.getLong("failed");
|
||||
double tps = total / 60.0;
|
||||
double errorRate = total > 0 ? (double) failed / total : 0.0;
|
||||
int activeRoutes = rs.getInt("active_routes");
|
||||
result.put(rs.getString("application_id"), new double[]{tps, errorRate, activeRoutes});
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.debug("Could not query agent metrics: {}", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, Double> queryAgentCpuUsage() {
|
||||
Map<String, Double> result = new HashMap<>();
|
||||
Instant now = Instant.now();
|
||||
Instant from2m = now.minus(2, ChronoUnit.MINUTES);
|
||||
try {
|
||||
jdbc.query(
|
||||
"SELECT instance_id, avg(metric_value) AS cpu_avg " +
|
||||
"FROM agent_metrics " +
|
||||
"WHERE metric_name = 'process.cpu.usage.value'" +
|
||||
" AND collected_at >= " + lit(from2m) + " AND collected_at < " + lit(now) +
|
||||
" GROUP BY instance_id",
|
||||
rs -> {
|
||||
result.put(rs.getString("instance_id"), rs.getDouble("cpu_avg"));
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.debug("Could not query agent CPU usage: {}", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String lit(Instant instant) {
|
||||
return "'" + java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||
.withZone(java.time.ZoneOffset.UTC)
|
||||
.format(instant.truncatedTo(ChronoUnit.SECONDS)) + "'";
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,13 @@ package com.cameleer.server.app.controller;
|
||||
|
||||
import com.cameleer.server.app.dto.AgentMetricsResponse;
|
||||
import com.cameleer.server.app.dto.MetricBucket;
|
||||
import com.cameleer.server.app.web.EnvPath;
|
||||
import com.cameleer.server.core.agent.AgentInfo;
|
||||
import com.cameleer.server.core.agent.AgentRegistryService;
|
||||
import com.cameleer.server.core.runtime.Environment;
|
||||
import com.cameleer.server.core.storage.MetricsQueryStore;
|
||||
import com.cameleer.server.core.storage.model.MetricTimeSeries;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.Instant;
|
||||
@@ -12,17 +17,21 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/agents/{agentId}/metrics")
|
||||
@RequestMapping("/api/v1/environments/{envSlug}/agents/{agentId}/metrics")
|
||||
public class AgentMetricsController {
|
||||
|
||||
private final MetricsQueryStore metricsQueryStore;
|
||||
private final AgentRegistryService registryService;
|
||||
|
||||
public AgentMetricsController(MetricsQueryStore metricsQueryStore) {
|
||||
public AgentMetricsController(MetricsQueryStore metricsQueryStore,
|
||||
AgentRegistryService registryService) {
|
||||
this.metricsQueryStore = metricsQueryStore;
|
||||
this.registryService = registryService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public AgentMetricsResponse getMetrics(
|
||||
public ResponseEntity<AgentMetricsResponse> getMetrics(
|
||||
@EnvPath Environment env,
|
||||
@PathVariable String agentId,
|
||||
@RequestParam String names,
|
||||
@RequestParam(required = false) Instant from,
|
||||
@@ -30,6 +39,13 @@ public class AgentMetricsController {
|
||||
@RequestParam(defaultValue = "60") int buckets,
|
||||
@RequestParam(defaultValue = "gauge") String mode) {
|
||||
|
||||
// Defence in depth: if the agent is currently in the registry, reject
|
||||
// requests that cross-env (path env doesn't match the agent's env).
|
||||
AgentInfo agent = registryService.findById(agentId);
|
||||
if (agent != null && !env.slug().equals(agent.environmentId())) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
if (from == null) from = Instant.now().minus(1, ChronoUnit.HOURS);
|
||||
if (to == null) to = Instant.now();
|
||||
|
||||
@@ -48,6 +64,6 @@ public class AgentMetricsController {
|
||||
(a, b) -> a,
|
||||
LinkedHashMap::new));
|
||||
|
||||
return new AgentMetricsResponse(result);
|
||||
return ResponseEntity.ok(new AgentMetricsResponse(result));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,123 +321,7 @@ public class AgentRegistrationController {
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "List all agents",
|
||||
description = "Returns all registered agents with runtime metrics, optionally filtered by status and/or application")
|
||||
@ApiResponse(responseCode = "200", description = "Agent list returned")
|
||||
@ApiResponse(responseCode = "400", description = "Invalid status filter",
|
||||
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
|
||||
public ResponseEntity<List<AgentInstanceResponse>> listAgents(
|
||||
@RequestParam(required = false) String status,
|
||||
@RequestParam(required = false) String application,
|
||||
@RequestParam(required = false) String environment) {
|
||||
List<AgentInfo> agents;
|
||||
|
||||
if (status != null) {
|
||||
try {
|
||||
AgentState stateFilter = AgentState.valueOf(status.toUpperCase());
|
||||
agents = registryService.findByState(stateFilter);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
} else {
|
||||
agents = registryService.findAll();
|
||||
}
|
||||
|
||||
// Apply application filter if specified
|
||||
if (application != null && !application.isBlank()) {
|
||||
agents = agents.stream()
|
||||
.filter(a -> application.equals(a.applicationId()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Apply environment filter if specified
|
||||
if (environment != null && !environment.isBlank()) {
|
||||
agents = agents.stream()
|
||||
.filter(a -> environment.equals(a.environmentId()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Enrich with runtime metrics from continuous aggregates
|
||||
Map<String, double[]> agentMetrics = queryAgentMetrics();
|
||||
Map<String, Double> cpuByInstance = queryAgentCpuUsage();
|
||||
final List<AgentInfo> finalAgents = agents;
|
||||
|
||||
List<AgentInstanceResponse> response = finalAgents.stream()
|
||||
.map(a -> {
|
||||
AgentInstanceResponse dto = AgentInstanceResponse.from(a);
|
||||
double[] m = agentMetrics.get(a.applicationId());
|
||||
if (m != null) {
|
||||
long appAgentCount = finalAgents.stream()
|
||||
.filter(ag -> ag.applicationId().equals(a.applicationId())).count();
|
||||
double agentTps = appAgentCount > 0 ? m[0] / appAgentCount : 0;
|
||||
double errorRate = m[1];
|
||||
int activeRoutes = (int) m[2];
|
||||
dto = dto.withMetrics(agentTps, errorRate, activeRoutes);
|
||||
}
|
||||
Double cpu = cpuByInstance.get(a.instanceId());
|
||||
if (cpu != null) {
|
||||
dto = dto.withCpuUsage(cpu);
|
||||
}
|
||||
return dto;
|
||||
})
|
||||
.toList();
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
private Map<String, double[]> queryAgentMetrics() {
|
||||
Map<String, double[]> result = new HashMap<>();
|
||||
Instant now = Instant.now();
|
||||
Instant from1m = now.minus(1, ChronoUnit.MINUTES);
|
||||
try {
|
||||
// Literal SQL — ClickHouse JDBC driver wraps prepared statements in sub-queries
|
||||
// that strip AggregateFunction column types, breaking -Merge combinators
|
||||
jdbc.query(
|
||||
"SELECT application_id, " +
|
||||
"uniqMerge(total_count) AS total, " +
|
||||
"uniqIfMerge(failed_count) AS failed, " +
|
||||
"COUNT(DISTINCT route_id) AS active_routes " +
|
||||
"FROM stats_1m_route WHERE bucket >= " + lit(from1m) + " AND bucket < " + lit(now) +
|
||||
" GROUP BY application_id",
|
||||
rs -> {
|
||||
long total = rs.getLong("total");
|
||||
long failed = rs.getLong("failed");
|
||||
double tps = total / 60.0;
|
||||
double errorRate = total > 0 ? (double) failed / total : 0.0;
|
||||
int activeRoutes = rs.getInt("active_routes");
|
||||
result.put(rs.getString("application_id"), new double[]{tps, errorRate, activeRoutes});
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.debug("Could not query agent metrics: {}", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Query average CPU usage per agent instance over the last 2 minutes. */
|
||||
private Map<String, Double> queryAgentCpuUsage() {
|
||||
Map<String, Double> result = new HashMap<>();
|
||||
Instant now = Instant.now();
|
||||
Instant from2m = now.minus(2, ChronoUnit.MINUTES);
|
||||
try {
|
||||
jdbc.query(
|
||||
"SELECT instance_id, avg(metric_value) AS cpu_avg " +
|
||||
"FROM agent_metrics " +
|
||||
"WHERE metric_name = 'process.cpu.usage.value'" +
|
||||
" AND collected_at >= " + lit(from2m) + " AND collected_at < " + lit(now) +
|
||||
" GROUP BY instance_id",
|
||||
rs -> {
|
||||
result.put(rs.getString("instance_id"), rs.getDouble("cpu_avg"));
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.debug("Could not query agent CPU usage: {}", e.getMessage());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Format an Instant as a ClickHouse DateTime literal. */
|
||||
private static String lit(Instant instant) {
|
||||
return "'" + java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||
.withZone(java.time.ZoneOffset.UTC)
|
||||
.format(instant.truncatedTo(ChronoUnit.SECONDS)) + "'";
|
||||
}
|
||||
// Agent list moved to AgentListController at /api/v1/environments/{envSlug}/agents.
|
||||
// Agent register/refresh/heartbeat/deregister remain here at /api/v1/agents/** —
|
||||
// these are JWT-authoritative and intentionally flat (env from token).
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package com.cameleer.server.app.controller;
|
||||
|
||||
import com.cameleer.common.graph.RouteGraph;
|
||||
import com.cameleer.server.app.web.EnvPath;
|
||||
import com.cameleer.server.core.agent.AgentInfo;
|
||||
import com.cameleer.server.core.agent.AgentRegistryService;
|
||||
import com.cameleer.server.core.diagram.DiagramLayout;
|
||||
import com.cameleer.server.core.diagram.DiagramRenderer;
|
||||
import com.cameleer.server.core.runtime.Environment;
|
||||
import com.cameleer.server.core.storage.DiagramStore;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
@@ -16,7 +18,6 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -24,16 +25,16 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* REST endpoint for rendering route diagrams.
|
||||
* Diagram rendering and lookup.
|
||||
* <p>
|
||||
* Supports content negotiation via Accept header:
|
||||
* <ul>
|
||||
* <li>{@code image/svg+xml} or default: returns SVG document</li>
|
||||
* <li>{@code application/json}: returns JSON layout with node positions</li>
|
||||
* </ul>
|
||||
* Content-addressed rendering stays flat at /api/v1/diagrams/{contentHash}/render:
|
||||
* the hash is globally unique, permalinks are valuable, and no env partitioning
|
||||
* is possible or needed.
|
||||
* <p>
|
||||
* By-app-and-route lookup is env-scoped at
|
||||
* /api/v1/environments/{envSlug}/apps/{appSlug}/routes/{routeId}/diagram.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/diagrams")
|
||||
@Tag(name = "Diagrams", description = "Diagram rendering endpoints")
|
||||
public class DiagramRenderController {
|
||||
|
||||
@@ -51,9 +52,10 @@ public class DiagramRenderController {
|
||||
this.registryService = registryService;
|
||||
}
|
||||
|
||||
@GetMapping("/{contentHash}/render")
|
||||
@Operation(summary = "Render a route diagram",
|
||||
description = "Returns SVG (default) or JSON layout based on Accept header")
|
||||
@GetMapping("/api/v1/diagrams/{contentHash}/render")
|
||||
@Operation(summary = "Render a route diagram by content hash",
|
||||
description = "Returns SVG (default) or JSON layout based on Accept header. "
|
||||
+ "Content hashes are globally unique, so this endpoint is intentionally flat (no env).")
|
||||
@ApiResponse(responseCode = "200", description = "Diagram rendered successfully",
|
||||
content = {
|
||||
@Content(mediaType = "image/svg+xml", schema = @Schema(type = "string")),
|
||||
@@ -73,9 +75,6 @@ public class DiagramRenderController {
|
||||
RouteGraph graph = graphOpt.get();
|
||||
String accept = request.getHeader("Accept");
|
||||
|
||||
// Return JSON only when the client explicitly requests application/json
|
||||
// without also accepting everything (*/*). This means "application/json"
|
||||
// must appear and wildcards must not dominate the preference.
|
||||
if (accept != null && isJsonPreferred(accept)) {
|
||||
DiagramLayout layout = diagramRenderer.layoutJson(graph, direction);
|
||||
return ResponseEntity.ok()
|
||||
@@ -83,25 +82,24 @@ public class DiagramRenderController {
|
||||
.body(layout);
|
||||
}
|
||||
|
||||
// Default to SVG for image/svg+xml, */* or no Accept header
|
||||
String svg = diagramRenderer.renderSvg(graph);
|
||||
return ResponseEntity.ok()
|
||||
.contentType(SVG_MEDIA_TYPE)
|
||||
.body(svg);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "Find diagram by application, environment, and route ID",
|
||||
description = "Resolves (application, environment) to agent IDs and finds the latest diagram for the route. "
|
||||
+ "The environment filter prevents cross-env diagram leakage — without it a dev route could return a prod diagram (or vice versa).")
|
||||
@GetMapping("/api/v1/environments/{envSlug}/apps/{appSlug}/routes/{routeId}/diagram")
|
||||
@Operation(summary = "Find the latest diagram for this app's route in this environment",
|
||||
description = "Resolves agents in this env for this app, then looks up the latest diagram for the route "
|
||||
+ "they reported. Env scope prevents a dev route from returning a prod diagram.")
|
||||
@ApiResponse(responseCode = "200", description = "Diagram layout returned")
|
||||
@ApiResponse(responseCode = "404", description = "No diagram found for the given application, environment, and route")
|
||||
public ResponseEntity<DiagramLayout> findByApplicationAndRoute(
|
||||
@RequestParam String application,
|
||||
@RequestParam String environment,
|
||||
@RequestParam String routeId,
|
||||
@ApiResponse(responseCode = "404", description = "No diagram found")
|
||||
public ResponseEntity<DiagramLayout> findByAppAndRoute(
|
||||
@EnvPath Environment env,
|
||||
@PathVariable String appSlug,
|
||||
@PathVariable String routeId,
|
||||
@RequestParam(defaultValue = "LR") String direction) {
|
||||
List<String> agentIds = registryService.findByApplicationAndEnvironment(application, environment).stream()
|
||||
List<String> agentIds = registryService.findByApplicationAndEnvironment(appSlug, env.slug()).stream()
|
||||
.map(AgentInfo::instanceId)
|
||||
.toList();
|
||||
|
||||
@@ -123,14 +121,6 @@ public class DiagramRenderController {
|
||||
return ResponseEntity.ok(layout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if JSON is the explicitly preferred format.
|
||||
* <p>
|
||||
* Returns true only when the first media type in the Accept header is
|
||||
* "application/json". Clients sending broad Accept lists like
|
||||
* "text/plain, application/json, */*" are treated as unspecific
|
||||
* and receive the SVG default.
|
||||
*/
|
||||
private boolean isJsonPreferred(String accept) {
|
||||
String[] parts = accept.split(",");
|
||||
if (parts.length == 0) return false;
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.cameleer.server.app.controller;
|
||||
|
||||
import com.cameleer.server.app.dto.LogEntryResponse;
|
||||
import com.cameleer.server.app.dto.LogSearchPageResponse;
|
||||
import com.cameleer.server.app.web.EnvPath;
|
||||
import com.cameleer.server.core.runtime.Environment;
|
||||
import com.cameleer.server.core.search.LogSearchRequest;
|
||||
import com.cameleer.server.core.search.LogSearchResponse;
|
||||
import com.cameleer.server.core.storage.LogIndex;
|
||||
@@ -18,8 +20,8 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/logs")
|
||||
@Tag(name = "Application Logs", description = "Query application logs")
|
||||
@RequestMapping("/api/v1/environments/{envSlug}")
|
||||
@Tag(name = "Application Logs", description = "Query application logs (env-scoped)")
|
||||
public class LogQueryController {
|
||||
|
||||
private final LogIndex logIndex;
|
||||
@@ -28,11 +30,12 @@ public class LogQueryController {
|
||||
this.logIndex = logIndex;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "Search application log entries",
|
||||
description = "Returns log entries with cursor-based pagination and level count aggregation. " +
|
||||
"Supports free-text search, multi-level filtering, and optional application scoping.")
|
||||
@GetMapping("/logs")
|
||||
@Operation(summary = "Search application log entries in this environment",
|
||||
description = "Cursor-paginated log search scoped to the env in the path. "
|
||||
+ "Supports free-text search, multi-level filtering, and optional application/agent scoping.")
|
||||
public ResponseEntity<LogSearchPageResponse> searchLogs(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam(required = false) String q,
|
||||
@RequestParam(required = false) String query,
|
||||
@RequestParam(required = false) String level,
|
||||
@@ -40,7 +43,6 @@ public class LogQueryController {
|
||||
@RequestParam(name = "agentId", required = false) String instanceId,
|
||||
@RequestParam(required = false) String exchangeId,
|
||||
@RequestParam(required = false) String logger,
|
||||
@RequestParam(required = false) String environment,
|
||||
@RequestParam(required = false) String source,
|
||||
@RequestParam(required = false) String from,
|
||||
@RequestParam(required = false) String to,
|
||||
@@ -51,7 +53,6 @@ public class LogQueryController {
|
||||
// q takes precedence over deprecated query param
|
||||
String searchText = q != null ? q : query;
|
||||
|
||||
// Parse CSV levels
|
||||
List<String> levels = List.of();
|
||||
if (level != null && !level.isEmpty()) {
|
||||
levels = Arrays.stream(level.split(","))
|
||||
@@ -65,7 +66,7 @@ public class LogQueryController {
|
||||
|
||||
LogSearchRequest request = new LogSearchRequest(
|
||||
searchText, levels, application, instanceId, exchangeId,
|
||||
logger, environment, source, fromInstant, toInstant, cursor, limit, sort);
|
||||
logger, env.slug(), source, fromInstant, toInstant, cursor, limit, sort);
|
||||
|
||||
LogSearchResponse result = logIndex.search(request);
|
||||
|
||||
|
||||
@@ -3,15 +3,16 @@ package com.cameleer.server.app.controller;
|
||||
import com.cameleer.server.app.dto.AgentSummary;
|
||||
import com.cameleer.server.app.dto.AppCatalogEntry;
|
||||
import com.cameleer.server.app.dto.RouteSummary;
|
||||
import com.cameleer.server.app.web.EnvPath;
|
||||
import com.cameleer.common.graph.RouteGraph;
|
||||
import com.cameleer.server.core.agent.AgentInfo;
|
||||
import com.cameleer.server.core.agent.AgentRegistryService;
|
||||
import com.cameleer.server.core.agent.AgentState;
|
||||
import com.cameleer.server.core.agent.RouteStateRegistry;
|
||||
import com.cameleer.server.core.runtime.Environment;
|
||||
import com.cameleer.server.core.storage.DiagramStore;
|
||||
import com.cameleer.server.core.storage.RouteCatalogEntry;
|
||||
import com.cameleer.server.core.storage.RouteCatalogStore;
|
||||
import com.cameleer.server.core.storage.StatsStore;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -34,8 +35,8 @@ import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/routes")
|
||||
@Tag(name = "Route Catalog", description = "Route catalog and discovery")
|
||||
@RequestMapping("/api/v1/environments/{envSlug}")
|
||||
@Tag(name = "Route Catalog", description = "Route catalog and discovery (env-scoped)")
|
||||
public class RouteCatalogController {
|
||||
|
||||
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RouteCatalogController.class);
|
||||
@@ -58,28 +59,22 @@ public class RouteCatalogController {
|
||||
this.routeCatalogStore = routeCatalogStore;
|
||||
}
|
||||
|
||||
@GetMapping("/catalog")
|
||||
@Operation(summary = "Get route catalog",
|
||||
description = "Returns all applications with their routes, agents, and health status")
|
||||
@GetMapping("/routes")
|
||||
@Operation(summary = "Get route catalog for this environment",
|
||||
description = "Returns all applications with their routes, agents, and health status — filtered to this environment")
|
||||
@ApiResponse(responseCode = "200", description = "Catalog returned")
|
||||
public ResponseEntity<List<AppCatalogEntry>> getCatalog(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam(required = false) String from,
|
||||
@RequestParam(required = false) String to,
|
||||
@RequestParam(required = false) String environment) {
|
||||
List<AgentInfo> allAgents = registryService.findAll();
|
||||
@RequestParam(required = false) String to) {
|
||||
String envSlug = env.slug();
|
||||
List<AgentInfo> allAgents = registryService.findAll().stream()
|
||||
.filter(a -> envSlug.equals(a.environmentId()))
|
||||
.toList();
|
||||
|
||||
// Filter agents by environment if specified
|
||||
if (environment != null && !environment.isBlank()) {
|
||||
allAgents = allAgents.stream()
|
||||
.filter(a -> environment.equals(a.environmentId()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Group agents by application name
|
||||
Map<String, List<AgentInfo>> agentsByApp = allAgents.stream()
|
||||
.collect(Collectors.groupingBy(AgentInfo::applicationId, LinkedHashMap::new, Collectors.toList()));
|
||||
|
||||
// Collect all distinct routes per app
|
||||
Map<String, Set<String>> routesByApp = new LinkedHashMap<>();
|
||||
for (var entry : agentsByApp.entrySet()) {
|
||||
Set<String> routes = new LinkedHashSet<>();
|
||||
@@ -91,21 +86,16 @@ public class RouteCatalogController {
|
||||
routesByApp.put(entry.getKey(), routes);
|
||||
}
|
||||
|
||||
// Time range for exchange counts — use provided range or default to last 24h
|
||||
Instant now = Instant.now();
|
||||
Instant rangeFrom = from != null ? Instant.parse(from) : now.minus(24, ChronoUnit.HOURS);
|
||||
Instant rangeTo = to != null ? Instant.parse(to) : now;
|
||||
// Route exchange counts from AggregatingMergeTree (literal SQL — ClickHouse JDBC driver
|
||||
// wraps prepared statements in sub-queries that strip AggregateFunction column types)
|
||||
Map<String, Long> routeExchangeCounts = new LinkedHashMap<>();
|
||||
Map<String, Instant> routeLastSeen = new LinkedHashMap<>();
|
||||
try {
|
||||
String envFilter = (environment != null && !environment.isBlank())
|
||||
? " AND environment = " + lit(environment) : "";
|
||||
jdbc.query(
|
||||
"SELECT application_id, route_id, uniqMerge(total_count) AS cnt, MAX(bucket) AS last_seen " +
|
||||
"FROM stats_1m_route WHERE bucket >= " + lit(rangeFrom) + " AND bucket < " + lit(rangeTo) +
|
||||
envFilter +
|
||||
" AND environment = " + lit(envSlug) +
|
||||
" GROUP BY application_id, route_id",
|
||||
rs -> {
|
||||
String key = rs.getString("application_id") + "/" + rs.getString("route_id");
|
||||
@@ -117,9 +107,6 @@ public class RouteCatalogController {
|
||||
log.warn("Failed to query route exchange counts: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// Merge route IDs from ClickHouse stats into routesByApp.
|
||||
// After server restart, auto-healed agents have empty routeIds, but
|
||||
// ClickHouse still has execution data with the correct route IDs.
|
||||
for (var countEntry : routeExchangeCounts.entrySet()) {
|
||||
String[] parts = countEntry.getKey().split("/", 2);
|
||||
if (parts.length == 2) {
|
||||
@@ -127,12 +114,8 @@ public class RouteCatalogController {
|
||||
}
|
||||
}
|
||||
|
||||
// Merge routes from persistent catalog (covers routes with 0 executions
|
||||
// and routes from previous app versions within the selected time window)
|
||||
try {
|
||||
List<RouteCatalogEntry> catalogEntries = (environment != null && !environment.isBlank())
|
||||
? routeCatalogStore.findByEnvironment(environment, rangeFrom, rangeTo)
|
||||
: routeCatalogStore.findAll(rangeFrom, rangeTo);
|
||||
List<RouteCatalogEntry> catalogEntries = routeCatalogStore.findByEnvironment(envSlug, rangeFrom, rangeTo);
|
||||
for (RouteCatalogEntry entry : catalogEntries) {
|
||||
routesByApp.computeIfAbsent(entry.applicationId(), k -> new LinkedHashSet<>())
|
||||
.add(entry.routeId());
|
||||
@@ -141,7 +124,6 @@ public class RouteCatalogController {
|
||||
log.warn("Failed to query route catalog: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// Build catalog entries — merge apps from agent registry + ClickHouse data
|
||||
Set<String> allAppIds = new LinkedHashSet<>(agentsByApp.keySet());
|
||||
allAppIds.addAll(routesByApp.keySet());
|
||||
|
||||
@@ -149,7 +131,6 @@ public class RouteCatalogController {
|
||||
for (String appId : allAppIds) {
|
||||
List<AgentInfo> agents = agentsByApp.getOrDefault(appId, List.of());
|
||||
|
||||
// Routes
|
||||
Set<String> routeIds = routesByApp.getOrDefault(appId, Set.of());
|
||||
List<String> agentIds = agents.stream().map(AgentInfo::instanceId).toList();
|
||||
List<RouteSummary> routeSummaries = routeIds.stream()
|
||||
@@ -159,21 +140,17 @@ public class RouteCatalogController {
|
||||
Instant lastSeen = routeLastSeen.get(key);
|
||||
String fromUri = resolveFromEndpointUri(routeId, agentIds);
|
||||
String state = routeStateRegistry.getState(appId, routeId).name().toLowerCase();
|
||||
// Only include non-default states (stopped/suspended); null means started
|
||||
String routeState = "started".equals(state) ? null : state;
|
||||
return new RouteSummary(routeId, count, lastSeen, fromUri, routeState);
|
||||
})
|
||||
.toList();
|
||||
|
||||
// Agent summaries
|
||||
List<AgentSummary> agentSummaries = agents.stream()
|
||||
.map(a -> new AgentSummary(a.instanceId(), a.displayName(), a.state().name().toLowerCase(), 0.0))
|
||||
.toList();
|
||||
|
||||
// Health = worst state among agents
|
||||
String health = computeWorstHealth(agents);
|
||||
|
||||
// Total exchange count for the app
|
||||
long totalExchanges = routeSummaries.stream().mapToLong(RouteSummary::exchangeCount).sum();
|
||||
|
||||
catalog.add(new AppCatalogEntry(appId, routeSummaries, agentSummaries,
|
||||
@@ -183,7 +160,6 @@ public class RouteCatalogController {
|
||||
return ResponseEntity.ok(catalog);
|
||||
}
|
||||
|
||||
/** Resolve the from() endpoint URI for a route by looking up its diagram. */
|
||||
private String resolveFromEndpointUri(String routeId, List<String> agentIds) {
|
||||
return diagramStore.findContentHashForRouteByAgents(routeId, agentIds)
|
||||
.flatMap(diagramStore::findByContentHash)
|
||||
@@ -192,14 +168,12 @@ public class RouteCatalogController {
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/** Format an Instant as a ClickHouse DateTime literal in UTC. */
|
||||
private static String lit(Instant instant) {
|
||||
return "'" + java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||
.withZone(java.time.ZoneOffset.UTC)
|
||||
.format(instant.truncatedTo(ChronoUnit.SECONDS)) + "'";
|
||||
}
|
||||
|
||||
/** Format a string as a ClickHouse SQL literal with backslash + quote escaping. */
|
||||
private static String lit(String value) {
|
||||
return "'" + value.replace("\\", "\\\\").replace("'", "\\'") + "'";
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ package com.cameleer.server.app.controller;
|
||||
|
||||
import com.cameleer.server.app.dto.ProcessorMetrics;
|
||||
import com.cameleer.server.app.dto.RouteMetrics;
|
||||
import com.cameleer.server.app.web.EnvPath;
|
||||
import com.cameleer.server.core.admin.AppSettings;
|
||||
import com.cameleer.server.core.admin.AppSettingsRepository;
|
||||
import com.cameleer.server.core.runtime.Environment;
|
||||
import com.cameleer.server.core.storage.StatsStore;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
@@ -15,24 +17,23 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/routes")
|
||||
@Tag(name = "Route Metrics", description = "Route performance metrics")
|
||||
@RequestMapping("/api/v1/environments/{envSlug}/routes")
|
||||
@Tag(name = "Route Metrics", description = "Route performance metrics (env-scoped)")
|
||||
public class RouteMetricsController {
|
||||
|
||||
private final JdbcTemplate jdbc;
|
||||
private final StatsStore statsStore;
|
||||
private final AppSettingsRepository appSettingsRepository;
|
||||
|
||||
public RouteMetricsController(@org.springframework.beans.factory.annotation.Qualifier("clickHouseJdbcTemplate") JdbcTemplate jdbc, StatsStore statsStore,
|
||||
public RouteMetricsController(@org.springframework.beans.factory.annotation.Qualifier("clickHouseJdbcTemplate") JdbcTemplate jdbc,
|
||||
StatsStore statsStore,
|
||||
AppSettingsRepository appSettingsRepository) {
|
||||
this.jdbc = jdbc;
|
||||
this.statsStore = statsStore;
|
||||
@@ -40,35 +41,32 @@ public class RouteMetricsController {
|
||||
}
|
||||
|
||||
@GetMapping("/metrics")
|
||||
@Operation(summary = "Get route metrics",
|
||||
description = "Returns aggregated performance metrics per route for the given time window")
|
||||
@Operation(summary = "Get route metrics for this environment",
|
||||
description = "Returns aggregated performance metrics per route for the given time window. "
|
||||
+ "Optional appId filter narrows to a single application.")
|
||||
@ApiResponse(responseCode = "200", description = "Metrics returned")
|
||||
public ResponseEntity<List<RouteMetrics>> getMetrics(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam(required = false) String from,
|
||||
@RequestParam(required = false) String to,
|
||||
@RequestParam(required = false) String appId,
|
||||
@RequestParam(required = false) String environment) {
|
||||
@RequestParam(required = false) String appId) {
|
||||
|
||||
Instant toInstant = to != null ? Instant.parse(to) : Instant.now();
|
||||
Instant fromInstant = from != null ? Instant.parse(from) : toInstant.minus(24, ChronoUnit.HOURS);
|
||||
long windowSeconds = Duration.between(fromInstant, toInstant).toSeconds();
|
||||
|
||||
// Literal SQL — ClickHouse JDBC driver wraps prepared statements in sub-queries
|
||||
// that strip AggregateFunction column types, breaking -Merge combinators
|
||||
var sql = new StringBuilder(
|
||||
"SELECT application_id, route_id, " +
|
||||
"uniqMerge(total_count) AS total, " +
|
||||
"uniqIfMerge(failed_count) AS failed, " +
|
||||
"CASE WHEN uniqMerge(total_count) > 0 THEN toFloat64(sumMerge(duration_sum)) / uniqMerge(total_count) ELSE 0 END AS avg_dur, " +
|
||||
"COALESCE(quantileMerge(0.99)(p99_duration), 0) AS p99_dur " +
|
||||
"FROM stats_1m_route WHERE bucket >= " + lit(fromInstant) + " AND bucket < " + lit(toInstant));
|
||||
"FROM stats_1m_route WHERE bucket >= " + lit(fromInstant) + " AND bucket < " + lit(toInstant) +
|
||||
" AND environment = " + lit(env.slug()));
|
||||
|
||||
if (appId != null) {
|
||||
sql.append(" AND application_id = " + lit(appId));
|
||||
}
|
||||
if (environment != null) {
|
||||
sql.append(" AND environment = " + lit(environment));
|
||||
}
|
||||
sql.append(" GROUP BY application_id, route_id ORDER BY application_id, route_id");
|
||||
|
||||
List<RouteMetrics> metrics = jdbc.query(sql.toString(), (rs, rowNum) -> {
|
||||
@@ -87,7 +85,7 @@ public class RouteMetricsController {
|
||||
avgDur, p99Dur, errorRate, tps, List.of(), -1.0);
|
||||
});
|
||||
|
||||
// Fetch sparklines (12 buckets over the time window)
|
||||
// Sparklines
|
||||
if (!metrics.isEmpty()) {
|
||||
int sparkBuckets = 12;
|
||||
long bucketSeconds = Math.max(windowSeconds / sparkBuckets, 60);
|
||||
@@ -95,15 +93,12 @@ public class RouteMetricsController {
|
||||
for (int i = 0; i < metrics.size(); i++) {
|
||||
RouteMetrics m = metrics.get(i);
|
||||
try {
|
||||
var sparkWhere = new StringBuilder(
|
||||
"FROM stats_1m_route WHERE bucket >= " + lit(fromInstant) + " AND bucket < " + lit(toInstant) +
|
||||
" AND application_id = " + lit(m.appId()) + " AND route_id = " + lit(m.routeId()));
|
||||
if (environment != null) {
|
||||
sparkWhere.append(" AND environment = " + lit(environment));
|
||||
}
|
||||
String sparkSql = "SELECT toStartOfInterval(bucket, toIntervalSecond(" + bucketSeconds + ")) AS period, " +
|
||||
"COALESCE(uniqMerge(total_count), 0) AS cnt " +
|
||||
sparkWhere + " GROUP BY period ORDER BY period";
|
||||
"FROM stats_1m_route WHERE bucket >= " + lit(fromInstant) + " AND bucket < " + lit(toInstant) +
|
||||
" AND environment = " + lit(env.slug()) +
|
||||
" AND application_id = " + lit(m.appId()) + " AND route_id = " + lit(m.routeId()) +
|
||||
" GROUP BY period ORDER BY period";
|
||||
List<Double> sparkline = jdbc.query(sparkSql,
|
||||
(rs, rowNum) -> rs.getDouble("cnt"));
|
||||
metrics.set(i, new RouteMetrics(m.routeId(), m.appId(), m.exchangeCount(),
|
||||
@@ -115,17 +110,16 @@ public class RouteMetricsController {
|
||||
}
|
||||
}
|
||||
|
||||
// Enrich with SLA compliance per route
|
||||
// SLA compliance
|
||||
if (!metrics.isEmpty()) {
|
||||
// Determine SLA threshold (per-app or default)
|
||||
String effectiveAppId = appId != null ? appId : (metrics.isEmpty() ? null : metrics.get(0).appId());
|
||||
int threshold = (effectiveAppId != null && environment != null && !environment.isBlank())
|
||||
? appSettingsRepository.findByApplicationAndEnvironment(effectiveAppId, environment)
|
||||
String effectiveAppId = appId != null ? appId : metrics.get(0).appId();
|
||||
int threshold = effectiveAppId != null
|
||||
? appSettingsRepository.findByApplicationAndEnvironment(effectiveAppId, env.slug())
|
||||
.map(AppSettings::slaThresholdMs).orElse(300)
|
||||
: 300;
|
||||
|
||||
Map<String, long[]> slaCounts = statsStore.slaCountsByRoute(fromInstant, toInstant,
|
||||
effectiveAppId, threshold, environment);
|
||||
effectiveAppId, threshold, env.slug());
|
||||
|
||||
for (int i = 0; i < metrics.size(); i++) {
|
||||
RouteMetrics m = metrics.get(i);
|
||||
@@ -142,24 +136,19 @@ public class RouteMetricsController {
|
||||
}
|
||||
|
||||
@GetMapping("/metrics/processors")
|
||||
@Operation(summary = "Get processor metrics",
|
||||
@Operation(summary = "Get processor metrics for this environment",
|
||||
description = "Returns aggregated performance metrics per processor for the given route and time window")
|
||||
@ApiResponse(responseCode = "200", description = "Metrics returned")
|
||||
public ResponseEntity<List<ProcessorMetrics>> getProcessorMetrics(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam String routeId,
|
||||
@RequestParam(required = false) String appId,
|
||||
@RequestParam(required = false) Instant from,
|
||||
@RequestParam(required = false) Instant to,
|
||||
@RequestParam(required = false) String environment) {
|
||||
@RequestParam(required = false) Instant to) {
|
||||
|
||||
Instant toInstant = to != null ? to : Instant.now();
|
||||
Instant fromInstant = from != null ? from : toInstant.minus(24, ChronoUnit.HOURS);
|
||||
|
||||
// Literal SQL for AggregatingMergeTree -Merge combinators.
|
||||
// Aliases (tc, fc) must NOT shadow column names (total_count, failed_count) —
|
||||
// ClickHouse 24.12 new analyzer resolves subsequent uniqMerge(total_count)
|
||||
// to the alias (UInt64) instead of the AggregateFunction column.
|
||||
// total_count/failed_count use uniq(execution_id) to deduplicate repeated inserts.
|
||||
var sql = new StringBuilder(
|
||||
"SELECT processor_id, processor_type, route_id, application_id, " +
|
||||
"uniqMerge(total_count) AS tc, " +
|
||||
@@ -168,14 +157,12 @@ public class RouteMetricsController {
|
||||
"quantileMerge(0.99)(p99_duration) AS p99_duration_ms " +
|
||||
"FROM stats_1m_processor_detail " +
|
||||
"WHERE bucket >= " + lit(fromInstant) + " AND bucket < " + lit(toInstant) +
|
||||
" AND environment = " + lit(env.slug()) +
|
||||
" AND route_id = " + lit(routeId));
|
||||
|
||||
if (appId != null) {
|
||||
sql.append(" AND application_id = " + lit(appId));
|
||||
}
|
||||
if (environment != null) {
|
||||
sql.append(" AND environment = " + lit(environment));
|
||||
}
|
||||
sql.append(" GROUP BY processor_id, processor_type, route_id, application_id");
|
||||
sql.append(" ORDER BY tc DESC");
|
||||
|
||||
@@ -198,14 +185,12 @@ public class RouteMetricsController {
|
||||
return ResponseEntity.ok(metrics);
|
||||
}
|
||||
|
||||
/** Format an Instant as a ClickHouse DateTime literal. */
|
||||
private static String lit(Instant instant) {
|
||||
return "'" + java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||
.withZone(java.time.ZoneOffset.UTC)
|
||||
.format(instant.truncatedTo(ChronoUnit.SECONDS)) + "'";
|
||||
}
|
||||
|
||||
/** Format a string as a ClickHouse SQL literal with backslash + quote escaping. */
|
||||
private static String lit(String value) {
|
||||
return "'" + value.replace("\\", "\\\\").replace("'", "\\'") + "'";
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package com.cameleer.server.app.controller;
|
||||
|
||||
import com.cameleer.server.app.web.EnvPath;
|
||||
import com.cameleer.server.core.admin.AppSettings;
|
||||
import com.cameleer.server.core.admin.AppSettingsRepository;
|
||||
import com.cameleer.server.core.runtime.Environment;
|
||||
import com.cameleer.server.core.search.ExecutionStats;
|
||||
import com.cameleer.server.core.search.ExecutionSummary;
|
||||
import com.cameleer.server.core.search.SearchRequest;
|
||||
@@ -25,14 +27,12 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Search endpoints for querying route executions.
|
||||
* <p>
|
||||
* GET supports basic filters via query parameters. POST accepts a full
|
||||
* {@link SearchRequest} JSON body for advanced search with all filter types.
|
||||
* Execution search and stats endpoints. Env is the path; env filter is
|
||||
* derived from the path and always applied to underlying ClickHouse queries.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/search")
|
||||
@Tag(name = "Search", description = "Transaction search endpoints")
|
||||
@RequestMapping("/api/v1/environments/{envSlug}")
|
||||
@Tag(name = "Search", description = "Transaction search and stats (env-scoped)")
|
||||
public class SearchController {
|
||||
|
||||
private final SearchService searchService;
|
||||
@@ -45,8 +45,9 @@ public class SearchController {
|
||||
}
|
||||
|
||||
@GetMapping("/executions")
|
||||
@Operation(summary = "Search executions with basic filters")
|
||||
@Operation(summary = "Search executions with basic filters (env from path)")
|
||||
public ResponseEntity<SearchResult<ExecutionSummary>> searchGet(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam(required = false) String status,
|
||||
@RequestParam(required = false) Instant timeFrom,
|
||||
@RequestParam(required = false) Instant timeTo,
|
||||
@@ -56,7 +57,6 @@ public class SearchController {
|
||||
@RequestParam(name = "agentId", required = false) String instanceId,
|
||||
@RequestParam(required = false) String processorType,
|
||||
@RequestParam(required = false) String application,
|
||||
@RequestParam(required = false) String environment,
|
||||
@RequestParam(defaultValue = "0") int offset,
|
||||
@RequestParam(defaultValue = "50") int limit,
|
||||
@RequestParam(required = false) String sortField,
|
||||
@@ -71,116 +71,116 @@ public class SearchController {
|
||||
application, null,
|
||||
offset, limit,
|
||||
sortField, sortDir,
|
||||
environment
|
||||
env.slug()
|
||||
);
|
||||
|
||||
return ResponseEntity.ok(searchService.search(request));
|
||||
}
|
||||
|
||||
@PostMapping("/executions")
|
||||
@Operation(summary = "Advanced search with all filters")
|
||||
@PostMapping("/executions/search")
|
||||
@Operation(summary = "Advanced search with all filters",
|
||||
description = "Env from the path overrides any environment field in the body.")
|
||||
public ResponseEntity<SearchResult<ExecutionSummary>> searchPost(
|
||||
@EnvPath Environment env,
|
||||
@RequestBody SearchRequest request) {
|
||||
return ResponseEntity.ok(searchService.search(request));
|
||||
SearchRequest scoped = request.withEnvironment(env.slug());
|
||||
return ResponseEntity.ok(searchService.search(scoped));
|
||||
}
|
||||
|
||||
@GetMapping("/stats")
|
||||
@Operation(summary = "Aggregate execution stats (P99 latency, active count, SLA compliance)")
|
||||
public ResponseEntity<ExecutionStats> stats(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam Instant from,
|
||||
@RequestParam(required = false) Instant to,
|
||||
@RequestParam(required = false) String routeId,
|
||||
@RequestParam(required = false) String application,
|
||||
@RequestParam(required = false) String environment) {
|
||||
@RequestParam(required = false) String application) {
|
||||
Instant end = to != null ? to : Instant.now();
|
||||
ExecutionStats stats;
|
||||
if (routeId == null && application == null) {
|
||||
stats = searchService.stats(from, end, environment);
|
||||
stats = searchService.stats(from, end, env.slug());
|
||||
} else if (routeId == null) {
|
||||
stats = searchService.statsForApp(from, end, application, environment);
|
||||
stats = searchService.statsForApp(from, end, application, env.slug());
|
||||
} else {
|
||||
stats = searchService.statsForRoute(from, end, routeId, application, environment);
|
||||
stats = searchService.statsForRoute(from, end, routeId, application, env.slug());
|
||||
}
|
||||
|
||||
// Enrich with SLA compliance (per-env threshold when both app and env are specified)
|
||||
int threshold = (application != null && !application.isBlank()
|
||||
&& environment != null && !environment.isBlank())
|
||||
? appSettingsRepository.findByApplicationAndEnvironment(application, environment)
|
||||
int threshold = application != null && !application.isBlank()
|
||||
? appSettingsRepository.findByApplicationAndEnvironment(application, env.slug())
|
||||
.map(AppSettings::slaThresholdMs).orElse(300)
|
||||
: 300;
|
||||
double sla = searchService.slaCompliance(from, end, threshold, application, routeId, environment);
|
||||
double sla = searchService.slaCompliance(from, end, threshold, application, routeId, env.slug());
|
||||
return ResponseEntity.ok(stats.withSlaCompliance(sla));
|
||||
}
|
||||
|
||||
@GetMapping("/stats/timeseries")
|
||||
@Operation(summary = "Bucketed time-series stats over a time window")
|
||||
public ResponseEntity<StatsTimeseries> timeseries(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam Instant from,
|
||||
@RequestParam(required = false) Instant to,
|
||||
@RequestParam(defaultValue = "24") int buckets,
|
||||
@RequestParam(required = false) String routeId,
|
||||
@RequestParam(required = false) String application,
|
||||
@RequestParam(required = false) String environment) {
|
||||
@RequestParam(required = false) String application) {
|
||||
Instant end = to != null ? to : Instant.now();
|
||||
if (routeId == null && application == null) {
|
||||
return ResponseEntity.ok(searchService.timeseries(from, end, buckets, environment));
|
||||
return ResponseEntity.ok(searchService.timeseries(from, end, buckets, env.slug()));
|
||||
}
|
||||
if (routeId == null) {
|
||||
return ResponseEntity.ok(searchService.timeseriesForApp(from, end, buckets, application, environment));
|
||||
return ResponseEntity.ok(searchService.timeseriesForApp(from, end, buckets, application, env.slug()));
|
||||
}
|
||||
return ResponseEntity.ok(searchService.timeseriesForRoute(from, end, buckets, routeId, application, environment));
|
||||
return ResponseEntity.ok(searchService.timeseriesForRoute(from, end, buckets, routeId, application, env.slug()));
|
||||
}
|
||||
|
||||
@GetMapping("/stats/timeseries/by-app")
|
||||
@Operation(summary = "Timeseries grouped by application")
|
||||
public ResponseEntity<Map<String, StatsTimeseries>> timeseriesByApp(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam Instant from,
|
||||
@RequestParam(required = false) Instant to,
|
||||
@RequestParam(defaultValue = "24") int buckets,
|
||||
@RequestParam(required = false) String environment) {
|
||||
@RequestParam(defaultValue = "24") int buckets) {
|
||||
Instant end = to != null ? to : Instant.now();
|
||||
return ResponseEntity.ok(searchService.timeseriesGroupedByApp(from, end, buckets, environment));
|
||||
return ResponseEntity.ok(searchService.timeseriesGroupedByApp(from, end, buckets, env.slug()));
|
||||
}
|
||||
|
||||
@GetMapping("/stats/timeseries/by-route")
|
||||
@Operation(summary = "Timeseries grouped by route for an application")
|
||||
public ResponseEntity<Map<String, StatsTimeseries>> timeseriesByRoute(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam Instant from,
|
||||
@RequestParam(required = false) Instant to,
|
||||
@RequestParam(defaultValue = "24") int buckets,
|
||||
@RequestParam String application,
|
||||
@RequestParam(required = false) String environment) {
|
||||
@RequestParam String application) {
|
||||
Instant end = to != null ? to : Instant.now();
|
||||
return ResponseEntity.ok(searchService.timeseriesGroupedByRoute(from, end, buckets, application, environment));
|
||||
return ResponseEntity.ok(searchService.timeseriesGroupedByRoute(from, end, buckets, application, env.slug()));
|
||||
}
|
||||
|
||||
@GetMapping("/stats/punchcard")
|
||||
@Operation(summary = "Transaction punchcard: weekday x hour grid (rolling 7 days)")
|
||||
public ResponseEntity<List<StatsStore.PunchcardCell>> punchcard(
|
||||
@RequestParam(required = false) String application,
|
||||
@RequestParam(required = false) String environment) {
|
||||
@EnvPath Environment env,
|
||||
@RequestParam(required = false) String application) {
|
||||
Instant to = Instant.now();
|
||||
Instant from = to.minus(java.time.Duration.ofDays(7));
|
||||
return ResponseEntity.ok(searchService.punchcard(from, to, application, environment));
|
||||
return ResponseEntity.ok(searchService.punchcard(from, to, application, env.slug()));
|
||||
}
|
||||
|
||||
@GetMapping("/attributes/keys")
|
||||
@Operation(summary = "Distinct attribute key names for the given environment",
|
||||
description = "Scoped to an environment to prevent cross-env attribute leakage in UI completions")
|
||||
public ResponseEntity<List<String>> attributeKeys(@RequestParam String environment) {
|
||||
return ResponseEntity.ok(searchService.distinctAttributeKeys(environment));
|
||||
@Operation(summary = "Distinct attribute key names for this environment")
|
||||
public ResponseEntity<List<String>> attributeKeys(@EnvPath Environment env) {
|
||||
return ResponseEntity.ok(searchService.distinctAttributeKeys(env.slug()));
|
||||
}
|
||||
|
||||
@GetMapping("/errors/top")
|
||||
@Operation(summary = "Top N errors with velocity trend")
|
||||
public ResponseEntity<List<TopError>> topErrors(
|
||||
@EnvPath Environment env,
|
||||
@RequestParam Instant from,
|
||||
@RequestParam(required = false) Instant to,
|
||||
@RequestParam(required = false) String application,
|
||||
@RequestParam(required = false) String routeId,
|
||||
@RequestParam(required = false) String environment,
|
||||
@RequestParam(defaultValue = "5") int limit) {
|
||||
Instant end = to != null ? to : Instant.now();
|
||||
return ResponseEntity.ok(searchService.topErrors(from, end, application, routeId, limit, environment));
|
||||
return ResponseEntity.ok(searchService.topErrors(from, end, application, routeId, limit, env.slug()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user