readying up for loading in commit messages to UI

This commit is contained in:
2023-02-15 23:45:50 -08:00
parent 5d6ca69a2b
commit e4decf6ee2
5 changed files with 80 additions and 51 deletions

View File

@@ -70,7 +70,7 @@ func initialModel() (model, error) {
if err != nil { if err != nil {
return m, err return m, err
} }
m.CommitLogModel, err = NewCommitLog() // m.CommitLogModel, err = NewCommitLog()
if err != nil { if err != nil {
return m, err return m, err
} }
@@ -93,7 +93,7 @@ func (m Settings) Init() tea.Cmd {
} }
func (m Settings) View() string { 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) { 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 { if m.Selected > 0 {
m.Selected-- m.Selected--
} }
default:
// m.Commits = commits.CommitSet
} }
} }
return m, nil return m, nil
@@ -118,7 +120,11 @@ func (m CommitLog) Init() tea.Cmd {
} }
func (m CommitLog) View() string { 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 { func YearLen(year int) int {
@@ -256,6 +262,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.GraphModel, _ = tmp.(Graph) m.GraphModel, _ = tmp.(Graph)
tmpC, cmd := m.CommitLogModel.Update(msg) tmpC, cmd := m.CommitLogModel.Update(msg)
m.CommitLogModel, _ = tmpC.(CommitLog) m.CommitLogModel, _ = tmpC.(CommitLog)
m.CommitLogModel.YearDay = m.GraphModel.Selected
return m, cmd return m, cmd
case settings: case settings:
tmp, cmd := m.SettingsModel.Update(msg) tmp, cmd := m.SettingsModel.Update(msg)

View File

@@ -14,38 +14,18 @@ import (
var ( var (
mapTex sync.RWMutex mapTex sync.RWMutex
freqHashCache map[int]map[string]map[string]types.ExpFreq 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 Repo Cache holds a list of all commits from HEAD back to parent
// the key is the repo path // the key is the repo path
// if the hash of the first commit / HEAD commit doesn't match the current HEAD, // if the hash of the first commit / HEAD commit doesn't match the current HEAD,
// then it can be discarded and reloaded // then it can be discarded and reloaded
repoCache map[string][]types.Commit
) )
func init() { func init() {
freqHashCache = 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) repoHashCache = make(map[int]map[string]map[string]types.ExpRepos)
} repoCache = make(map[string]types.ExpRepo)
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
}
} }
func hashSlice(in []string) string { 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) a := hashSlice(authors)
r := hashSlice(repoPaths) r := hashSlice(repoPaths)
mapTex.Lock() mapTex.Lock()
defer mapTex.Unlock() defer mapTex.Unlock()
if _, ok := repoHashCache[year]; !ok { 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 { 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() { go func() {
time.Sleep(time.Hour * 1) time.Sleep(time.Hour * 1)
mapTex.Lock() mapTex.Lock()

View File

@@ -20,10 +20,10 @@ func (paths RepoSet) GetRepoCommits(year int, authors []string) ([][]types.Commi
for i := 0; i < yearLength; i++ { for i := 0; i < yearLength; i++ {
commits[i] = []types.Commit{} commits[i] = []types.Commit{}
} }
// cache, ok := GetCachedCommits(year, authors, paths) cache, ok := GetCachedRepos(year, authors, paths)
// if ok { if ok {
// return cache, nil return cache, nil
// } }
outChan := make(chan types.Commit, 10) outChan := make(chan types.Commit, 10)
var wg sync.WaitGroup var wg sync.WaitGroup
for _, p := range paths { for _, p := range paths {
@@ -52,9 +52,12 @@ func (paths RepoSet) GetRepoCommits(year int, authors []string) ([][]types.Commi
wg.Wait() wg.Wait()
close(outChan) close(outChan)
}() }()
freq := YearFreqFromChan(outChan, year) for commit := range outChan {
CacheGraph(year, authors, paths, freq) d := commit.TimeStamp.YearDay() - 1
return freq, nil 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) { 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() wg.Wait()
close(outChan) 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) freq := YearFreqFromChan(outChan, year)
CacheGraph(year, authors, paths, freq) CacheGraph(year, authors, paths, freq)
return freq, nil return freq, nil

View File

@@ -26,7 +26,7 @@ var (
func GetWeekSVG(frequencies []int, shouldHighlight bool) 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)
fmt.Println(frequencies) // fmt.Println(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))
} }

View File

@@ -23,7 +23,10 @@ type (
YearFreq Freq YearFreq Freq
Created time.Time Created time.Time
} }
ExpRepos struct {
Commits [][]Commit
Created time.Time
}
ExpRepo struct { ExpRepo struct {
Commits []Commit Commits []Commit
Created time.Time Created time.Time