Add goal notifications and collector health
Test and publish / test (push) Successful in 1m40s
Test and publish / publish (push) Successful in 3m45s

This commit is contained in:
2026-10-23 20:45:51 +01:00
parent 667091efb0
commit 453cef40af
8 changed files with 375 additions and 3 deletions
+39
View File
@@ -39,6 +39,14 @@ type Device struct {
RevokedAt *time.Time `json:"revoked_at"`
}
// CollectorHealth is the device status and all-time ingestion counters used by
// the admin health view.
type CollectorHealth struct {
Device Device
ActivityCount int64
SampleCount int64
}
// HashKey returns the stored representation of an API key.
func HashKey(key string) string {
sum := sha256.Sum256([]byte(key))
@@ -76,6 +84,23 @@ func (s *Store) ListDevices(ctx context.Context) ([]Device, error) {
})
}
func (s *Store) CollectorHealth(ctx context.Context) ([]CollectorHealth, error) {
rows, err := s.pool.Query(ctx, `SELECT d.id, d.name, d.kind, d.created_at,
d.last_seen_at, d.revoked_at,
(SELECT count(*) FROM activities a WHERE a.device_id = d.id),
(SELECT count(*) FROM samples sm WHERE sm.device_id = d.id)
FROM devices d ORDER BY d.id`)
if err != nil {
return nil, err
}
return pgx.CollectRows(rows, func(r pgx.CollectableRow) (CollectorHealth, error) {
var h CollectorHealth
err := r.Scan(&h.Device.ID, &h.Device.Name, &h.Device.Kind, &h.Device.CreatedAt,
&h.Device.LastSeenAt, &h.Device.RevokedAt, &h.ActivityCount, &h.SampleCount)
return h, err
})
}
func (s *Store) RevokeDevice(ctx context.Context, name string) error {
tag, err := s.pool.Exec(ctx,
`UPDATE devices SET revoked_at = now() WHERE name = $1 AND revoked_at IS NULL`, name)
@@ -549,6 +574,20 @@ func (s *Store) UpdateNotificationSettings(ctx context.Context, settings Notific
return settings, err
}
func (s *Store) GoalNotificationSent(ctx context.Context, goalID int64, date time.Time) (bool, error) {
var sent bool
err := s.pool.QueryRow(ctx, `SELECT EXISTS (
SELECT 1 FROM goal_notifications WHERE goal_id = $1 AND notification_date = $2::date
)`, goalID, date.Format(time.DateOnly)).Scan(&sent)
return sent, err
}
func (s *Store) RecordGoalNotification(ctx context.Context, goalID int64, date time.Time) error {
_, err := s.pool.Exec(ctx, `INSERT INTO goal_notifications (goal_id, notification_date)
VALUES ($1, $2::date) ON CONFLICT (goal_id, notification_date) DO NOTHING`, goalID, date.Format(time.DateOnly))
return err
}
// PurgeBefore applies the privacy retention policy to all user data tables.
func (s *Store) PurgeBefore(ctx context.Context, before time.Time) (int64, error) {
tx, err := s.pool.Begin(ctx)