From aa28e5b264e00d6aa66f2afb743e493024dcf97f Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Mon, 30 Jan 2023 23:03:08 -0800 Subject: [PATCH] finish merging --- cmd/cli/cli.go | 9 ++- cmd/mgfetch/mgfetch.go | 2 +- cmd/server/svg-server.go | 22 +----- commits/cache.go | 22 +++--- commits/chancommits.go | 25 ++++--- commits/commits.go | 143 +-------------------------------------- commits/common.go | 1 + graph/svg/svg.go | 6 +- graph/term/term.go | 10 +-- types/helpers.go | 6 +- 10 files changed, 49 insertions(+), 197 deletions(-) diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index b17db3d..d21d5e8 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -9,13 +9,16 @@ import ( ) func main() { - wfreq := []int{} - yd := time.Now().YearDay() + n := time.Now() repoPaths, err := commits.GetMRRepos() if err != nil { panic(err) } - freq, err := repoPaths.GlobalFrequency(time.Now().Year(), []string{""}) + freq, err := repoPaths.Frequency(n.Year(), []string{""}) + if err != nil { + panic(err) + } + wfreq, err := repoPaths.GetWeekFreq([]string{""}) if err != nil { panic(err) } diff --git a/cmd/mgfetch/mgfetch.go b/cmd/mgfetch/mgfetch.go index ccd8ade..9a8a28b 100644 --- a/cmd/mgfetch/mgfetch.go +++ b/cmd/mgfetch/mgfetch.go @@ -14,7 +14,7 @@ type Repo git.Repository func main() { year := time.Now().Year() - 1 authors := []string{"Groot"} - gfreq, err := commits.GlobalFrequencyChan(year, authors) + gfreq, err := commits.FrequencyChan(year, authors) if err != nil { panic(err) } diff --git a/cmd/server/svg-server.go b/cmd/server/svg-server.go index f9fa634..d246f75 100644 --- a/cmd/server/svg-server.go +++ b/cmd/server/svg-server.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "net/http" "strconv" "time" @@ -19,33 +18,14 @@ func main() { r.HandleFunc("/weekly.svg", func(w http.ResponseWriter, r *http.Request) { author := r.URL.Query().Get("author") w.Header().Add("Content-Type", "text/html") - now := time.Now() - year := now.Year() repoPaths, err := commits.GetMRRepos() if err != nil { panic(err) } - freq, err := repoPaths.FrequencyChan(year, []string{author}) + week, err := repoPaths.GetWeekFreq([]string{author}) if err != nil { panic(err) } - today := now.YearDay() - 1 - fmt.Println(today) - if today < 6 { - curYear := year - 1 - curFreq, err := repoPaths.FrequencyChan(curYear, []string{author}) - if err != nil { - panic(err) - } - freq = append(curFreq, freq...) - today += 365 - if curYear%4 == 0 { - today++ - } - } - fmt.Println(freq) - - week := freq[today-6 : today+1] svg := svg.GetWeekSVG(week) svg.WriteTo(w) }) diff --git a/commits/cache.go b/commits/cache.go index 517a1f2..5aac841 100644 --- a/commits/cache.go +++ b/commits/cache.go @@ -13,11 +13,11 @@ import ( var ( mapTex sync.RWMutex - hashCache map[int]map[string]map[string]types.ExpYearFreq + hashCache map[int]map[string]map[string]types.ExpFreq ) func init() { - hashCache = make(map[int]map[string]map[string]types.ExpYearFreq) + hashCache = make(map[int]map[string]map[string]types.ExpFreq) } func hashSlice(in []string) string { @@ -32,22 +32,22 @@ func hashSlice(in []string) string { return fmt.Sprintf("%x\n", b) } -func GetCachedGraph(year int, authors []string, repoPaths []string) (types.YearFreq, bool) { +func GetCachedGraph(year int, authors []string, repoPaths []string) (types.Freq, bool) { a := hashSlice(authors) r := hashSlice(repoPaths) mapTex.RLock() defer mapTex.RUnlock() if m1, ok := hashCache[year]; !ok { - return types.YearFreq{}, false + return types.Freq{}, false } else { if m2, ok := m1[a]; !ok { - return types.YearFreq{}, false + return types.Freq{}, false } else { if freq, ok := m2[r]; !ok { - return types.YearFreq{}, false + return types.Freq{}, false } else { if freq.Created.Before(time.Now().Add(-15 * time.Minute)) { - return types.YearFreq{}, false + return types.Freq{}, false } else { return freq.YearFreq, true } @@ -56,18 +56,18 @@ func GetCachedGraph(year int, authors []string, repoPaths []string) (types.YearF } } -func CacheGraph(year int, authors, repoPaths []string, freq types.YearFreq) { +func CacheGraph(year int, authors, repoPaths []string, freq types.Freq) { a := hashSlice(authors) r := hashSlice(repoPaths) mapTex.Lock() defer mapTex.Unlock() if _, ok := hashCache[year]; !ok { - hashCache[year] = make(map[string]map[string]types.ExpYearFreq) + hashCache[year] = make(map[string]map[string]types.ExpFreq) } if _, ok := hashCache[year][a]; !ok { - hashCache[year][a] = make(map[string]types.ExpYearFreq) + hashCache[year][a] = make(map[string]types.ExpFreq) } - hashCache[year][a][r] = types.ExpYearFreq{YearFreq: freq, Created: time.Now()} + hashCache[year][a][r] = types.ExpFreq{YearFreq: freq, Created: time.Now()} go func() { time.Sleep(time.Minute * 15) mapTex.Lock() diff --git a/commits/chancommits.go b/commits/chancommits.go index c4878ab..f1271e0 100644 --- a/commits/chancommits.go +++ b/commits/chancommits.go @@ -8,19 +8,13 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" "github.com/taigrr/gico/types" - "github.com/taigrr/mg/parse" ) -func GlobalFrequencyChan(year int, authors []string) (types.YearFreq, error) { +func (paths RepoSet) FrequencyChan(year int, authors []string) (types.Freq, error) { yearLength := 365 if year%4 == 0 { yearLength++ } - mrconf, err := parse.LoadMRConfig() - if err != nil { - return types.YearFreq{}, err - } - paths := mrconf.GetRepoPaths() cache, ok := GetCachedGraph(year, authors, paths) if ok { return cache, nil @@ -58,6 +52,21 @@ func GlobalFrequencyChan(year int, authors []string) (types.YearFreq, error) { return freq, nil } +func YearFreqFromChan(cc chan types.Commit, year int) types.Freq { + yearLength := 365 + if year%4 == 0 { + yearLength++ + } + freq := make([]int, yearLength) + for commit := range cc { + if commit.TimeStamp.Year() != year { + continue + } + freq[commit.TimeStamp.YearDay()-1]++ + } + return freq +} + func (repo Repo) GetCommitChan() (chan types.Commit, error) { cc := make(chan types.Commit, 30) r := git.Repository(repo) @@ -81,7 +90,7 @@ func (repo Repo) GetCommitChan() (chan types.Commit, error) { return cc, nil } -func YearFreqFromChan(cc chan types.Commit, year int) types.YearFreq { +func FreqFromChan(cc chan types.Commit, year int) types.Freq { yearLength := 365 if year%4 == 0 { yearLength++ diff --git a/commits/commits.go b/commits/commits.go index a2b71a6..dd2a8d2 100644 --- a/commits/commits.go +++ b/commits/commits.go @@ -1,99 +1,15 @@ package commits import ( - "crypto/md5" - "fmt" "regexp" - "sort" - "strings" - "sync" "time" git "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" "github.com/taigrr/gico/types" - "github.com/taigrr/mg/parse" ) -type Repo git.Repository - -type RepoSet []string - -var ( - mapTex sync.RWMutex - hashCache map[int]map[string]map[string]types.ExpFreq -) - -func init() { - hashCache = make(map[int]map[string]map[string]types.ExpFreq) -} - -func hashSlice(in []string) string { - sort.Strings(in) - sb := strings.Builder{} - for _, s := range in { - sb.WriteString(s) - } - h := md5.New() - h.Write([]byte(sb.String())) - b := h.Sum(nil) - return fmt.Sprintf("%x\n", b) -} - -func GetCachedGraph(year int, authors []string, repoPaths []string) (types.Freq, bool) { - a := hashSlice(authors) - r := hashSlice(repoPaths) - mapTex.RLock() - defer mapTex.RUnlock() - if m1, ok := hashCache[year]; !ok { - return types.Freq{}, false - } else { - if m2, ok := m1[a]; !ok { - return types.Freq{}, false - } else { - if freq, ok := m2[r]; !ok { - return types.Freq{}, false - } else { - if freq.Created.Before(time.Now().Add(-15 * time.Minute)) { - return types.Freq{}, false - } else { - return freq.YearFreq, true - } - } - } - } -} - -func CacheGraph(year int, authors, repoPaths []string, freq types.Freq) { - a := hashSlice(authors) - r := hashSlice(repoPaths) - mapTex.Lock() - defer mapTex.Unlock() - if _, ok := hashCache[year]; !ok { - hashCache[year] = make(map[string]map[string]types.ExpFreq) - } - if _, ok := hashCache[year][a]; !ok { - hashCache[year][a] = make(map[string]types.ExpFreq) - } - hashCache[year][a][r] = types.ExpFreq{YearFreq: freq, Created: time.Now()} - go func() { - time.Sleep(time.Minute * 15) - mapTex.Lock() - defer mapTex.Unlock() - delete(hashCache[year][a], r) - }() -} - -func GetMRRepos() (RepoSet, error) { - mrconf, err := parse.LoadMRConfig() - if err != nil { - return RepoSet{}, err - } - paths := mrconf.GetRepoPaths() - return RepoSet(paths), nil -} - func (paths RepoSet) GetWeekFreq(authors []string) (types.Freq, error) { now := time.Now() year := now.Year() @@ -119,64 +35,7 @@ func (paths RepoSet) GetWeekFreq(authors []string) (types.Freq, error) { return week, nil } -func (paths RepoSet) FrequencyChan(year int, authors []string) (types.Freq, error) { - yearLength := 365 - if year%4 == 0 { - yearLength++ - } - cache, ok := GetCachedGraph(year, authors, paths) - if ok { - return cache, nil - } - outChan := make(chan types.Commit, 10) - var wg sync.WaitGroup - for _, p := range paths { - wg.Add(1) - go func(path string) { - repo, err := OpenRepo(path) - if err != nil { - return - } - cc, err := repo.GetCommitChan() - if err != nil { - return - } - cc = FilterCChanByYear(cc, year) - cc, err = FilterCChanByAuthor(cc, authors) - if err != nil { - return - } - for c := range cc { - outChan <- c - } - wg.Done() - }(p) - } - go func() { - wg.Wait() - close(outChan) - }() - freq := YearFreqFromChan(outChan, year) - CacheGraph(year, authors, paths, freq) - return freq, nil -} - -func YearFreqFromChan(cc chan types.Commit, year int) types.Freq { - yearLength := 365 - if year%4 == 0 { - yearLength++ - } - freq := make([]int, yearLength) - for commit := range cc { - if commit.TimeStamp.Year() != year { - continue - } - freq[commit.TimeStamp.YearDay()-1]++ - } - return freq -} - -func (paths RepoSet) GlobalFrequency(year int, authors []string) (types.Freq, error) { +func (paths RepoSet) Frequency(year int, authors []string) (types.Freq, error) { yearLength := 365 if year%4 == 0 { yearLength++ diff --git a/commits/common.go b/commits/common.go index 37a6b17..765d618 100644 --- a/commits/common.go +++ b/commits/common.go @@ -16,6 +16,7 @@ type ( Commits []types.Commit Year int } + RepoSet []string ) func OpenRepo(directory string) (Repo, error) { diff --git a/graph/svg/svg.go b/graph/svg/svg.go index f5978a4..3ef5362 100644 --- a/graph/svg/svg.go +++ b/graph/svg/svg.go @@ -54,10 +54,10 @@ func GetYearSVG(frequencies []int) bytes.Buffer { for _, f := range frequencies { squareColors = append(squareColors, common.ColorForFrequency(f, min, max)) } - return drawYearImage(squareColors) + return drawYearImage(squareColors, frequencies) } -func drawYearImage(c []sc.SimpleColor) bytes.Buffer { +func drawYearImage(c []sc.SimpleColor, freq []int) bytes.Buffer { var sb bytes.Buffer sbw := bufio.NewWriter(&sb) squareLength := 10 @@ -66,7 +66,7 @@ func drawYearImage(c []sc.SimpleColor) bytes.Buffer { canvas := svg.New(sbw) canvas.Start(width, height) for i, s := range c { - canvas.Square(2*squareLength+width/(len(c)/7+1)*(i/7)+squareLength*2, squareLength/2+height/7*(i%7), squareLength, fmt.Sprintf("fill:%s", s.ToHex())) + 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, int(float64(squareLength)*6.5), "Wed", fmt.Sprintf("text-anchor:middle;font-size:%dpx;fill:black", squareLength)) diff --git a/graph/term/term.go b/graph/term/term.go index c31f57f..87d73ef 100644 --- a/graph/term/term.go +++ b/graph/term/term.go @@ -55,14 +55,14 @@ func drawYearUnicode(c []sc.SimpleColor) string { // o := termenv.NewOutput(os.Stdout) var s strings.Builder o := termenv.NewOutputWithProfile(os.Stdout, termenv.TrueColor) - weeks := [7][]sc.SimpleColor{{}} + weekRows := [7][]sc.SimpleColor{{}} for i := 0; i < 7; i++ { - weeks[i] = []sc.SimpleColor{} + weekRows[i] = []sc.SimpleColor{} } - for i := range c { - weeks[i%7] = append(weeks[i%7], c[i]) + for i := 0; i < len(c); i++ { + weekRows[i%7] = append(weekRows[i%7], c[i]) } - for _, row := range weeks { + for _, row := range weekRows { for w, d := range row { style := o.String(block).Foreground(termenv.TrueColor.Color(d.ToHex())) s.WriteString(style.String()) diff --git a/types/helpers.go b/types/helpers.go index 903a7af..6332982 100644 --- a/types/helpers.go +++ b/types/helpers.go @@ -18,17 +18,17 @@ func NewCommit(Author, Message, Repo, Path string, LOC int) Commit { } } -func (yf YearFreq) String() string { +func (yf Freq) String() string { return gterm.GetYearUnicode(yf) } -func (a YearFreq) Merge(b YearFreq) YearFreq { +func (a Freq) Merge(b Freq) Freq { x := len(a) y := len(b) if x < y { x = y } - c := make(YearFreq, x) + c := make(Freq, x) copy(c, a) for i := 0; i < y; i++ { c[i] += b[i]