/*
* 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.
*/
const appConfig = {
apiBaseUrl: 'http://localhost:8082'
};
let _redirectHandler = null;
const remoteApi = {
setRedirectHandler: (handler) => {
_redirectHandler = handler;
},
buildUrl: (endpoint) => {
if (endpoint.charAt(0) === '/') {
endpoint = endpoint.substring(1);
}
return `${appConfig.apiBaseUrl}/${endpoint}`;
},
async getCsrfToken() {
const csrfToken = this.getCookie();
if (csrfToken) {
return csrfToken;
}
const response = await fetch(remoteApi.buildUrl("/rocketmq-dashboard/csrf-token"), {
method: 'GET',
credentials: 'include'
});
const newCsrfToken = this.getCookie();
if (!newCsrfToken) {
console.error("Failed to get CSRF Token");
throw new Error("CSRF Token not available");
}
return newCsrfToken;
},
getCookie() {
return document.cookie.replace(/(?:(?:^|.*;\s*)XSRF-TOKEN\s*\=\s*([^;]*).*$)|^.*$/, '$1')
},
_fetch: async (url, options = {}) => {
const headers = {
...options.headers,
'Content-Type': 'application/json',
};
const csrfToken = await remoteApi.getCsrfToken();
console.log(csrfToken)
if (!csrfToken) {
console.warn('CSRF Token not found');
}else{
headers["X-XSRF-TOKEN"] = csrfToken;
}
console.log(csrfToken)
try {
const response = await fetch(url, {
...options,
headers,
credentials: 'include',
});
if (response.redirected) {
if (_redirectHandler) {
_redirectHandler();
}
return {__isRedirectHandled: true};
}
if(response.status == 403){
window.localStorage.removeItem("csrfToken");
console.log(111)
await remoteApi.getCsrfToken()
}
return response;
} catch (error) {
console.error('fetch error:', error);
window.localStorage.removeItem("csrfToken");
console.log(111)
await remoteApi.getCsrfToken()
}
},
queryTopic: async (skipSysProcess) => {
try {
const params = new URLSearchParams();
if (skipSysProcess) {
params.append('skipSysProcess', 'true');
}
const response = await remoteApi._fetch(remoteApi.buildUrl(`/topic/list.query?${params.toString()}`));
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching topic list:", error);
}
},
listUsers: async (brokerName, clusterName) => {
const params = new URLSearchParams();
if (brokerName) params.append('brokerName', brokerName);
if (clusterName) params.append('clusterName', clusterName);
const response = await remoteApi._fetch(remoteApi.buildUrl(`/acl/users.query?${params.toString()}`));
return await response.json();
},
createUser: async (brokerName, userInfo, clusterName) => {
const response = await remoteApi._fetch(remoteApi.buildUrl('/acl/createUser.do'), {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({brokerName, userInfo, clusterName})
});
return await response.json();
},
updateUser: async (brokerName, userInfo, clusterName) => {
const response = await remoteApi._fetch(remoteApi.buildUrl('/acl/updateUser.do'), {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({brokerName, userInfo, clusterName})
});
return await response.json();
},
deleteUser: async (brokerName, username, clusterName) => {
const params = new URLSearchParams();
if (brokerName) params.append('brokerName', brokerName);
if (clusterName) params.append('clusterName', clusterName);
params.append('username', username);
const response = await remoteApi._fetch(remoteApi.buildUrl(`/acl/deleteUser.do?${params.toString()}`), {
method: 'DELETE'
});
return await response.json();
},
listAcls: async (brokerName, searchParam, clusterName) => {
const params = new URLSearchParams();
if (brokerName) params.append('brokerName', brokerName);
if (clusterName) params.append('clusterName', clusterName);
if (searchParam) params.append('searchParam', searchParam);
if (searchParam != null) console.log(1111)
const response = await remoteApi._fetch(remoteApi.buildUrl(`/acl/acls.query?${params.toString()}`));
return await response.json();
},
createAcl: async (brokerName, subject, policies, clusterName) => {
const response = await remoteApi._fetch(remoteApi.buildUrl('/acl/createAcl.do'), {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({brokerName, subject, policies, clusterName})
});
return await response.json();
},
updateAcl: async (brokerName, subject, policies, clusterName) => {
const response = await remoteApi._fetch(remoteApi.buildUrl('/acl/updateAcl.do'), {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({brokerName, subject, policies, clusterName})
});
return await response.json();
},
deleteAcl: async (brokerName, subject, resource, clusterName) => {
const params = new URLSearchParams();
if (brokerName) params.append('brokerAddress', brokerName);
params.append('subject', subject);
if (resource) params.append('resource', resource);
if (clusterName) params.append('clusterName', clusterName);
const response = await remoteApi._fetch(remoteApi.buildUrl(`/acl/deleteAcl.do?${params.toString()}`), {
method: 'DELETE'
});
return await response.json();
},
queryMessageByMessageId: async (msgId, topic, callback) => {
try {
const params = new URLSearchParams();
params.append('msgId', msgId);
params.append('topic', topic);
const response = await remoteApi._fetch(remoteApi.buildUrl(`/messageTrace/viewMessage.query?${params.toString()}`));
const data = await response.json();
return data
} catch (error) {
console.error("Error querying message by ID:", error);
callback({status: 1, errMsg: "Failed to query message by ID"});
}
},
queryMessageTraceByMessageId: async (msgId, traceTopic, callback) => {
try {
const params = new URLSearchParams();
params.append('msgId', msgId);
params.append('traceTopic', traceTopic);
const response = await remoteApi._fetch(remoteApi.buildUrl(`/messageTrace/viewMessageTraceGraph.query?${params.toString()}`));
const data = await response.json();
return data;
} catch (error) {
console.error("Error querying message trace:", error);
}
},
queryDlqMessageByConsumerGroup: async (consumerGroup, beginTime, endTime, pageNum, pageSize, taskId) => {
try {
const response = await remoteApi._fetch(remoteApi.buildUrl("/dlqMessage/queryDlqMessageByConsumerGroup.query"), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
topic: `%DLQ%${consumerGroup}`,
begin: beginTime,
end: endTime,
pageNum: pageNum,
pageSize: pageSize,
taskId: taskId,
}),
});
const data = await response.json();
return data;
} catch (error) {
console.error("Error querying DLQ messages by consumer group:", error);
return {status: 1, errMsg: "Failed to query DLQ messages by consumer group"};
}
},
resendDlqMessage: async (msgId, consumerGroup, topic) => {
try {
const response = await remoteApi._fetch(remoteApi.buildUrl("/message/consumeMessageDirectly.do"), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
params: {
msgId: msgId,
consumerGroup: consumerGroup,
topic: topic
},
});
const data = await response.json();
return data;
} catch (error) {
console.error("Error resending DLQ message:", error);
return {status: 1, errMsg: "Failed to resend DLQ message"};
}
},
exportDlqMessage: async (msgId, consumerGroup) => {
try {
const response = await remoteApi._fetch(remoteApi.buildUrl(`/dlqMessage/exportDlqMessage.do?msgId=${msgId}&consumerGroup=${consumerGroup}`));
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
const newWindow = window.open('', '_blank');
if (!newWindow) {
return {status: 1, errMsg: "Failed to open new window. Please allow pop-ups for this site."};
}
newWindow.document.write('
DLQ 导出内容');
newWindow.document.write('DLQ 导出 JSON 内容
');
newWindow.document.write('' + JSON.stringify(data, null, 2) + '
');
newWindow.document.write('');
newWindow.document.close();
return {status: 0, msg: "导出请求成功,内容已在新页面显示"};
} catch (error) {
console.error("Error exporting DLQ message:", error);
return {status: 1, errMsg: "Failed to export DLQ message: " + error.message};
}
},
batchResendDlqMessage: async (messages) => {
try {
const response = await remoteApi._fetch(remoteApi.buildUrl("/dlqMessage/batchResendDlqMessage.do"), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(messages),
});
const data = await response.json();
return data;
} catch (error) {
console.error("Error batch resending DLQ messages:", error);
return {status: 1, errMsg: "Failed to batch resend DLQ messages"};
}
},
/**
* Queries messages by topic with pagination.
* @param {string} topic The topic to query.
* @param {number} begin Timestamp in milliseconds for the start time.
* @param {number} end Timestamp in milliseconds for the end time.
* @param {number} pageNum The current page number (1-based).
* @param {number} pageSize The number of items per page.
* @param {string} taskId Optional task ID for continuous queries.
* @returns {Promise