Files
knowledge-governance/web/src/qaMonthly.ts
T

201 lines
5.9 KiB
TypeScript

import { parseQAOrganizationVersion } from './qaOps'
import type { QAOrganizationVersion } from './qaOps'
import { getPayload } from './qaOps'
export interface MonthlyQuery {
month: string
cutoff?: string
region?: string
department_id?: string
activity?: string
search?: string
page?: number
limit?: number
directory_id?: string
revision_id?: string
}
export interface MonthlyPerson {
key: string
name: string
departments: string[]
region: string
state: 'matched' | 'unassigned' | 'cross_region'
questions: number
succeeded: number
failed: number
other: number
}
export interface MonthlyPeople {
organization: QAOrganizationVersion
source: '2.0'
timezone: 'Asia/Shanghai'
month: string
start: string
end: string
month_end: string
as_of: string
partial: boolean
questions: number
linked_questions: number
unresolved_questions: number
roster_people: number
used_people: number
zero_people: number
missing_names: number
total: number
page: number
limit: number
has_more: boolean
rows: MonthlyPerson[]
department_options: { id: string; path: string }[]
}
const fail = (): never => {
throw new Error('个人月明细返回格式异常,未展示可能失真的统计')
}
const obj = (v: unknown): Record<string, unknown> =>
v && typeof v === 'object' && !Array.isArray(v) ? (v as Record<string, unknown>) : fail()
const text = (v: unknown): string => (typeof v === 'string' ? v : fail())
const count = (v: unknown): number => (typeof v === 'number' && Number.isSafeInteger(v) && v >= 0 ? v : fail())
const flag = (v: unknown): boolean => (typeof v === 'boolean' ? v : fail())
const instant = (v: unknown): string => {
const t = text(v)
return Number.isFinite(Date.parse(t)) ? t : fail()
}
const regions = ['国内工业', 'APAC', 'EMEA', 'America']
export function parseQAMonthly(value: unknown): MonthlyPeople {
const v = obj(value),
organization = parseQAOrganizationVersion(v.organization)
if (organization.state !== 'ready' || v.source !== '2.0' || v.timezone !== 'Asia/Shanghai') fail()
const month = text(v.month)
if (!/^(?!0000)\d{4}-(0[1-9]|1[0-2])$/.test(month)) fail()
const start = instant(v.start),
end = instant(v.end),
month_end = instant(v.month_end),
as_of = instant(v.as_of),
partial = flag(v.partial)
const expectedEnd = new Date(`${month}-01T00:00:00Z`)
expectedEnd.setUTCMonth(expectedEnd.getUTCMonth() + 1)
if (
Date.parse(start) !== Date.parse(`${month}-01T00:00:00+08:00`) ||
Date.parse(month_end) !== expectedEnd.getTime() - 8 * 60 * 60 * 1000 ||
Date.parse(end) <= Date.parse(start) ||
Date.parse(end) > Date.parse(month_end) ||
partial !== Date.parse(end) < Date.parse(month_end)
)
fail()
const questions = count(v.questions),
linked_questions = count(v.linked_questions),
unresolved_questions = count(v.unresolved_questions),
roster_people = count(v.roster_people),
used_people = count(v.used_people),
zero_people = count(v.zero_people),
missing_names = count(v.missing_names),
total = count(v.total),
page = count(v.page),
limit = count(v.limit),
has_more = flag(v.has_more)
if (
questions !== linked_questions + unresolved_questions ||
roster_people !== used_people + zero_people ||
missing_names > roster_people ||
total > roster_people ||
page < 1 ||
page > 100000 ||
limit < 1 ||
limit > 100
)
fail()
if (!Array.isArray(v.rows) || !Array.isArray(v.department_options)) return fail()
const keys = new Set<string>()
const rows = v.rows.map((raw: unknown) => {
const r = obj(raw),
key = text(r.key),
name = text(r.name),
region = text(r.region),
state = text(r.state)
if (
!key ||
keys.has(key) ||
name.length > 1000 ||
!['matched', 'unassigned', 'cross_region'].includes(state) ||
(state === 'matched' && !regions.includes(region)) ||
(state !== 'matched' && region !== '')
)
fail()
keys.add(key)
if (!Array.isArray(r.departments)) return fail()
const q = count(r.questions),
s = count(r.succeeded),
f = count(r.failed),
o = count(r.other)
if (s + f + o !== q) fail()
return {
key,
name,
region,
state: state as MonthlyPerson['state'],
departments: r.departments.map(text),
questions: q,
succeeded: s,
failed: f,
other: o,
}
})
const offset = (page - 1) * limit
if (
rows.length !== Math.max(0, Math.min(limit, total - offset)) ||
has_more !== offset + rows.length < total ||
rows.reduce((n, r) => n + r.questions, 0) > linked_questions
)
fail()
const ids = new Set<string>()
const department_options = v.department_options.map((raw: unknown) => {
const d = obj(raw),
id = text(d.id),
path = text(d.path)
if (!id || !path || ids.has(id)) fail()
ids.add(id)
return { id, path }
})
return {
organization,
source: '2.0',
timezone: 'Asia/Shanghai',
month,
start,
end,
month_end,
as_of,
partial,
questions,
linked_questions,
unresolved_questions,
roster_people,
used_people,
zero_people,
missing_names,
total,
page,
limit,
has_more,
rows,
department_options,
}
}
export async function fetchQAMonthly(q: MonthlyQuery, signal?: AbortSignal): Promise<MonthlyPeople> {
return parseQAMonthly(
await getPayload('/api/v1/qa-ops/usage/people', signal, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Governance-Action': 'qa-usage-read' },
body: JSON.stringify(q),
})
)
}
export function beijingMonth(now = new Date()): string {
const parts = new Intl.DateTimeFormat('en-CA', {
timeZone: 'Asia/Shanghai',
year: 'numeric',
month: '2-digit',
}).formatToParts(now)
return `${parts.find((p) => p.type === 'year')!.value}-${parts.find((p) => p.type === 'month')!.value}`
}