From 41641c6f558128b3258a2424e556b7beb1917e0b Mon Sep 17 00:00:00 2001 From: xy <10816187@qq.com> Date: Mon, 23 Nov 2026 17:19:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20git=E5=B5=8C=E5=85=A5=E9=A1=B5=E6=96=B0?= =?UTF-8?q?=E5=A2=9Egitea=E8=B4=A1=E7=8C=AE=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ContributionHeatmap.astro | 282 ++++++++++++++++++ src/pages/embed/gitea.astro | 346 +++++++++++++++-------- src/types/stats.ts | 7 + 3 files changed, 517 insertions(+), 118 deletions(-) create mode 100644 src/components/ContributionHeatmap.astro create mode 100644 src/types/stats.ts diff --git a/src/components/ContributionHeatmap.astro b/src/components/ContributionHeatmap.astro new file mode 100644 index 0000000..0676cda --- /dev/null +++ b/src/components/ContributionHeatmap.astro @@ -0,0 +1,282 @@ +--- +import type {HeatmapPoint} from "@/types/stats.ts"; + +interface Props { + points?: HeatmapPoint[] + /** IANA 时区,用于把时间戳归到某一天 */ + timeZone?: string +} + +const {points = [], timeZone = "Asia/Shanghai"} = Astro.props; + +const CELL = 11; +const GAP = 3; +const STEP = CELL + GAP; +const TOP = 20; +const LEFT = 26; +const DAY_MS = 86_400_000; + +type GridCell = { + key: string + x: number + y: number + value: number + level: number +} + +const dayFormatter = new Intl.DateTimeFormat("en-CA", { + timeZone, + year: "numeric", + month: "2-digit", + day: "2-digit" +}); +const monthFormatter = new Intl.DateTimeFormat("zh-CN", {timeZone: "UTC", month: "numeric"}); + +const toDayKey = (ms: number) => dayFormatter.format(new Date(ms)); +const toDayMs = (key: string) => { + const [year, month, day] = key.split("-").map(Number); + return Date.UTC(year, month - 1, day); +}; +const toDateText = (key: string) => key.replaceAll("-", "."); + +// 按天聚合:接口按分钟返回,同一天可能有多条 +const byDay = new Map(); +for (const {timestamp, contributions} of points) { + if (!Number.isFinite(timestamp) || !Number.isFinite(contributions)) continue; + const key = toDayKey(timestamp * 1000); + byDay.set(key, (byDay.get(key) ?? 0) + Math.max(0, contributions)); +} + +const days = [...byDay.keys()].sort(); + +const cells: GridCell[] = []; +const monthLabels: {x: number, label: string}[] = []; +const weekdayLabels = [ + {row: 0, label: "一"}, + {row: 2, label: "三"}, + {row: 4, label: "五"} +]; + +let weeks = 0; +let total = 0; +let firstDate = ""; +let lastDate = ""; + +const firstKey = days.at(0); + +if (firstKey) { + const todayKey = toDayKey(Date.now()); + const lastDataKey = days.at(-1) ?? firstKey; + // 末尾补到今天,保证最后一列是完整的自然周 + const endDayMs = Math.max(toDayMs(lastDataKey), toDayMs(todayKey)); + + const startMs = toDayMs(firstKey); + const start = startMs - ((new Date(startMs).getUTCDay() + 6) % 7) * DAY_MS; + const end = endDayMs + (6 - ((new Date(endDayMs).getUTCDay() + 6) % 7)) * DAY_MS; + weeks = Math.floor((end - start) / (7 * DAY_MS)) + 1; + + total = [...byDay.values()].reduce((sum, value) => sum + value, 0); + firstDate = firstKey; + lastDate = toDayKey(endDayMs); + + // 用非零数据的分位数划分 4 档,避免个别高峰压扁其余格子 + const nonZero = [...byDay.values()].filter((value) => value > 0).sort((a, b) => a - b); + const quantile = (p: number) => nonZero[Math.min(nonZero.length - 1, Math.floor(p * (nonZero.length - 1)))]; + const t1 = Math.max(1, quantile(0.4) ?? 1); + const t2 = Math.max(t1 + 1, quantile(0.7) ?? 2); + const t3 = Math.max(t2 + 1, quantile(0.9) ?? 3); + const levelOf = (value: number) => + value <= 0 ? 0 : value <= t1 ? 1 : value <= t2 ? 2 : value <= t3 ? 3 : 4; + + for (let time = start; time <= end; time += DAY_MS) { + const key = new Date(time).toISOString().slice(0, 10); + const week = Math.floor((time - start) / (7 * DAY_MS)); + const weekday = (new Date(time).getUTCDay() + 6) % 7; + const value = byDay.get(key) ?? 0; + + cells.push({ + key, + x: LEFT + week * STEP, + y: TOP + weekday * STEP, + value, + level: levelOf(value) + }); + } + + // 每月第一次出现的列打上月份标签 + let lastMonth = ""; + for (let week = 0; week < weeks; week++) { + const monday = new Date(start + week * 7 * DAY_MS); + const month = monthFormatter.format(monday); + if (month !== lastMonth) { + monthLabels.push({x: LEFT + week * STEP, label: month}); + lastMonth = month; + } + } +} + +const width = weeks > 0 ? LEFT + (weeks - 1) * STEP + CELL + 2 : 0; +const height = TOP + 7 * STEP - GAP + 2; +const levels = [0, 1, 2, 3, 4]; +--- + +{ + firstKey ? ( +
+
+ {toDateText(firstDate)} - {toDateText(lastDate)} + 共 {total} 次贡献 +
+ +
+ + {monthLabels.map(({x, label}) => ( + {label} + ))} + {weekdayLabels.map(({row, label}) => ( + {label} + ))} + {cells.map((cell) => ( + + {cell.key} · {cell.value > 0 ? `${cell.value} 次贡献` : "无贡献"} + + ))} + +
+ +
+ +
+
+ ) : ( +

暂无贡献数据

+ ) +} + + + + diff --git a/src/pages/embed/gitea.astro b/src/pages/embed/gitea.astro index 557b54f..a2ea62b 100644 --- a/src/pages/embed/gitea.astro +++ b/src/pages/embed/gitea.astro @@ -1,144 +1,254 @@ --- +import ContributionHeatmap from "@/components/ContributionHeatmap.astro"; +import { createHttp } from "@/lib/request.ts"; +import type { HeatmapPoint } from "@/types/stats.ts"; + const noindex = true; + +// 与接口 https://admin.ua42.com/api/stats/gitea/x/heatmap 对应 +const GITEA_USER = "x"; + +let heatmap: HeatmapPoint[] = []; + +try { + const { data } = await createHttp().get>( + `stats/gitea/${GITEA_USER}/heatmap`, + { + timeout: 5000, + retry: 0, + }, + ); + if (Array.isArray(data)) heatmap = data; +} catch (error) { + // 统计接口不可用时不影响入口页展示 + console.error("[embed/gitea] 热力图数据获取失败", error); +} + +const entries = [ + { label: "博客地址", desc: "ua42.com", href: "https://ua42.com" }, + { + label: "博客管理后台", + desc: "admin.ua42.com · 支持游客查看", + href: "https://admin.ua42.com", + }, + { + label: "仓库地址", + desc: "git.ua42.com/beggin", + href: "https://git.ua42.com/beggin", + }, + { + label: "博客服务端仓库", + desc: "git.ua42.com/beggin/blog-server", + href: "https://git.ua42.com/beggin/blog-server", + }, + { + label: "博客服务端 API 文档", + desc: "dev.ua42.com/docs", + href: "https://dev.ua42.com/docs/server-documentation.html", + }, + { + label: "Panda Dock", + desc: "github.com/forestRhapsody/panda-dock", + href: "https://github.com/forestRhapsody/panda-dock", + }, +]; --- - - - - - - Gitea 入口 - - - - - -
-
-
常用入口
-
-
- 博客地址 - 点击访问 -
+ .entry__desc { + overflow: hidden; + font-size: 12px; + color: var(--text-secondary); + white-space: nowrap; + text-overflow: ellipsis; + } -
- 博客管理后台(支持游客查看) - 点击访问 -
+ .entry__arrow { + position: absolute; + top: 14px; + right: 16px; + font-size: 13px; + line-height: 1; + color: var(--text-secondary); + transition: + color 0.15s ease, + transform 0.15s ease; + } -
- 仓库地址 - 点击访问 -
+ .entry:hover .entry__arrow { + color: var(--primary); + transform: translate(1px, -1px); + } -
- 博客服务端仓库 - 点击访问 -
+ .card { + width: 100%; + background: var(--card-bg); + border: 1px solid var(--border); + border-radius: 10px; + box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04); + overflow: hidden; + } -
- 博客服务端 API文档 - 点击访问 -
+ @media (max-width: 640px) { + .page-container { + gap: 20px; + padding: 16px; + } + } + + + +
+
+
+

常用入口

+
+
+ {entries.map(({ label, desc, href }) => ( + + + {desc} + + + ))} +
+
-
- Panda Dock - 点击访问 -
+ {heatmap.length > 0 && ( +
+
+

代码贡献

+
+
+ +
+
+ )}
-
-
- + - diff --git a/src/types/stats.ts b/src/types/stats.ts new file mode 100644 index 0000000..8817aef --- /dev/null +++ b/src/types/stats.ts @@ -0,0 +1,7 @@ +/** Gitea 贡献热力图接口返回的原始数据点 */ +export type HeatmapPoint = { + /** Unix 秒级时间戳 */ + timestamp: number + /** 该时间点产生的贡献次数 */ + contributions: number +}