@@ -0,0 +1,111 @@
1+package site
2+
3+import (
4+ "fmt"
5+ "slices"
6+ "strconv"
7+ "time"
8+
9+ "xonecas.io/site/internal/project"
10+)
11+
12+// Cell is one month of commits on a heat wall.
13+type Cell struct {
14+ Month string // YYYY-MM
15+ Count, Heat int
16+}
17+
18+// WallYear is one row of the heat wall.
19+type WallYear struct {
20+ Year string
21+ Cells []Cell
22+}
23+
24+// Pinned is a project card on the home page, with its months of the latest year.
25+type Pinned struct {
26+ Row ProjectRow
27+ P project.Project
28+ Strip []Cell
29+}
30+
31+// HomeData is the home page: totals, the wall of every month, and the busiest projects of the latest year.
32+type HomeData struct {
33+ Commits, Projects, Languages int
34+ Busiest, Quietest, Year string
35+ Wall []WallYear
36+ Pinned []Pinned
37+ Tags []string
38+}
39+
40+// level is the heat of a commit count, 0 to 6.
41+func level(n int) (h int) {
42+ for _, t := range heat {
43+ if n >= t {
44+ h++
45+ }
46+ }
47+ return h
48+}
49+
50+// months is the cells of year from per-month counts.
51+func months(year string, counts map[string]int) []Cell {
52+ cells := make([]Cell, 12)
53+ for i := range cells {
54+ m := fmt.Sprintf("%s-%02d", year, i+1)
55+ cells[i] = Cell{m, counts[m], level(counts[m])}
56+ }
57+ return cells
58+}
59+
60+func homeData(shown []project.Project, rows []ProjectRow) HomeData {
61+ d := HomeData{Projects: len(shown), Tags: []string{"frontend", "backend", "small", "big", "pre-AI", "post-AI"}}
62+ all, langs, first, last := map[string]int{}, map[string]bool{}, "9999", "0000"
63+ for _, p := range shown {
64+ d.Commits += p.Commits
65+ for m, n := range p.Months {
66+ all[m] += n
67+ }
68+ if len(p.Languages) > 0 {
69+ langs[p.Languages[0].Name] = true
70+ }
71+ first, last = min(first, p.First[:4]), max(last, p.Last[:4])
72+ }
73+ d.Languages, d.Year = len(langs), last
74+ busiest, quiet, quietN := "", "", -1
75+ for y := mustAtoi(first); y <= mustAtoi(last); y++ {
76+ year := strconv.Itoa(y)
77+ row := WallYear{year, months(year, all)}
78+ total := 0
79+ for _, c := range row.Cells {
80+ total += c.Count
81+ if busiest == "" || c.Count > all[busiest] {
82+ busiest = c.Month
83+ }
84+ }
85+ if quietN < 0 || total < quietN {
86+ quiet, quietN = year, total
87+ }
88+ d.Wall = append(d.Wall, row)
89+ }
90+ t, _ := time.Parse("2006-01", busiest)
91+ d.Busiest = fmt.Sprintf("%s · %d", t.Format("Jan 2006"), all[busiest])
92+ d.Quietest = fmt.Sprintf("%s · %d", quiet, quietN)
93+
94+ for i, p := range shown {
95+ d.Pinned = append(d.Pinned, Pinned{rows[i], p, months(last, p.Months)})
96+ }
97+ inYear := func(p Pinned) (n int) {
98+ for _, c := range p.Strip {
99+ n += c.Count
100+ }
101+ return n
102+ }
103+ slices.SortStableFunc(d.Pinned, func(a, b Pinned) int { return inYear(b) - inYear(a) })
104+ d.Pinned = d.Pinned[:min(6, len(d.Pinned))]
105+ return d
106+}
107+
108+func mustAtoi(s string) int {
109+ n, _ := strconv.Atoi(s)
110+ return n
111+}