Skip to content

Commit cd2741a

Browse files
committed
active users in last30
1 parent 2824505 commit cd2741a

File tree

6 files changed

+45
-0
lines changed

6 files changed

+45
-0
lines changed

client/telemetry/TelemetryEventClient.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ func (impl *TelemetryEventClientImpl) SendSummaryEvent(eventType string) error {
339339
payload.PolicyCount = impl.getPolicyCount()
340340
payload.AppliedPolicyRowCount = impl.getAppliedPolicyRowCount()
341341
payload.PhysicalClusterCount, payload.IsolatedClusterCount = impl.getClusterCounts()
342+
payload.ActiveUsersLast30Days = impl.getActiveUsersLast30Days()
342343

343344
payload.ClusterProvider, err = impl.GetCloudProvider()
344345
if err != nil {

client/telemetry/TelemetryEventClientExtended.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ func (impl *TelemetryEventClientImplExtended) SendSummaryEvent(eventType string)
347347
payload.PolicyCount = impl.getPolicyCount()
348348
payload.AppliedPolicyRowCount = impl.getAppliedPolicyRowCount()
349349
payload.PhysicalClusterCount, payload.IsolatedClusterCount = impl.getClusterCounts()
350+
payload.ActiveUsersLast30Days = impl.getActiveUsersLast30Days()
350351

351352
payload.ClusterProvider, err = impl.GetCloudProvider()
352353
if err != nil {

client/telemetry/bean.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ type TelemetryEventEA struct {
8787
AppliedPolicyRowCount int `json:"appliedPolicyRowCount,omitempty"`
8888
PhysicalClusterCount int `json:"physicalClusterCount,omitempty"`
8989
IsolatedClusterCount int `json:"isolatedClusterCount,omitempty"`
90+
ActiveUsersLast30Days int `json:"activeUsersLast30Days,omitempty"`
9091
}
9192

9293
const AppsCount int = 50
@@ -160,4 +161,5 @@ type TelemetryEventDto struct {
160161
AppliedPolicyRowCount int `json:"appliedPolicyRowCount,omitempty"`
161162
PhysicalClusterCount int `json:"physicalClusterCount,omitempty"`
162163
IsolatedClusterCount int `json:"isolatedClusterCount,omitempty"`
164+
ActiveUsersLast30Days int `json:"activeUsersLast30Days,omitempty"`
163165
}

client/telemetry/telemetryQueries.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,19 @@ func (impl *TelemetryEventClientImpl) getJobPipelineSucceededLast24h() int {
156156
func (impl *TelemetryEventClientImpl) getAppliedPolicyRowCount() int {
157157
return 0
158158
}
159+
160+
func (impl *TelemetryEventClientImpl) getActiveUsersLast30Days() int {
161+
if impl.userAuditService == nil {
162+
impl.logger.Warnw("userAuditService not available for active users count")
163+
return -1
164+
}
165+
166+
count, err := impl.userAuditService.GetActiveUsersCountInLast30Days()
167+
if err != nil {
168+
impl.logger.Errorw("error getting active users count in last 30 days", "err", err)
169+
return -1
170+
}
171+
172+
impl.logger.Debugw("counted active users in last 30 days", "count", count)
173+
return count
174+
}

pkg/auth/user/UserAuditService.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type UserAuditService interface {
3636
GetLatestByUserId(userId int32) (*UserAudit, error)
3737
GetLatestUser() (*UserAudit, error)
3838
Update(userAudit *UserAudit) error
39+
GetActiveUsersCountInLast30Days() (int, error)
3940
}
4041

4142
type UserAuditServiceImpl struct {
@@ -123,3 +124,13 @@ func (impl UserAuditServiceImpl) GetLatestUser() (*UserAudit, error) {
123124
UpdatedOn: userAuditDb.UpdatedOn,
124125
}, nil
125126
}
127+
128+
func (impl UserAuditServiceImpl) GetActiveUsersCountInLast30Days() (int, error) {
129+
impl.logger.Info("Getting active users count in last 30 days")
130+
count, err := impl.userAuditRepository.GetActiveUsersCountInLast30Days()
131+
if err != nil {
132+
impl.logger.Errorw("error while getting active users count in last 30 days", "err", err)
133+
return 0, err
134+
}
135+
return count, nil
136+
}

pkg/auth/user/repository/UserAuditRepository.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type UserAuditRepository interface {
3939
GetLatestByUserId(userId int32) (*UserAudit, error)
4040
GetLatestUser() (*UserAudit, error)
4141
Update(userAudit *UserAudit) error
42+
GetActiveUsersCountInLast30Days() (int, error)
4243
}
4344

4445
type UserAuditRepositoryImpl struct {
@@ -86,3 +87,16 @@ func (impl UserAuditRepositoryImpl) GetLatestUser() (*UserAudit, error) {
8687
Select()
8788
return userAudit, err
8889
}
90+
91+
func (impl UserAuditRepositoryImpl) GetActiveUsersCountInLast30Days() (int, error) {
92+
var count int
93+
// Calculate the date 30 days ago from now
94+
thirtyDaysAgo := time.Now().AddDate(0, 0, -30)
95+
96+
// Count distinct users who have been active (have updated_on) in the last 30 days
97+
_, err := impl.dbConnection.Query(&count,
98+
"SELECT COUNT(DISTINCT user_id) FROM user_audit WHERE updated_on >= ?",
99+
thirtyDaysAgo)
100+
101+
return count, err
102+
}

0 commit comments

Comments
 (0)