Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit 93372ca

Browse files
committed
Impl ListContributionCollections
1 parent 31074ec commit 93372ca

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

infra/store/github/github_store.go

+75-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"database/sql"
66
"fmt"
7+
"strconv"
78
"strings"
89
"time"
910

1011
"github.com/go-redis/redis"
1112
"github.com/pkg/errors"
1213
"github.com/volatiletech/sqlboiler/boil"
14+
"github.com/volatiletech/sqlboiler/queries/qm"
1315

1416
"github.com/ProgrammingLab/prolab-accounts/infra/record"
1517
"github.com/ProgrammingLab/prolab-accounts/infra/store"
@@ -33,15 +35,15 @@ func NewGitHubStore(ctx context.Context, db *sqlutil.DB, cli *redis.Client) stor
3335
}
3436

3537
const (
36-
contributionTotalCount = "contributions-total-count"
38+
contributionTotalCountKey = "contributions-total-count"
3739
)
3840

3941
func (s *githubStoreImpl) UpdateContributionDays(c *model.GitHubContributionCollection) ([]*record.GithubContributionDay, error) {
4042
z := redis.Z{
4143
Score: float64(c.TotalCount),
4244
Member: int64(c.UserID),
4345
}
44-
err := s.cli.ZAdd(contributionTotalCount, z).Err()
46+
err := s.cli.ZAdd(contributionTotalCountKey, z).Err()
4547
if err != nil {
4648
return nil, errors.WithStack(err)
4749
}
@@ -64,6 +66,77 @@ func (s *githubStoreImpl) UpdateContributionDays(c *model.GitHubContributionColl
6466
return days, nil
6567
}
6668

69+
func (s *githubStoreImpl) ListContributionCollections(usersLimit int) ([]*model.GitHubContributionCollection, error) {
70+
cols, err := s.getTopUsers(usersLimit)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
err = s.loadDays(cols)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
return cols, nil
81+
}
82+
83+
func (s *githubStoreImpl) getTopUsers(usersLimit int) ([]*model.GitHubContributionCollection, error) {
84+
values, err := s.cli.ZRevRangeWithScores(contributionTotalCountKey, 0, int64(usersLimit)-1).Result()
85+
if err != nil {
86+
return nil, errors.WithStack(err)
87+
}
88+
89+
cols := make([]*model.GitHubContributionCollection, 0, len(values))
90+
for _, v := range values {
91+
id, err := strconv.ParseInt(v.Member.(string), 10, 64)
92+
if err != nil {
93+
return nil, errors.WithStack(err)
94+
}
95+
col := &model.GitHubContributionCollection{
96+
UserID: model.UserID(id),
97+
TotalCount: int(v.Score),
98+
}
99+
cols = append(cols, col)
100+
}
101+
102+
return cols, nil
103+
}
104+
105+
func (s *githubStoreImpl) loadDays(cols []*model.GitHubContributionCollection) error {
106+
userIDs := make([]int64, 0, len(cols))
107+
for _, c := range cols {
108+
userIDs = append(userIDs, int64(c.UserID))
109+
}
110+
111+
mods := []qm.QueryMod{
112+
qm.Load("User.Profile.Department"),
113+
qm.Load("User.Profile.Role"),
114+
qm.From("github_contribution_days as days"),
115+
qm.InnerJoin("users on users.id = days.user_id"),
116+
qm.InnerJoin("profiles on profiles.id = users.profile_id"),
117+
qm.Where("profiles.profile_scope = ?", model.Public),
118+
qm.WhereIn("users.id in ?", sqlutil.Int64SliceToAbstractSlice(userIDs)...),
119+
qm.OrderBy("days.date"),
120+
}
121+
var days []*record.GithubContributionDay
122+
err := record.NewQuery(mods...).Bind(s.ctx, s.db, &days)
123+
if err != nil {
124+
return errors.WithStack(err)
125+
}
126+
127+
daysMap := make(map[model.UserID][]*record.GithubContributionDay, len(cols))
128+
for _, d := range days {
129+
id := model.UserID(d.UserID)
130+
daysMap[id] = append(daysMap[id], d)
131+
}
132+
133+
for _, c := range cols {
134+
c.Days = daysMap[c.UserID]
135+
}
136+
137+
return nil
138+
}
139+
67140
func bulkInsert(ctx context.Context, tx *sql.Tx, days []*record.GithubContributionDay) error {
68141
if len(days) == 0 {
69142
return nil

infra/store/github_store.go

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ import (
88
// GitHubStore provides github data
99
type GitHubStore interface {
1010
UpdateContributionDays(c *model.GitHubContributionCollection) ([]*record.GithubContributionDay, error)
11+
ListContributionCollections(usersLimit int) ([]*model.GitHubContributionCollection, error)
1112
}

sqlutil/conv.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package sqlutil
2+
3+
// Int32SliceToInt64Slice converts int32 array to int64 array
4+
func Int32SliceToInt64Slice(a []int32) []int64 {
5+
res := make([]int64, 0, len(a))
6+
for _, v := range a {
7+
res = append(res, int64(v))
8+
}
9+
return res
10+
}
11+
12+
// Int64SliceToAbstractSlice converts int64 array to []interface{}
13+
func Int64SliceToAbstractSlice(a []int64) []interface{} {
14+
res := []interface{}{}
15+
for _, v := range a {
16+
res = append(res, v)
17+
}
18+
return res
19+
}

0 commit comments

Comments
 (0)