event

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package event tracks notification events and daily usage summaries.

Index

Constants

View Source
const (
	TypeNotificationCreated   = "notification_created"
	TypeNotificationDelivered = "notification_delivered"
	TypeNotificationFailed    = "notification_failed"
)

Event type constants.

View Source
const (
	SourceCLI     = "cli"
	SourceWebhook = "webhook"
	SourceAPI     = "api"
)

Source constants.

View Source
const (
	EncryptionServerTrusted = "server_trusted"
	EncryptionE2E           = "e2e"
)

Encryption mode constants.

View Source
const (
	FailSubscriptionGone = "subscription_gone"
	FailRateLimited      = "rate_limited"
	FailServerError      = "server_error"
	FailUnknown          = "unknown"
)

Fail reason constants for stable analytics codes.

Variables

This section is empty.

Functions

func DayStartMs

func DayStartMs(t time.Time) int64

DayStartMs returns the UnixMilli timestamp for the start of the UTC day containing t.

func NewEventID

func NewEventID() string

NewEventID generates a new UUIDv7 string for event IDs.

Types

type AccountUsageDayResponse

type AccountUsageDayResponse struct {
	Date                        string `json:"date"`
	NotificationsCreated        int    `json:"notifications_created"`
	DeliveryAttempts            int    `json:"delivery_attempts"`
	DeliveriesSucceeded         int    `json:"deliveries_succeeded"`
	DeliveriesFailed            int    `json:"deliveries_failed"`
	DevicesLost                 int    `json:"devices_lost"`
	NotificationsWithAttachment int    `json:"notifications_with_attachment"`
	AttachmentBytesTotal        int64  `json:"attachment_bytes_total"`
	NotificationsServerTrusted  int    `json:"notifications_server_trusted"`
	NotificationsE2E            int    `json:"notifications_e2e"`
	SourcesCLI                  int    `json:"sources_cli"`
	SourcesWebhook              int    `json:"sources_webhook"`
	SourcesAPI                  int    `json:"sources_api"`
}

AccountUsageDayResponse is a single day's usage for the account dashboard.

func ToAccountUsageDayResponse

func ToAccountUsageDayResponse(s *DailyUsageSummary) AccountUsageDayResponse

ToAccountUsageDayResponse converts a DailyUsageSummary to an account-level response.

type AccountUsageResponse

type AccountUsageResponse struct {
	Data []AccountUsageDayResponse `json:"data"`
}

AccountUsageResponse is the HTTP response for the user account dashboard.

func ToAccountUsageResponse

func ToAccountUsageResponse(summaries []DailyUsageSummary, fromMs, toMs int64) AccountUsageResponse

ToAccountUsageResponse converts a slice of summaries to an account usage response, filling gaps with zero-value days so every day in [fromMs, toMs] is represented.

type DailyUsageSummary

type DailyUsageSummary struct {
	UserID                     string `db:"user_id"`
	DayStartMs                 int64  `db:"day_start_ms"`
	NotificationsTotal         int    `db:"notifications_total"`
	NotificationsServerTrusted int    `db:"notifications_server_trusted"`
	NotificationsE2E           int    `db:"notifications_e2e"`
	NotificationsDelivered     int    `db:"notifications_delivered"`
	NotificationsFailed        int    `db:"notifications_failed"`
	AttachmentsCount           int    `db:"attachments_count"`
	AttachmentsBytesTotal      int64  `db:"attachments_bytes_total"`
	SourcesCLI                 int    `db:"sources_cli"`
	SourcesWebhook             int    `db:"sources_webhook"`
	SourcesAPI                 int    `db:"sources_api"`
	DevicesLost                int    `db:"devices_lost"`
	CreatedAt                  int64  `db:"created_at"`
	UpdatedAt                  int64  `db:"updated_at"`
}

DailyUsageSummary is the DB struct for the daily_usage_summary table.

type DailyUsageSummaryResponse

type DailyUsageSummaryResponse struct {
	Date                        string    `json:"date"`
	NotificationsCreated        int       `json:"notifications_created"`
	DeliveryAttempts            int       `json:"delivery_attempts"`
	DeliveriesSucceeded         int       `json:"deliveries_succeeded"`
	DeliveriesFailed            int       `json:"deliveries_failed"`
	NotificationsWithAttachment int       `json:"notifications_with_attachment"`
	AttachmentBytesTotal        int64     `json:"attachment_bytes_total"`
	NotificationsServerTrusted  int       `json:"notifications_server_trusted"`
	NotificationsE2E            int       `json:"notifications_e2e"`
	SourcesCLI                  int       `json:"sources_cli"`
	SourcesWebhook              int       `json:"sources_webhook"`
	SourcesAPI                  int       `json:"sources_api"`
	DevicesLost                 int       `json:"devices_lost"`
	UpdatedAt                   time.Time `json:"updated_at"`
}

DailyUsageSummaryResponse is the HTTP response struct for dashboard data.

func FillDailyUsageSummaryResponses

func FillDailyUsageSummaryResponses(summaries []DailyUsageSummary, fromMs, toMs int64) []DailyUsageSummaryResponse

FillDailyUsageSummaryResponses fills missing days with zero-value responses.

func ToDailyUsageSummaryResponse

func ToDailyUsageSummaryResponse(s *DailyUsageSummary) DailyUsageSummaryResponse

ToDailyUsageSummaryResponse converts a DailyUsageSummary DB struct to a response.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles HTTP requests for the event domain.

func NewHandler

func NewHandler(svc *Service, logger *slog.Logger) *Handler

NewHandler creates a new event handler.

func (*Handler) AccountUsage

func (h *Handler) AccountUsage(w http.ResponseWriter, r *http.Request)

AccountUsage returns usage metrics for the authenticated user's account dashboard.

