imbajin commented on code in PR #3008:
URL: https://github.com/apache/hugegraph/pull/3008#discussion_r3187075735


##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/GraphsAPI.java:
##########
@@ -120,6 +128,102 @@ public Object list(@Context GraphManager manager,
         return ImmutableMap.of("graphs", filterGraphs);
     }
 
+    @GET
+    @Timed
+    @Path("profile")
+    @Produces(APPLICATION_JSON_WITH_CHARSET)
+    @RolesAllowed({"space_member", "$dynamic"})
+    public Object listProfile(@Context GraphManager manager,
+                              @Parameter(description = "The graph space name")
+                              @PathParam("graphspace") String graphSpace,
+                              @Parameter(description = "Filter graphs by name 
or nickname prefix")
+                              @QueryParam("prefix") String prefix,
+                              @Context SecurityContext sc) {
+        LOG.debug("List graph profiles in graph space {}", graphSpace);
+        if (null == manager.graphSpace(graphSpace)) {
+            throw new HugeException("Graphspace not exist!");
+        }
+        GraphSpace gs = manager.graphSpace(graphSpace);
+        // graphSpace.nickname() may be null in non-PD mode (GraphManager 
returns
+        // a placeholder GraphSpace without a nickname set)
+        String gsNickname = gs.nickname() != null ? gs.nickname() : graphSpace;
+
+        AuthManager authManager = manager.authManager();
+        String user = HugeGraphAuthProxy.username();
+        // Default graph concept relies on PD meta storage; in non-PD 
standalone
+        // mode there is no persistent store for this, so we gracefully 
degrade.
+        Map<String, Date> defaultGraphs;
+        if (manager.isPDEnabled()) {
+            defaultGraphs = authManager.getDefaultGraph(graphSpace, user);
+        } else {
+            defaultGraphs = new HashMap<>();
+        }
+
+        Set<String> graphs = manager.graphs(graphSpace);
+        List<Map<String, Object>> profiles = new ArrayList<>();
+        List<Map<String, Object>> defaultProfiles = new ArrayList<>();
+        for (String graph : graphs) {
+            String role = RequiredPerm.roleFor(graphSpace, graph,
+                                               HugePermission.READ);
+            if (!sc.isUserInRole(role)) {
+                continue;
+            }
+            try {
+                HugeGraph hg = graph(manager, graphSpace, graph);
+                HugeConfig config = (HugeConfig) hg.configuration();
+                String configResp = ConfigUtil.writeConfigToString(config);
+                Map<String, Object> profile =
+                        JsonUtil.fromJson(configResp, Map.class);
+                profile.put("name", graph);
+                profile.put("nickname", hg.nickname());
+                if (!isPrefix(profile, prefix)) {
+                    continue;
+                }
+                profile.put("graphspace_nickname", gsNickname);
+                
+                boolean isDefault = defaultGraphs.containsKey(graph);
+                profile.put("default", isDefault);
+                if (isDefault) {
+                    Date defaultUpdateTime = defaultGraphs.get(graph);
+                    if (defaultUpdateTime != null) {
+                        LocalDateTime ldt = defaultUpdateTime.toInstant()
+                                
.atZone(ZoneId.systemDefault()).toLocalDateTime();
+                        profile.put("default_update_time",
+                                    DATE_FORMATTER.format(ldt));
+                    }
+                }
+                
+                Date createTime = hg.createTime();
+                if (createTime != null) {
+                    LocalDateTime ldt = createTime.toInstant()
+                            .atZone(ZoneId.systemDefault()).toLocalDateTime();
+                    profile.put("create_time", DATE_FORMATTER.format(ldt));
+                }
+                
+                if (isDefault) {
+                    defaultProfiles.add(profile);
+                } else {
+                    profiles.add(profile);
+                }
+            } catch (ForbiddenException ignored) {
+                // ignore graphs the current user has no access to
+            }
+        }
+        defaultProfiles.addAll(profiles);
+        return defaultProfiles;
+    }
+
+    private static boolean isPrefix(Map<String, Object> profile, String 
prefix) {
+        if (StringUtils.isEmpty(prefix)) {
+            return true;
+        }
+        // graph name or nickname is not empty
+        String name = profile.get("name").toString();
+        Object nicknameObj = profile.get("nickname");
+        String nickname = nicknameObj != null ? nicknameObj.toString() : "";
+        return name.startsWith(prefix) || nickname.startsWith(prefix);
+    }

Review Comment:
   ‼️ **Blocker: duplicate `isPrefix()` now breaks compilation**
   
   `API` already defines a `protected static isPrefix(Map<String, Object>, 
String)`, so this `private static` method in `GraphsAPI` attempts to hide it 
with weaker access privileges and causes `hugegraph-api` compilation to fail. 
Since both implementations are now equivalent, please remove this duplicate 
method and reuse the inherited helper.
   
   ```suggestion
   ```
   



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to