gnodet-bot commented on code in PR #28185:
URL: https://github.com/apache/camel/pull/28185#discussion_r4326337968


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/ObservedBodies.java:
##########
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.commands.ai;
+
+import java.nio.file.Path;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.camel.dsl.jbang.core.common.ActionClient;
+import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.RuntimeHelper;
+import org.apache.camel.dsl.yaml.validator.ProjectFacts;
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
+
+/**
+ * What a running integration of a route file saw at the endpoints of its 
routes (CAMEL-25750): the body each consumer
+ * gave, and the body each producer returned, as the step after it was reached 
with while the message history was on
+ * (the dev profile turns it on). The validator fills the gaps of its walk 
with them: what the walk alone does not know,
+ * such as the stream cache of an HTTP request, or the List a sql call returns.
+ * <p/>
+ * The facts are per endpoint, not per line: the validator checks the next 
version of the file while the integration
+ * runs the one before, and what an endpoint gives does not move with the 
lines.
+ */
+final class ObservedBodies {
+
+    /** How long to wait for the integration to answer: validation does not 
wait for long. */
+    private static final long TIMEOUT = 3000;
+
+    /** How long the facts of a file are kept: a write validates the file 
before and after the change. */
+    private static final long KEEP = 2000;
+
+    private static final Map<Path, Facts> CACHE = new ConcurrentHashMap<>();
+
+    private record Facts(long time, Map<String, ProjectFacts.Observed> 
observed) {
+    }
+
+    private ObservedBodies() {
+    }
+
+    /**
+     * What a running integration of the file saw; nothing when no integration 
runs routes of the file, or it does not
+     * answer in time.
+     */
+    static Map<String, ProjectFacts.Observed> read(String fileName, Path 
directory) {
+        if (fileName == null || fileName.isBlank()) {
+            return Map.of();
+        }
+        Path file = (directory != null ? directory.resolve(fileName) : 
Path.of(fileName)).toAbsolutePath().normalize();
+        Facts cached = CACHE.get(file);
+        if (cached != null && System.currentTimeMillis() - cached.time() < 
KEEP) {
+            return cached.observed();
+        }
+        Map<String, ProjectFacts.Observed> answer = Map.of();
+        // the processes are only looked for when an integration runs at all: 
most validations have none
+        String[] statusFiles = 
CommandLineHelper.getCamelDir().toFile().list((d, n) -> 
n.endsWith("-status.json"));
+        if (statusFiles == null || statusFiles.length == 0) {
+            return answer;

Review Comment:
   💡 **Cache miss on empty result:** when no integration is running (the common 
case), this early return bypasses the `CACHE.put` below (line 85). Every 
subsequent call will re-stat the Camel directory via 
`getCamelDir().toFile().list(...)` — once per file per validation, in a tight 
write-validate-reload loop. Consider caching the empty result too:
   
   ```suggestion
           if (statusFiles == null || statusFiles.length == 0) {
               CACHE.put(file, new Facts(System.currentTimeMillis(), answer));
               return answer;
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to