8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / site / projects.go
5.2 KB
1package site 2 3import ( 4 "fmt" 5 "net/url" 6 "slices" 7 "strconv" 8 "strings" 9) 10 11// PerList is how many projects one page of /projects lists. 12const PerList = 20 13 14// Chip is one tag filter on /projects, with how many shown projects have the tag. 15type Chip struct { 16 Name string 17 Count int 18 On bool 19} 20 21// Bar is one month under the date range, with how many projects were active in it. 22type Bar struct { 23 Month string 24 Count int 25 Height float64 // percent of the busiest month 26 On bool // between the range's thumbs 27} 28 29// ProjectsPage is /projects for one query: the matches on one page, and the filters that picked them. 30type ProjectsPage struct { 31 Rows []ProjectRow 32 Total, Matches int 33 Pairs [][]Chip 34 Sort, Q string 35 Bars []Bar 36 From, To int // the range's thumbs, as indexes into Bars 37 Span string // the months the thumbs are on 38 Pager []PagerItem 39 URL string // this view's address, with the defaults left out 40} 41 42var sorts = map[string]func(a, b ProjectRow) int{ 43 "recent": func(a, b ProjectRow) int { return strings.Compare(b.Last, a.Last) }, 44 "oldest": func(a, b ProjectRow) int { return strings.Compare(a.First, b.First) }, 45 "commits": func(a, b ProjectRow) int { return b.El.Weight - a.El.Weight }, 46} 47 48// NewProjectsPage filters, sorts and pages rows by the query: tag (any number), sort, q, from, to and page. 49// Tags from different pairs must all match, and tags of the same pair match either. q matches a name in part, 50// or a topic whole. from and to are months, YYYY-MM or an index from the range, and keep a project with any 51// commit between them. A page past the end shows the last one. 52func NewProjectsPage(rows []ProjectRow, q url.Values) ProjectsPage { 53 d := ProjectsPage{Total: len(rows), Sort: q.Get("sort"), Q: strings.ToLower(strings.TrimSpace(q.Get("q")))} 54 if sorts[d.Sort] == nil { 55 d.Sort = "recent" 56 } 57 d.Pairs = [][]Chip{{{Name: "frontend"}, {Name: "backend"}}, {{Name: "small"}, {Name: "big"}}, {{Name: "pre-AI"}, {Name: "post-AI"}}} 58 for _, pair := range d.Pairs { 59 for i := range pair { 60 pair[i].On = slices.Contains(q["tag"], pair[i].Name) 61 for _, r := range rows { 62 if slices.Contains(r.Tags, pair[i].Name) { 63 pair[i].Count++ 64 } 65 } 66 } 67 } 68 d.Bars = bars(rows) 69 d.From, d.To = thumb(q.Get("from"), d.Bars, 0), thumb(q.Get("to"), d.Bars, len(d.Bars)-1) 70 d.From, d.To = min(d.From, d.To), max(d.From, d.To) 71 from, to := "", "9999" 72 if len(d.Bars) > 0 { 73 from, to = d.Bars[d.From].Month, d.Bars[d.To].Month 74 d.Span = from + " – " + to 75 } 76 for i := range d.Bars { 77 d.Bars[i].On = i >= d.From && i <= d.To 78 } 79 80 var matches []ProjectRow 81 for _, r := range rows { 82 if (strings.Contains(strings.ToLower(r.El.Name), d.Q) || slices.Contains(r.Topics, d.Q)) && d.tagged(r) && 83 slices.ContainsFunc(strings.Fields(r.Months), func(m string) bool { return m >= from && m <= to }) { 84 matches = append(matches, r) 85 } 86 } 87 slices.SortStableFunc(matches, sorts["recent"]) 88 slices.SortStableFunc(matches, sorts[d.Sort]) 89 d.Matches = len(matches) 90 91 v := url.Values{} 92 for _, pair := range d.Pairs { 93 for _, c := range pair { 94 if c.On { 95 v.Add("tag", c.Name) 96 } 97 } 98 } 99 if d.Sort != "recent" { 100 v.Set("sort", d.Sort) 101 } 102 if d.Q != "" { 103 v.Set("q", d.Q) 104 } 105 if d.From > 0 { 106 v.Set("from", from) 107 } 108 if d.To < len(d.Bars)-1 { 109 v.Set("to", to) 110 } 111 href := func(n int) string { 112 p := url.Values{} 113 for k, vs := range v { 114 p[k] = vs 115 } 116 if n > 1 { 117 p.Set("page", strconv.Itoa(n)) 118 } 119 if len(p) == 0 { 120 return "/projects/" 121 } 122 return "/projects/?" + p.Encode() 123 } 124 pages := max(1, (len(matches)+PerList-1)/PerList) 125 page, _ := strconv.Atoi(q.Get("page")) 126 page = min(max(page, 1), pages) 127 d.Rows = matches[(page-1)*PerList : min(len(matches), page*PerList)] 128 d.Pager, d.URL = Pager(page, pages, href), href(page) 129 return d 130} 131 132// tagged says whether r has a tag from every pair that has one on. 133func (d ProjectsPage) tagged(r ProjectRow) bool { 134 for _, pair := range d.Pairs { 135 on := slices.ContainsFunc(pair, func(c Chip) bool { return c.On }) 136 if on && !slices.ContainsFunc(pair, func(c Chip) bool { return c.On && slices.Contains(r.Tags, c.Name) }) { 137 return false 138 } 139 } 140 return true 141} 142 143// bars is every month from the first commit in rows to the last, with how many of rows have commits in it. 144func bars(rows []ProjectRow) []Bar { 145 if len(rows) == 0 { 146 return nil 147 } 148 lo, hi := rows[0].First, rows[0].Last 149 for _, r := range rows { 150 lo, hi = min(lo, r.First), max(hi, r.Last) 151 } 152 var list []Bar 153 peak := 1 154 for y, m := mustAtoi(lo[:4]), mustAtoi(lo[5:7]); fmt.Sprintf("%d-%02d", y, m) <= hi[:7]; y, m = y+(m/12), m%12+1 { 155 b := Bar{Month: fmt.Sprintf("%d-%02d", y, m)} 156 for _, r := range rows { 157 if strings.Contains(r.Months, b.Month) { 158 b.Count++ 159 } 160 } 161 peak, list = max(peak, b.Count), append(list, b) 162 } 163 for i := range list { 164 list[i].Height = float64(100*list[i].Count) / float64(peak) 165 } 166 return list 167} 168 169// thumb is the index of the month v, given as YYYY-MM or as an index, in bars, or def. 170func thumb(v string, bars []Bar, def int) int { 171 if i := slices.IndexFunc(bars, func(b Bar) bool { return b.Month == v }); i >= 0 { 172 return i 173 } 174 if i, err := strconv.Atoi(v); err == nil && i >= 0 && i < len(bars) { 175 return i 176 } 177 return max(def, 0) 178}