diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index f5f3f0a..f079302 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -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 } @@ -93,7 +93,7 @@ func (m Settings) Init() tea.Cmd { } func (m Settings) View() string { - return fmt.Sprintf("This is the settings view") + return fmt.Sprintf("This is the settings view %s", "fmt") } func (m CommitLog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { @@ -108,6 +108,8 @@ func (m CommitLog) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.Selected > 0 { m.Selected-- } + default: + // m.Commits = commits.CommitSet } } return m, nil @@ -118,7 +120,11 @@ func (m CommitLog) Init() tea.Cmd { } func (m CommitLog) View() string { - return fmt.Sprintf("This is the Commit Log, selected %v", m) + if len(m.Commits) == 0 { + return "No commits to display" + } + return fmt.Sprintf("%v", m.Commits[m.YearDay]) + // return fmt.Sprintf("This is the Commit Log, selected %v", "sd") } func YearLen(year int) int { @@ -256,6 +262,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.GraphModel, _ = tmp.(Graph) tmpC, cmd := m.CommitLogModel.Update(msg) m.CommitLogModel, _ = tmpC.(CommitLog) + m.CommitLogModel.YearDay = m.GraphModel.Selected return m, cmd case settings: tmp, cmd := m.SettingsModel.Update(msg) diff --git a/commits/cache.go b/commits/cache.go index ff95a6e..9dfa57f 100644 --- a/commits/cache.go +++ b/commits/cache.go @@ -14,38 +14,18 @@ import ( var ( mapTex sync.RWMutex freqHashCache map[int]map[string]map[string]types.ExpFreq - repoHashCache map[int]map[string]map[string]types.ExpRepo + repoHashCache map[int]map[string]map[string]types.ExpRepos + repoCache = make(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, // then it can be discarded and reloaded - repoCache map[string][]types.Commit ) func init() { freqHashCache = make(map[int]map[string]map[string]types.ExpFreq) - repoCache = make(map[string][]types.Commit) -} - -func GetCachedRepo(path string, head string) ([]types.Commit, bool) { - mapTex.RLock() - defer mapTex.RUnlock() - if commits, ok := repoCache[path]; !ok { - return []types.Commit{}, false - } else if len(commits) > 0 && commits[0].Hash == head { - return commits, true - } - return []types.Commit{}, false -} - -func IsRepoCached(path string, head string) bool { - mapTex.RLock() - defer mapTex.RUnlock() - if commits, ok := repoCache[path]; !ok { - return false - } else { - return len(commits) > 0 && commits[0].Hash == head - } + repoHashCache = make(map[int]map[string]map[string]types.ExpRepos) + repoCache = make(map[string]types.ExpRepo) } func hashSlice(in []string) string { @@ -84,18 +64,65 @@ func GetCachedGraph(year int, authors []string, repoPaths []string) (types.Freq, } } -func CacheRepo(year int, authors, repoPaths []string, commits []types.Commit) { +func GetCachedRepo(path string, head string) ([]types.Commit, bool) { + mapTex.RLock() + defer mapTex.RUnlock() + if commits, ok := repoCache[path]; !ok { + return []types.Commit{}, false + } else if len(commits.Commits) > 0 && commits.Commits[0].Hash == head { + return commits.Commits, true + } + return []types.Commit{}, false +} + +func CacheRepo(path string, commits []types.Commit) { + mapTex.Lock() + defer mapTex.Unlock() + repoCache[path] = types.ExpRepo{Commits: commits, Created: time.Now()} + go func() { + time.Sleep(time.Hour * 1) + mapTex.Lock() + defer mapTex.Unlock() + delete(repoCache, path) + }() +} + +func GetCachedRepos(year int, authors, repoPaths []string) ([][]types.Commit, bool) { + a := hashSlice(authors) + r := hashSlice(repoPaths) + mapTex.RLock() + defer mapTex.RUnlock() + if m1, ok := repoHashCache[year]; !ok { + return [][]types.Commit{{}}, false + } else { + if m2, ok := m1[a]; !ok { + return [][]types.Commit{{}}, false + } else { + if commits, ok := m2[r]; !ok { + return [][]types.Commit{{}}, false + } else { + if commits.Created.Before(time.Now().Add(-15 * time.Minute)) { + return [][]types.Commit{{}}, false + } else { + return commits.Commits, true + } + } + } + } +} + +func CacheRepos(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) + repoHashCache[year] = make(map[string]map[string]types.ExpRepos) } if _, ok := repoHashCache[year][a]; !ok { - repoHashCache[year][a] = make(map[string]types.ExpRepo) + repoHashCache[year][a] = make(map[string]types.ExpRepos) } - repoHashCache[year][a][r] = types.ExpRepo{Commits: commits, Created: time.Now()} + repoHashCache[year][a][r] = types.ExpRepos{Commits: commits, Created: time.Now()} go func() { time.Sleep(time.Hour * 1) mapTex.Lock() diff --git a/commits/chancommits.go b/commits/chancommits.go index 8cc645e..6e01234 100644 --- a/commits/chancommits.go +++ b/commits/chancommits.go @@ -20,10 +20,10 @@ func (paths RepoSet) GetRepoCommits(year int, authors []string) ([][]types.Commi for i := 0; i < yearLength; i++ { commits[i] = []types.Commit{} } - // cache, ok := GetCachedCommits(year, authors, paths) - // if ok { - // return cache, nil - // } + cache, ok := GetCachedRepos(year, authors, paths) + if ok { + return cache, nil + } outChan := make(chan types.Commit, 10) var wg sync.WaitGroup for _, p := range paths { @@ -52,9 +52,12 @@ func (paths RepoSet) GetRepoCommits(year int, authors []string) ([][]types.Commi wg.Wait() close(outChan) }() - freq := YearFreqFromChan(outChan, year) - CacheGraph(year, authors, paths, freq) - return freq, nil + for commit := range outChan { + d := commit.TimeStamp.YearDay() - 1 + commits[d] = append(commits[d], commit) + } + CacheRepos(year, authors, paths, commits) + return commits, nil } func (paths RepoSet) FrequencyChan(year int, authors []string) (types.Freq, error) { @@ -94,18 +97,7 @@ 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/graph/svg/svg.go b/graph/svg/svg.go index 8eb575b..a086c7a 100644 --- a/graph/svg/svg.go +++ b/graph/svg/svg.go @@ -26,7 +26,7 @@ var ( func GetWeekSVG(frequencies []int, shouldHighlight bool) bytes.Buffer { squareColors := []sc.SimpleColor{} min, max := common.MinMax(frequencies) - fmt.Println(frequencies) + // fmt.Println(frequencies) for _, f := range frequencies { squareColors = append(squareColors, common.ColorForFrequency(f, min, max)) } diff --git a/types/types.go b/types/types.go index 5bf9700..0b7dca9 100644 --- a/types/types.go +++ b/types/types.go @@ -23,7 +23,10 @@ type ( YearFreq Freq Created time.Time } - + ExpRepos struct { + Commits [][]Commit + Created time.Time + } ExpRepo struct { Commits []Commit Created time.Time