Skip to content
Closed
Show file tree
Hide file tree
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
@@ -0,0 +1,88 @@
/*
* 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 com.rocketmq.studio.ops.ai.tool;

import com.rocketmq.studio.instance.group.ConsumerGroupVO;
import com.rocketmq.studio.instance.topic.MetadataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class ConsumerGroupListToolHandler implements ToolHandler {

private static final String NAME = "rmq.group.list";

private final MetadataService metadataService;

@Override
public String name() {
return NAME;
}

@Override
public Object execute(Map<String, Object> input) {
String clusterId = (String) input.get("cluster");
String search = (String) input.get("search");
return metadataService.listConsumerGroups(clusterId, search).stream()
.map(ConsumerGroupListToolHandler::safeProjection)
.toList();
}

private static Map<String, Object> safeProjection(ConsumerGroupVO group) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("name", require(group.getName(), "name"));
result.put("namespace", blankIfNull(group.getNamespace()));
result.put("clusterId", blankIfNull(group.getClusterId()));
result.put("subscriptionMode", requiredEnumName(
group.getSubscriptionMode(), "subscriptionMode", group.getName()));
result.put("consumeType", requiredEnumName(
group.getConsumeType(), "consumeType", group.getName()));
result.put("onlineInstances", group.getOnlineInstances());
result.put("totalLag", group.getTotalLag());
result.put("subscribedTopics", copyList(group.getSubscribedTopics()));
result.put("retryMaxTimes", group.getRetryMaxTimes());
return result;
}

private static String requiredEnumName(Enum<?> value, String field, String groupName) {
if (value == null) {
throw new IllegalStateException("Consumer group " + field
+ " is unavailable: " + groupName);
}
return value.name();
}

private static String require(String value, String field) {
if (value == null || value.isBlank()) {
throw new IllegalStateException("Consumer group " + field + " is unavailable");
}
return value;
}

private static List<String> copyList(List<String> value) {
return value == null ? List.of() : List.copyOf(value);
}

private static String blankIfNull(String value) {
return value == null ? "" : value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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 com.rocketmq.studio.ops.ai.tool;

import com.rocketmq.studio.instance.topic.MetadataService;
import com.rocketmq.studio.instance.topic.TopicVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class TopicListToolHandler implements ToolHandler {

private static final String NAME = "rmq.topic.list";

private final MetadataService metadataService;

@Override
public String name() {
return NAME;
}

@Override
public Object execute(Map<String, Object> input) {
String clusterId = (String) input.get("cluster");
String type = (String) input.get("type");
String search = (String) input.get("search");
return metadataService.listTopics(clusterId, type, search).stream()
.map(TopicListToolHandler::safeProjection)
.toList();
}

private static Map<String, Object> safeProjection(TopicVO topic) {
Map<String, Object> result = new LinkedHashMap<>();
result.put("name", require(topic.getName(), "name"));
result.put("namespace", blankIfNull(topic.getNamespace()));
result.put("clusterId", blankIfNull(topic.getClusterId()));
result.put("type", requiredEnumName(topic.getType(), "type", topic.getName()));
result.put("writeQueues", topic.getWriteQueues());
result.put("readQueues", topic.getReadQueues());
result.put("perm", requiredEnumName(topic.getPerm(), "perm", topic.getName()));
result.put("messageCount", topic.getMessageCount());
result.put("tps", topic.getTps());
result.put("consumerGroupCount", topic.getConsumerGroupCount());
return result;
}

private static String requiredEnumName(Enum<?> value, String field, String topicName) {
if (value == null) {
throw new IllegalStateException("Topic " + field + " is unavailable: " + topicName);
}
return value.name();
}

private static String require(String value, String field) {
if (value == null || value.isBlank()) {
throw new IllegalStateException("Topic " + field + " is unavailable");
}
return value;
}

private static String blankIfNull(String value) {
return value == null ? "" : value;
}
}
122 changes: 122 additions & 0 deletions server/src/main/resources/tool-catalog/rmq-tools.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,125 @@ tools:
type: string
viewHint: object
deprecated: false
- name: rmq.topic.list
cli:
resource: topic
verb: list
description: List RocketMQ topics in one Studio cluster.
riskLevel: L1
permission: topic:read
requiredCapabilities:
- REMOTING
inputSchema:
type: object
required:
- cluster
additionalProperties: false
properties:
cluster:
type: string
minLength: 1
type:
type: string
minLength: 1
search:
type: string
minLength: 1
outputSchema:
type: array
items:
type: object
required:
- name
- namespace
- clusterId
- type
- writeQueues
- readQueues
- perm
- messageCount
- tps
- consumerGroupCount
additionalProperties: false
properties:
name:
type: string
namespace:
type: string
clusterId:
type: string
type:
type: string
writeQueues:
type: integer
readQueues:
type: integer
perm:
type: string
messageCount:
type: integer
tps:
type: number
consumerGroupCount:
type: integer
viewHint: table
deprecated: false
- name: rmq.group.list
cli:
resource: group
verb: list
description: List RocketMQ consumer groups in one Studio cluster.
riskLevel: L1
permission: group:read
requiredCapabilities:
- REMOTING
inputSchema:
type: object
required:
- cluster
additionalProperties: false
properties:
cluster:
type: string
minLength: 1
search:
type: string
minLength: 1
outputSchema:
type: array
items:
type: object
required:
- name
- namespace
- clusterId
- subscriptionMode
- consumeType
- onlineInstances
- totalLag
- subscribedTopics
- retryMaxTimes
additionalProperties: false
properties:
name:
type: string
namespace:
type: string
clusterId:
type: string
subscriptionMode:
type: string
consumeType:
type: string
onlineInstances:
type: integer
totalLag:
type: integer
subscribedTopics:
type: array
items:
type: string
retryMaxTimes:
type: integer
viewHint: table
deprecated: false
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ void loadsAndIndexesTheCanonicalCatalog() {
assertThat(catalog.getMinimumClientVersion()).isEqualTo("1.0.0");
assertThat(catalog.getDigest()).matches("[0-9a-f]{64}");
assertThat(catalog.list()).extracting(ToolDefinition::getName)
.containsExactly("rmq.cluster.list", "rmq.capabilities");
.containsExactly(
"rmq.cluster.list",
"rmq.capabilities",
"rmq.topic.list",
"rmq.group.list");
assertThat(catalog.find("rmq.cluster.list")).isPresent();
assertThat(catalog.find("rmq.unknown")).isEmpty();
}
Expand Down
Loading
Loading