优化监控任务多账号配置
This commit is contained in:
@@ -4,6 +4,8 @@ import {
|
||||
saveNotificationConfig,
|
||||
} from '../../notification/config-service.js'
|
||||
import { sendInternalNotification } from '../../notification/notification-service.js'
|
||||
import { listCloudtentaclesSources } from '../../platforms/cloudtentacles/source-config-service.js'
|
||||
import { getAllCloudtentaclesSessionStates } from '../../platforms/cloudtentacles/session-state-service.js'
|
||||
import {
|
||||
getScheduledJobsConfig,
|
||||
getScheduledJobsFilePath,
|
||||
@@ -14,7 +16,7 @@ import {
|
||||
reloadScheduledJobs,
|
||||
runScheduledJobNow,
|
||||
} from '../../scheduler/scheduler-service.js'
|
||||
import { maskSecret } from './mappers.js'
|
||||
import { maskPhone, maskSecret } from './mappers.js'
|
||||
|
||||
import type {
|
||||
AdminNotificationConfigInput,
|
||||
@@ -58,6 +60,7 @@ export function getAdminScheduledJobsConfig() {
|
||||
filePath: getScheduledJobsFilePath(),
|
||||
source: mapAdminScheduledJobsConfig(config),
|
||||
runtime: getScheduledJobRuntimeStates(),
|
||||
cloudtentaclesAccounts: listAdminCloudtentaclesMonitorAccounts(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +72,7 @@ export function updateAdminScheduledJobsConfig(payload: AdminScheduledJobsConfig
|
||||
filePath: getScheduledJobsFilePath(),
|
||||
source: mapAdminScheduledJobsConfig(saved),
|
||||
runtime: getScheduledJobRuntimeStates(),
|
||||
cloudtentaclesAccounts: listAdminCloudtentaclesMonitorAccounts(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,6 +118,11 @@ function mapAdminNotificationConfig(config: JsonObject = {}) {
|
||||
}
|
||||
|
||||
function mapAdminScheduledJobsConfig(config: JsonObject = {}) {
|
||||
const cloudtentaclesAccountMap = new Map(
|
||||
listAdminCloudtentaclesMonitorAccounts()
|
||||
.map((item) => [item.sourceKey, item]),
|
||||
)
|
||||
|
||||
return {
|
||||
enabled: config.enabled !== false,
|
||||
jobs: (Array.isArray(config.jobs) ? config.jobs : []).map((item) => ({
|
||||
@@ -123,8 +132,67 @@ function mapAdminScheduledJobsConfig(config: JsonObject = {}) {
|
||||
intervalSeconds: Number(item.intervalSeconds || 300),
|
||||
cooldownSeconds: Number(item.cooldownSeconds || 1800),
|
||||
config: {
|
||||
assetThreshold: Number(item.config?.assetThreshold || 500),
|
||||
assetThreshold: normalizeNonNegativeNumber(item.config?.assetThreshold, 500),
|
||||
accounts: mapScheduledJobCloudtentaclesAccounts(
|
||||
item.config?.accounts,
|
||||
normalizeNonNegativeNumber(item.config?.assetThreshold, 500),
|
||||
cloudtentaclesAccountMap,
|
||||
),
|
||||
},
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
function listAdminCloudtentaclesMonitorAccounts() {
|
||||
const sourcesConfig = listCloudtentaclesSources()
|
||||
const sessionsConfig = getAllCloudtentaclesSessionStates()
|
||||
const sessions = sessionsConfig.sessions || {}
|
||||
|
||||
return (Array.isArray(sourcesConfig.sources) ? sourcesConfig.sources : []).map((source) => {
|
||||
const sourceKey = String(source.key || '').trim()
|
||||
const session = sessions[sourceKey] || {}
|
||||
const label = String(source.label || source.username || sourceKey).trim() || sourceKey
|
||||
|
||||
return {
|
||||
sourceKey,
|
||||
label,
|
||||
enabled: source.enabled !== false,
|
||||
username: String(source.username || '').trim(),
|
||||
phoneMasked: maskPhone(source.phone || session.phone),
|
||||
hasToken: Boolean(String(session.token || '').trim()),
|
||||
loggedInAt: String(session.loggedInAt || '').trim(),
|
||||
}
|
||||
}).filter((item) => item.sourceKey)
|
||||
}
|
||||
|
||||
function mapScheduledJobCloudtentaclesAccounts(
|
||||
rawAccounts: unknown,
|
||||
defaultAssetThreshold: number,
|
||||
cloudtentaclesAccountMap: Map<string, JsonObject>,
|
||||
) {
|
||||
const accounts = Array.isArray(rawAccounts) ? rawAccounts : []
|
||||
return accounts.map((item) => {
|
||||
const source = isPlainObject(item) ? item : {}
|
||||
const sourceKey = String(source.sourceKey || source.key || '').trim()
|
||||
const option = cloudtentaclesAccountMap.get(sourceKey) || {}
|
||||
|
||||
return {
|
||||
sourceKey,
|
||||
label: String(source.label || option.label || sourceKey).trim(),
|
||||
enabled: source.enabled !== false,
|
||||
assetThreshold: normalizeNonNegativeNumber(
|
||||
source.assetThreshold ?? source.threshold,
|
||||
defaultAssetThreshold,
|
||||
),
|
||||
}
|
||||
}).filter((item) => item.sourceKey)
|
||||
}
|
||||
|
||||
function normalizeNonNegativeNumber(value: unknown, fallback: number) {
|
||||
const parsed = Number(value)
|
||||
return Number.isFinite(parsed) && parsed >= 0 ? parsed : fallback
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is JsonObject {
|
||||
return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user