Skip to content

Commit

Permalink
feat: add support for tracking host
Browse files Browse the repository at this point in the history
  • Loading branch information
Dotunj committed Jun 11, 2022
1 parent 4be8c78 commit ed10ae5
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 52 deletions.
8 changes: 4 additions & 4 deletions analytics/active_group_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type ActiveGroupAnalytics struct {
groupRepo datastore.GroupRepository
eventRepo datastore.EventRepository
client AnalyticsClient
source AnalyticsSource
host string
}

func newActiveGroupAnalytics(groupRepo datastore.GroupRepository, eventRepo datastore.EventRepository, client AnalyticsClient, source AnalyticsSource) *ActiveGroupAnalytics {
func newActiveGroupAnalytics(groupRepo datastore.GroupRepository, eventRepo datastore.EventRepository, client AnalyticsClient, host string) *ActiveGroupAnalytics {
return &ActiveGroupAnalytics{
groupRepo: groupRepo,
eventRepo: eventRepo,
client: client,
source: source,
host: host,
}

}
Expand Down Expand Up @@ -50,7 +50,7 @@ func (a *ActiveGroupAnalytics) Track() error {
}
}

return a.client.Export(a.Name(), Event{"Count": count, "Source": a.source})
return a.client.Export(a.Name(), Event{"Count": count, "Host": a.host})

}

Expand Down
2 changes: 1 addition & 1 deletion analytics/active_group_analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func provideActiveGroupAnalytics(ctrl *gomock.Controller) *ActiveGroupAnalytics
eventRepo := mocks.NewMockEventRepository(ctrl)
client := NewNoopAnalyticsClient()

return newActiveGroupAnalytics(groupRepo, eventRepo, client, OSSAnalyticsSource)
return newActiveGroupAnalytics(groupRepo, eventRepo, client, TestHost)
}

