Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/lib/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@
"Migrate agents from file repository to MongoDB": "Migrate agents from file repository to MongoDB",
"Setting": "Setting",
"Start Migration": "Start Migration",
"Select agents to migrate": "Select agents to migrate",
"Search agents": "Search agents",
"Select the agents to copy to the MongoDB datastore. Only the selected agents are migrated.": "Select the agents to copy to the MongoDB datastore. Only the selected agents are migrated.",
"You will migrate {n} selected agent(s) to mongoDb.": "You will migrate {n} selected agent(s) to mongoDb.",
"Failed to load agents.": "Failed to load agents.",
"Failed to migrate agents.": "Failed to migrate agents.",
"No agent files found, or the default database is the file repository.": "No agent files found, or the default database is the file repository.",
"Plugin": "Plugin",
"My Profile": "My Profile",
"Detail": "Detail",
Expand Down
7 changes: 7 additions & 0 deletions src/lib/langs/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@
"Migrate agents from file repository to MongoDB": "将代理从文件存储库迁移到MongoDB",
"Setting": "设置",
"Start Migration": "开始迁移",
"Select agents to migrate": "选择要迁移的代理",
"Search agents": "搜索代理",
"Select the agents to copy to the MongoDB datastore. Only the selected agents are migrated.": "选择要复制到MongoDB的代理,仅迁移所选代理。",
"You will migrate {n} selected agent(s) to mongoDb.": "将把所选的 {n} 个代理迁移到MongoDB。",
"Failed to load agents.": "加载代理列表失败。",
"Failed to migrate agents.": "迁移代理失败。",
"No agent files found, or the default database is the file repository.": "未找到代理文件,或默认数据库是文件存储库。",
"Plugin": "插件",
"My Profile": "我的资料",
"Detail": "细节",
Expand Down
22 changes: 18 additions & 4 deletions src/lib/services/agent-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,26 @@ export async function deleteAgent(agentId, options = null) {
}

