feat: add MirrorOUC frontend preview

This commit is contained in:
2026-11-01 16:08:29 +08:00
parent 939bc437a6
commit 046db6ece2
37 changed files with 3934 additions and 1 deletions
+63
View File
@@ -0,0 +1,63 @@
import commonZh from '../src/locales/zh-CN/common.ts'
import commonEn from '../src/locales/en-US/common.ts'
import mirrorsZh from '../src/locales/zh-CN/mirrors.ts'
import mirrorsEn from '../src/locales/en-US/mirrors.ts'
import announcementsZh from '../src/locales/zh-CN/announcements.ts'
import announcementsEn from '../src/locales/en-US/announcements.ts'
import monitorZh from '../src/locales/zh-CN/monitor.ts'
import monitorEn from '../src/locales/en-US/monitor.ts'
import { datetimeFormats as datetimeZh, numberFormats as numbersZh } from '../src/locales/zh-CN/formats.ts'
import { datetimeFormats as datetimeEn, numberFormats as numbersEn } from '../src/locales/en-US/formats.ts'
import { createI18n } from 'vue-i18n'
function flatten(value: Record<string, unknown>, prefix = ''): Record<string, string> {
return Object.fromEntries(Object.entries(value).flatMap(([key, entry]) => {
const path = prefix ? `${prefix}.${key}` : key
return typeof entry === 'string' ? [[path, entry]] : Object.entries(flatten(entry as Record<string, unknown>, path))
}))
}
const zh = flatten({ common: commonZh, mirrors: mirrorsZh, announcements: announcementsZh, monitor: monitorZh })
const en = flatten({ common: commonEn, mirrors: mirrorsEn, announcements: announcementsEn, monitor: monitorEn })
const errors: string[] = []
const formats = {
'zh-CN': { datetimeFormats: datetimeZh, numberFormats: numbersZh },
'en-US': { datetimeFormats: datetimeEn, numberFormats: numbersEn },
}
for (const kind of ['datetimeFormats', 'numberFormats'] as const) {
const zhNames = Object.keys(formats['zh-CN'][kind])
const enNames = Object.keys(formats['en-US'][kind])
for (const name of new Set([...zhNames, ...enNames])) {
if (!zhNames.includes(name) || !enNames.includes(name)) errors.push(`Missing format: ${kind}.${name}`)
}
}
for (const key of new Set([...Object.keys(zh), ...Object.keys(en)])) {
if (!(key in zh) || !(key in en)) { errors.push(`Missing key: ${key}`); continue }
const params = (value: string) => [...new Set([...value.matchAll(/\{(\w+)\}/g)].map(match => match[1]))].sort().join(',')
if (params(zh[key]!) !== params(en[key]!)) errors.push(`Mismatched parameters: ${key}`)
}
if (errors.length) throw new Error(errors.join('\n'))
// Compile every message too: special characters such as @ can be valid TS but invalid i18n syntax.
for (const [locale, messages] of Object.entries({ 'zh-CN': zh, 'en-US': en })) {
const definitions = formats[locale as keyof typeof formats]
const i18n = createI18n({
legacy: false, locale, flatJson: true, messages: { [locale]: { ...messages } },
datetimeFormats: { [locale]: definitions.datetimeFormats },
numberFormats: { [locale]: definitions.numberFormats },
})
for (const [key, value] of Object.entries(messages)) {
const params = Object.fromEntries([...value.matchAll(/\{(\w+)\}/g)].map(match => [match[1], '1']))
try { i18n.global.t(key, params) }
catch (error) { errors.push(`${locale}.${key}: ${String(error)}`) }
}
for (const name of Object.keys(definitions.datetimeFormats)) {
try { i18n.global.d(new Date('2026-11-01T00:00:00+08:00'), name) }
catch (error) { errors.push(`${locale}.datetimeFormats.${name}: ${String(error)}`) }
}
for (const name of Object.keys(definitions.numberFormats)) {
try { i18n.global.n(1234.5, name) }
catch (error) { errors.push(`${locale}.numberFormats.${name}: ${String(error)}`) }
}
}
if (errors.length) throw new Error(errors.join('\n'))
console.log(`Locale check passed: ${Object.keys(zh).length} keys, interpolation parameters, message syntax and format definitions match.`)
+26
View File
@@ -0,0 +1,26 @@
import assert from 'node:assert/strict'
import { createMonitoringSnapshot, demoDisks, type MonitoringRange } from '../src/monitoring.ts'
const end = Date.parse('2026-11-01T16:00:00+08:00')
for (const hours of [1, 6, 24] as MonitoringRange[]) {
const snapshot = createMonitoringSnapshot(end, hours)
assert.deepEqual(snapshot, createMonitoringSnapshot(end, hours), 'The same window must reproduce the same data')
assert.equal(snapshot.timestamps.length, 145)
assert.equal(snapshot.timestamps[0], end - hours * 3_600_000)
assert.equal(snapshot.timestamps.at(-1), end)
assert.equal(snapshot.panels.length, 6)
for (const panel of snapshot.panels) {
for (const series of panel.series) {
assert.equal(series.values.length, snapshot.timestamps.length)
assert.ok(series.values.every(value => Number.isFinite(value) && value >= 0 && value <= panel.maximum))
}
for (let index = 0; index < snapshot.timestamps.length; index++) {
if (index) assert.ok(snapshot.timestamps[index]! > snapshot.timestamps[index - 1]!)
const sum = panel.series.reduce((total, series) => total + series.values[index]!, 0)
if (panel.stacked) assert.ok(sum <= panel.maximum + 1e-8, `${panel.id} exceeds the axis limit`)
if (panel.id === 'cpu' || panel.id === 'memory') assert.ok(Math.abs(sum - panel.maximum) < 1e-8, `${panel.id} must preserve its total`)
}
}
}
for (const disk of demoDisks) assert.ok(disk.used >= 0 && disk.used <= disk.total)
console.log('Monitoring check passed: deterministic windows, timestamps, valid samples, stack limits, CPU/memory totals and disk capacities.')