-
Notifications
You must be signed in to change notification settings - Fork 28
/
project_weekly_report.go
152 lines (135 loc) · 3.8 KB
/
project_weekly_report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package analytic
import (
"errors"
"fmt"
"time"
"github.com/authgear/authgear-server/pkg/api/event/nonblocking"
"github.com/authgear/authgear-server/pkg/lib/infra/db/appdb"
"github.com/authgear/authgear-server/pkg/lib/infra/db/auditdb"
"github.com/authgear/authgear-server/pkg/lib/infra/db/globaldb"
"github.com/authgear/authgear-server/pkg/util/timeutil"
)
type ProjectWeeklyReportOptions struct {
Year int
Week int
PortalAppID string
}
type ProjectWeeklyReport struct {
GlobalHandle *globaldb.Handle
GlobalDBStore *GlobalDBStore
AppDBHandle *appdb.Handle
AppDBStore *AppDBStore
AuditDBHandle *auditdb.ReadHandle
MeterAuditDBStore MeterAuditDBReadStore
AuditDBStore *AuditDBReadStore
}
func (r *ProjectWeeklyReport) Run(options *ProjectWeeklyReportOptions) (data *ReportData, err error) {
rangeFormPtr, err := timeutil.FirstDayOfISOWeek(options.Year, options.Week, time.UTC)
if err != nil {
err = fmt.Errorf("invalid year or week number: %w", err)
return
}
rangeFrom := *rangeFormPtr
rangeTo := rangeFrom.AddDate(0, 0, 7)
var appIDs []string
var appOwners []*AppCollaborator
if err = r.GlobalHandle.WithTx(func() error {
appIDs, err = r.GlobalDBStore.GetAppIDs()
if err != nil {
return fmt.Errorf("failed to fetch app ids: %w", err)
}
appOwners, err = r.GlobalDBStore.GetAppOwners(nil, nil)
if err != nil {
return fmt.Errorf("failed to fetch app owners: %w", err)
}
return nil
}); err != nil {
return
}
userIDs := []string{}
userIDsSet := map[string]interface{}{}
appToOwnerUserIDMap := map[string]string{}
for _, ap := range appOwners {
appToOwnerUserIDMap[ap.AppID] = ap.UserID
if _, ok := userIDsSet[ap.UserID]; !ok {
userIDsSet[ap.UserID] = struct{}{}
userIDs = append(userIDs, ap.UserID)
}
}
var userIDToEmailMap map[string]string
if err = r.AppDBHandle.WithTx(func() (e error) {
userIDToEmailMap, err = r.AppDBStore.GetUserVerifiedEmails(options.PortalAppID, userIDs)
return err
}); err != nil {
err = fmt.Errorf("failed to fetch owner's email: %w", err)
return
}
values := make([][]interface{}, len(appIDs))
for i, appID := range appIDs {
ownerID := appToOwnerUserIDMap[appID]
email := userIDToEmailMap[ownerID]
countMap := map[string]int{
string(nonblocking.UserAuthenticated): 0,
string(nonblocking.UserCreated): 0,
string(nonblocking.EmailSent): 0,
string(nonblocking.SMSSent): 0,
}
weeklyActiveUserCount := 0
err = r.AuditDBHandle.ReadOnly(func() (e error) {
for activityType := range countMap {
countMap[activityType], err = r.MeterAuditDBStore.GetCountByActivityType(
appID, activityType, &rangeFrom, &rangeTo)
if err != nil {
err = fmt.Errorf("failed to fetch count for activityType %s: %w", activityType, err)
return err
}
}
c, err := r.AuditDBStore.GetAnalyticCountByType(
appID,
WeeklyActiveUserCountType,
&rangeFrom,
)
if err != nil {
if !errors.Is(err, ErrAnalyticCountNotFound) {
return fmt.Errorf("failed to fetch weekly active user: %w", err)
}
} else {
weeklyActiveUserCount = c.Count
}
return nil
})
if err != nil {
return
}
values[i] = []interface{}{
options.Year,
options.Week,
rangeFrom.UTC().Format(time.RFC3339),
rangeTo.UTC().Format(time.RFC3339),
appID,
email,
countMap[string(nonblocking.UserCreated)],
countMap[string(nonblocking.UserAuthenticated)],
countMap[string(nonblocking.EmailSent)],
countMap[string(nonblocking.SMSSent)],
weeklyActiveUserCount,
}
}
data = &ReportData{
Header: []interface{}{
"Year",
"Week",
"Range From",
"Range To",
"Project ID",
"Owner email",
"Number of signup",
"Number of login",
"Email sent",
"SMS sent",
"Weekly active user",
},
Values: values,
}
return
}