Skip to content

Commit 5037f6d

Browse files
committed
Add DateString to coding sessions
1 parent e43ab01 commit 5037f6d

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

redis/redis.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ func New(addr, password string) *Client {
2424
}
2525

2626
func (c *Client) Write(ctx context.Context, session pulse.CodingSession) error {
27-
key := session.Date.Format("2006-01-02")
28-
2927
// Check if we already have a session for this date.
30-
cmd := c.redisClient.Get(ctx, key)
28+
cmd := c.redisClient.Get(ctx, session.DateString())
3129
data, err := cmd.Result()
3230
if err != nil && !errors.Is(err, redis.Nil) {
3331
var prevSession pulse.CodingSession
@@ -43,7 +41,7 @@ func (c *Client) Write(ctx context.Context, session pulse.CodingSession) error {
4341
return err
4442
}
4543

46-
return c.redisClient.Set(ctx, key, bytes, 0).Err()
44+
return c.redisClient.Set(ctx, session.DateString(), bytes, 0).Err()
4745
}
4846

4947
func (c *Client) ReadAll(ctx context.Context) (pulse.CodingSessions, error) {

session.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,20 @@ func NewCodingSession(buffers Buffers, now time.Time) CodingSession {
5454
}
5555

5656
// Merge takes two coding sessions, merges them, and returns the result.
57-
func (a CodingSession) Merge(b CodingSession) CodingSession {
57+
func (c CodingSession) Merge(other CodingSession) CodingSession {
5858
mergedSession := CodingSession{
59-
Date: cmp.Or(a.Date, b.Date),
60-
Duration: a.Duration + b.Duration,
61-
Repositories: a.Repositories.merge(b.Repositories),
59+
Date: cmp.Or(c.Date, other.Date),
60+
Duration: c.Duration + other.Duration,
61+
Repositories: c.Repositories.merge(other.Repositories),
6262
}
6363

6464
return mergedSession
6565
}
6666

67+
func (c CodingSession) DateString() string {
68+
return c.Date.Format("2006-01-02")
69+
}
70+
6771
// CodingSessions represents a slice of coding sessions.
6872
type CodingSessions []CodingSession
6973

0 commit comments

Comments
 (0)