按稳定身份展示零使用人员、个人次数与最近提问时间,直接部门去重计算兼属数量;保留原分支统计、配置和源问答。 验证:396项前端测试及类型检查通过;隔离PG全mirror/org竞态回归通过,个人/别名/同名/零使用/未关联与原部门汇总回归通过。 正式同镜像及上线验证尚未执行。
This commit is contained in:
@@ -20,7 +20,15 @@ export interface DepartmentStatistic {
|
||||
visits: number
|
||||
last_asked_at: string | null
|
||||
}
|
||||
export interface DepartmentPersonStatistic {
|
||||
key: string
|
||||
name: string
|
||||
department_ids: string[]
|
||||
visits: number
|
||||
last_asked_at: string | null
|
||||
}
|
||||
export interface DepartmentStatisticsSnapshot {
|
||||
people: DepartmentPersonStatistic[]
|
||||
rows: DepartmentStatistic[]
|
||||
total_visits: number
|
||||
unmapped_visits: number
|
||||
@@ -167,12 +175,75 @@ export function parseDepartmentStatistics(raw: unknown, query: DepartmentStatist
|
||||
)
|
||||
return bad()
|
||||
}
|
||||
if (!Array.isArray(d.people) || d.people.length !== total_people || d.people.length > 100000) return bad()
|
||||
const personKeys = new Set<string>()
|
||||
const people: DepartmentPersonStatistic[] = d.people.map((value) => {
|
||||
const p = obj(value),
|
||||
key = text(p.key),
|
||||
name = text(p.name),
|
||||
visits = count(p.visits)
|
||||
const last = p.last_asked_at === null ? null : instant(p.last_asked_at)
|
||||
if (
|
||||
Object.keys(p).some((k) => !['key', 'name', 'department_ids', 'visits', 'last_asked_at'].includes(k)) ||
|
||||
!/^person-[a-f0-9]{64}$/.test(key) ||
|
||||
personKeys.has(key) ||
|
||||
name.length > 1000 ||
|
||||
name.includes('\0') ||
|
||||
!Array.isArray(p.department_ids) ||
|
||||
p.department_ids.length > 1000 ||
|
||||
p.department_ids.some((id) => typeof id !== 'string' || !byID.has(id)) ||
|
||||
new Set(p.department_ids).size !== p.department_ids.length ||
|
||||
visits > total_visits ||
|
||||
(visits === 0) !== (last === null) ||
|
||||
(last !== null &&
|
||||
(Date.parse(last) >= Date.parse(end) || (start !== null && Date.parse(last) < Date.parse(start))))
|
||||
)
|
||||
return bad()
|
||||
personKeys.add(key)
|
||||
return { key, name, department_ids: p.department_ids as string[], visits, last_asked_at: last }
|
||||
})
|
||||
// Expanded people are an explanation of the existing branch totals, never an extra sum.
|
||||
const branches = new Map(rows.map((r) => [r.id, { total: 0, visitors: 0, visits: 0, last: null as string | null }]))
|
||||
let mapped = 0,
|
||||
allVisits = 0
|
||||
for (const person of people) {
|
||||
allVisits += person.visits
|
||||
if (person.department_ids.length) mapped += person.visits
|
||||
const seen = new Set<string>()
|
||||
for (let id of person.department_ids)
|
||||
while (id && !seen.has(id)) {
|
||||
seen.add(id)
|
||||
id = byID.get(id)!.parent_id
|
||||
}
|
||||
for (const id of seen) {
|
||||
const b = branches.get(id)!
|
||||
b.total++
|
||||
b.visits += person.visits
|
||||
if (person.visits) b.visitors++
|
||||
if (person.last_asked_at && (!b.last || Date.parse(person.last_asked_at) > Date.parse(b.last)))
|
||||
b.last = person.last_asked_at
|
||||
}
|
||||
}
|
||||
if (allVisits > total_visits || mapped !== total_visits - unmapped_visits) return bad()
|
||||
for (const row of rows) {
|
||||
const b = branches.get(row.id)!
|
||||
if (
|
||||
b.total !== row.total_people ||
|
||||
b.visitors !== row.visitors ||
|
||||
b.visits !== row.visits ||
|
||||
(b.last === null
|
||||
? row.last_asked_at !== null
|
||||
: row.last_asked_at === null || Date.parse(b.last) !== Date.parse(row.last_asked_at))
|
||||
)
|
||||
return bad()
|
||||
}
|
||||
return {
|
||||
state: 'ready',
|
||||
cache_key,
|
||||
cache_hit: v.cache_hit,
|
||||
retry_after: 0,
|
||||
snapshot: {
|
||||
people,
|
||||
rows,
|
||||
total_people,
|
||||
total_visits,
|
||||
|
||||
Reference in New Issue
Block a user