From 9c832bc07dba4f1c65ca6195ee59987072c31c89 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sun, 29 Jan 2023 22:06:52 -0800 Subject: [PATCH] add channel func for calculating freqs --- commits/commits.go | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/commits/commits.go b/commits/commits.go index 77b28a9..f0c39ba 100644 --- a/commits/commits.go +++ b/commits/commits.go @@ -54,15 +54,44 @@ func GlobalFrequencyChan(year int, authors []string) (types.YearFreq, error) { wg.Wait() close(outChan) }() - commitSet := CommitSet{Year: year, Commits: []types.Commit{}} - for c := range outChan { - commitSet.Commits = append(commitSet.Commits, c) - } - freq := commitSet.ToYearFreq() + freq := YearFreqFromChan(outChan, year) return freq, nil } +func YearFreqFromChan(cc chan types.Commit, year int) types.YearFreq { + yearLength := 365 + if year%4 == 0 { + yearLength++ + } + freq := make([]int, yearLength) + data := types.NewDataSet() + for commit := range cc { + ts := commit.TimeStamp + roundedTS := ts.Round(time.Hour * 24) + wd, ok := data[roundedTS] + if !ok { + wd = types.WorkDay{} + wd.Commits = []types.Commit{} + } + wd.Commits = append(wd.Commits, commit) + wd.Count++ + wd.Day = roundedTS + data[roundedTS] = wd + } + for k, v := range data { + if k.Year() != year { + continue + } + // this is equivalent to adding len(commits) to the freq total, but + // it's a stub for later when we do more here + for range v.Commits { + freq[k.YearDay()-1]++ + } + } + return freq +} + func GlobalFrequency(year int, authors []string) (types.YearFreq, error) { yearLength := 365 if year%4 == 0 {