feat(datasource): enhance DataSourceSettings and DataSourceSyncLogs functionality
- Implemented polling mechanism in DataSourceSettings for real-time updates on data source synchronization status. - Improved loading state management and user feedback during data source operations. - Added load more functionality in DataSourceSyncLogs for better log retrieval experience. - Updated UI elements for consistency, including icon changes and styling adjustments for better visual clarity.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, onMounted } from 'vue'
|
import { ref, onMounted, onBeforeUnmount } from 'vue'
|
||||||
import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
|
import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
import {
|
import {
|
||||||
@@ -26,17 +26,39 @@ const editingDs = ref<DataSource | null>(null)
|
|||||||
const logsVisible = ref(false)
|
const logsVisible = ref(false)
|
||||||
const logsDsId = ref('')
|
const logsDsId = ref('')
|
||||||
const logsDsName = ref('')
|
const logsDsName = ref('')
|
||||||
|
const pollTimer = ref<number | null>(null)
|
||||||
|
|
||||||
async function loadList() {
|
function stopPolling() {
|
||||||
loading.value = true
|
if (pollTimer.value !== null) {
|
||||||
|
window.clearTimeout(pollTimer.value)
|
||||||
|
pollTimer.value = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function schedulePolling() {
|
||||||
|
stopPolling()
|
||||||
|
pollTimer.value = window.setTimeout(() => {
|
||||||
|
loadList(true)
|
||||||
|
}, 3000)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadList(silent = false) {
|
||||||
|
if (!silent) loading.value = true
|
||||||
try {
|
try {
|
||||||
const res = await listDataSources(props.kbId)
|
const res = await listDataSources(props.kbId)
|
||||||
dataSources.value = res?.data || res || []
|
dataSources.value = res?.data || res || []
|
||||||
emit('count', dataSources.value.length)
|
emit('count', dataSources.value.length)
|
||||||
|
|
||||||
|
const hasRunningSync = dataSources.value.some(ds => ds.latest_sync_log?.status === 'running')
|
||||||
|
if (hasRunningSync) {
|
||||||
|
schedulePolling()
|
||||||
|
} else {
|
||||||
|
stopPolling()
|
||||||
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false
|
if (!silent) loading.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +101,7 @@ async function handleSync(ds: DataSource) {
|
|||||||
try {
|
try {
|
||||||
await triggerSync(ds.id)
|
await triggerSync(ds.id)
|
||||||
MessagePlugin.success(t('datasource.syncTriggered'))
|
MessagePlugin.success(t('datasource.syncTriggered'))
|
||||||
loadList()
|
await loadList(true)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
MessagePlugin.error(e?.message || e?.error || t('datasource.syncFailed'))
|
MessagePlugin.error(e?.message || e?.error || t('datasource.syncFailed'))
|
||||||
}
|
}
|
||||||
@@ -167,12 +189,17 @@ function lastSyncStatusColor(ds: DataSource) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isSyncRunning(ds: DataSource) {
|
||||||
|
return ds.latest_sync_log?.status === 'running'
|
||||||
|
}
|
||||||
|
|
||||||
function onEditorSaved() {
|
function onEditorSaved() {
|
||||||
editorVisible.value = false
|
editorVisible.value = false
|
||||||
loadList()
|
loadList()
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(loadList)
|
onMounted(loadList)
|
||||||
|
onBeforeUnmount(stopPolling)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -203,7 +230,7 @@ onMounted(loadList)
|
|||||||
<div v-for="ds in dataSources" :key="ds.id" class="ds-card">
|
<div v-for="ds in dataSources" :key="ds.id" class="ds-card">
|
||||||
<div class="ds-card-header">
|
<div class="ds-card-header">
|
||||||
<div class="ds-card-title-wrap">
|
<div class="ds-card-title-wrap">
|
||||||
<DataSourceTypeIcon :type="ds.type" :size="24" />
|
<DataSourceTypeIcon :type="ds.type" :size="32" />
|
||||||
<div class="ds-title-text">
|
<div class="ds-title-text">
|
||||||
<div class="ds-name-row">
|
<div class="ds-name-row">
|
||||||
<span class="ds-name" :title="ds.name">{{ ds.name }}</span>
|
<span class="ds-name" :title="ds.name">{{ ds.name }}</span>
|
||||||
@@ -216,14 +243,22 @@ onMounted(loadList)
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="ds-card-actions">
|
<div class="ds-card-actions">
|
||||||
<t-tooltip :content="t('datasource.syncNow')">
|
<t-tooltip :content="isSyncRunning(ds) ? t('datasource.logStatus.running') : t('datasource.syncNow')">
|
||||||
<t-button size="small" variant="text" theme="primary" @click="handleSync(ds)">
|
<t-button
|
||||||
<template #icon><t-icon name="refresh" /></template>
|
size="small"
|
||||||
|
variant="text"
|
||||||
|
theme="primary"
|
||||||
|
:disabled="isSyncRunning(ds)"
|
||||||
|
@click="handleSync(ds)"
|
||||||
|
>
|
||||||
|
<template #icon>
|
||||||
|
<t-icon name="refresh" :class="{ 'ds-icon-spin': isSyncRunning(ds) }" />
|
||||||
|
</template>
|
||||||
</t-button>
|
</t-button>
|
||||||
</t-tooltip>
|
</t-tooltip>
|
||||||
<t-tooltip :content="t('datasource.logs')">
|
<t-tooltip :content="t('datasource.logs')">
|
||||||
<t-button size="small" variant="text" @click="openLogs(ds)">
|
<t-button size="small" variant="text" @click="openLogs(ds)">
|
||||||
<template #icon><t-icon name="history" /></template>
|
<template #icon><t-icon name="root-list" /></template>
|
||||||
</t-button>
|
</t-button>
|
||||||
</t-tooltip>
|
</t-tooltip>
|
||||||
<t-dropdown trigger="click" :min-column-width="120">
|
<t-dropdown trigger="click" :min-column-width="120">
|
||||||
@@ -394,8 +429,8 @@ onMounted(loadList)
|
|||||||
position: relative;
|
position: relative;
|
||||||
background: var(--td-bg-color-container);
|
background: var(--td-bg-color-container);
|
||||||
border: 1px solid var(--td-border-level-2-color);
|
border: 1px solid var(--td-border-level-2-color);
|
||||||
border-radius: 12px;
|
border-radius: 8px;
|
||||||
padding: 20px;
|
padding: 15px 20px;
|
||||||
transition: all 0.2s ease;
|
transition: all 0.2s ease;
|
||||||
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
|
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
|
||||||
}
|
}
|
||||||
@@ -527,6 +562,15 @@ onMounted(loadList)
|
|||||||
.ds-pill.skipped { background: var(--td-bg-color-component); color: var(--td-text-color-placeholder); }
|
.ds-pill.skipped { background: var(--td-bg-color-component); color: var(--td-text-color-placeholder); }
|
||||||
.ds-pill.failed { background: var(--td-error-color-1); color: var(--td-error-color); }
|
.ds-pill.failed { background: var(--td-error-color-1); color: var(--td-error-color); }
|
||||||
|
|
||||||
|
.ds-icon-spin {
|
||||||
|
animation: ds-spin 1s linear infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes ds-spin {
|
||||||
|
from { transform: rotate(0deg); }
|
||||||
|
to { transform: rotate(360deg); }
|
||||||
|
}
|
||||||
|
|
||||||
/* --- Error alert --- */
|
/* --- Error alert --- */
|
||||||
.ds-card-error {
|
.ds-card-error {
|
||||||
margin-top: 16px;
|
margin-top: 16px;
|
||||||
@@ -547,10 +591,10 @@ onMounted(loadList)
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
gap: 12px;
|
gap: 12px;
|
||||||
padding: 15px;
|
padding: 6px 15px;
|
||||||
background: var(--td-bg-color-secondarycontainer);
|
background: var(--td-bg-color-secondarycontainer);
|
||||||
border: 1px solid transparent;
|
border: 1px solid transparent;
|
||||||
border-radius: 12px;
|
border-radius: 8px;
|
||||||
color: var(--td-text-color-secondary);
|
color: var(--td-text-color-secondary);
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
|
|||||||
@@ -12,28 +12,50 @@ const { t } = useI18n()
|
|||||||
|
|
||||||
const logs = ref<SyncLog[]>([])
|
const logs = ref<SyncLog[]>([])
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
|
const loadingMore = ref(false)
|
||||||
|
const hasMore = ref(false)
|
||||||
const expandedId = ref('')
|
const expandedId = ref('')
|
||||||
|
const pageSize = 50
|
||||||
|
|
||||||
async function fetchLogs() {
|
async function fetchLogs(reset = true) {
|
||||||
if (!props.dataSourceId) return
|
if (!props.dataSourceId) return
|
||||||
loading.value = true
|
|
||||||
|
if (reset) {
|
||||||
|
loading.value = true
|
||||||
|
} else {
|
||||||
|
loadingMore.value = true
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await getSyncLogs(props.dataSourceId, 50, 0)
|
const offset = reset ? 0 : logs.value.length
|
||||||
logs.value = res?.data || res || []
|
const res = await getSyncLogs(props.dataSourceId, pageSize, offset)
|
||||||
|
const items = res?.data || res || []
|
||||||
|
logs.value = reset ? items : [...logs.value, ...items]
|
||||||
|
hasMore.value = items.length === pageSize
|
||||||
} catch { /* ignore */ }
|
} catch { /* ignore */ }
|
||||||
loading.value = false
|
|
||||||
|
if (reset) {
|
||||||
|
loading.value = false
|
||||||
|
} else {
|
||||||
|
loadingMore.value = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(visible, (v) => {
|
watch(visible, (v) => {
|
||||||
if (!v) return
|
if (!v) return
|
||||||
expandedId.value = ''
|
expandedId.value = ''
|
||||||
fetchLogs()
|
fetchLogs(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
function toggleExpand(id: string) {
|
function toggleExpand(id: string) {
|
||||||
expandedId.value = expandedId.value === id ? '' : id
|
expandedId.value = expandedId.value === id ? '' : id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadMore() {
|
||||||
|
if (loading.value || loadingMore.value || !hasMore.value) return
|
||||||
|
fetchLogs(false)
|
||||||
|
}
|
||||||
|
|
||||||
// --- Stats ---
|
// --- Stats ---
|
||||||
const stats = computed(() => {
|
const stats = computed(() => {
|
||||||
const total = logs.value.length
|
const total = logs.value.length
|
||||||
@@ -154,7 +176,7 @@ const groupedLogs = computed(() => {
|
|||||||
<div v-if="loading" style="text-align:center;padding:60px"><t-loading /></div>
|
<div v-if="loading" style="text-align:center;padding:60px"><t-loading /></div>
|
||||||
|
|
||||||
<div v-else-if="logs.length === 0" class="logs-empty">
|
<div v-else-if="logs.length === 0" class="logs-empty">
|
||||||
<t-icon name="history" size="40px" />
|
<t-icon name="root-list" size="40px" />
|
||||||
<p>{{ t('datasource.noLogs') }}</p>
|
<p>{{ t('datasource.noLogs') }}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -238,6 +260,19 @@ const groupedLogs = computed(() => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="logs-load-more">
|
||||||
|
<t-button
|
||||||
|
v-if="hasMore"
|
||||||
|
variant="outline"
|
||||||
|
block
|
||||||
|
:loading="loadingMore"
|
||||||
|
@click="loadMore"
|
||||||
|
>
|
||||||
|
{{ t('common.loadMore') }}
|
||||||
|
</t-button>
|
||||||
|
<span v-else class="logs-load-more-text">{{ t('common.noMoreData') }}</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</t-drawer>
|
</t-drawer>
|
||||||
@@ -326,6 +361,16 @@ const groupedLogs = computed(() => {
|
|||||||
margin-bottom: 4px;
|
margin-bottom: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.logs-load-more {
|
||||||
|
padding: 16px 0 8px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logs-load-more-text {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--td-text-color-placeholder);
|
||||||
|
}
|
||||||
|
|
||||||
/* --- Dot column: continuous line --- */
|
/* --- Dot column: continuous line --- */
|
||||||
.tl-dot-col {
|
.tl-dot-col {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
@@ -272,7 +272,9 @@ func (s *DataSourceService) ManualSync(ctx context.Context, dsID string) (*types
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if ds.Status != types.DataSourceStatusActive && ds.Status != types.DataSourceStatusError {
|
if ds.Status != types.DataSourceStatusActive &&
|
||||||
|
ds.Status != types.DataSourceStatusError &&
|
||||||
|
ds.Status != types.DataSourceStatusPaused {
|
||||||
return nil, datasource.ErrDataSourceNotActive
|
return nil, datasource.ErrDataSourceNotActive
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,7 +309,9 @@ func (s *DataSourceService) ManualSync(ctx context.Context, dsID string) (*types
|
|||||||
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
||||||
syncLog.ErrorMessage = err.Error()
|
syncLog.ErrorMessage = err.Error()
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = s.syncLogRepo.Update(ctx, syncLog)
|
||||||
ds.Status = types.DataSourceStatusError
|
if ds.Status != types.DataSourceStatusPaused {
|
||||||
|
ds.Status = types.DataSourceStatusError
|
||||||
|
}
|
||||||
ds.ErrorMessage = fmt.Sprintf("Failed to enqueue sync: %v", err)
|
ds.ErrorMessage = fmt.Sprintf("Failed to enqueue sync: %v", err)
|
||||||
_ = s.dsRepo.Update(ctx, ds)
|
_ = s.dsRepo.Update(ctx, ds)
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -408,6 +412,8 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wasPaused := ds.Status == types.DataSourceStatusPaused
|
||||||
|
|
||||||
// Get connector
|
// Get connector
|
||||||
connector, err := s.connectorRegistry.Get(ds.Type)
|
connector, err := s.connectorRegistry.Get(ds.Type)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -416,7 +422,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
||||||
syncLog.ErrorMessage = fmt.Sprintf("Connector not found: %s", ds.Type)
|
syncLog.ErrorMessage = fmt.Sprintf("Connector not found: %s", ds.Type)
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = s.syncLogRepo.Update(ctx, syncLog)
|
||||||
ds.Status = types.DataSourceStatusError
|
if !wasPaused {
|
||||||
|
ds.Status = types.DataSourceStatusError
|
||||||
|
}
|
||||||
ds.ErrorMessage = syncLog.ErrorMessage
|
ds.ErrorMessage = syncLog.ErrorMessage
|
||||||
_ = s.dsRepo.Update(ctx, ds)
|
_ = s.dsRepo.Update(ctx, ds)
|
||||||
return err
|
return err
|
||||||
@@ -430,7 +438,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
||||||
syncLog.ErrorMessage = fmt.Sprintf("Invalid configuration: %v", err)
|
syncLog.ErrorMessage = fmt.Sprintf("Invalid configuration: %v", err)
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = s.syncLogRepo.Update(ctx, syncLog)
|
||||||
ds.Status = types.DataSourceStatusError
|
if !wasPaused {
|
||||||
|
ds.Status = types.DataSourceStatusError
|
||||||
|
}
|
||||||
ds.ErrorMessage = syncLog.ErrorMessage
|
ds.ErrorMessage = syncLog.ErrorMessage
|
||||||
_ = s.dsRepo.Update(ctx, ds)
|
_ = s.dsRepo.Update(ctx, ds)
|
||||||
return err
|
return err
|
||||||
@@ -458,7 +468,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
||||||
syncLog.ErrorMessage = fmt.Sprintf("Fetch failed: %v", fetchErr)
|
syncLog.ErrorMessage = fmt.Sprintf("Fetch failed: %v", fetchErr)
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = s.syncLogRepo.Update(ctx, syncLog)
|
||||||
ds.Status = types.DataSourceStatusError
|
if !wasPaused {
|
||||||
|
ds.Status = types.DataSourceStatusError
|
||||||
|
}
|
||||||
ds.ErrorMessage = syncLog.ErrorMessage
|
ds.ErrorMessage = syncLog.ErrorMessage
|
||||||
_ = s.dsRepo.Update(ctx, ds)
|
_ = s.dsRepo.Update(ctx, ds)
|
||||||
return fetchErr
|
return fetchErr
|
||||||
@@ -479,7 +491,9 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
syncLog.FinishedAt = timePtr(time.Now().UTC())
|
||||||
syncLog.ErrorMessage = fmt.Sprintf("Failed to get tenant info: %v", err)
|
syncLog.ErrorMessage = fmt.Sprintf("Failed to get tenant info: %v", err)
|
||||||
_ = s.syncLogRepo.Update(ctx, syncLog)
|
_ = s.syncLogRepo.Update(ctx, syncLog)
|
||||||
ds.Status = types.DataSourceStatusError
|
if !wasPaused {
|
||||||
|
ds.Status = types.DataSourceStatusError
|
||||||
|
}
|
||||||
ds.ErrorMessage = syncLog.ErrorMessage
|
ds.ErrorMessage = syncLog.ErrorMessage
|
||||||
_ = s.dsRepo.Update(ctx, ds)
|
_ = s.dsRepo.Update(ctx, ds)
|
||||||
return err
|
return err
|
||||||
@@ -553,7 +567,11 @@ func (s *DataSourceService) ProcessSync(ctx context.Context, task *asynq.Task) e
|
|||||||
}
|
}
|
||||||
|
|
||||||
ds.LastSyncAt = timePtr(time.Now().UTC())
|
ds.LastSyncAt = timePtr(time.Now().UTC())
|
||||||
ds.Status = types.DataSourceStatusActive
|
if wasPaused {
|
||||||
|
ds.Status = types.DataSourceStatusPaused
|
||||||
|
} else {
|
||||||
|
ds.Status = types.DataSourceStatusActive
|
||||||
|
}
|
||||||
ds.ErrorMessage = ""
|
ds.ErrorMessage = ""
|
||||||
|
|
||||||
// Store result
|
// Store result
|
||||||
|
|||||||
Reference in New Issue
Block a user