8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / site / activity.go
3.0 KB
1package site 2 3import ( 4 "slices" 5 "strconv" 6 "strings" 7 "time" 8 9 "xonecas.io/site/internal/project" 10) 11 12// YearCount is one row of the activity page's year list. 13type YearCount struct { 14 Year string 15 Count, Heat int 16} 17 18// Day is one day of the activity feed, newest commit first. 19type Day struct { 20 Label string 21 Commits []Commit 22} 23 24// ActivityPage is one page of a year of my commits: the year list, its months, and a page of its commits by day. 25type ActivityPage struct { 26 Year string 27 Total, Page, Pages int 28 Years []YearCount 29 Months []Cell 30 Days []Day 31 Pager []PagerItem 32} 33 34// perPage is how many commits an activity page shows. 35const perPage = 100 36 37// Feed is my commits in logs, the logs of the projects in rows, newest first. 38func Feed(rows []ProjectRow, logs [][]LogEntry) []Commit { 39 var feed []Commit 40 for i, log := range logs { 41 for _, c := range log { 42 if project.Me[strings.ToLower(c.Author)] { 43 feed = append(feed, Commit{rows[i].El, c.Short, c.Subject, c.Author, c.Date, "/projects/" + rows[i].El.Name + "/commit/" + c.Hash + "/"}) 44 } 45 } 46 } 47 slices.SortStableFunc(feed, func(a, b Commit) int { return strings.Compare(b.Date, a.Date) }) 48 return feed 49} 50 51// NewActivityPage is page n, from 1, of year ("" for the latest) in feed, my commits across the shown projects. 52// Every year from the first commit to the last has pages, even an empty one. 53func NewActivityPage(shown []project.Project, feed []Commit, year string, n int) (ActivityPage, error) { 54 all, years := map[string]int{}, map[string]int{} 55 first, last := "9999", "0000" 56 for _, p := range shown { 57 for m, c := range p.Months { 58 all[m] += c 59 years[m[:4]] += c 60 } 61 first, last = min(first, p.First[:4]), max(last, p.Last[:4]) 62 } 63 if year == "" { 64 year = last 65 } 66 if year < first || year > last || len(year) != 4 { 67 return ActivityPage{}, ErrNotFound 68 } 69 var list []YearCount 70 lo, _ := strconv.Atoi(first) 71 hi, _ := strconv.Atoi(last) 72 for y := hi; y >= lo; y-- { 73 s := strconv.Itoa(y) 74 list = append(list, YearCount{s, years[s], level(years[s])}) 75 } 76 var mine []Commit 77 for _, c := range feed { 78 if strings.HasPrefix(c.Date, year) { 79 mine = append(mine, c) 80 } 81 } 82 pages := max(1, (len(mine)+perPage-1)/perPage) 83 if n < 1 || n > pages { 84 return ActivityPage{}, ErrNotFound 85 } 86 href := func(p int) string { 87 if p == 1 { 88 return "/activity/" + year + "/" 89 } 90 return "/activity/" + year + "/" + strconv.Itoa(p) + "/" 91 } 92 return ActivityPage{year, years[year], n, pages, list, months(year, all), days(mine[(n-1)*perPage : min(n*perPage, len(mine))]), Pager(n, pages, href)}, nil 93} 94 95// days groups commits, newest first, by the day they were made. 96func days(commits []Commit) []Day { 97 var out []Day 98 day := "" 99 for _, c := range commits { 100 if c.Date != day { 101 t, _ := time.Parse(time.DateOnly, c.Date) 102 out, day = append(out, Day{Label: t.Format("Mon 2 Jan")}), c.Date 103 } 104 c.Date = "" // the day's heading says it 105 out[len(out)-1].Commits = append(out[len(out)-1].Commits, c) 106 } 107 return out 108}