Skip to content

Commit 682070d

Browse files
committed
improve telemetry
1 parent 04e22a1 commit 682070d

15 files changed

Lines changed: 490 additions & 94 deletions

api

351 KB
Binary file not shown.

internal/app/app.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type AppInterface interface {
4646
GetMessageHistoryRepository() domain.MessageHistoryRepository
4747
GetContactListRepository() domain.ContactListRepository
4848
GetTransactionalNotificationRepository() domain.TransactionalNotificationRepository
49+
GetTelemetryRepository() domain.TelemetryRepository
4950

5051
// Server status methods
5152
IsServerCreated() bool
@@ -81,6 +82,7 @@ type App struct {
8182
transactionalNotificationRepo domain.TransactionalNotificationRepository
8283
messageHistoryRepo domain.MessageHistoryRepository
8384
webhookEventRepo domain.WebhookEventRepository
85+
telemetryRepo domain.TelemetryRepository
8486

8587
// Services
8688
authService *service.AuthService
@@ -288,6 +290,7 @@ func (a *App) InitRepositories() error {
288290
a.transactionalNotificationRepo = repository.NewTransactionalNotificationRepository(a.workspaceRepo)
289291
a.messageHistoryRepo = repository.NewMessageHistoryRepository(a.workspaceRepo)
290292
a.webhookEventRepo = repository.NewWebhookEventRepository(a.workspaceRepo)
293+
a.telemetryRepo = repository.NewTelemetryRepository(a.workspaceRepo)
291294

292295
return nil
293296
}
@@ -531,6 +534,7 @@ func (a *App) InitServices() error {
531534
Enabled: a.config.Telemetry,
532535
APIEndpoint: a.config.APIEndpoint,
533536
WorkspaceRepo: a.workspaceRepo,
537+
TelemetryRepo: a.telemetryRepo,
534538
Logger: a.logger,
535539
HTTPClient: httpClient, // Reuse the HTTP client created above
536540
}
@@ -830,6 +834,10 @@ func (a *App) GetTransactionalNotificationRepository() domain.TransactionalNotif
830834
return a.transactionalNotificationRepo
831835
}
832836

837+
func (a *App) GetTelemetryRepository() domain.TelemetryRepository {
838+
return a.telemetryRepo
839+
}
840+
833841
// SetHandler allows setting a custom HTTP handler
834842
func (a *App) SetHandler(handler http.Handler) {
835843
a.mux = handler.(*http.ServeMux)

internal/domain/mocks/mock_telemetry_repository.go

Lines changed: 157 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/domain/mocks/mock_workspace_repository.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/domain/telemetry.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package domain
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
)
7+
8+
//go:generate mockgen -destination mocks/mock_telemetry_repository.go -package mocks github.com/Notifuse/notifuse/internal/domain TelemetryRepository
9+
10+
// TelemetryMetrics represents aggregated metrics for a workspace
11+
type TelemetryMetrics struct {
12+
ContactsCount int `json:"contacts_count"`
13+
BroadcastsCount int `json:"broadcasts_count"`
14+
TransactionalCount int `json:"transactional_count"`
15+
MessagesCount int `json:"messages_count"`
16+
ListsCount int `json:"lists_count"`
17+
UsersCount int `json:"users_count"`
18+
LastMessageAt string `json:"last_message_at"`
19+
}
20+
21+
// TelemetryRepository defines the interface for telemetry data operations
22+
type TelemetryRepository interface {
23+
// GetWorkspaceMetrics retrieves aggregated metrics for a specific workspace
24+
GetWorkspaceMetrics(ctx context.Context, workspaceID string) (*TelemetryMetrics, error)
25+
26+
// CountContacts counts the total number of contacts in a workspace
27+
CountContacts(ctx context.Context, db *sql.DB) (int, error)
28+
29+
// CountBroadcasts counts the total number of broadcasts in a workspace
30+
CountBroadcasts(ctx context.Context, db *sql.DB) (int, error)
31+
32+
// CountTransactional counts the total number of transactional notifications in a workspace
33+
CountTransactional(ctx context.Context, db *sql.DB) (int, error)
34+
35+
// CountMessages counts the total number of messages in a workspace
36+
CountMessages(ctx context.Context, db *sql.DB) (int, error)
37+
38+
// CountLists counts the total number of lists in a workspace
39+
CountLists(ctx context.Context, db *sql.DB) (int, error)
40+
41+
// CountUsers counts the total number of users in a workspace from the system database
42+
CountUsers(ctx context.Context, systemDB *sql.DB, workspaceID string) (int, error)
43+
44+
// GetLastMessageAt gets the timestamp of the last message sent from the workspace
45+
GetLastMessageAt(ctx context.Context, db *sql.DB) (string, error)
46+
}

internal/domain/workspace.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,7 @@ type WorkspaceRepository interface {
642642

643643
// Database management
644644
GetConnection(ctx context.Context, workspaceID string) (*sql.DB, error)
645+
GetSystemConnection(ctx context.Context) (*sql.DB, error)
645646
CreateDatabase(ctx context.Context, workspaceID string) error
646647
DeleteDatabase(ctx context.Context, workspaceID string) error
647648

0 commit comments

Comments
 (0)