func Test_TrackActiveGroupAnalytics(t *testing.T) {
Expand Down
41 changes: 14 additions & 27 deletions analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/base64"
"errors"
"strings"

"github.com/dukex/mixpanel"
"github.com/frain-dev/convoy/config"
Expand All @@ -14,18 +13,14 @@ import (
log "github.com/sirupsen/logrus"
)

type AnalyticsSource string

const (
DailyEventCount string = "Daily Event Count"
DailyOrganisationCount string = "Daily Organization Count"
DailyGroupCount string = "Daily Project Count"
DailyActiveGroupCount string = "Daily Active Project Count"
DailyUserCount string = "Daily User Count"
MixPanelDevToken string = "YTAwYWI1ZWE3OTE2MzQwOWEwMjk4ZTA1NTNkNDQ0M2M="
MixPanelProdToken string = "YWViNzUwYWRmYjM0YTZmZjJkMzg2YTYyYWVhY2M2NWI="
OSSAnalyticsSource AnalyticsSource = "OSS"
CloudAnalyticsSource AnalyticsSource = "Cloud"
DailyEventCount string = "Daily Event Count"
DailyOrganisationCount string = "Daily Organization Count"
DailyGroupCount string = "Daily Project Count"
DailyActiveGroupCount string = "Daily Active Project Count"
DailyUserCount string = "Daily User Count"
MixPanelDevToken string = "YTAwYWI1ZWE3OTE2MzQwOWEwMjk4ZTA1NTNkNDQ0M2M="
MixPanelProdToken string = "YWViNzUwYWRmYjM0YTZmZjJkMzg2YTYyYWVhY2M2NWI="
)

type Tracker interface {
Expand Down Expand Up @@ -53,7 +48,7 @@ type Analytics struct {
Repo *Repo
trackers analyticsMap
client AnalyticsClient
source AnalyticsSource
host string
}

func newAnalytics(Repo *Repo, cfg config.Configuration) (*Analytics, error) {
Expand All @@ -63,8 +58,8 @@ func newAnalytics(Repo *Repo, cfg config.Configuration) (*Analytics, error) {
}

a := &Analytics{Repo: Repo, client: client}
a.host = cfg.Host

a.RegisterSource(cfg)
a.RegisterTrackers()
return a, nil
}
Expand Down Expand Up @@ -108,23 +103,15 @@ func (a *Analytics) trackDailyAnalytics() {

func (a *Analytics) RegisterTrackers() {
a.trackers = analyticsMap{
DailyEventCount: newEventAnalytics(a.Repo.EventRepo, a.Repo.GroupRepo, a.Repo.OrgRepo, a.client, a.source),
DailyOrganisationCount: newOrganisationAnalytics(a.Repo.OrgRepo, a.client, a.source),
DailyGroupCount: newGroupAnalytics(a.Repo.GroupRepo, a.client, a.source),
DailyActiveGroupCount: newActiveGroupAnalytics(a.Repo.GroupRepo, a.Repo.EventRepo, a.client, a.source),
DailyUserCount: newUserAnalytics(a.Repo.UserRepo, a.client, a.source),
DailyEventCount: newEventAnalytics(a.Repo.EventRepo, a.Repo.GroupRepo, a.Repo.OrgRepo, a.client, a.host),
DailyOrganisationCount: newOrganisationAnalytics(a.Repo.OrgRepo, a.client, a.host),
DailyGroupCount: newGroupAnalytics(a.Repo.GroupRepo, a.client, a.host),
DailyActiveGroupCount: newActiveGroupAnalytics(a.Repo.GroupRepo, a.Repo.EventRepo, a.client, a.host),
DailyUserCount: newUserAnalytics(a.Repo.UserRepo, a.client, a.host),
}

}

func (a *Analytics) RegisterSource(cfg config.Configuration) {
a.source = OSSAnalyticsSource

if strings.Contains(strings.ToLower(cfg.BaseUrl), "cloud.getconvoy.io") {
a.source = CloudAnalyticsSource
}
}

type MixPanelClient struct {
client mixpanel.Mixpanel
}
Expand Down
8 changes: 4 additions & 4 deletions analytics/event_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ type EventAnalytics struct {
groupRepo datastore.GroupRepository
orgRepo datastore.OrganisationRepository
client AnalyticsClient
source AnalyticsSource
host string
}

func newEventAnalytics(eventRepo datastore.EventRepository, groupRepo datastore.GroupRepository, orgRepo datastore.OrganisationRepository, client AnalyticsClient, source AnalyticsSource) *EventAnalytics {
func newEventAnalytics(eventRepo datastore.EventRepository, groupRepo datastore.GroupRepository, orgRepo datastore.OrganisationRepository, client AnalyticsClient, host string) *EventAnalytics {
return &EventAnalytics{
eventRepo: eventRepo,
groupRepo: groupRepo,
orgRepo: orgRepo,
client: client,
source: source,
host: host,
}
}

Expand All @@ -45,7 +45,7 @@ func (ea *EventAnalytics) Track() error {
continue
}

err = ea.client.Export(ea.Name(), Event{"Count": pagination.Total, "Project": group.Name, "Organization": org.Name, "Source": ea.source})
err = ea.client.Export(ea.Name(), Event{"Count": pagination.Total, "Project": group.Name, "Organization": org.Name, "Host": ea.host})
if err != nil {
log.WithError(err).Error("failed to load export metrics")
continue
Expand Down
4 changes: 3 additions & 1 deletion analytics/event_analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/stretchr/testify/require"
)

var TestHost = "http://test-host.com"

func provideEventAnalytics(ctrl *gomock.Controller) *EventAnalytics {
eventRepo := mocks.NewMockEventRepository(ctrl)
groupRepo := mocks.NewMockGroupRepository(ctrl)
orgRepo := mocks.NewMockOrganisationRepository(ctrl)
client := NewNoopAnalyticsClient()

return newEventAnalytics(eventRepo, groupRepo, orgRepo, client, OSSAnalyticsSource)
return newEventAnalytics(eventRepo, groupRepo, orgRepo, client, TestHost)
}

func Test_TrackEventAnalytics(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions analytics/group_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
type GroupAnalytics struct {
groupRepo datastore.GroupRepository
client AnalyticsClient
source AnalyticsSource
host string
}

func newGroupAnalytics(groupRepo datastore.GroupRepository, client AnalyticsClient, source AnalyticsSource) *GroupAnalytics {
func newGroupAnalytics(groupRepo datastore.GroupRepository, client AnalyticsClient, host string) *GroupAnalytics {
return &GroupAnalytics{
groupRepo: groupRepo,
client: client,
source: source,
host: host,
}
}

Expand All @@ -26,7 +26,7 @@ func (g *GroupAnalytics) Track() error {
return err
}

return g.client.Export(g.Name(), Event{"Count": len(groups), "Source": g.source})
return g.client.Export(g.Name(), Event{"Count": len(groups), "Host": g.host})
}

func (g *GroupAnalytics) Name() string {
Expand Down
2 changes: 1 addition & 1 deletion analytics/group_analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func provideGroupAnalytics(ctrl *gomock.Controller) *GroupAnalytics {
groupRepo := mocks.NewMockGroupRepository(ctrl)
client := NewNoopAnalyticsClient()

return newGroupAnalytics(groupRepo, client, OSSAnalyticsSource)
return newGroupAnalytics(groupRepo, client, TestHost)
}

func Test_TrackGroupAnalytics(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions analytics/organisation_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
type OrganisationAnalytics struct {
orgRepo datastore.OrganisationRepository
client AnalyticsClient
source AnalyticsSource
host string
}

func newOrganisationAnalytics(orgRepo datastore.OrganisationRepository, client AnalyticsClient, source AnalyticsSource) *OrganisationAnalytics {
func newOrganisationAnalytics(orgRepo datastore.OrganisationRepository, client AnalyticsClient, host string) *OrganisationAnalytics {
return &OrganisationAnalytics{
orgRepo: orgRepo,
client: client,
source: source,
host: host,
}
}

Expand All @@ -26,7 +26,7 @@ func (o *OrganisationAnalytics) Track() error {
return err
}

return o.client.Export(o.Name(), Event{"Count": pagination.Total, "Source": o.source})
return o.client.Export(o.Name(), Event{"Count": pagination.Total, "Host": o.host})
}

func (o *OrganisationAnalytics) Name() string {
Expand Down
2 changes: 1 addition & 1 deletion analytics/organisation_analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func provideOrganisationAnalytics(ctrl *gomock.Controller) *OrganisationAnalytic
orgRepo := mocks.NewMockOrganisationRepository(ctrl)
client := NewNoopAnalyticsClient()

return newOrganisationAnalytics(orgRepo, client, OSSAnalyticsSource)
return newOrganisationAnalytics(orgRepo, client, TestHost)
}

func Test_TrackOrganisationAnalytics(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions analytics/user_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
type UserAnalytics struct {
userRepo datastore.UserRepository
client AnalyticsClient
source AnalyticsSource
host string
}

func newUserAnalytics(userRepo datastore.UserRepository, client AnalyticsClient, source AnalyticsSource) *UserAnalytics {
func newUserAnalytics(userRepo datastore.UserRepository, client AnalyticsClient, host string) *UserAnalytics {
return &UserAnalytics{
userRepo: userRepo,
client: client,
source: source,
host: host,
}
}

Expand All @@ -26,7 +26,7 @@ func (u *UserAnalytics) Track() error {
return err
}

return u.client.Export(u.Name(), Event{"Count": pagination.Total, "Source": u.source})
return u.client.Export(u.Name(), Event{"Count": pagination.Total, "Host": u.host})
}

func (u *UserAnalytics) Name() string {
Expand Down
2 changes: 1 addition & 1 deletion analytics/user_analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func provideUserAnalytics(ctrl *gomock.Controller) *UserAnalytics {
userRepo := mocks.NewMockUserRepository(ctrl)
client := NewNoopAnalyticsClient()

return newUserAnalytics(userRepo, client, OSSAnalyticsSource)
return newUserAnalytics(userRepo, client, TestHost)
}

func Test_TrackUserAnalytics(t *testing.T) {
Expand Down

0 comments on commit ed10ae5

Please sign in to comment.