BlueGit

timfa bluegit

Files

bluegit / internal / store / activity.go

activity.go

feat: activiteitsheatmap op dashboard, groep en repository
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed d5433bc7 History
activity.go 3.7 KiB Download Raw
1package store
2
3import (
4 "context"
5 "strings"
6 "time"
7)
8
9// Contribution is één getelde bijdrage.
10type Contribution struct {
11 UserID int64
12 RepoID int64
13 Kind string
14 Ref string
15 At time.Time
16}
17
18// AddContributions bewaart bijdragen. Een herhaling van dezelfde ref telt niet opnieuw.
19func (s *Store) AddContributions(ctx context.Context, rows []Contribution) error {
20 if len(rows) == 0 {
21 return nil
22 }
23 tx, err := s.db.BeginTx(ctx, nil)
24 if err != nil {
25 return err
26 }
27 defer func() { _ = tx.Rollback() }()
28 for _, row := range rows {
29 if row.UserID == 0 || row.RepoID == 0 || row.Kind == "" || row.Ref == "" {
30 continue
31 }
32 at := row.At.UTC()
33 if at.IsZero() {
34 at = time.Now().UTC()
35 }
36 if _, err := tx.ExecContext(ctx, `INSERT OR IGNORE INTO contributions (user_id, repo_id, kind, ref, occurred_at) VALUES (?, ?, ?, ?, ?)`,
37 row.UserID, row.RepoID, row.Kind, row.Ref, at.Format(time.RFC3339)); err != nil {
38 return err
39 }
40 }
41 return tx.Commit()
42}
43
44// ContributionTimes geeft tijdstippen binnen de filter.
45// userID 0 is iedereen. Een lege repolijst geeft niets terug.
46func (s *Store) ContributionTimes(ctx context.Context, userID int64, repoIDs []int64, since time.Time) ([]time.Time, error) {
47 if len(repoIDs) == 0 {
48 return nil, nil
49 }
50 holders := make([]string, len(repoIDs))
51 args := make([]any, 0, len(repoIDs)+2)
52 args = append(args, since.UTC().Format(time.RFC3339))
53 for i, id := range repoIDs {
54 holders[i] = "?"
55 args = append(args, id)
56 }
57 q := `SELECT occurred_at FROM contributions WHERE occurred_at >= ? AND repo_id IN (` + strings.Join(holders, ",") + `)`
58 if userID != 0 {
59 q += ` AND user_id = ?`
60 args = append(args, userID)
61 }
62 rows, err := s.db.QueryContext(ctx, q, args...)
63 if err != nil {
64 return nil, err
65 }
66 defer rows.Close()
67 var out []time.Time
68 for rows.Next() {
69 var raw string
70 if err := rows.Scan(&raw); err != nil {
71 return nil, err
72 }
73 out = append(out, parseTime(raw))
74 }
75 return out, rows.Err()
76}
77
78// ContributionSynced meldt of de bestaande commits van een repository al zijn overgenomen.
79func (s *Store) ContributionSynced(ctx context.Context, repoID int64) (bool, error) {
80 var n int
81 err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM contribution_sync WHERE repo_id = ?`, repoID).Scan(&n)
82 return n > 0, err
83}
84
85// MarkContributionSynced onthoudt dat de bestaande commits zijn overgenomen.
86func (s *Store) MarkContributionSynced(ctx context.Context, repoID int64) error {
87 _, err := s.db.ExecContext(ctx, `INSERT OR REPLACE INTO contribution_sync (repo_id, synced_at) VALUES (?, ?)`, repoID, now())
88 return err
89}
90
91// DropContributions wist de telling van één repository, zodat die opnieuw uit git komt.
92func (s *Store) DropContributions(ctx context.Context, repoID int64) error {
93 if _, err := s.db.ExecContext(ctx, `DELETE FROM contributions WHERE repo_id = ?`, repoID); err != nil {
94 return err
95 }
96 _, err := s.db.ExecContext(ctx, `DELETE FROM contribution_sync WHERE repo_id = ?`, repoID)
97 return err
98}
99
100// UserIDByGitAuthor koppelt een commit aan een account via het e-mailadres of de naam.
101func (s *Store) UserIDByGitAuthor(ctx context.Context, email, name string) (int64, bool) {
102 if id, ok := s.userIDByName(ctx, emailLocal(email)); ok {
103 return id, true
104 }
105 if id, ok := s.userIDByName(ctx, name); ok {
106 return id, true
107 }
108 return 0, false
109}
110
111func (s *Store) userIDByName(ctx context.Context, name string) (int64, bool) {
112 name = strings.ToLower(strings.TrimSpace(name))
113 if name == "" || strings.ContainsAny(name, " @") {
114 return 0, false
115 }
116 u, err := s.UserByUsername(ctx, name)
117 if err != nil {
118 return 0, false
119 }
120 return u.ID, true
121}
122
123func emailLocal(email string) string {
124 email = strings.ToLower(strings.TrimSpace(email))
125 if i := strings.LastIndex(email, "@"); i >= 0 {
126 email = email[:i]
127 }
128 return email
129}