From 5d6ca69a2b0d11fb7cd5311781f44f465d15f256 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 14 Feb 2023 22:51:54 -0800 Subject: [PATCH] prepare for git commit chan aggregation --- cmd/cli/cli.go | 11 +++++----- commits/cache.go | 47 ++++++++++++++++++++++++++++-------------- commits/chancommits.go | 16 ++++++++++++-- commits/commits.go | 2 +- types/types.go | 5 +++++ 5 files changed, 57 insertions(+), 24 deletions(-) diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 4d80024..f5f3f0a 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -29,7 +29,7 @@ type model struct { type CommitLog struct { Year int YearDay int - Commits []types.Commit + Commits [][]types.Commit Selected int Authors []string Repos []string @@ -70,7 +70,7 @@ func initialModel() (model, error) { if err != nil { return m, err } - m.CommitLogModel, err := NewCommitLog() + m.CommitLogModel, err = NewCommitLog() if err != nil { return m, err } @@ -172,6 +172,7 @@ func (m Graph) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } return m, nil } + func NewCommitLog() (CommitLog, error) { var m CommitLog now := time.Now() @@ -188,8 +189,8 @@ func NewCommitLog() (CommitLog, error) { m.Authors = authors m.Year = year m.Selected = today - m.Commits = mr.GetDayCommits(m.Year, m.Selected, m.Authors) - return m, nil + m.Commits, err = mr.GetRepoCommits(m.Year, m.Authors) + return m, err } func NewGraph() (Graph, error) { @@ -249,7 +250,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { default: } switch m.cursor { - // multiple cursors defined for extensibility, but only graph is used + // multiple cursors defined for extensibility, but only graph is used case graph, commitLog: tmp, _ := m.GraphModel.Update(msg) m.GraphModel, _ = tmp.(Graph) diff --git a/commits/cache.go b/commits/cache.go index e6ec48e..ff95a6e 100644 --- a/commits/cache.go +++ b/commits/cache.go @@ -12,8 +12,9 @@ import ( ) var ( - mapTex sync.RWMutex - hashCache map[int]map[string]map[string]types.ExpFreq + mapTex sync.RWMutex + freqHashCache map[int]map[string]map[string]types.ExpFreq + repoHashCache map[int]map[string]map[string]types.ExpRepo // the Repo Cache holds a list of all commits from HEAD back to parent // the key is the repo path // if the hash of the first commit / HEAD commit doesn't match the current HEAD, @@ -22,16 +23,10 @@ var ( ) func init() { - hashCache = make(map[int]map[string]map[string]types.ExpFreq) + freqHashCache = make(map[int]map[string]map[string]types.ExpFreq) repoCache = make(map[string][]types.Commit) } -func CacheRepo(path string, commits []types.Commit) { - mapTex.Lock() - defer mapTex.Unlock() - repoCache[path] = commits -} - func GetCachedRepo(path string, head string) ([]types.Commit, bool) { mapTex.RLock() defer mapTex.RUnlock() @@ -70,7 +65,7 @@ func GetCachedGraph(year int, authors []string, repoPaths []string) (types.Freq, r := hashSlice(repoPaths) mapTex.RLock() defer mapTex.RUnlock() - if m1, ok := hashCache[year]; !ok { + if m1, ok := freqHashCache[year]; !ok { return types.Freq{}, false } else { if m2, ok := m1[a]; !ok { @@ -89,22 +84,42 @@ func GetCachedGraph(year int, authors []string, repoPaths []string) (types.Freq, } } +func CacheRepo(year int, authors, repoPaths []string, commits []types.Commit) { + a := hashSlice(authors) + r := hashSlice(repoPaths) + mapTex.Lock() + defer mapTex.Unlock() + if _, ok := repoHashCache[year]; !ok { + repoHashCache[year] = make(map[string]map[string]types.ExpRepo) + } + if _, ok := repoHashCache[year][a]; !ok { + repoHashCache[year][a] = make(map[string]types.ExpRepo) + } + repoHashCache[year][a][r] = types.ExpRepo{Commits: commits, Created: time.Now()} + go func() { + time.Sleep(time.Hour * 1) + mapTex.Lock() + defer mapTex.Unlock() + delete(repoHashCache[year][a], r) + }() +} + 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 := freqHashCache[year]; !ok { + freqHashCache[year] = make(map[string]map[string]types.ExpFreq) } - if _, ok := hashCache[year][a]; !ok { - hashCache[year][a] = make(map[string]types.ExpFreq) + if _, ok := freqHashCache[year][a]; !ok { + freqHashCache[year][a] = make(map[string]types.ExpFreq) } - hashCache[year][a][r] = types.ExpFreq{YearFreq: freq, Created: time.Now()} + freqHashCache[year][a][r] = types.ExpFreq{YearFreq: freq, Created: time.Now()} go func() { time.Sleep(time.Hour * 1) mapTex.Lock() defer mapTex.Unlock() - delete(hashCache[year][a], r) + delete(freqHashCache[year][a], r) }() } diff --git a/commits/chancommits.go b/commits/chancommits.go index ea5ce44..8cc645e 100644 --- a/commits/chancommits.go +++ b/commits/chancommits.go @@ -10,7 +10,7 @@ import ( "github.com/taigrr/gico/types" ) -func (paths RepoSet) GetDayCommits(year int, authors []string) ([]types.Commit, error) { +func (paths RepoSet) GetRepoCommits(year int, authors []string) ([][]types.Commit, error) { yearLength := 365 if year%4 == 0 { yearLength++ @@ -53,7 +53,7 @@ func (paths RepoSet) GetDayCommits(year int, authors []string) ([]types.Commit, close(outChan) }() freq := YearFreqFromChan(outChan, year) - // CacheGraph(year, authors, paths, freq) + CacheGraph(year, authors, paths, freq) return freq, nil } @@ -94,6 +94,18 @@ func (paths RepoSet) FrequencyChan(year int, authors []string) (types.Freq, erro wg.Wait() close(outChan) }() + yearLength := 365 + if year%4 == 0 { + yearLength++ + } + freq := make([][]types.Commit, yearLength) + for commit := range cc { + freq[commit.TimeStamp.YearDay()-1]++ + } + return freq + + for commit := range outChan { + } freq := YearFreqFromChan(outChan, year) CacheGraph(year, authors, paths, freq) return freq, nil diff --git a/commits/commits.go b/commits/commits.go index e15bd91..b99327e 100644 --- a/commits/commits.go +++ b/commits/commits.go @@ -107,7 +107,7 @@ func (repo Repo) GetCommitSet() (CommitSet, error) { return nil }) cs.Commits = commits - CacheRepo(repo.Path, cs.Commits) + // CacheRepo(repo.Path, cs.Commits) return cs, nil } diff --git a/types/types.go b/types/types.go index 0f9dbc8..5bf9700 100644 --- a/types/types.go +++ b/types/types.go @@ -23,6 +23,11 @@ type ( YearFreq Freq Created time.Time } + + ExpRepo struct { + Commits []Commit + Created time.Time + } WorkDay struct { Day time.Time `json:"day"` Count int `json:"count"`