-
Notifications
You must be signed in to change notification settings - Fork 27
/
project_monthly_report.go
79 lines (69 loc) · 1.62 KB
/
project_monthly_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
package analytic
import (
"errors"
"fmt"
"time"
"github.com/authgear/authgear-server/pkg/lib/infra/db/auditdb"
"github.com/authgear/authgear-server/pkg/lib/infra/db/globaldb"
)
type ProjectMonthlyReportOptions struct {
Year int
Month int
}
type ProjectMonthlyReport struct {
GlobalHandle *globaldb.Handle
GlobalDBStore *GlobalDBStore
AuditDBHandle *auditdb.ReadHandle
AuditDBStore *AuditDBReadStore
}
func (r *ProjectMonthlyReport) Run(options *ProjectMonthlyReportOptions) (data *ReportData, err error) {
firstDayOfMonth := time.Date(options.Year, time.Month(options.Month), 1, 0, 0, 0, 0, time.UTC)
var appIDs []string
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)
}
return nil
}); err != nil {
return
}
values := make([][]interface{}, len(appIDs))
for i, appID := range appIDs {
monthlyActiveUserCount := 0
err = r.AuditDBHandle.ReadOnly(func() (e error) {
c, err := r.AuditDBStore.GetAnalyticCountByType(
appID,
MonthlyActiveUserCountType,
&firstDayOfMonth,
)
if err != nil {
if !errors.Is(err, ErrAnalyticCountNotFound) {
return fmt.Errorf("failed to fetch monthly active user: %w", err)
}
} else {
monthlyActiveUserCount = c.Count
}
return nil
})
if err != nil {
return
}
values[i] = []interface{}{
options.Year,
options.Month,
appID,
monthlyActiveUserCount,
}
}
data = &ReportData{
Header: []interface{}{
"Year",
"Month",
"Project ID",
"Monthly active user",
},
Values: values,
}
return data, nil
}