prepare for git commit chan aggregation

This commit is contained in:
2023-02-14 22:51:54 -08:00
parent f932d2ce21
commit 5d6ca69a2b
5 changed files with 57 additions and 24 deletions

View File

@@ -29,7 +29,7 @@ type model struct {
type CommitLog struct { type CommitLog struct {
Year int Year int
YearDay int YearDay int
Commits []types.Commit Commits [][]types.Commit
Selected int Selected int
Authors []string Authors []string
Repos []string Repos []string
@@ -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
} }
@@ -172,6 +172,7 @@ func (m Graph) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
return m, nil return m, nil
} }
func NewCommitLog() (CommitLog, error) { func NewCommitLog() (CommitLog, error) {
var m CommitLog var m CommitLog
now := time.Now() now := time.Now()
@@ -188,8 +189,8 @@ func NewCommitLog() (CommitLog, error) {
m.Authors = authors m.Authors = authors
m.Year = year m.Year = year
m.Selected = today m.Selected = today
m.Commits = mr.GetDayCommits(m.Year, m.Selected, m.Authors) m.Commits, err = mr.GetRepoCommits(m.Year, m.Authors)
return m, nil return m, err
} }
func NewGraph() (Graph, error) { func NewGraph() (Graph, error) {

View File

@@ -13,7 +13,8 @@ import (
var ( var (
mapTex sync.RWMutex mapTex sync.RWMutex
hashCache 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
// 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,
@@ -22,16 +23,10 @@ var (
) )
func init() { 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) 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) { func GetCachedRepo(path string, head string) ([]types.Commit, bool) {
mapTex.RLock() mapTex.RLock()
defer mapTex.RUnlock() defer mapTex.RUnlock()
@@ -70,7 +65,7 @@ func GetCachedGraph(year int, authors []string, repoPaths []string) (types.Freq,
r := hashSlice(repoPaths) r := hashSlice(repoPaths)
mapTex.RLock() mapTex.RLock()
defer mapTex.RUnlock() defer mapTex.RUnlock()
if m1, ok := hashCache[year]; !ok { if m1, ok := freqHashCache[year]; !ok {
return types.Freq{}, false return types.Freq{}, false
} else { } else {
if m2, ok := m1[a]; !ok { 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) { func CacheGraph(year int, authors, repoPaths []string, freq types.Freq) {
a := hashSlice(authors) a := hashSlice(authors)
r := hashSlice(repoPaths) r := hashSlice(repoPaths)
mapTex.Lock() mapTex.Lock()
defer mapTex.Unlock() defer mapTex.Unlock()
if _, ok := hashCache[year]; !ok { if _, ok := freqHashCache[year]; !ok {
hashCache[year] = make(map[string]map[string]types.ExpFreq) freqHashCache[year] = make(map[string]map[string]types.ExpFreq)
} }
if _, ok := hashCache[year][a]; !ok { if _, ok := freqHashCache[year][a]; !ok {
hashCache[year][a] = make(map[string]types.ExpFreq) 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() { go func() {
time.Sleep(time.Hour * 1) time.Sleep(time.Hour * 1)
mapTex.Lock() mapTex.Lock()
defer mapTex.Unlock() defer mapTex.Unlock()
delete(hashCache[year][a], r) delete(freqHashCache[year][a], r)
}() }()
} }

View File

@@ -10,7 +10,7 @@ import (
"github.com/taigrr/gico/types" "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 yearLength := 365
if year%4 == 0 { if year%4 == 0 {
yearLength++ yearLength++
@@ -53,7 +53,7 @@ func (paths RepoSet) GetDayCommits(year int, authors []string) ([]types.Commit,
close(outChan) close(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
} }
@@ -94,6 +94,18 @@ 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

@@ -107,7 +107,7 @@ func (repo Repo) GetCommitSet() (CommitSet, error) {
return nil return nil
}) })
cs.Commits = commits cs.Commits = commits
CacheRepo(repo.Path, cs.Commits) // CacheRepo(repo.Path, cs.Commits)
return cs, nil return cs, nil
} }

View File

@@ -23,6 +23,11 @@ type (
YearFreq Freq YearFreq Freq
Created time.Time Created time.Time
} }
ExpRepo struct {
Commits []Commit
Created time.Time
}
WorkDay struct { WorkDay struct {
Day time.Time `json:"day"` Day time.Time `json:"day"`
Count int `json:"count"` Count int `json:"count"`