-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
134 lines (122 loc) · 2.93 KB
/
db.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
package main
import (
"context"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
type activity struct {
ActivityType string `bson:"activityType"`
BoardId string `bson:"boardId"`
ListId string `bson:"listId"`
UserId string `bson:"userId"`
Card card `bson:"card"`
}
type card struct {
ID string `bson:"_id"`
Members []string `bson:"members"`
UserID string `bson:"userId"`
Title string `bson:"title"`
}
type board struct {
ID string `bson:"_id"`
Title string `bson:"title"`
Labels []interface{} `bson:"labels"`
Slug string `bson:"slug"`
}
type user struct {
ID string `bson:"_id"`
Username string `bson:"username"`
Services struct {
OIDC struct {
Email string `bson:"email"`
} `bson:"oidc"`
} `bson:"services"`
Profile struct {
Fullname string `bson:"fullname"`
} `bson:"profile"`
Emails []userEmail `bson:"emails"`
}
type userEmail struct {
Address string `bson:"address"`
Verified bool `bson:"verified"`
}
func connect(ctx context.Context) *mongo.Database {
client, err := mongo.Connect(ctx, options.Client().ApplyURI(MONGO))
if err != nil {
panic(err)
}
err = client.Ping(ctx, readpref.Primary())
if err != nil {
panic(err)
}
return client.Database(DB)
}
func lookupActivities(ctx context.Context, database *mongo.Database, from time.Time, to time.Time) []activity {
cursor, err := database.Collection("activities").Aggregate(ctx, getActivitiesPipeline(from, to))
if err != nil {
panic(err)
}
var activities = make([]activity, 0)
err = cursor.All(ctx, &activities)
if err != nil {
panic(err)
}
return activities
}
func lookupBoards(ctx context.Context, database *mongo.Database) map[string]board {
var boards []board
cursor, err := database.Collection("boards").Find(ctx, bson.M{})
if err != nil {
panic(err)
}
err = cursor.All(ctx, &boards)
if err != nil {
panic(err)
}
mapBoards := make(map[string]board)
for _, b := range boards {
mapBoards[b.ID] = b
}
return mapBoards
}
func lookupUsers(ctx context.Context, database *mongo.Database) map[string]user {
var users []user
cursor, err := database.Collection("users").Find(ctx, bson.M{
"$or": bson.A{
bson.M{"loginDisabled": false},
bson.M{"loginDisabled": bson.M{"$exists": false}},
},
})
if err != nil {
panic(err)
}
err = cursor.All(ctx, &users)
if err != nil {
panic(err)
}
mapUsers := make(map[string]user)
for _, u := range users {
mapUsers[u.ID] = u
}
return mapUsers
}
func getActivitiesPipeline(from time.Time, to time.Time) bson.A {
return bson.A{
bson.M{"$match": bson.M{
"createdAt": bson.M{
"$gte": from,
"$lt": to,
},
}},
bson.M{"$lookup": bson.M{
"from": "cards",
"localField": "cardId",
"foreignField": "_id",
"as": "card",
}},
bson.M{"$unwind": "$card"},
}
}