This is an automated email from the ASF dual-hosted git repository.
lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git
The following commit(s) were added to refs/heads/master by this push:
new dab7a789b fix(web): import an alert-rule file that carries a legacy
duration (#6855)
dab7a789b is described below
commit dab7a789bb57a4d4aad6d48d593f055283e5c5b5
Author: bai mengxuan <[email protected]>
AuthorDate: Fri Nov 27 12:24:55 2026 +0800
fix(web): import an alert-rule file that carries a legacy duration (#6855)
---
web/src/pages/ops/__tests__/AlertsPage.test.tsx | 39 +++++++++++++++++++++++++
web/src/pages/ops/alerts.tsx | 15 ++++++++--
2 files changed, 52 insertions(+), 2 deletions(-)
diff --git a/web/src/pages/ops/__tests__/AlertsPage.test.tsx
b/web/src/pages/ops/__tests__/AlertsPage.test.tsx
index e9db83941..b8b499b24 100644
--- a/web/src/pages/ops/__tests__/AlertsPage.test.tsx
+++ b/web/src/pages/ops/__tests__/AlertsPage.test.tsx
@@ -1233,6 +1233,45 @@ describe('AlertsPage', () => {
expect(screen.queryByText('导入失败,请选择当前页面导出的规则文件')).not.toBeInTheDocument();
});
+ it('maps the legacy duration of an imported rule document to its canonical
value', async () => {
+ vi.mocked(importAlertRulesTransfer).mockResolvedValue([]);
+ const user = userEvent.setup();
+ const { container } = renderPage();
+ await screen.findByText('Broker disk usage');
+ const input =
container.querySelector<HTMLInputElement>('input[type="file"]');
+ if (!input) throw new Error('Alert rule import input not found');
+ const file = new File(
+ [
+ JSON.stringify({
+ version: 1,
+ domain: 'CLUSTER',
+ rules: [
+ {
+ name: 'Legacy lag',
+ metric: 'broker.disk.usage',
+ operator: '>',
+ threshold: 85,
+ duration: '5分钟',
+ channels: ['dingtalk'],
+ enabled: true,
+ },
+ ],
+ }),
+ ],
+ 'cluster-alert-rules.json',
+ { type: 'application/json' },
+ );
+
+ await user.upload(input, file);
+
+ // A document written before the canonical options existed - the console
maps those labels when
+ // it opens such a rule for editing, and a rule exported from a store that
still holds one keeps
+ // the label in the file. The server re-validates the whole document, so
an upload that kept the
+ // label is rejected as a 400 and nothing at all is imported.
+ await waitFor(() =>
expect(importAlertRulesTransfer).toHaveBeenCalledTimes(1));
+
expect(vi.mocked(importAlertRulesTransfer).mock.calls[0][0].rules[0].duration).toBe('5m');
+ });
+
it('disables other alert rule mutations while a bulk action is running',
async () => {
let resolveToggle:
| ((result: {
diff --git a/web/src/pages/ops/alerts.tsx b/web/src/pages/ops/alerts.tsx
index 0f8572b2f..064be4ef5 100644
--- a/web/src/pages/ops/alerts.tsx
+++ b/web/src/pages/ops/alerts.tsx
@@ -525,10 +525,21 @@ const AlertsPageContent = ({ domain = 'CLUSTER', embedded
= false }: AlertsPageP
if (!file) return;
setTransferringRules(true);
try {
- const transfer = JSON.parse(await file.text()) as AlertRuleTransfer;
- if (transfer.version !== 1 || transfer.domain !== domain ||
!Array.isArray(transfer.rules)) {
+ const parsed = JSON.parse(await file.text()) as AlertRuleTransfer;
+ if (parsed.version !== 1 || parsed.domain !== domain ||
!Array.isArray(parsed.rules)) {
throw new Error('invalid transfer document');
}
+ // A rule exported from a store that still holds a pre-canonical
duration keeps that label in
+ // the file, and the server validates the uploaded document as a whole:
one legacy label turns
+ // the entire import into a 400. The editor maps the same labels when it
opens such a rule, so
+ // the upload maps them here too instead of refusing the file the
console itself wrote.
+ const transfer: AlertRuleTransfer = {
+ ...parsed,
+ rules: parsed.rules.map((rule) => ({
+ ...rule,
+ duration: normalizeDuration(rule.duration),
+ })),
+ };
const imported = await importAlertRulesTransfer(transfer, domain);
setPage(1);
refreshRules();