add red square for failure to commit today

This commit is contained in:
2023-02-02 22:00:24 -08:00
parent dc1d43e69e
commit ada61387f2
2 changed files with 28 additions and 8 deletions

View File

@@ -18,6 +18,9 @@ func main() {
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/weekly.svg", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/weekly.svg", func(w http.ResponseWriter, r *http.Request) {
author := r.URL.Query().Get("author") author := r.URL.Query().Get("author")
highlight := r.URL.Query().Get("highlight")
shouldHighlight := highlight != ""
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
repoPaths, err := commits.GetMRRepos() repoPaths, err := commits.GetMRRepos()
if err != nil { if err != nil {
@@ -27,7 +30,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
svg := svg.GetWeekSVG(week) svg := svg.GetWeekSVG(week, shouldHighlight)
svg.WriteTo(w) svg.WriteTo(w)
}) })
r.HandleFunc("/stats.json", func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/stats.json", func(w http.ResponseWriter, r *http.Request) {
@@ -54,8 +57,13 @@ func main() {
year := time.Now().Year() year := time.Now().Year()
yst := r.URL.Query().Get("year") yst := r.URL.Query().Get("year")
author := r.URL.Query().Get("author") author := r.URL.Query().Get("author")
highlight := r.URL.Query().Get("highlight")
shouldHighlight := highlight != ""
y, err := strconv.Atoi(yst) y, err := strconv.Atoi(yst)
if err == nil { if err == nil {
if year != y {
shouldHighlight = false
}
year = y year = y
} }
repoPaths, err := commits.GetMRRepos() repoPaths, err := commits.GetMRRepos()
@@ -66,7 +74,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
svg := svg.GetYearSVG(freq) svg := svg.GetYearSVG(freq, shouldHighlight)
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
svg.WriteTo(w) svg.WriteTo(w)
}) })

View File

@@ -8,6 +8,7 @@ import (
"image/png" "image/png"
"os" "os"
"sync" "sync"
"time"
svg "github.com/ajstarks/svgo" svg "github.com/ajstarks/svgo"
"github.com/srwiley/oksvg" "github.com/srwiley/oksvg"
@@ -22,16 +23,16 @@ var (
colorScheme []sc.SimpleColor colorScheme []sc.SimpleColor
) )
func GetWeekSVG(frequencies []int) bytes.Buffer { func GetWeekSVG(frequencies []int, shouldHighlight bool) bytes.Buffer {
squareColors := []sc.SimpleColor{} squareColors := []sc.SimpleColor{}
min, max := common.MinMax(frequencies) min, max := common.MinMax(frequencies)
for _, f := range frequencies { for _, f := range frequencies {
squareColors = append(squareColors, common.ColorForFrequency(f, min, max)) squareColors = append(squareColors, common.ColorForFrequency(f, min, max))
} }
return drawWeekImage(squareColors, frequencies) return drawWeekImage(squareColors, frequencies, shouldHighlight)
} }
func drawWeekImage(c []sc.SimpleColor, freq []int) bytes.Buffer { func drawWeekImage(c []sc.SimpleColor, freq []int, shouldHighlight bool) bytes.Buffer {
var sb bytes.Buffer var sb bytes.Buffer
sbw := bufio.NewWriter(&sb) sbw := bufio.NewWriter(&sb)
squareLength := 10 squareLength := 10
@@ -41,6 +42,11 @@ func drawWeekImage(c []sc.SimpleColor, freq []int) bytes.Buffer {
canvas.Start(width, height) canvas.Start(width, height)
canvas.Rect(0, 0, width, height, "fill:black") canvas.Rect(0, 0, width, height, "fill:black")
for i, s := range c { for i, s := range c {
if shouldHighlight && i == len(c)-1 {
if freq[i] == 0 {
s = sc.FromHexString("#FF0000")
}
}
canvas.Square(squareLength*2*(i+1), squareLength/2, squareLength, fmt.Sprintf("fill:%s; value:%d", s.ToHex(), freq[i])) canvas.Square(squareLength*2*(i+1), squareLength/2, squareLength, fmt.Sprintf("fill:%s; value:%d", s.ToHex(), freq[i]))
} }
canvas.End() canvas.End()
@@ -48,17 +54,18 @@ func drawWeekImage(c []sc.SimpleColor, freq []int) bytes.Buffer {
return sb return sb
} }
func GetYearSVG(frequencies []int) bytes.Buffer { func GetYearSVG(frequencies []int, shouldHighlight bool) bytes.Buffer {
squareColors := []sc.SimpleColor{} squareColors := []sc.SimpleColor{}
min, max := common.MinMax(frequencies) min, max := common.MinMax(frequencies)
for _, f := range frequencies { for _, f := range frequencies {
squareColors = append(squareColors, common.ColorForFrequency(f, min, max)) squareColors = append(squareColors, common.ColorForFrequency(f, min, max))
} }
return drawYearImage(squareColors, frequencies) return drawYearImage(squareColors, frequencies, shouldHighlight)
} }
func drawYearImage(c []sc.SimpleColor, freq []int) bytes.Buffer { func drawYearImage(c []sc.SimpleColor, freq []int, shouldHighlight bool) bytes.Buffer {
var sb bytes.Buffer var sb bytes.Buffer
now := time.Now()
sbw := bufio.NewWriter(&sb) sbw := bufio.NewWriter(&sb)
squareLength := 10 squareLength := 10
width := (len(c)/7+1)*squareLength*2 + squareLength*5 width := (len(c)/7+1)*squareLength*2 + squareLength*5
@@ -66,6 +73,11 @@ func drawYearImage(c []sc.SimpleColor, freq []int) bytes.Buffer {
canvas := svg.New(sbw) canvas := svg.New(sbw)
canvas.Start(width, height) canvas.Start(width, height)
for i, s := range c { for i, s := range c {
if shouldHighlight && i == now.YearDay()-1 {
if freq[i] == 0 {
s = sc.FromHexString("#FF0000")
}
}
canvas.Square(2*squareLength+width/(len(c)/7+1)*(i/7)+squareLength*2, squareLength/2+height/7*(i%7), squareLength, fmt.Sprintf("fill:%s; value:%d", s.ToHex(), freq[i])) canvas.Square(2*squareLength+width/(len(c)/7+1)*(i/7)+squareLength*2, squareLength/2+height/7*(i%7), squareLength, fmt.Sprintf("fill:%s; value:%d", s.ToHex(), freq[i]))
} }
// canvas.Text(2*squareLength, squareLength*3, "Mon", fmt.Sprintf("text-anchor:middle;font-size:%dpx;fill:black", squareLength)) // canvas.Text(2*squareLength, squareLength*3, "Mon", fmt.Sprintf("text-anchor:middle;font-size:%dpx;fill:black", squareLength))