/**
* Refresh agent data
* Migrate the given agents from the file repository to the database
* @param {string[]} agentIds
* @returns {Promise<string>}
*/
export async function refreshAgents() {
const url = endpoints.agentRefreshUrl;
const response = await axios.post(url, {});
export async function refreshAgents(agentIds) {
if (!agentIds.length) {
// An empty list would make the server migrate every agent.
throw new Error('Select at least one agent to migrate.');
}

const response = await axios.post(endpoints.agentRefreshUrl, { agent_ids: agentIds });
return response.data;
}

/**
* Get agents defined in the file repository (candidates to migrate)
* @returns {Promise<import('$commonTypes').IdName[]>}
*/
export async function getFileAgents() {
const response = await axios.get(endpoints.fileAgentsUrl);
return response.data;
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/services/api-endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const endpoints = {
agentOptionsUrl: `${host}/agent/options`,
agentDetailUrl: `${host}/agent/{id}`,
agentRefreshUrl: `${host}/refresh-agents`,
fileAgentsUrl: `${host}/file-agents`,
agentCreateUrl: `${host}/agent`,
agentUtilityOptionsUrl: `${host}/agent/utility/options`,
agentRuleOptionsUrl: `${host}/rule/triggers`,
Expand Down
100 changes: 2 additions & 98 deletions src/routes/page/setting/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
<script>
import { onMount } from 'svelte';
import { _ } from 'svelte-i18n';
import ConfirmModal from '$lib/common/modals/ConfirmModal.svelte';
import Breadcrumb from '$lib/common/shared/Breadcrumb.svelte';
import HeadTitle from '$lib/common/shared/HeadTitle.svelte';
import { getSettings, getSettingDetail } from '$lib/services/setting-service';
import { JSONEditor } from 'svelte-jsoneditor';
import { refreshAgents } from '$lib/services/agent-service';
import { formatNumber } from '$lib/helpers/utils/common';
import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte';

const duration = 3000;
let isLoading = $state(false);
let isComplete = $state(false);
let isError = $state(false);
let successText = $state('');
let errorText = $state('');

let confirmOpen = $state(false);
import AgentMigration from './agent-migration.svelte';

let selectedTab = $state('1');
/** @type {string[]} */
Expand All @@ -41,66 +30,11 @@
json: detail
};
}

function readyToRefresh() {
confirmOpen = true;
}

function closeConfirm() {
confirmOpen = false;
}

function onConfirmRefresh() {
closeConfirm();
refreshAgentData();
}

const refreshAgentData = () => {
isLoading = true;
refreshAgents().then(res => {
isComplete = true;
isLoading = false;
successText = res;
setTimeout(() => {
isComplete = false;
successText = '';
}, duration);
}).catch(() => {
isLoading = false;
isComplete = false;
isError = true;
errorText = 'Failed to migrate agents.';
setTimeout(() => {
isError = false;
errorText = '';
}, duration);
});
};
</script>

<HeadTitle title={$_('Settings')} />
<Breadcrumb title={$_('Settings')} pagetitle={$_('Detail')} />

<LoadingToComplete
{isLoading}
{isComplete}
{isError}
{successText}
{errorText}
/>

<ConfirmModal
isOpen={confirmOpen}
icon="warning"
title="Are you sure?"
text="You will migrate all agents data to mongoDb."
confirmBtnText="Yes"
cancelBtnText="No"
confirm={onConfirmRefresh}
cancel={closeConfirm}
toggleModal={closeConfirm}
/>

<div class="mb-4 rounded-2xl bg-white shadow-xl ring-1 ring-black/5 dark:bg-gray-800 dark:ring-white/10">
<div class="border-b border-gray-100 px-6 py-4 dark:border-gray-700">
<div class="flex items-center gap-3">
Expand Down Expand Up @@ -145,34 +79,4 @@
</div>
</div>

<div class="rounded-2xl bg-white shadow-xl ring-1 ring-black/5 dark:bg-gray-800 dark:ring-white/10">
<div class="border-b border-gray-100 px-6 py-4 dark:border-gray-700">
<div class="flex items-center gap-3">
<span class="flex h-10 w-10 items-center justify-center rounded-full bg-warning/15 text-warning">
<i class="mdi mdi-database-export-outline text-xl"></i>
</span>
<div class="grow">
<h4 class="mb-0 text-base font-semibold text-dark dark:text-gray-100">{$_('Migrate agents from file repository to MongoDB')}</h4>
<p class="mb-0 text-xs text-muted">One-time migration. All agents will be copied to the MongoDB datastore.</p>
</div>
</div>
</div>

<div class="p-4 sm:p-6">
<div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<div class="inline-flex items-center gap-2 text-sm text-muted">
<i class="mdi mdi-alert-circle-outline shrink-0 text-base leading-none text-warning"></i>
<span>This action cannot be undone. Make sure you have a backup before proceeding.</span>
</div>
<button
type="button"
class="inline-flex shrink-0 items-center gap-1.5 rounded-md bg-primary px-4 py-2 text-sm font-medium text-white shadow-sm transition-colors cursor-pointer hover:bg-primary-hover disabled:cursor-not-allowed disabled:opacity-60"
onclick={() => readyToRefresh()}
disabled={isLoading}
>
<i class="bx bx-copy"></i>
{$_('Start Migration')}
</button>
</div>
</div>
</div>
<AgentMigration />
139 changes: 139 additions & 0 deletions src/routes/page/setting/agent-migration.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<script>
import { onMount } from 'svelte';
import { _ } from 'svelte-i18n';
import ConfirmModal from '$lib/common/modals/ConfirmModal.svelte';
import Select from '$lib/common/dropdowns/Select.svelte';
import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte';
import { getFileAgents, refreshAgents } from '$lib/services/agent-service';

const RESULT_DURATION = 3000;

/** @type {import('$commonTypes').LabelValuePair[]} */
let agentOptions = $state([]);
let optionsLoaded = $state(false);
/** @type {string[]} */
let selectedAgentIds = $state([]);

let confirmOpen = $state(false);
let isLoading = $state(false);
/** @type {{ ok: boolean, text: string } | null} */
let result = $state(null);
/** @type {ReturnType<typeof setTimeout> | undefined} */
let resultTimer;

onMount(() => {
loadAgentOptions();
return () => clearTimeout(resultTimer);
});

async function loadAgentOptions() {
try {
const agents = await getFileAgents();
agentOptions = agents
.filter(x => x.id)
.map(x => ({ label: x.name, value: /** @type {string} */ (x.id) }));
selectedAgentIds = agentOptions.map(x => x.value);
} catch (error) {
console.error('Failed to load agents:', error);
showResult(false, $_('Failed to load agents.'));
} finally {
optionsLoaded = true;
}
}

/** @param {{ detail: { selecteds: import('$commonTypes').LabelValuePair[] } }} e */
function selectAgents(e) {
selectedAgentIds = e.detail.selecteds.map(x => x.value);
}

async function migrate() {
confirmOpen = false;
isLoading = true;
try {
showResult(true, await refreshAgents(selectedAgentIds));
} catch (error) {
console.error('Failed to migrate agents:', error);
showResult(false, $_('Failed to migrate agents.'));
} finally {
isLoading = false;
}
}

/**
* @param {boolean} ok
* @param {string} text
*/
function showResult(ok, text) {
clearTimeout(resultTimer);
result = { ok, text };
resultTimer = setTimeout(() => (result = null), RESULT_DURATION);
}
</script>

<LoadingToComplete
{isLoading}
isComplete={result?.ok === true}
isError={result?.ok === false}
successText={result?.text}
errorText={result?.text}
/>

<ConfirmModal
isOpen={confirmOpen}
icon="warning"
title="Are you sure?"
text={$_('You will migrate {n} selected agent(s) to mongoDb.', { values: { n: selectedAgentIds.length } })}
confirmBtnText="Yes"
cancelBtnText="No"
confirm={migrate}
cancel={() => (confirmOpen = false)}
toggleModal={() => (confirmOpen = false)}
/>

<div class="rounded-2xl bg-white shadow-xl ring-1 ring-black/5 dark:bg-gray-800 dark:ring-white/10">
<div class="border-b border-gray-100 px-6 py-4 dark:border-gray-700">
<div class="flex items-center gap-3">
<span class="flex h-10 w-10 items-center justify-center rounded-full bg-warning/15 text-warning">
<i class="mdi mdi-database-export-outline text-xl"></i>
</span>
<div class="grow">
<h4 class="mb-0 text-base font-semibold text-dark dark:text-gray-100">{$_('Migrate agents from file repository to MongoDB')}</h4>
<p class="mb-0 text-xs text-muted">{$_('Select the agents to copy to the MongoDB datastore. Only the selected agents are migrated.')}</p>
</div>
</div>
</div>

<div class="p-4 sm:p-6">
<div class="mb-4">
<Select
tag={'migrate-agent-select'}
placeholder={$_('Select agents to migrate')}
searchPlaceholder={$_('Search agents')}
selectedText={'agents'}
multiSelect
searchMode
selectedValues={selectedAgentIds}
options={agentOptions}
onselect={selectAgents}
/>
{#if optionsLoaded && agentOptions.length === 0}
<p class="mb-0 mt-2 text-xs text-muted">{$_('No agent files found, or the default database is the file repository.')}</p>
{/if}
</div>
<div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
<div class="inline-flex items-center gap-2 text-sm text-muted">
<i class="mdi mdi-alert-circle-outline shrink-0 text-base leading-none text-warning"></i>
<span>This action cannot be undone. Make sure you have a backup before proceeding.</span>
</div>
<button
type="button"
class="inline-flex shrink-0 items-center gap-1.5 rounded-md bg-primary px-4 py-2 text-sm font-medium text-white shadow-sm transition-colors cursor-pointer hover:bg-primary-hover disabled:cursor-not-allowed disabled:opacity-60"
onclick={() => (confirmOpen = true)}
disabled={isLoading || selectedAgentIds.length === 0}
>
<i class="bx bx-copy"></i>
{$_('Start Migration')}
</button>
</div>
</div>
</div>