this repo has no description
1package db 2 3import ( 4 "encoding/json" 5 "fmt" 6 "time" 7) 8 9type RepoEvent struct { 10 Repo *Repo 11 Source *Repo 12} 13 14type ProfileTimeline struct { 15 ByMonth []ByMonth 16} 17 18type ByMonth struct { 19 RepoEvents []RepoEvent 20 IssueEvents IssueEvents 21 PullEvents PullEvents 22} 23 24type IssueEvents struct { 25 Items []*Issue 26} 27 28type IssueEventStats struct { 29 Open int 30 Closed int 31} 32 33func (i IssueEvents) Stats() IssueEventStats { 34 var open, closed int 35 for _, issue := range i.Items { 36 if issue.Open { 37 open += 1 38 } else { 39 closed += 1 40 } 41 } 42 43 return IssueEventStats{ 44 Open: open, 45 Closed: closed, 46 } 47} 48 49type PullEvents struct { 50 Items []*Pull 51} 52 53func (p PullEvents) Stats() PullEventStats { 54 var open, merged, closed int 55 for _, pull := range p.Items { 56 switch pull.State { 57 case PullOpen: 58 open += 1 59 case PullMerged: 60 merged += 1 61 case PullClosed: 62 closed += 1 63 } 64 } 65 66 return PullEventStats{ 67 Open: open, 68 Merged: merged, 69 Closed: closed, 70 } 71} 72 73type PullEventStats struct { 74 Closed int 75 Open int 76 Merged int 77} 78 79const TimeframeMonths = 3 80 81func MakeProfileTimeline(e Execer, forDid string) (*ProfileTimeline, error) { 82 timeline := ProfileTimeline{ 83 ByMonth: make([]ByMonth, TimeframeMonths), 84 } 85 currentMonth := time.Now().Month() 86 timeframe := fmt.Sprintf("-%d months", TimeframeMonths) 87 88 pulls, err := GetPullsByOwnerDid(e, forDid, timeframe) 89 if err != nil { 90 return nil, fmt.Errorf("error getting pulls by owner did: %w", err) 91 } 92 93 // group pulls by month 94 for _, pull := range pulls { 95 pullMonth := pull.Created.Month() 96 97 if currentMonth-pullMonth > TimeframeMonths { 98 // shouldn't happen; but times are weird 99 continue 100 } 101 102 idx := currentMonth - pullMonth 103 items := &timeline.ByMonth[idx].PullEvents.Items 104 105 *items = append(*items, &pull) 106 } 107 108 issues, err := GetIssuesByOwnerDid(e, forDid, timeframe) 109 if err != nil { 110 return nil, fmt.Errorf("error getting issues by owner did: %w", err) 111 } 112 113 for _, issue := range issues { 114 issueMonth := issue.Created.Month() 115 116 if currentMonth-issueMonth > TimeframeMonths { 117 // shouldn't happen; but times are weird 118 continue 119 } 120 121 idx := currentMonth - issueMonth 122 items := &timeline.ByMonth[idx].IssueEvents.Items 123 124 *items = append(*items, &issue) 125 } 126 127 repos, err := GetAllReposByDid(e, forDid) 128 if err != nil { 129 return nil, fmt.Errorf("error getting all repos by did: %w", err) 130 } 131 132 for _, repo := range repos { 133 // TODO: get this in the original query; requires COALESCE because nullable 134 var sourceRepo *Repo 135 if repo.Source != "" { 136 sourceRepo, err = GetRepoByAtUri(e, repo.Source) 137 if err != nil { 138 return nil, err 139 } 140 } 141 142 repoMonth := repo.Created.Month() 143 144 if currentMonth-repoMonth > TimeframeMonths { 145 // shouldn't happen; but times are weird 146 continue 147 } 148 149 idx := currentMonth - repoMonth 150 151 items := &timeline.ByMonth[idx].RepoEvents 152 *items = append(*items, RepoEvent{ 153 Repo: &repo, 154 Source: sourceRepo, 155 }) 156 } 157 158 x, _ := json.MarshalIndent(timeline, "", "\t") 159 fmt.Println(string(x)) 160 161 return &timeline, nil 162}