| // Copyright 2026 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package browserdevicedb |
| |
| import ( |
| "context" |
| |
| "go.chromium.org/luci/common/errors" |
| |
| "go.chromium.org/infra/fleetconsole/api/fleetconsolerpc" |
| "go.chromium.org/infra/fleetconsole/internal/database" |
| "go.chromium.org/infra/fleetconsole/internal/database/queryutils" |
| "go.chromium.org/infra/fleetconsole/internal/ent/entx" |
| "go.chromium.org/infra/fleetconsole/internal/ent/generated/browserdevice" |
| "go.chromium.org/infra/fleetconsole/internal/ent/generated/migrate" |
| "go.chromium.org/infra/fleetconsole/internal/utils" |
| ) |
| |
| // List lists the devices from the database. |
| func List(ctx context.Context, filter, orderby string, offset, pageSize int, realms []string) ([]*fleetconsolerpc.BrowserDevice, bool, error) { |
| p, err := queryutils.ToEntPredicate(filter, migrate.BrowserDevicesTable) |
| if err != nil { |
| return nil, false, errors.Fmt("failed to parse filter: %w", err) |
| } |
| |
| o, err := queryutils.ToEntOrderBy(orderby, migrate.BrowserDevicesTable, browserdevice.FieldID) |
| if err != nil { |
| return nil, false, utils.InvalidOrderByError(err) |
| } |
| |
| // Fetch one extra row to check whether there is more data available |
| entities, err := database.GetEntClient(ctx).BrowserDevice. |
| Query(). |
| Where(p). |
| Where(entx.RealmFilter(browserdevice.FieldRealm, realms)). |
| Order(o). |
| Offset(offset).Limit(pageSize + 1). |
| All(ctx) |
| |
| if err != nil { |
| return nil, false, errors.Fmt("failed to list devices: %w", err) |
| } |
| |
| var hasMoreData bool |
| if len(entities) > pageSize { |
| hasMoreData = true |
| entities = entities[:pageSize] |
| } |
| |
| return utils.Map(entities, toRPCDevice), hasMoreData, nil |
| } |