Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed host to instance ID #821

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions analytics/active_group_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
)

type ActiveGroupAnalytics struct {
groupRepo datastore.GroupRepository
eventRepo datastore.EventRepository
client AnalyticsClient
host string
groupRepo datastore.GroupRepository
eventRepo datastore.EventRepository
client AnalyticsClient
instanceID string
}

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

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

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

}

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, TestHost)
return newActiveGroupAnalytics(groupRepo, eventRepo, client, TestInstanceID)
}

func Test_TrackActiveGroupAnalytics(t *testing.T) {
Expand Down
52 changes: 28 additions & 24 deletions analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ type Repo struct {
}

type Analytics struct {
Repo *Repo
trackers analyticsMap
client AnalyticsClient
host string
Repo *Repo
trackers analyticsMap
client AnalyticsClient
instanceID string
}

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

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

config, err := a.Repo.ConfigRepo.LoadConfiguration(context.Background())
if err != nil {
if errors.Is(err, datastore.ErrConfigNotFound) {
return nil, err
}

log.WithError(err).Error("failed to track metrics")
return nil, err
}

if !config.IsAnalyticsEnabled {
return nil, nil
}

a.instanceID = config.UID

a.RegisterTrackers()
return a, nil
Expand All @@ -68,7 +84,8 @@ func TrackDailyAnalytics(Repo *Repo, cfg config.Configuration) func(context.Cont
return func(ctx context.Context, t *asynq.Task) error {
a, err := newAnalytics(Repo, cfg)
if err != nil {
log.Fatal(err)
log.WithError(err).Error("Failed to initialize analytics")
return nil
}

a.trackDailyAnalytics()
Expand All @@ -78,19 +95,6 @@ func TrackDailyAnalytics(Repo *Repo, cfg config.Configuration) func(context.Cont
}

func (a *Analytics) trackDailyAnalytics() {
config, err := a.Repo.ConfigRepo.LoadConfiguration(context.Background())
if err != nil {
if errors.Is(err, datastore.ErrConfigNotFound) {
return
}

log.WithError(err).Error("failed to track metrics")
}

if !config.IsAnalyticsEnabled {
return
}

for _, tracker := range a.trackers {
go func(tracker Tracker) {
err := tracker.Track()
Expand All @@ -103,11 +107,11 @@ 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.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),
DailyEventCount: newEventAnalytics(a.Repo.EventRepo, a.Repo.GroupRepo, a.Repo.OrgRepo, a.client, a.instanceID),
DailyOrganisationCount: newOrganisationAnalytics(a.Repo.OrgRepo, a.client, a.instanceID),
DailyGroupCount: newGroupAnalytics(a.Repo.GroupRepo, a.client, a.instanceID),
DailyActiveGroupCount: newActiveGroupAnalytics(a.Repo.GroupRepo, a.Repo.EventRepo, a.client, a.instanceID),
DailyUserCount: newUserAnalytics(a.Repo.UserRepo, a.client, a.instanceID),
}

}
Expand Down
24 changes: 12 additions & 12 deletions analytics/event_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
)

type EventAnalytics struct {
eventRepo datastore.EventRepository
groupRepo datastore.GroupRepository
orgRepo datastore.OrganisationRepository
client AnalyticsClient
host string
eventRepo datastore.EventRepository
groupRepo datastore.GroupRepository
orgRepo datastore.OrganisationRepository
client AnalyticsClient
instanceID string
}

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

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

var TestHost = "http://test-host.com"
var TestInstanceID = "3d9c49ce-8367-43ec-8884-568c8d43faec"

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, TestHost)
return newEventAnalytics(eventRepo, groupRepo, orgRepo, client, TestInstanceID)
}

func Test_TrackEventAnalytics(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions analytics/group_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
)

type GroupAnalytics struct {
groupRepo datastore.GroupRepository
client AnalyticsClient
host string
groupRepo datastore.GroupRepository
client AnalyticsClient
instanceID string
}

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

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

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

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, TestHost)
return newGroupAnalytics(groupRepo, client, TestInstanceID)
}

func Test_TrackGroupAnalytics(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions analytics/organisation_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
)

type OrganisationAnalytics struct {
orgRepo datastore.OrganisationRepository
client AnalyticsClient
host string
orgRepo datastore.OrganisationRepository
client AnalyticsClient
instanceID string
}

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

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

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

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, TestHost)
return newOrganisationAnalytics(orgRepo, client, TestInstanceID)
}

func Test_TrackOrganisationAnalytics(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions analytics/user_analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
)

type UserAnalytics struct {
userRepo datastore.UserRepository
client AnalyticsClient
host string
userRepo datastore.UserRepository
client AnalyticsClient
instanceID string
}

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

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

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

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, TestHost)
return newUserAnalytics(userRepo, client, TestInstanceID)
}

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