record watcher hits

This commit is contained in:
Andre Medeiros 2021-09-25 14:27:58 -04:00
parent 24ef6cce6b
commit 537d1711fe
4 changed files with 21 additions and 3 deletions

View file

@ -140,6 +140,7 @@ type watcherItem struct {
Keyword string Keyword string
Flair string Flair string
Domain string Domain string
Hits int64
} }
func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) { func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) {
@ -163,6 +164,7 @@ func (a *api) listWatchersHandler(w http.ResponseWriter, r *http.Request) {
Keyword: watcher.Keyword, Keyword: watcher.Keyword,
Flair: watcher.Flair, Flair: watcher.Flair,
Domain: watcher.Domain, Domain: watcher.Domain,
Hits: watcher.Hits,
} }
wis[i] = wi wis[i] = wi

View file

@ -14,6 +14,7 @@ type Watcher struct {
Keyword string Keyword string
Flair string Flair string
Domain string Domain string
Hits int64
} }
type WatcherRepository interface { type WatcherRepository interface {
@ -23,5 +24,6 @@ type WatcherRepository interface {
Create(ctx context.Context, watcher *Watcher) error Create(ctx context.Context, watcher *Watcher) error
Update(ctx context.Context, watcher *Watcher) error Update(ctx context.Context, watcher *Watcher) error
IncrementHits(ctx context.Context, id int64) error
Delete(ctx context.Context, id int64) error Delete(ctx context.Context, id int64) error
} }

View file

@ -38,6 +38,7 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg
&watcher.Keyword, &watcher.Keyword,
&watcher.Flair, &watcher.Flair,
&watcher.Domain, &watcher.Domain,
&watcher.Hits,
); err != nil { ); err != nil {
return nil, err return nil, err
} }
@ -48,7 +49,7 @@ func (p *postgresWatcherRepository) fetch(ctx context.Context, query string, arg
func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (domain.Watcher, error) { func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (domain.Watcher, error) {
query := ` query := `
SELECT id, created_at, device_id, account_id, subreddit_id, upvotes, keyword, flair, domain SELECT id, created_at, device_id, account_id, subreddit_id, upvotes, keyword, flair, domain, hits
FROM watchers FROM watchers
WHERE id = $1` WHERE id = $1`
@ -65,7 +66,7 @@ func (p *postgresWatcherRepository) GetByID(ctx context.Context, id int64) (doma
func (p *postgresWatcherRepository) GetBySubredditID(ctx context.Context, id int64) ([]domain.Watcher, error) { func (p *postgresWatcherRepository) GetBySubredditID(ctx context.Context, id int64) ([]domain.Watcher, error) {
query := ` query := `
SELECT id, created_at, device_id, account_id, subreddit_id, upvotes, keyword, flair, domain SELECT id, created_at, device_id, account_id, subreddit_id, upvotes, keyword, flair, domain, hits
FROM watchers FROM watchers
WHERE subreddit_id = $1` WHERE subreddit_id = $1`
@ -83,7 +84,8 @@ func (p *postgresWatcherRepository) GetByDeviceAPNSTokenAndAccountRedditID(ctx c
watchers.upvotes, watchers.upvotes,
watchers.keyword, watchers.keyword,
watchers.flair, watchers.flair,
watchers.domain watchers.domain,
watchers.hits
FROM watchers FROM watchers
INNER JOIN accounts ON watchers.account_id = accounts.id INNER JOIN accounts ON watchers.account_id = accounts.id
INNER JOIN devices ON watchers.device_id = devices.id INNER JOIN devices ON watchers.device_id = devices.id
@ -142,6 +144,16 @@ func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.
return err return err
} }
func (p *postgresWatcherRepository) IncrementHits(ctx context.Context, id int64) error {
query := `UPDATE watchers SET hits = hits + 1 WHERE id = $1`
res, err := p.pool.Exec(ctx, query, id)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err
}
func (p *postgresWatcherRepository) Delete(ctx context.Context, id int64) error { func (p *postgresWatcherRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM watchers WHERE id = $1` query := `DELETE FROM watchers WHERE id = $1`
res, err := p.pool.Exec(ctx, query, id) res, err := p.pool.Exec(ctx, query, id)

View file

@ -329,6 +329,8 @@ func (sc *subredditsConsumer) Consume(delivery rmq.Delivery) {
continue continue
} }
_ = sc.watcherRepo.IncrementHits(ctx, watcher.ID)
lockKey := fmt.Sprintf("watcher:%d:%s", watcher.DeviceID, post.ID) lockKey := fmt.Sprintf("watcher:%d:%s", watcher.DeviceID, post.ID)
notified, _ := sc.redis.Get(ctx, lockKey).Bool() notified, _ := sc.redis.Get(ctx, lockKey).Bool()