xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / site / home.go

2.9 KB
1package site
2
3import (
4	"cmp"
5	"fmt"
6	"slices"
7	"strconv"
8	"strings"
9	"time"
10
11	"xonecas.io/site/internal/project"
12)
13
14// Cell is one month of commits on a heat wall.
15type Cell struct {
16	Month       string // YYYY-MM
17	Count, Heat int
18}
19
20// WallYear is one row of the heat wall.
21type WallYear struct {
22	Year  string
23	Cells []Cell
24}
25
26// Recent is a project card on the home page, with its months of the latest year.
27type Recent struct {
28	Row   ProjectRow
29	P     project.Project
30	Strip []Cell
31}
32
33// HomeData is the home page: totals, the wall of every month, and the projects with the latest commits.
34type HomeData struct {
35	Commits, Projects int
36	Busiest, Quietest string
37	Wall              []WallYear
38	Recent            []Recent
39	Tags              []string
40	Topics            []TagCount // used by two projects or more, commonest first
41}
42
43// level is the heat of a commit count, 0 to 6.
44func level(n int) (h int) {
45	for _, t := range heat {
46		if n >= t {
47			h++
48		}
49	}
50	return h
51}
52
53// months is the cells of year from per-month counts.
54func months(year string, counts map[string]int) []Cell {
55	cells := make([]Cell, 12)
56	for i := range cells {
57		m := fmt.Sprintf("%s-%02d", year, i+1)
58		cells[i] = Cell{m, counts[m], level(counts[m])}
59	}
60	return cells
61}
62
63// NewHomeData is the home page of the shown projects, whose rows are rows.
64func NewHomeData(shown []project.Project, rows []ProjectRow) HomeData {
65	d := HomeData{Projects: len(shown), Tags: []string{"frontend", "backend", "small", "big", "pre-AI", "post-AI"}}
66	all, first, last := map[string]int{}, "9999", "0000"
67	for _, p := range shown {
68		d.Commits += p.Commits
69		for m, n := range p.Months {
70			all[m] += n
71		}
72		first, last = min(first, p.First[:4]), max(last, p.Last[:4])
73	}
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.Recent = append(d.Recent, Recent{rows[i], p, months(last, p.Months)})
96	}
97	slices.SortStableFunc(d.Recent, func(a, b Recent) int { return strings.Compare(b.P.Last, a.P.Last) })
98	d.Recent = d.Recent[:min(12, len(d.Recent))]
99
100	counts := map[string]int{}
101	for _, p := range shown {
102		for _, t := range p.Topics {
103			counts[t]++
104		}
105	}
106	for t, n := range counts {
107		if n >= 2 {
108			d.Topics = append(d.Topics, TagCount{t, n})
109		}
110	}
111	slices.SortFunc(d.Topics, func(a, b TagCount) int { return cmp.Or(b.Count-a.Count, strings.Compare(a.Name, b.Name)) })
112	return d
113}
114
115func mustAtoi(s string) int {
116	n, _ := strconv.Atoi(s)
117	return n
118}