mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 17:30:23 +08:00
refactor: 优化调度候选排序与用量写入链路并改进 Fernet 缓存与前端批量列表
This commit is contained in:
@@ -40,4 +40,43 @@ describe('getOAuthOrgBadge', () => {
|
||||
|
||||
expect(badge).toBeNull()
|
||||
})
|
||||
|
||||
it('reuses cached badge objects when identity fields are unchanged', () => {
|
||||
const identity = {
|
||||
oauth_account_id: 'acct-demo-001',
|
||||
oauth_account_name: 'Workspace Alpha',
|
||||
oauth_account_user_id: 'user-1__acct-demo-001',
|
||||
oauth_organizations: [
|
||||
{ id: 'org-personal-1234', title: 'Personal', is_default: true },
|
||||
],
|
||||
}
|
||||
|
||||
const first = getOAuthOrgBadge(identity)
|
||||
const second = getOAuthOrgBadge(identity)
|
||||
|
||||
expect(first).toBe(second)
|
||||
})
|
||||
|
||||
it('invalidates the cached badge when the selected organization changes in place', () => {
|
||||
const organizations = [
|
||||
{ id: 'org-personal-1234', title: 'Personal', is_default: true },
|
||||
{ id: 'org-team-5678', title: 'Team', is_default: false },
|
||||
]
|
||||
const identity = {
|
||||
oauth_account_id: 'acct-demo-001',
|
||||
oauth_organizations: organizations,
|
||||
}
|
||||
|
||||
const first = getOAuthOrgBadge(identity)
|
||||
organizations[0].is_default = false
|
||||
organizations[1].is_default = true
|
||||
const second = getOAuthOrgBadge(identity)
|
||||
|
||||
expect(first).not.toBe(second)
|
||||
expect(second).toEqual({
|
||||
id: 'org-team-5678',
|
||||
label: 'org:team-5678',
|
||||
title: 'account_id: acct-demo-001 | org_id: org-team-5678 | org_title: Team',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -7,23 +7,65 @@ type OAuthIdentityDisplayValue = {
|
||||
oauth_organizations?: OAuthOrganizationInfo[] | null
|
||||
} | null | undefined
|
||||
|
||||
function formatOAuthIdentityShort(
|
||||
value: string | null | undefined,
|
||||
head = 8,
|
||||
tail = 6,
|
||||
): string {
|
||||
const normalized = String(value || '').trim()
|
||||
if (!normalized) return ''
|
||||
if (normalized.length <= head + tail + 3) return normalized
|
||||
return `${normalized.slice(0, head)}...${normalized.slice(-tail)}`
|
||||
type OAuthOrgBadge = {
|
||||
id: string
|
||||
label: string
|
||||
title: string
|
||||
}
|
||||
|
||||
type SelectedOAuthOrganization = {
|
||||
id: string
|
||||
title: string
|
||||
rawId: unknown
|
||||
rawTitle: unknown
|
||||
rawIsDefault: boolean | null | undefined
|
||||
index: number
|
||||
}
|
||||
|
||||
type OAuthOrgBadgeCacheEntry = {
|
||||
rawAccountId: unknown
|
||||
rawAccountName: unknown
|
||||
rawAccountUserId: unknown
|
||||
rawOrganizations: OAuthOrganizationInfo[] | null
|
||||
organizationCount: number
|
||||
selectedOrganizationIndex: number
|
||||
selectedOrganizationRawId: unknown
|
||||
selectedOrganizationRawTitle: unknown
|
||||
selectedOrganizationRawIsDefault: boolean | null | undefined
|
||||
result: OAuthOrgBadge | null
|
||||
}
|
||||
|
||||
const oauthOrgBadgeCache = new WeakMap<object, OAuthOrgBadgeCacheEntry>()
|
||||
|
||||
function readStr(raw: unknown): string {
|
||||
return typeof raw === 'string' ? raw.trim() : ''
|
||||
}
|
||||
|
||||
function formatOAuthIdentityShort(value: string, head = 8, tail = 6): string {
|
||||
if (!value) return ''
|
||||
if (value.length <= head + tail + 3) return value
|
||||
return `${value.slice(0, head)}...${value.slice(-tail)}`
|
||||
}
|
||||
|
||||
function stripOAuthOrganizationPrefix(orgId: string): string {
|
||||
return orgId.replace(/^org[-_:]+/i, '').trim()
|
||||
if (orgId.length < 4) return orgId
|
||||
|
||||
const first = orgId.charCodeAt(0) | 32
|
||||
const second = orgId.charCodeAt(1) | 32
|
||||
const third = orgId.charCodeAt(2) | 32
|
||||
|
||||
if (first !== 111 || second !== 114 || third !== 103) {
|
||||
return orgId
|
||||
}
|
||||
|
||||
let index = 3
|
||||
while (index < orgId.length) {
|
||||
const code = orgId.charCodeAt(index)
|
||||
if (code !== 45 && code !== 95 && code !== 58) break
|
||||
index += 1
|
||||
}
|
||||
|
||||
return index === 3 ? orgId : orgId.slice(index)
|
||||
}
|
||||
|
||||
function formatOAuthOrganizationBadge(orgId: string): string {
|
||||
@@ -45,60 +87,154 @@ function formatOAuthAccountUserBadge(accountUserId: string): string {
|
||||
|
||||
function getPrimaryOAuthOrganization(
|
||||
value: OAuthIdentityDisplayValue,
|
||||
): { id: string; title: string } | null {
|
||||
const organizations: OAuthOrganizationInfo[] = Array.isArray(value?.oauth_organizations)
|
||||
): SelectedOAuthOrganization | null {
|
||||
const organizations = Array.isArray(value?.oauth_organizations)
|
||||
? value.oauth_organizations
|
||||
: []
|
||||
let firstWithId: OAuthOrganizationInfo | null = null
|
||||
: null
|
||||
if (!organizations?.length) return null
|
||||
|
||||
let firstWithId: SelectedOAuthOrganization | null = null
|
||||
|
||||
for (let index = 0; index < organizations.length; index += 1) {
|
||||
const org = organizations[index]
|
||||
if (typeof org?.id !== 'string' || !org.id.trim()) continue
|
||||
if (!firstWithId) firstWithId = org
|
||||
const rawId = org?.id
|
||||
if (typeof rawId !== 'string') continue
|
||||
|
||||
const id = rawId.trim()
|
||||
if (!id) continue
|
||||
|
||||
const selectedOrganization: SelectedOAuthOrganization = {
|
||||
id,
|
||||
title: readStr(org?.title),
|
||||
rawId,
|
||||
rawTitle: org?.title,
|
||||
rawIsDefault: org?.is_default,
|
||||
index,
|
||||
}
|
||||
|
||||
if (!firstWithId) firstWithId = selectedOrganization
|
||||
if (org.is_default) {
|
||||
firstWithId = org
|
||||
firstWithId = selectedOrganization
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (!firstWithId?.id) return null
|
||||
return firstWithId
|
||||
}
|
||||
|
||||
return {
|
||||
id: firstWithId.id.trim(),
|
||||
title: typeof firstWithId.title === 'string' ? firstWithId.title.trim() : '',
|
||||
function appendTitlePart(title: string, label: string, value: string): string {
|
||||
if (!value) return title
|
||||
if (!title) return `${label}: ${value}`
|
||||
return `${title} | ${label}: ${value}`
|
||||
}
|
||||
|
||||
function buildOAuthIdentityTitle(
|
||||
accountName: string,
|
||||
accountId: string,
|
||||
accountUserId: string,
|
||||
organization: SelectedOAuthOrganization | null,
|
||||
): string {
|
||||
let title = ''
|
||||
|
||||
title = appendTitlePart(title, 'name', accountName)
|
||||
title = appendTitlePart(title, 'account_id', accountId)
|
||||
title = appendTitlePart(title, 'account_user_id', accountUserId)
|
||||
title = appendTitlePart(title, 'org_id', organization?.id || '')
|
||||
title = appendTitlePart(title, 'org_title', organization?.title || '')
|
||||
|
||||
return title
|
||||
}
|
||||
|
||||
function getCachedOAuthOrgBadge(
|
||||
value: OAuthIdentityDisplayValue,
|
||||
organization: SelectedOAuthOrganization | null,
|
||||
): OAuthOrgBadge | null | undefined {
|
||||
if (!value || typeof value !== 'object') return undefined
|
||||
|
||||
const cached = oauthOrgBadgeCache.get(value)
|
||||
if (!cached) return undefined
|
||||
|
||||
const organizations = Array.isArray(value.oauth_organizations)
|
||||
? value.oauth_organizations
|
||||
: null
|
||||
|
||||
if (
|
||||
cached.rawAccountId === value.oauth_account_id
|
||||
&& cached.rawAccountName === value.oauth_account_name
|
||||
&& cached.rawAccountUserId === value.oauth_account_user_id
|
||||
&& cached.rawOrganizations === organizations
|
||||
&& cached.organizationCount === (organizations?.length || 0)
|
||||
&& cached.selectedOrganizationIndex === (organization?.index ?? -1)
|
||||
&& cached.selectedOrganizationRawId === organization?.rawId
|
||||
&& cached.selectedOrganizationRawTitle === organization?.rawTitle
|
||||
&& cached.selectedOrganizationRawIsDefault === organization?.rawIsDefault
|
||||
) {
|
||||
return cached.result
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
function setCachedOAuthOrgBadge(
|
||||
value: OAuthIdentityDisplayValue,
|
||||
organization: SelectedOAuthOrganization | null,
|
||||
result: OAuthOrgBadge | null,
|
||||
): void {
|
||||
if (!value || typeof value !== 'object') return
|
||||
|
||||
const organizations = Array.isArray(value.oauth_organizations)
|
||||
? value.oauth_organizations
|
||||
: null
|
||||
|
||||
oauthOrgBadgeCache.set(value, {
|
||||
rawAccountId: value.oauth_account_id,
|
||||
rawAccountName: value.oauth_account_name,
|
||||
rawAccountUserId: value.oauth_account_user_id,
|
||||
rawOrganizations: organizations,
|
||||
organizationCount: organizations?.length || 0,
|
||||
selectedOrganizationIndex: organization?.index ?? -1,
|
||||
selectedOrganizationRawId: organization?.rawId,
|
||||
selectedOrganizationRawTitle: organization?.rawTitle,
|
||||
selectedOrganizationRawIsDefault: organization?.rawIsDefault,
|
||||
result,
|
||||
})
|
||||
}
|
||||
|
||||
export function getOAuthOrgBadge(
|
||||
value: OAuthIdentityDisplayValue,
|
||||
): { id: string; label: string; title: string } | null {
|
||||
): OAuthOrgBadge | null {
|
||||
const org = getPrimaryOAuthOrganization(value)
|
||||
const cached = getCachedOAuthOrgBadge(value, org)
|
||||
if (cached !== undefined) return cached
|
||||
|
||||
const accountId = readStr(value?.oauth_account_id)
|
||||
const accountName = readStr(value?.oauth_account_name)
|
||||
const accountUserId = readStr(value?.oauth_account_user_id)
|
||||
|
||||
const badgeId = org?.id || accountId || accountUserId || ''
|
||||
const label = org?.id
|
||||
if (!badgeId) {
|
||||
setCachedOAuthOrgBadge(value, org, null)
|
||||
return null
|
||||
}
|
||||
|
||||
const label = org
|
||||
? formatOAuthOrganizationBadge(org.id)
|
||||
: accountId
|
||||
? formatOAuthAccountBadge(accountId)
|
||||
: accountUserId
|
||||
? formatOAuthAccountUserBadge(accountUserId)
|
||||
: ''
|
||||
if (!badgeId || !label) return null
|
||||
if (!label) {
|
||||
setCachedOAuthOrgBadge(value, org, null)
|
||||
return null
|
||||
}
|
||||
|
||||
const titleParts = [
|
||||
accountName ? `name: ${accountName}` : '',
|
||||
accountId ? `account_id: ${accountId}` : '',
|
||||
accountUserId ? `account_user_id: ${accountUserId}` : '',
|
||||
org?.id ? `org_id: ${org.id}` : '',
|
||||
org?.title ? `org_title: ${org.title}` : '',
|
||||
].filter(Boolean)
|
||||
|
||||
return {
|
||||
const result: OAuthOrgBadge = {
|
||||
id: badgeId,
|
||||
label,
|
||||
title: titleParts.join(' | '),
|
||||
title: buildOAuthIdentityTitle(accountName, accountId, accountUserId, org),
|
||||
}
|
||||
|
||||
setCachedOAuthOrgBadge(value, org, result)
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user