mirror of
https://github.com/taigrr/gico.git
synced 2026-04-02 03:09:07 -07:00
finish merging
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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++
|
||||
|
||||
@@ -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++
|
||||
|
||||
@@ -16,6 +16,7 @@ type (
|
||||
Commits []types.Commit
|
||||
Year int
|
||||
}
|
||||
RepoSet []string
|
||||
)
|
||||
|
||||
func OpenRepo(directory string) (Repo, error) {
|
||||
|
||||
Reference in New Issue
Block a user