|
| 1 | +package pg |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" //nolint:gosec |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/shellhub-io/shellhub/api/store" |
| 8 | + "github.com/shellhub-io/shellhub/api/store/pg/entity" |
| 9 | + "github.com/shellhub-io/shellhub/pkg/clock" |
| 10 | + "github.com/shellhub-io/shellhub/pkg/models" |
| 11 | + "github.com/uptrace/bun" |
| 12 | + "github.com/uptrace/bun/dialect/pgdialect" |
| 13 | +) |
| 14 | + |
| 15 | +func (pg *Pg) DeviceCreate(ctx context.Context, device *models.Device) (string, error) { |
| 16 | + device.CreatedAt = clock.Now() |
| 17 | + |
| 18 | + e := entity.DeviceFromModel(device) |
| 19 | + if _, err := pg.driver.NewInsert().Model(e).Exec(ctx); err != nil { |
| 20 | + return "", fromSqlError(err) |
| 21 | + } |
| 22 | + |
| 23 | + return e.ID, nil |
| 24 | +} |
| 25 | + |
| 26 | +func (pg *Pg) DeviceConflicts(ctx context.Context, target *models.DeviceConflicts) ([]string, bool, error) { |
| 27 | + devices := make([]map[string]any, 0) |
| 28 | + if err := pg.driver.NewSelect().Model((*entity.Device)(nil)).Column("name").Where("name = ?", target.Name).Scan(ctx, &devices); err != nil { |
| 29 | + return nil, false, fromSqlError(err) |
| 30 | + } |
| 31 | + |
| 32 | + conflicts := make([]string, 0) |
| 33 | + for _, device := range devices { |
| 34 | + if device["name"] == target.Name { |
| 35 | + conflicts = append(conflicts, "name") |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return conflicts, len(conflicts) > 0, nil |
| 40 | +} |
| 41 | + |
| 42 | +func (pg *Pg) DeviceList(ctx context.Context, opts ...store.QueryOption) ([]models.Device, int, error) { |
| 43 | + entities := make([]entity.Device, 0) |
| 44 | + |
| 45 | + query := pg.driver. |
| 46 | + NewSelect(). |
| 47 | + Model(&entities). |
| 48 | + Column("device.*"). |
| 49 | + Relation("Namespace"). |
| 50 | + ColumnExpr(` |
| 51 | + CASE |
| 52 | + WHEN "device"."disconnected_at" IS NULL AND "device"."seen_at" > ? |
| 53 | + THEN true |
| 54 | + ELSE false |
| 55 | + END AS "online"`, |
| 56 | + time.Now().Add(-2*time.Minute), |
| 57 | + ). |
| 58 | + ColumnExpr(` |
| 59 | + CASE |
| 60 | + WHEN "device"."status" <> 'accepted' |
| 61 | + THEN true |
| 62 | + ELSE false |
| 63 | + END AS "acceptable"`, |
| 64 | + ) |
| 65 | + |
| 66 | + if err := applyOptions(ctx, query, opts...); err != nil { |
| 67 | + return nil, 0, fromSqlError(err) |
| 68 | + } |
| 69 | + |
| 70 | + count, err := query.ScanAndCount(ctx) |
| 71 | + if err != nil { |
| 72 | + return nil, 0, fromSqlError(err) |
| 73 | + } |
| 74 | + |
| 75 | + devices := make([]models.Device, len(entities)) |
| 76 | + for i, e := range entities { |
| 77 | + devices[i] = *entity.DeviceToModel(&e) |
| 78 | + } |
| 79 | + |
| 80 | + return devices, count, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (pg *Pg) DeviceResolve(ctx context.Context, resolver store.DeviceResolver, val string, opts ...store.QueryOption) (*models.Device, error) { |
| 84 | + d := new(entity.Device) |
| 85 | + |
| 86 | + query := pg.driver. |
| 87 | + NewSelect(). |
| 88 | + Model(d). |
| 89 | + Where("? = ?", bun.Ident("device."+resolver.String()), val). |
| 90 | + Column("device.*"). |
| 91 | + Relation("Namespace"). |
| 92 | + ColumnExpr(` |
| 93 | + CASE |
| 94 | + WHEN "device"."disconnected_at" IS NULL AND "device"."seen_at" > ? |
| 95 | + THEN true |
| 96 | + ELSE false |
| 97 | + END AS "online"`, |
| 98 | + time.Now().Add(-2*time.Minute), |
| 99 | + ) |
| 100 | + |
| 101 | + if err := query.Scan(ctx); err != nil { |
| 102 | + return nil, fromSqlError(err) |
| 103 | + } |
| 104 | + |
| 105 | + return entity.DeviceToModel(d), nil |
| 106 | +} |
| 107 | + |
| 108 | +func (pg *Pg) DeviceSave(ctx context.Context, device *models.Device) error { |
| 109 | + d := entity.DeviceFromModel(device) |
| 110 | + d.UpdatedAt = clock.Now() |
| 111 | + _, err := pg.driver.NewUpdate().Model(d).WherePK().Exec(ctx) |
| 112 | + |
| 113 | + return fromSqlError(err) |
| 114 | +} |
| 115 | + |
| 116 | +func (pg *Pg) DeviceUpdateSeenAt(ctx context.Context, ids []string, to time.Time) (int64, error) { |
| 117 | + r, err := pg.driver.NewUpdate(). |
| 118 | + Model((*entity.Device)(nil)). |
| 119 | + Set("seen_at = ?", to). |
| 120 | + Set("disconnected_at = NULL"). |
| 121 | + TableExpr("(SELECT unnest(?::varchar[]) as id) as _data", pgdialect.Array(ids)). |
| 122 | + Where("device.id = _data.id"). |
| 123 | + Exec(ctx) |
| 124 | + if err != nil { |
| 125 | + return 0, fromSqlError(err) |
| 126 | + } |
| 127 | + |
| 128 | + return r.RowsAffected() |
| 129 | +} |
| 130 | + |
| 131 | +func (pg *Pg) DeviceDelete(ctx context.Context, device *models.Device) error { |
| 132 | + d := entity.DeviceFromModel(device) |
| 133 | + _, err := pg.driver.NewDelete().Model(d).WherePK().Exec(ctx) |
| 134 | + |
| 135 | + return fromSqlError(err) |
| 136 | +} |
0 commit comments