-
Notifications
You must be signed in to change notification settings - Fork 3
/
signin.go
202 lines (171 loc) · 4.57 KB
/
signin.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package data
import (
"time"
"github.com/ritsec/ops-bot-iii/ent"
"github.com/ritsec/ops-bot-iii/ent/signin"
"github.com/ritsec/ops-bot-iii/ent/user"
"github.com/ritsec/ops-bot-iii/structs"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
// Signin is the interface for interacting with the signin table
type signin_s struct{}
// Create creates a new signin for a user
func (*signin_s) Create(userID string, signinType signin.Type, ctx ddtrace.SpanContext) (*ent.Signin, error) {
span := tracer.StartSpan(
"data.signin:Create",
tracer.ResourceName("Data.Signin.Create"),
tracer.ChildOf(ctx),
)
defer span.Finish()
entUser, err := User.Get(userID, span.Context())
if err != nil {
return nil, err
}
return Client.Signin.Create().
SetUser(entUser).
SetType(signinType).
Save(Ctx)
}
// GetSignins gets all signins for a user
func (*signin_s) GetSignins(id string, ctx ddtrace.SpanContext) (int, error) {
span := tracer.StartSpan(
"data.signin:GetSignins",
tracer.ResourceName("Data.Signin.GetSignins"),
tracer.ChildOf(ctx),
)
defer span.Finish()
return Client.Signin.Query().
Where(signin.HasUserWith(user.IDEQ(id))).
Count(Ctx)
}
// GetSigninsByType gets all signins for a user of a specific type
func (*signin_s) GetSigninsByType(id string, signinType signin.Type, ctx ddtrace.SpanContext) (int, error) {
span := tracer.StartSpan(
"data.signin:GetSigninsByType",
tracer.ResourceName("Data.Signin.GetSigninsByType"),
tracer.ChildOf(ctx),
)
defer span.Finish()
return Client.Signin.Query().
Where(
signin.HasUserWith(user.IDEQ(id)),
signin.TypeEQ(signinType),
).
Count(Ctx)
}
// RecentSignin checks if a user has signed in recently
func (*signin_s) RecentSignin(userID string, signinType signin.Type, ctx ddtrace.SpanContext) (bool, error) {
span := tracer.StartSpan(
"data.signin:RecentSignin",
tracer.ResourceName("Data.Signin.RecentSignin"),
tracer.ChildOf(ctx),
)
defer span.Finish()
ok, err := Client.Signin.Query().
Where(
signin.HasUserWith(user.IDEQ(userID)),
signin.TypeEQ(signinType),
signin.TimestampGTE(time.Now().Add(-12*time.Hour)),
).
Exist(Ctx)
if err != nil {
return false, err
}
return ok, nil
}
func (*signin_s) Query(delta time.Duration, signinType signin.Type, ctx ddtrace.SpanContext) (structs.PairList[string], error) {
span := tracer.StartSpan(
"data.signin:Query",
tracer.ResourceName("Data.Signin.Query"),
tracer.ChildOf(ctx),
)
defer span.Finish()
var (
entSignins []*ent.Signin
err error
)
if signinType == "All" {
entSignins, err = Client.Signin.Query().
Where(
signin.TimestampGTE(time.Now().Add(-delta)),
).
WithUser().
All(Ctx)
} else {
entSignins, err = Client.Signin.Query().
Where(
signin.TypeEQ(signinType),
signin.TimestampGTE(time.Now().Add(-delta)),
).
WithUser().
All(Ctx)
}
if err != nil {
return nil, err
}
userCount := make(map[string]int)
for _, entSignin := range entSignins {
userCount[entSignin.Edges.User.ID]++
}
pairList := make(structs.PairList[string], len(userCount))
i := 0
for userID, count := range userCount {
pairList[i] = structs.Pair[string]{Key: userID, Value: count}
i++
}
pairList.Sort()
pairList.Reverse()
return pairList, nil
}
func (s *signin_s) DQuery(date time.Time, signinType signin.Type, ctx ddtrace.SpanContext) (structs.PairList[string], error) {
span := tracer.StartSpan(
"data.signin:Query",
tracer.ResourceName("Data.Signin.Query"),
tracer.ChildOf(ctx),
)
defer span.Finish()
var (
entSignins []*ent.Signin
err error
)
// Calculate start and end of the specified date
// Make the time 00:00:00
startOfDay := date.Truncate(24 * time.Hour)
// Set end time to end of the day 11:59:59
endOfDay := startOfDay.Add(24 * time.Hour).Add(-time.Second)
if signinType == "All" {
entSignins, err = Client.Signin.Query().
Where(
signin.TimestampGTE(startOfDay),
signin.TimestampLTE(endOfDay),
).
WithUser().
All(Ctx)
} else {
entSignins, err = Client.Signin.Query().
Where(
signin.TypeEQ(signinType),
signin.TimestampGTE(startOfDay),
signin.TimestampLTE(endOfDay),
).
WithUser().
All(Ctx)
}
if err != nil {
return nil, err
}
userCount := make(map[string]int)
for _, entSignin := range entSignins {
userCount[entSignin.Edges.User.ID]++
}
pairList := make(structs.PairList[string], len(userCount))
i := 0
for userID, count := range userCount {
pairList[i] = structs.Pair[string]{Key: userID, Value: count}
i++
}
pairList.Sort()
pairList.Reverse()
return pairList, nil
}