-
Notifications
You must be signed in to change notification settings - Fork 302
/
entity_search.go
285 lines (261 loc) · 10.2 KB
/
entity_search.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"context"
"fmt"
"runtime/trace"
"github.com/jinzhu/gorm"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
// GetEntitySearch returns an EntitySearch on the given db (or transaction).
func GetEntitySearch(db *gorm.DB) EntitySearch {
return &entitySearch{store: newStore(db)}
}
type entitySearch struct {
*store
}
type metaFields interface {
GetIdContains() string
GetNameContains() string
GetDescriptionContains() string
GetAttributesContain() map[string]string
}
func (s *entitySearch) queryMetaFields(ctx context.Context, query *gorm.DB, entityType string, req metaFields) *gorm.DB {
if v := req.GetIdContains(); v != "" {
switch entityType {
case "organization", "user":
query = query.Where(`"accounts"."uid" ILIKE ?`, "%"+v+"%")
case "end_device":
query = query.Where(`"end_devices"."device_id" ILIKE ?`, "%"+v+"%")
default:
query = query.Where(fmt.Sprintf(`"%[1]ss"."%[1]s_id" ILIKE ?`, entityType), "%"+v+"%")
}
}
if v := req.GetNameContains(); v != "" {
query = query.Where("name ILIKE ?", fmt.Sprintf("%%%s%%", v))
}
if v := req.GetDescriptionContains(); v != "" {
if dbKind, ok := query.Get("db:kind"); ok && dbKind == "PostgreSQL" {
query = query.Where("to_tsvector('english', description) @@ websearch_to_tsquery('english', ?)", v)
} else {
query = query.Where("description ILIKE ?", fmt.Sprintf("%%%s%%", v))
}
}
if kv := req.GetAttributesContain(); len(kv) > 0 {
sub := s.query(ctx, &Attribute{}).Select("entity_id")
switch entityType {
case "end_device":
sub = sub.Where("entity_type = ?", "device")
default:
sub = sub.Where("entity_type = ?", entityType)
}
for k, v := range kv {
sub = sub.Where("key = ? AND value ILIKE ?", k, fmt.Sprintf("%%%s%%", v))
}
query = query.Where(fmt.Sprintf(`"%ss"."id" IN (?)`, entityType), sub.QueryExpr())
}
return query
}
func (s *entitySearch) queryMembership(ctx context.Context, query *gorm.DB, entityType string, member *ttnpb.OrganizationOrUserIdentifiers) *gorm.DB {
if member == nil {
return query
}
membershipsQuery := (&membershipStore{store: s.store}).queryMemberships(ctx, member, entityType, nil, true).Select(`"direct_memberships"."entity_id"`).QueryExpr()
if entityType == "organization" {
query = query.Where(`"accounts"."account_type" = ? AND "accounts"."account_id" IN (?)`, entityType, membershipsQuery)
} else {
query = query.Where(fmt.Sprintf(`"%[1]ss"."id" IN (?)`, entityType), membershipsQuery)
}
return query
}
type searchResult struct {
FriendlyID string
}
func (s *entitySearch) runPaginatedQuery(ctx context.Context, query *gorm.DB, entityType string) ([]searchResult, error) {
query = query.Order(orderFromContext(ctx, fmt.Sprintf("%ss", entityType), "friendly_id", "ASC"))
page := query
if limit, offset := limitAndOffsetFromContext(ctx); limit != 0 {
page = query.Limit(limit).Offset(offset)
}
var results []searchResult
if err := page.Scan(&results).Error; err != nil {
return nil, err
}
if limit, offset := limitAndOffsetFromContext(ctx); limit != 0 && (offset > 0 || len(results) == int(limit)) {
countTotal(ctx, query)
} else {
setTotal(ctx, uint64(len(results)))
}
return results, nil
}
const application = "application"
func (s *entitySearch) FindApplications(ctx context.Context, member *ttnpb.OrganizationOrUserIdentifiers, req *ttnpb.SearchApplicationsRequest) ([]*ttnpb.ApplicationIdentifiers, error) {
defer trace.StartRegion(ctx, "find applications").End()
if req.Deleted {
ctx = WithSoftDeleted(ctx, true)
}
query := s.query(ctx, &Application{})
query = query.Select(fmt.Sprintf(`"%[1]ss"."%[1]s_id" AS "friendly_id"`, application))
query = s.queryMembership(ctx, query, application, member)
query = s.queryMetaFields(ctx, query, application, req)
results, err := s.runPaginatedQuery(ctx, query, application)
if err != nil {
return nil, err
}
identifiers := make([]*ttnpb.ApplicationIdentifiers, len(results))
for i, result := range results {
identifiers[i] = &ttnpb.ApplicationIdentifiers{ApplicationId: result.FriendlyID}
}
return identifiers, nil
}
const client = "client"
func (s *entitySearch) FindClients(ctx context.Context, member *ttnpb.OrganizationOrUserIdentifiers, req *ttnpb.SearchClientsRequest) ([]*ttnpb.ClientIdentifiers, error) {
defer trace.StartRegion(ctx, "find clients").End()
if req.Deleted {
ctx = WithSoftDeleted(ctx, true)
}
query := s.query(ctx, &Client{})
query = query.Select(fmt.Sprintf(`"%[1]ss"."%[1]s_id" AS "friendly_id"`, client))
query = s.queryMembership(ctx, query, client, member)
query = s.queryMetaFields(ctx, query, client, req)
if len(req.State) > 0 {
stateNumbers := make([]int, len(req.State))
for i, state := range req.State {
stateNumbers[i] = int(state)
}
query = query.Where(fmt.Sprintf(`"%[1]ss"."state" IN (?)`, client), stateNumbers)
}
results, err := s.runPaginatedQuery(ctx, query, client)
if err != nil {
return nil, err
}
identifiers := make([]*ttnpb.ClientIdentifiers, len(results))
for i, result := range results {
identifiers[i] = &ttnpb.ClientIdentifiers{ClientId: result.FriendlyID}
}
return identifiers, nil
}
const gateway = "gateway"
func (s *entitySearch) FindGateways(ctx context.Context, member *ttnpb.OrganizationOrUserIdentifiers, req *ttnpb.SearchGatewaysRequest) ([]*ttnpb.GatewayIdentifiers, error) {
defer trace.StartRegion(ctx, "find gateways").End()
if req.Deleted {
ctx = WithSoftDeleted(ctx, true)
}
query := s.query(ctx, &Gateway{})
query = query.Select(fmt.Sprintf(`"%[1]ss"."%[1]s_id" AS "friendly_id"`, gateway))
query = s.queryMembership(ctx, query, gateway, member)
query = s.queryMetaFields(ctx, query, gateway, req)
if v := req.EuiContains; v != "" {
query = query.Where(fmt.Sprintf(`"%[1]ss"."gateway_eui" ILIKE ?`, gateway), fmt.Sprintf("%%%s%%", v))
}
results, err := s.runPaginatedQuery(ctx, query, gateway)
if err != nil {
return nil, err
}
identifiers := make([]*ttnpb.GatewayIdentifiers, len(results))
for i, result := range results {
identifiers[i] = &ttnpb.GatewayIdentifiers{GatewayId: result.FriendlyID}
}
return identifiers, nil
}
const organization = "organization"
func (s *entitySearch) FindOrganizations(ctx context.Context, member *ttnpb.OrganizationOrUserIdentifiers, req *ttnpb.SearchOrganizationsRequest) ([]*ttnpb.OrganizationIdentifiers, error) {
defer trace.StartRegion(ctx, "find organizations").End()
if req.Deleted {
ctx = WithSoftDeleted(ctx, true)
}
query := s.query(ctx, &Organization{})
query = query.
Joins(`JOIN "accounts" ON "accounts"."account_type" = 'organization' AND "accounts"."account_id" = "organizations"."id"`).
Select(`"accounts"."uid" AS "friendly_id"`)
query = s.queryMembership(ctx, query, organization, member)
query = s.queryMetaFields(ctx, query, organization, req)
results, err := s.runPaginatedQuery(ctx, query, organization)
if err != nil {
return nil, err
}
identifiers := make([]*ttnpb.OrganizationIdentifiers, len(results))
for i, result := range results {
identifiers[i] = &ttnpb.OrganizationIdentifiers{OrganizationId: result.FriendlyID}
}
return identifiers, nil
}
const user = "user"
func (s *entitySearch) FindUsers(ctx context.Context, member *ttnpb.OrganizationOrUserIdentifiers, req *ttnpb.SearchUsersRequest) ([]*ttnpb.UserIdentifiers, error) {
defer trace.StartRegion(ctx, "find users").End()
if req.Deleted {
ctx = WithSoftDeleted(ctx, true)
}
query := s.query(ctx, &User{})
query = query.
Joins(`JOIN "accounts" ON "accounts"."account_type" = 'user' AND "accounts"."account_id" = "users"."id"`).
Select(`"accounts"."uid" AS "friendly_id"`)
query = s.queryMembership(ctx, query, user, member)
query = s.queryMetaFields(ctx, query, user, req)
if len(req.State) > 0 {
stateNumbers := make([]int, len(req.State))
for i, state := range req.State {
stateNumbers[i] = int(state)
}
query = query.Where(fmt.Sprintf(`"%[1]ss"."state" IN (?)`, user), stateNumbers)
}
results, err := s.runPaginatedQuery(ctx, query, user)
if err != nil {
return nil, err
}
identifiers := make([]*ttnpb.UserIdentifiers, len(results))
for i, result := range results {
identifiers[i] = &ttnpb.UserIdentifiers{UserId: result.FriendlyID}
}
return identifiers, nil
}
func (s *entitySearch) FindEndDevices(ctx context.Context, req *ttnpb.SearchEndDevicesRequest) ([]*ttnpb.EndDeviceIdentifiers, error) {
defer trace.StartRegion(ctx, "find end devices").End()
query := s.query(ctx, &EndDevice{}).
Where(&EndDevice{ApplicationID: req.GetApplicationIds().GetApplicationId()}).
Select(`"end_devices"."device_id" AS "friendly_id"`)
query = s.queryMetaFields(ctx, query, "end_device", req)
if v := req.DevEuiContains; v != "" {
query = query.Where("dev_eui ILIKE ?", fmt.Sprintf("%%%s%%", v))
}
if v := req.JoinEuiContains; v != "" {
query = query.Where("join_eui ILIKE ?", fmt.Sprintf("%%%s%%", v))
}
// DevAddrContains
query = query.Order(orderFromContext(ctx, "end_devices", "device_id", "ASC"))
page := query
if limit, offset := limitAndOffsetFromContext(ctx); limit != 0 {
page = query.Limit(limit).Offset(offset)
}
var results []struct {
FriendlyID string
}
if err := page.Scan(&results).Error; err != nil {
return nil, err
}
if limit, offset := limitAndOffsetFromContext(ctx); limit != 0 && (offset > 0 || len(results) == int(limit)) {
countTotal(ctx, query)
} else {
setTotal(ctx, uint64(len(results)))
}
identifiers := make([]*ttnpb.EndDeviceIdentifiers, len(results))
for i, result := range results {
identifiers[i] = &ttnpb.EndDeviceIdentifiers{
ApplicationIdentifiers: ttnpb.ApplicationIdentifiers{ApplicationId: req.GetApplicationIds().GetApplicationId()},
DeviceId: result.FriendlyID,
}
}
return identifiers, nil
}