func (*Handler) Dashboard

func (h *Handler) Dashboard(w http.ResponseWriter, r *http.Request)

Dashboard returns platform-wide dashboard metrics for the admin panel.

type NotificationEvent

type NotificationEvent struct {
	ID              string  `db:"id"`
	UserID          string  `db:"user_id"`
	EventType       string  `db:"event_type"`
	DeviceID        *string `db:"device_id"`
	Topic           *string `db:"topic"`
	Source          *string `db:"source"`
	EncryptionMode  *string `db:"encryption_mode"`
	AttachmentBytes *int64  `db:"attachment_bytes"`
	FailReason      *string `db:"fail_reason"`
	CreatedAt       int64   `db:"created_at"`
}

NotificationEvent is the DB struct for the notification_events table.

type PlatformDashboardResponse

type PlatformDashboardResponse struct {
	NotificationsCreated        int                         `json:"notifications_created"`
	DeliveryAttempts            int                         `json:"delivery_attempts"`
	DeliveriesSucceeded         int                         `json:"deliveries_succeeded"`
	DeliveriesFailed            int                         `json:"deliveries_failed"`
	DeliverySuccessRate         float64                     `json:"delivery_success_rate"`
	ActiveUsers                 int                         `json:"active_users"`
	NotificationsServerTrusted  int                         `json:"notifications_server_trusted"`
	NotificationsE2E            int                         `json:"notifications_e2e"`
	NotificationsWithAttachment int                         `json:"notifications_with_attachment"`
	AttachmentBytesTotal        int64                       `json:"attachment_bytes_total"`
	SourcesCLI                  int                         `json:"sources_cli"`
	SourcesWebhook              int                         `json:"sources_webhook"`
	SourcesAPI                  int                         `json:"sources_api"`
	DevicesLost                 int                         `json:"devices_lost"`
	DailyBreakdown              []DailyUsageSummaryResponse `json:"daily_breakdown"`
}

PlatformDashboardResponse is the HTTP response for the admin dashboard.

type PlatformSummaryRow

type PlatformSummaryRow struct {
	NotificationsTotal         int   `db:"notifications_total"`
	NotificationsServerTrusted int   `db:"notifications_server_trusted"`
	NotificationsE2E           int   `db:"notifications_e2e"`
	NotificationsDelivered     int   `db:"notifications_delivered"`
	NotificationsFailed        int   `db:"notifications_failed"`
	AttachmentsCount           int   `db:"attachments_count"`
	AttachmentsBytesTotal      int64 `db:"attachments_bytes_total"`
	SourcesCLI                 int   `db:"sources_cli"`
	SourcesWebhook             int   `db:"sources_webhook"`
	SourcesAPI                 int   `db:"sources_api"`
	DevicesLost                int   `db:"devices_lost"`
	ActiveUsers                int   `db:"active_users"`
}

PlatformSummaryRow holds the result of a platform-wide aggregation query.

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

Repository provides data access for the event domain.

func NewRepository

func NewRepository(db *sqlx.DB) *Repository

NewRepository creates a new event repository.

func (*Repository) DeleteEventsOlderThan

func (r *Repository) DeleteEventsOlderThan(ctx context.Context, cutoffMs int64, batchSize int) (int64, error)

DeleteEventsOlderThan deletes events older than cutoffMs in batches. Returns the total number of rows deleted.

func (*Repository) GetDailySummaries

func (r *Repository) GetDailySummaries(ctx context.Context, userID string, fromMs, toMs int64) ([]DailyUsageSummary, error)

GetDailySummaries retrieves daily usage summaries for a user within a time range.

func (*Repository) GetPlatformDailyBreakdown

func (r *Repository) GetPlatformDailyBreakdown(ctx context.Context, fromMs, toMs int64) ([]DailyUsageSummary, error)

GetPlatformDailyBreakdown returns daily totals aggregated across all users.

func (*Repository) GetPlatformSummary

func (r *Repository) GetPlatformSummary(ctx context.Context, fromMs, toMs int64) (*PlatformSummaryRow, error)

GetPlatformSummary aggregates daily_usage_summary across all users within a time range.

func (*Repository) InsertEvent

func (r *Repository) InsertEvent(ctx context.Context, ev *NotificationEvent) error

InsertEvent inserts a notification event and atomically increments the daily summary.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service provides event tracking business logic.

func NewService

func NewService(repo *Repository, log *slog.Logger) *Service

NewService creates a new event service.

func (*Service) CompactOldEvents

func (s *Service) CompactOldEvents(ctx context.Context) (int64, error)

CompactOldEvents deletes raw events older than the retention period.

func (*Service) GetAccountUsage

func (s *Service) GetAccountUsage(ctx context.Context, userID string, days int) (*AccountUsageResponse, error)

GetAccountUsage retrieves daily usage for a user over the last N days, with zero-filled gaps so every day in the range is represented.

func (*Service) GetPlatformDashboard

func (s *Service) GetPlatformDashboard(ctx context.Context, days int) (*PlatformDashboardResponse, error)

GetPlatformDashboard retrieves platform-wide dashboard data for the last N days.

func (*Service) RecordNotificationCreated

func (s *Service) RecordNotificationCreated(ctx context.Context, userID, topic, source, encryptionMode string, attachmentBytes *int64)

RecordNotificationCreated records that a notification was created.

func (*Service) RecordNotificationDelivered

func (s *Service) RecordNotificationDelivered(ctx context.Context, userID, deviceID string)

RecordNotificationDelivered records a successful push delivery to a device.

func (*Service) RecordNotificationFailed

func (s *Service) RecordNotificationFailed(ctx context.Context, userID, deviceID, failReason string)

RecordNotificationFailed records a failed push delivery to a device.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL