From 6bc6024cec710284326b8c4768003fb409782d18 Mon Sep 17 00:00:00 2001 From: KCarretto Date: Sun, 21 Jan 2024 18:02:17 +0000 Subject: [PATCH 1/2] Added next_seen_at field, updated tests --- tavern/internal/c2/api_claim_tasks_test.go | 81 +++++++--- tavern/internal/ent/beacon.go | 13 +- tavern/internal/ent/beacon/beacon.go | 15 ++ tavern/internal/ent/beacon/where.go | 55 +++++++ tavern/internal/ent/beacon_create.go | 91 ++++++++++- tavern/internal/ent/beacon_update.go | 52 +++++++ tavern/internal/ent/client.go | 3 +- tavern/internal/ent/gql_collection.go | 5 + tavern/internal/ent/gql_pagination.go | 18 +++ tavern/internal/ent/gql_where_input.go | 42 ++++++ tavern/internal/ent/migrate/schema.go | 3 +- tavern/internal/ent/mutation.go | 75 +++++++++- tavern/internal/ent/runtime/runtime.go | 2 + tavern/internal/ent/schema/beacon.go | 51 ++++++- .../graphql/generated/ent.generated.go | 141 +++++++++++++++++- .../graphql/generated/mutation.generated.go | 2 + .../graphql/generated/root_.generated.go | 22 +++ tavern/internal/graphql/schema.graphql | 14 ++ tavern/internal/graphql/schema/ent.graphql | 14 ++ tavern/internal/www/schema.graphql | 14 ++ 20 files changed, 685 insertions(+), 28 deletions(-) diff --git a/tavern/internal/c2/api_claim_tasks_test.go b/tavern/internal/c2/api_claim_tasks_test.go index e737b8d45..b535988f1 100644 --- a/tavern/internal/c2/api_claim_tasks_test.go +++ b/tavern/internal/c2/api_claim_tasks_test.go @@ -3,8 +3,10 @@ package c2_test import ( "context" "testing" + "time" "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -12,6 +14,7 @@ import ( "realm.pub/tavern/internal/c2/c2pb" "realm.pub/tavern/internal/c2/c2test" "realm.pub/tavern/internal/ent" + "realm.pub/tavern/internal/ent/beacon" ) func TestClaimTasks(t *testing.T) { @@ -28,13 +31,18 @@ func TestClaimTasks(t *testing.T) { } // Test Cases - type testCase struct { + tests := []struct { name string req *c2pb.ClaimTasksRequest wantResp *c2pb.ClaimTasksResponse wantCode codes.Code - } - tests := []testCase{ + + wantBeaconExist bool + wantBeaconLastSeenAtBefore time.Time + wantBeaconLastSeenAtAfter time.Time + wantBeaconNextSeenAtBefore time.Time + wantBeaconNextSeenAtAfter time.Time + }{ { name: "First_Callback", req: &c2pb.ClaimTasksRequest{ @@ -50,11 +58,17 @@ func TestClaimTasks(t *testing.T) { Platform: c2pb.Host_PLATFORM_LINUX, PrimaryIp: "127.0.0.1", }, - Interval: uint64(100), + Interval: uint64(60), }, }, wantResp: &c2pb.ClaimTasksResponse{}, wantCode: codes.OK, + + wantBeaconExist: true, + wantBeaconNextSeenAtBefore: time.Now().UTC().Add(120 * time.Second), + wantBeaconNextSeenAtAfter: time.Now().UTC().Add(-120 * time.Second), + wantBeaconLastSeenAtBefore: time.Now().UTC().Add(10 * time.Second), + wantBeaconLastSeenAtAfter: time.Now().UTC().Add(-10 * time.Second), }, { name: "Second_Callback", @@ -76,9 +90,15 @@ func TestClaimTasks(t *testing.T) { }, wantResp: &c2pb.ClaimTasksResponse{}, wantCode: codes.OK, + + wantBeaconExist: true, + wantBeaconNextSeenAtBefore: time.Now().UTC().Add(200 * time.Second), + wantBeaconNextSeenAtAfter: time.Now().UTC().Add(-200 * time.Second), + wantBeaconLastSeenAtBefore: time.Now().UTC().Add(10 * time.Second), + wantBeaconLastSeenAtAfter: time.Now().UTC().Add(-10 * time.Second), }, { - name: "Callback_With_Tasks", + name: "Existing_Beacon", req: &c2pb.ClaimTasksRequest{ Beacon: &c2pb.Beacon{ Identifier: existingBeacon.Identifier, @@ -102,26 +122,49 @@ func TestClaimTasks(t *testing.T) { }, }, wantCode: codes.OK, + + wantBeaconExist: true, + wantBeaconNextSeenAtBefore: time.Now().UTC().Add(200 * time.Second), + wantBeaconNextSeenAtAfter: time.Now().UTC().Add(-200 * time.Second), + wantBeaconLastSeenAtBefore: time.Now().UTC().Add(10 * time.Second), + wantBeaconLastSeenAtAfter: time.Now().UTC().Add(-10 * time.Second), }, } - testHandler := func(t *testing.T, tc testCase) { - resp, err := client.ClaimTasks(ctx, tc.req) - require.Equal(t, tc.wantCode.String(), status.Code(err).String(), err) - if status.Code(err) != codes.OK { - // Do not continue if we expected error code - return - } + // Run Tests + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Callback + resp, err := client.ClaimTasks(ctx, tc.req) - if diff := cmp.Diff(tc.wantResp, resp, protocmp.Transform()); diff != "" { - t.Errorf("invalid response (-want +got): %v", diff) - } + // Assert Response Code + require.Equal(t, tc.wantCode.String(), status.Code(err).String(), err) + if status.Code(err) != codes.OK { + // Do not continue if we expected error code + return + } - } + // Assert Response + if diff := cmp.Diff(tc.wantResp, resp, protocmp.Transform()); diff != "" { + t.Errorf("invalid response (-want +got): %v", diff) + } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - testHandler(t, tc) + // Load Beacon + testBeacon, err := graph.Beacon.Query(). + Where( + beacon.Identifier(tc.req.Beacon.Identifier), + ).Only(ctx) + if ent.IsNotFound(err) && !tc.wantBeaconExist { + return + } + if err != nil { + t.Errorf("failed to load beacon: %v", err) + return + } + + // Beacon Assertions + assert.WithinRange(t, testBeacon.LastSeenAt, tc.wantBeaconLastSeenAtAfter, tc.wantBeaconLastSeenAtBefore) + assert.WithinRange(t, testBeacon.NextSeenAt, tc.wantBeaconNextSeenAtAfter, tc.wantBeaconNextSeenAtBefore) }) } } diff --git a/tavern/internal/ent/beacon.go b/tavern/internal/ent/beacon.go index 7f752517d..727ed7412 100644 --- a/tavern/internal/ent/beacon.go +++ b/tavern/internal/ent/beacon.go @@ -28,6 +28,8 @@ type Beacon struct { AgentIdentifier string `json:"agent_identifier,omitempty"` // Timestamp of when a task was last claimed or updated for the beacon. LastSeenAt time.Time `json:"last_seen_at,omitempty"` + // Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead). + NextSeenAt time.Time `json:"next_seen_at,omitempty"` // Duration until next callback, in seconds. Interval uint64 `json:"interval,omitempty"` // Edges holds the relations/edges for other nodes in the graph. @@ -83,7 +85,7 @@ func (*Beacon) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullInt64) case beacon.FieldName, beacon.FieldPrincipal, beacon.FieldIdentifier, beacon.FieldAgentIdentifier: values[i] = new(sql.NullString) - case beacon.FieldLastSeenAt: + case beacon.FieldLastSeenAt, beacon.FieldNextSeenAt: values[i] = new(sql.NullTime) case beacon.ForeignKeys[0]: // beacon_host values[i] = new(sql.NullInt64) @@ -138,6 +140,12 @@ func (b *Beacon) assignValues(columns []string, values []any) error { } else if value.Valid { b.LastSeenAt = value.Time } + case beacon.FieldNextSeenAt: + if value, ok := values[i].(*sql.NullTime); !ok { + return fmt.Errorf("unexpected type %T for field next_seen_at", values[i]) + } else if value.Valid { + b.NextSeenAt = value.Time + } case beacon.FieldInterval: if value, ok := values[i].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for field interval", values[i]) @@ -212,6 +220,9 @@ func (b *Beacon) String() string { builder.WriteString("last_seen_at=") builder.WriteString(b.LastSeenAt.Format(time.ANSIC)) builder.WriteString(", ") + builder.WriteString("next_seen_at=") + builder.WriteString(b.NextSeenAt.Format(time.ANSIC)) + builder.WriteString(", ") builder.WriteString("interval=") builder.WriteString(fmt.Sprintf("%v", b.Interval)) builder.WriteByte(')') diff --git a/tavern/internal/ent/beacon/beacon.go b/tavern/internal/ent/beacon/beacon.go index 7b7e8550b..7fae1d93d 100644 --- a/tavern/internal/ent/beacon/beacon.go +++ b/tavern/internal/ent/beacon/beacon.go @@ -3,6 +3,7 @@ package beacon import ( + "entgo.io/ent" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" ) @@ -22,6 +23,8 @@ const ( FieldAgentIdentifier = "agent_identifier" // FieldLastSeenAt holds the string denoting the last_seen_at field in the database. FieldLastSeenAt = "last_seen_at" + // FieldNextSeenAt holds the string denoting the next_seen_at field in the database. + FieldNextSeenAt = "next_seen_at" // FieldInterval holds the string denoting the interval field in the database. FieldInterval = "interval" // EdgeHost holds the string denoting the host edge name in mutations. @@ -54,6 +57,7 @@ var Columns = []string{ FieldIdentifier, FieldAgentIdentifier, FieldLastSeenAt, + FieldNextSeenAt, FieldInterval, } @@ -78,7 +82,13 @@ func ValidColumn(column string) bool { return false } +// Note that the variables below are initialized by the runtime +// package on the initialization of the application. Therefore, +// it should be imported in the main as follows: +// +// import _ "realm.pub/tavern/internal/ent/runtime" var ( + Hooks [1]ent.Hook // DefaultName holds the default value on creation for the "name" field. DefaultName func() string // NameValidator is a validator for the "name" field. It is called by the builders before save. @@ -126,6 +136,11 @@ func ByLastSeenAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldLastSeenAt, opts...).ToFunc() } +// ByNextSeenAt orders the results by the next_seen_at field. +func ByNextSeenAt(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldNextSeenAt, opts...).ToFunc() +} + // ByInterval orders the results by the interval field. func ByInterval(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldInterval, opts...).ToFunc() diff --git a/tavern/internal/ent/beacon/where.go b/tavern/internal/ent/beacon/where.go index 0f5c15a00..6e0263bf6 100644 --- a/tavern/internal/ent/beacon/where.go +++ b/tavern/internal/ent/beacon/where.go @@ -80,6 +80,11 @@ func LastSeenAt(v time.Time) predicate.Beacon { return predicate.Beacon(sql.FieldEQ(FieldLastSeenAt, v)) } +// NextSeenAt applies equality check predicate on the "next_seen_at" field. It's identical to NextSeenAtEQ. +func NextSeenAt(v time.Time) predicate.Beacon { + return predicate.Beacon(sql.FieldEQ(FieldNextSeenAt, v)) +} + // Interval applies equality check predicate on the "interval" field. It's identical to IntervalEQ. func Interval(v uint64) predicate.Beacon { return predicate.Beacon(sql.FieldEQ(FieldInterval, v)) @@ -415,6 +420,56 @@ func LastSeenAtNotNil() predicate.Beacon { return predicate.Beacon(sql.FieldNotNull(FieldLastSeenAt)) } +// NextSeenAtEQ applies the EQ predicate on the "next_seen_at" field. +func NextSeenAtEQ(v time.Time) predicate.Beacon { + return predicate.Beacon(sql.FieldEQ(FieldNextSeenAt, v)) +} + +// NextSeenAtNEQ applies the NEQ predicate on the "next_seen_at" field. +func NextSeenAtNEQ(v time.Time) predicate.Beacon { + return predicate.Beacon(sql.FieldNEQ(FieldNextSeenAt, v)) +} + +// NextSeenAtIn applies the In predicate on the "next_seen_at" field. +func NextSeenAtIn(vs ...time.Time) predicate.Beacon { + return predicate.Beacon(sql.FieldIn(FieldNextSeenAt, vs...)) +} + +// NextSeenAtNotIn applies the NotIn predicate on the "next_seen_at" field. +func NextSeenAtNotIn(vs ...time.Time) predicate.Beacon { + return predicate.Beacon(sql.FieldNotIn(FieldNextSeenAt, vs...)) +} + +// NextSeenAtGT applies the GT predicate on the "next_seen_at" field. +func NextSeenAtGT(v time.Time) predicate.Beacon { + return predicate.Beacon(sql.FieldGT(FieldNextSeenAt, v)) +} + +// NextSeenAtGTE applies the GTE predicate on the "next_seen_at" field. +func NextSeenAtGTE(v time.Time) predicate.Beacon { + return predicate.Beacon(sql.FieldGTE(FieldNextSeenAt, v)) +} + +// NextSeenAtLT applies the LT predicate on the "next_seen_at" field. +func NextSeenAtLT(v time.Time) predicate.Beacon { + return predicate.Beacon(sql.FieldLT(FieldNextSeenAt, v)) +} + +// NextSeenAtLTE applies the LTE predicate on the "next_seen_at" field. +func NextSeenAtLTE(v time.Time) predicate.Beacon { + return predicate.Beacon(sql.FieldLTE(FieldNextSeenAt, v)) +} + +// NextSeenAtIsNil applies the IsNil predicate on the "next_seen_at" field. +func NextSeenAtIsNil() predicate.Beacon { + return predicate.Beacon(sql.FieldIsNull(FieldNextSeenAt)) +} + +// NextSeenAtNotNil applies the NotNil predicate on the "next_seen_at" field. +func NextSeenAtNotNil() predicate.Beacon { + return predicate.Beacon(sql.FieldNotNull(FieldNextSeenAt)) +} + // IntervalEQ applies the EQ predicate on the "interval" field. func IntervalEQ(v uint64) predicate.Beacon { return predicate.Beacon(sql.FieldEQ(FieldInterval, v)) diff --git a/tavern/internal/ent/beacon_create.go b/tavern/internal/ent/beacon_create.go index 84f7c64d8..bb03ddac5 100644 --- a/tavern/internal/ent/beacon_create.go +++ b/tavern/internal/ent/beacon_create.go @@ -94,6 +94,20 @@ func (bc *BeaconCreate) SetNillableLastSeenAt(t *time.Time) *BeaconCreate { return bc } +// SetNextSeenAt sets the "next_seen_at" field. +func (bc *BeaconCreate) SetNextSeenAt(t time.Time) *BeaconCreate { + bc.mutation.SetNextSeenAt(t) + return bc +} + +// SetNillableNextSeenAt sets the "next_seen_at" field if the given value is not nil. +func (bc *BeaconCreate) SetNillableNextSeenAt(t *time.Time) *BeaconCreate { + if t != nil { + bc.SetNextSeenAt(*t) + } + return bc +} + // SetInterval sets the "interval" field. func (bc *BeaconCreate) SetInterval(u uint64) *BeaconCreate { bc.mutation.SetInterval(u) @@ -141,7 +155,9 @@ func (bc *BeaconCreate) Mutation() *BeaconMutation { // Save creates the Beacon in the database. func (bc *BeaconCreate) Save(ctx context.Context) (*Beacon, error) { - bc.defaults() + if err := bc.defaults(); err != nil { + return nil, err + } return withHooks(ctx, bc.sqlSave, bc.mutation, bc.hooks) } @@ -168,15 +184,22 @@ func (bc *BeaconCreate) ExecX(ctx context.Context) { } // defaults sets the default values of the builder before save. -func (bc *BeaconCreate) defaults() { +func (bc *BeaconCreate) defaults() error { if _, ok := bc.mutation.Name(); !ok { + if beacon.DefaultName == nil { + return fmt.Errorf("ent: uninitialized beacon.DefaultName (forgotten import ent/runtime?)") + } v := beacon.DefaultName() bc.mutation.SetName(v) } if _, ok := bc.mutation.Identifier(); !ok { + if beacon.DefaultIdentifier == nil { + return fmt.Errorf("ent: uninitialized beacon.DefaultIdentifier (forgotten import ent/runtime?)") + } v := beacon.DefaultIdentifier() bc.mutation.SetIdentifier(v) } + return nil } // check runs all checks and user-defined validators on the builder. @@ -257,6 +280,10 @@ func (bc *BeaconCreate) createSpec() (*Beacon, *sqlgraph.CreateSpec) { _spec.SetField(beacon.FieldLastSeenAt, field.TypeTime, value) _node.LastSeenAt = value } + if value, ok := bc.mutation.NextSeenAt(); ok { + _spec.SetField(beacon.FieldNextSeenAt, field.TypeTime, value) + _node.NextSeenAt = value + } if value, ok := bc.mutation.Interval(); ok { _spec.SetField(beacon.FieldInterval, field.TypeUint64, value) _node.Interval = value @@ -412,6 +439,24 @@ func (u *BeaconUpsert) ClearLastSeenAt() *BeaconUpsert { return u } +// SetNextSeenAt sets the "next_seen_at" field. +func (u *BeaconUpsert) SetNextSeenAt(v time.Time) *BeaconUpsert { + u.Set(beacon.FieldNextSeenAt, v) + return u +} + +// UpdateNextSeenAt sets the "next_seen_at" field to the value that was provided on create. +func (u *BeaconUpsert) UpdateNextSeenAt() *BeaconUpsert { + u.SetExcluded(beacon.FieldNextSeenAt) + return u +} + +// ClearNextSeenAt clears the value of the "next_seen_at" field. +func (u *BeaconUpsert) ClearNextSeenAt() *BeaconUpsert { + u.SetNull(beacon.FieldNextSeenAt) + return u +} + // SetInterval sets the "interval" field. func (u *BeaconUpsert) SetInterval(v uint64) *BeaconUpsert { u.Set(beacon.FieldInterval, v) @@ -558,6 +603,27 @@ func (u *BeaconUpsertOne) ClearLastSeenAt() *BeaconUpsertOne { }) } +// SetNextSeenAt sets the "next_seen_at" field. +func (u *BeaconUpsertOne) SetNextSeenAt(v time.Time) *BeaconUpsertOne { + return u.Update(func(s *BeaconUpsert) { + s.SetNextSeenAt(v) + }) +} + +// UpdateNextSeenAt sets the "next_seen_at" field to the value that was provided on create. +func (u *BeaconUpsertOne) UpdateNextSeenAt() *BeaconUpsertOne { + return u.Update(func(s *BeaconUpsert) { + s.UpdateNextSeenAt() + }) +} + +// ClearNextSeenAt clears the value of the "next_seen_at" field. +func (u *BeaconUpsertOne) ClearNextSeenAt() *BeaconUpsertOne { + return u.Update(func(s *BeaconUpsert) { + s.ClearNextSeenAt() + }) +} + // SetInterval sets the "interval" field. func (u *BeaconUpsertOne) SetInterval(v uint64) *BeaconUpsertOne { return u.Update(func(s *BeaconUpsert) { @@ -874,6 +940,27 @@ func (u *BeaconUpsertBulk) ClearLastSeenAt() *BeaconUpsertBulk { }) } +// SetNextSeenAt sets the "next_seen_at" field. +func (u *BeaconUpsertBulk) SetNextSeenAt(v time.Time) *BeaconUpsertBulk { + return u.Update(func(s *BeaconUpsert) { + s.SetNextSeenAt(v) + }) +} + +// UpdateNextSeenAt sets the "next_seen_at" field to the value that was provided on create. +func (u *BeaconUpsertBulk) UpdateNextSeenAt() *BeaconUpsertBulk { + return u.Update(func(s *BeaconUpsert) { + s.UpdateNextSeenAt() + }) +} + +// ClearNextSeenAt clears the value of the "next_seen_at" field. +func (u *BeaconUpsertBulk) ClearNextSeenAt() *BeaconUpsertBulk { + return u.Update(func(s *BeaconUpsert) { + s.ClearNextSeenAt() + }) +} + // SetInterval sets the "interval" field. func (u *BeaconUpsertBulk) SetInterval(v uint64) *BeaconUpsertBulk { return u.Update(func(s *BeaconUpsert) { diff --git a/tavern/internal/ent/beacon_update.go b/tavern/internal/ent/beacon_update.go index dbc1c31b7..acd7b06fe 100644 --- a/tavern/internal/ent/beacon_update.go +++ b/tavern/internal/ent/beacon_update.go @@ -104,6 +104,26 @@ func (bu *BeaconUpdate) ClearLastSeenAt() *BeaconUpdate { return bu } +// SetNextSeenAt sets the "next_seen_at" field. +func (bu *BeaconUpdate) SetNextSeenAt(t time.Time) *BeaconUpdate { + bu.mutation.SetNextSeenAt(t) + return bu +} + +// SetNillableNextSeenAt sets the "next_seen_at" field if the given value is not nil. +func (bu *BeaconUpdate) SetNillableNextSeenAt(t *time.Time) *BeaconUpdate { + if t != nil { + bu.SetNextSeenAt(*t) + } + return bu +} + +// ClearNextSeenAt clears the value of the "next_seen_at" field. +func (bu *BeaconUpdate) ClearNextSeenAt() *BeaconUpdate { + bu.mutation.ClearNextSeenAt() + return bu +} + // SetInterval sets the "interval" field. func (bu *BeaconUpdate) SetInterval(u uint64) *BeaconUpdate { bu.mutation.ResetInterval() @@ -272,6 +292,12 @@ func (bu *BeaconUpdate) sqlSave(ctx context.Context) (n int, err error) { if bu.mutation.LastSeenAtCleared() { _spec.ClearField(beacon.FieldLastSeenAt, field.TypeTime) } + if value, ok := bu.mutation.NextSeenAt(); ok { + _spec.SetField(beacon.FieldNextSeenAt, field.TypeTime, value) + } + if bu.mutation.NextSeenAtCleared() { + _spec.ClearField(beacon.FieldNextSeenAt, field.TypeTime) + } if value, ok := bu.mutation.Interval(); ok { _spec.SetField(beacon.FieldInterval, field.TypeUint64, value) } @@ -449,6 +475,26 @@ func (buo *BeaconUpdateOne) ClearLastSeenAt() *BeaconUpdateOne { return buo } +// SetNextSeenAt sets the "next_seen_at" field. +func (buo *BeaconUpdateOne) SetNextSeenAt(t time.Time) *BeaconUpdateOne { + buo.mutation.SetNextSeenAt(t) + return buo +} + +// SetNillableNextSeenAt sets the "next_seen_at" field if the given value is not nil. +func (buo *BeaconUpdateOne) SetNillableNextSeenAt(t *time.Time) *BeaconUpdateOne { + if t != nil { + buo.SetNextSeenAt(*t) + } + return buo +} + +// ClearNextSeenAt clears the value of the "next_seen_at" field. +func (buo *BeaconUpdateOne) ClearNextSeenAt() *BeaconUpdateOne { + buo.mutation.ClearNextSeenAt() + return buo +} + // SetInterval sets the "interval" field. func (buo *BeaconUpdateOne) SetInterval(u uint64) *BeaconUpdateOne { buo.mutation.ResetInterval() @@ -647,6 +693,12 @@ func (buo *BeaconUpdateOne) sqlSave(ctx context.Context) (_node *Beacon, err err if buo.mutation.LastSeenAtCleared() { _spec.ClearField(beacon.FieldLastSeenAt, field.TypeTime) } + if value, ok := buo.mutation.NextSeenAt(); ok { + _spec.SetField(beacon.FieldNextSeenAt, field.TypeTime, value) + } + if buo.mutation.NextSeenAtCleared() { + _spec.ClearField(beacon.FieldNextSeenAt, field.TypeTime) + } if value, ok := buo.mutation.Interval(); ok { _spec.SetField(beacon.FieldInterval, field.TypeUint64, value) } diff --git a/tavern/internal/ent/client.go b/tavern/internal/ent/client.go index 88ef3dc98..d97a77577 100644 --- a/tavern/internal/ent/client.go +++ b/tavern/internal/ent/client.go @@ -400,7 +400,8 @@ func (c *BeaconClient) QueryTasks(b *Beacon) *TaskQuery { // Hooks returns the client hooks. func (c *BeaconClient) Hooks() []Hook { - return c.hooks.Beacon + hooks := c.hooks.Beacon + return append(hooks[:len(hooks):len(hooks)], beacon.Hooks[:]...) } // Interceptors returns the client interceptors. diff --git a/tavern/internal/ent/gql_collection.go b/tavern/internal/ent/gql_collection.go index 6edfff931..7a1cc4b39 100644 --- a/tavern/internal/ent/gql_collection.go +++ b/tavern/internal/ent/gql_collection.go @@ -86,6 +86,11 @@ func (b *BeaconQuery) collectField(ctx context.Context, opCtx *graphql.Operation selectedFields = append(selectedFields, beacon.FieldLastSeenAt) fieldSeen[beacon.FieldLastSeenAt] = struct{}{} } + case "nextSeenAt": + if _, ok := fieldSeen[beacon.FieldNextSeenAt]; !ok { + selectedFields = append(selectedFields, beacon.FieldNextSeenAt) + fieldSeen[beacon.FieldNextSeenAt] = struct{}{} + } case "interval": if _, ok := fieldSeen[beacon.FieldInterval]; !ok { selectedFields = append(selectedFields, beacon.FieldInterval) diff --git a/tavern/internal/ent/gql_pagination.go b/tavern/internal/ent/gql_pagination.go index 50196d99b..e896c12c4 100644 --- a/tavern/internal/ent/gql_pagination.go +++ b/tavern/internal/ent/gql_pagination.go @@ -325,6 +325,20 @@ var ( } }, } + // BeaconOrderFieldNextSeenAt orders Beacon by next_seen_at. + BeaconOrderFieldNextSeenAt = &BeaconOrderField{ + Value: func(b *Beacon) (ent.Value, error) { + return b.NextSeenAt, nil + }, + column: beacon.FieldNextSeenAt, + toTerm: beacon.ByNextSeenAt, + toCursor: func(b *Beacon) Cursor { + return Cursor{ + ID: b.ID, + Value: b.NextSeenAt, + } + }, + } // BeaconOrderFieldInterval orders Beacon by interval. BeaconOrderFieldInterval = &BeaconOrderField{ Value: func(b *Beacon) (ent.Value, error) { @@ -347,6 +361,8 @@ func (f BeaconOrderField) String() string { switch f.column { case BeaconOrderFieldLastSeenAt.column: str = "LAST_SEEN_AT" + case BeaconOrderFieldNextSeenAt.column: + str = "NEXT_SEEN_AT" case BeaconOrderFieldInterval.column: str = "INTERVAL" } @@ -367,6 +383,8 @@ func (f *BeaconOrderField) UnmarshalGQL(v interface{}) error { switch str { case "LAST_SEEN_AT": *f = *BeaconOrderFieldLastSeenAt + case "NEXT_SEEN_AT": + *f = *BeaconOrderFieldNextSeenAt case "INTERVAL": *f = *BeaconOrderFieldInterval default: diff --git a/tavern/internal/ent/gql_where_input.go b/tavern/internal/ent/gql_where_input.go index 35f291807..0675b4500 100644 --- a/tavern/internal/ent/gql_where_input.go +++ b/tavern/internal/ent/gql_where_input.go @@ -111,6 +111,18 @@ type BeaconWhereInput struct { LastSeenAtIsNil bool `json:"lastSeenAtIsNil,omitempty"` LastSeenAtNotNil bool `json:"lastSeenAtNotNil,omitempty"` + // "next_seen_at" field predicates. + NextSeenAt *time.Time `json:"nextSeenAt,omitempty"` + NextSeenAtNEQ *time.Time `json:"nextSeenAtNEQ,omitempty"` + NextSeenAtIn []time.Time `json:"nextSeenAtIn,omitempty"` + NextSeenAtNotIn []time.Time `json:"nextSeenAtNotIn,omitempty"` + NextSeenAtGT *time.Time `json:"nextSeenAtGT,omitempty"` + NextSeenAtGTE *time.Time `json:"nextSeenAtGTE,omitempty"` + NextSeenAtLT *time.Time `json:"nextSeenAtLT,omitempty"` + NextSeenAtLTE *time.Time `json:"nextSeenAtLTE,omitempty"` + NextSeenAtIsNil bool `json:"nextSeenAtIsNil,omitempty"` + NextSeenAtNotNil bool `json:"nextSeenAtNotNil,omitempty"` + // "interval" field predicates. Interval *uint64 `json:"interval,omitempty"` IntervalNEQ *uint64 `json:"intervalNEQ,omitempty"` @@ -425,6 +437,36 @@ func (i *BeaconWhereInput) P() (predicate.Beacon, error) { if i.LastSeenAtNotNil { predicates = append(predicates, beacon.LastSeenAtNotNil()) } + if i.NextSeenAt != nil { + predicates = append(predicates, beacon.NextSeenAtEQ(*i.NextSeenAt)) + } + if i.NextSeenAtNEQ != nil { + predicates = append(predicates, beacon.NextSeenAtNEQ(*i.NextSeenAtNEQ)) + } + if len(i.NextSeenAtIn) > 0 { + predicates = append(predicates, beacon.NextSeenAtIn(i.NextSeenAtIn...)) + } + if len(i.NextSeenAtNotIn) > 0 { + predicates = append(predicates, beacon.NextSeenAtNotIn(i.NextSeenAtNotIn...)) + } + if i.NextSeenAtGT != nil { + predicates = append(predicates, beacon.NextSeenAtGT(*i.NextSeenAtGT)) + } + if i.NextSeenAtGTE != nil { + predicates = append(predicates, beacon.NextSeenAtGTE(*i.NextSeenAtGTE)) + } + if i.NextSeenAtLT != nil { + predicates = append(predicates, beacon.NextSeenAtLT(*i.NextSeenAtLT)) + } + if i.NextSeenAtLTE != nil { + predicates = append(predicates, beacon.NextSeenAtLTE(*i.NextSeenAtLTE)) + } + if i.NextSeenAtIsNil { + predicates = append(predicates, beacon.NextSeenAtIsNil()) + } + if i.NextSeenAtNotNil { + predicates = append(predicates, beacon.NextSeenAtNotNil()) + } if i.Interval != nil { predicates = append(predicates, beacon.IntervalEQ(*i.Interval)) } diff --git a/tavern/internal/ent/migrate/schema.go b/tavern/internal/ent/migrate/schema.go index dbe7d4e38..3cbed7e03 100644 --- a/tavern/internal/ent/migrate/schema.go +++ b/tavern/internal/ent/migrate/schema.go @@ -16,6 +16,7 @@ var ( {Name: "identifier", Type: field.TypeString, Unique: true}, {Name: "agent_identifier", Type: field.TypeString, Nullable: true}, {Name: "last_seen_at", Type: field.TypeTime, Nullable: true}, + {Name: "next_seen_at", Type: field.TypeTime, Nullable: true}, {Name: "interval", Type: field.TypeUint64, Nullable: true}, {Name: "beacon_host", Type: field.TypeInt}, } @@ -27,7 +28,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "beacons_hosts_host", - Columns: []*schema.Column{BeaconsColumns[7]}, + Columns: []*schema.Column{BeaconsColumns[8]}, RefColumns: []*schema.Column{HostsColumns[0]}, OnDelete: schema.NoAction, }, diff --git a/tavern/internal/ent/mutation.go b/tavern/internal/ent/mutation.go index 6ddbfa74e..c5ca1e765 100644 --- a/tavern/internal/ent/mutation.go +++ b/tavern/internal/ent/mutation.go @@ -52,6 +52,7 @@ type BeaconMutation struct { identifier *string agent_identifier *string last_seen_at *time.Time + next_seen_at *time.Time interval *uint64 addinterval *int64 clearedFields map[string]struct{} @@ -382,6 +383,55 @@ func (m *BeaconMutation) ResetLastSeenAt() { delete(m.clearedFields, beacon.FieldLastSeenAt) } +// SetNextSeenAt sets the "next_seen_at" field. +func (m *BeaconMutation) SetNextSeenAt(t time.Time) { + m.next_seen_at = &t +} + +// NextSeenAt returns the value of the "next_seen_at" field in the mutation. +func (m *BeaconMutation) NextSeenAt() (r time.Time, exists bool) { + v := m.next_seen_at + if v == nil { + return + } + return *v, true +} + +// OldNextSeenAt returns the old "next_seen_at" field's value of the Beacon entity. +// If the Beacon object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *BeaconMutation) OldNextSeenAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldNextSeenAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldNextSeenAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldNextSeenAt: %w", err) + } + return oldValue.NextSeenAt, nil +} + +// ClearNextSeenAt clears the value of the "next_seen_at" field. +func (m *BeaconMutation) ClearNextSeenAt() { + m.next_seen_at = nil + m.clearedFields[beacon.FieldNextSeenAt] = struct{}{} +} + +// NextSeenAtCleared returns if the "next_seen_at" field was cleared in this mutation. +func (m *BeaconMutation) NextSeenAtCleared() bool { + _, ok := m.clearedFields[beacon.FieldNextSeenAt] + return ok +} + +// ResetNextSeenAt resets all changes to the "next_seen_at" field. +func (m *BeaconMutation) ResetNextSeenAt() { + m.next_seen_at = nil + delete(m.clearedFields, beacon.FieldNextSeenAt) +} + // SetInterval sets the "interval" field. func (m *BeaconMutation) SetInterval(u uint64) { m.interval = &u @@ -579,7 +629,7 @@ func (m *BeaconMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *BeaconMutation) Fields() []string { - fields := make([]string, 0, 6) + fields := make([]string, 0, 7) if m.name != nil { fields = append(fields, beacon.FieldName) } @@ -595,6 +645,9 @@ func (m *BeaconMutation) Fields() []string { if m.last_seen_at != nil { fields = append(fields, beacon.FieldLastSeenAt) } + if m.next_seen_at != nil { + fields = append(fields, beacon.FieldNextSeenAt) + } if m.interval != nil { fields = append(fields, beacon.FieldInterval) } @@ -616,6 +669,8 @@ func (m *BeaconMutation) Field(name string) (ent.Value, bool) { return m.AgentIdentifier() case beacon.FieldLastSeenAt: return m.LastSeenAt() + case beacon.FieldNextSeenAt: + return m.NextSeenAt() case beacon.FieldInterval: return m.Interval() } @@ -637,6 +692,8 @@ func (m *BeaconMutation) OldField(ctx context.Context, name string) (ent.Value, return m.OldAgentIdentifier(ctx) case beacon.FieldLastSeenAt: return m.OldLastSeenAt(ctx) + case beacon.FieldNextSeenAt: + return m.OldNextSeenAt(ctx) case beacon.FieldInterval: return m.OldInterval(ctx) } @@ -683,6 +740,13 @@ func (m *BeaconMutation) SetField(name string, value ent.Value) error { } m.SetLastSeenAt(v) return nil + case beacon.FieldNextSeenAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetNextSeenAt(v) + return nil case beacon.FieldInterval: v, ok := value.(uint64) if !ok { @@ -744,6 +808,9 @@ func (m *BeaconMutation) ClearedFields() []string { if m.FieldCleared(beacon.FieldLastSeenAt) { fields = append(fields, beacon.FieldLastSeenAt) } + if m.FieldCleared(beacon.FieldNextSeenAt) { + fields = append(fields, beacon.FieldNextSeenAt) + } if m.FieldCleared(beacon.FieldInterval) { fields = append(fields, beacon.FieldInterval) } @@ -770,6 +837,9 @@ func (m *BeaconMutation) ClearField(name string) error { case beacon.FieldLastSeenAt: m.ClearLastSeenAt() return nil + case beacon.FieldNextSeenAt: + m.ClearNextSeenAt() + return nil case beacon.FieldInterval: m.ClearInterval() return nil @@ -796,6 +866,9 @@ func (m *BeaconMutation) ResetField(name string) error { case beacon.FieldLastSeenAt: m.ResetLastSeenAt() return nil + case beacon.FieldNextSeenAt: + m.ResetNextSeenAt() + return nil case beacon.FieldInterval: m.ResetInterval() return nil diff --git a/tavern/internal/ent/runtime/runtime.go b/tavern/internal/ent/runtime/runtime.go index 57804764e..a518c0f54 100644 --- a/tavern/internal/ent/runtime/runtime.go +++ b/tavern/internal/ent/runtime/runtime.go @@ -20,6 +20,8 @@ import ( // (default values, validators, hooks and policies) and stitches it // to their package variables. func init() { + beaconHooks := schema.Beacon{}.Hooks() + beacon.Hooks[0] = beaconHooks[0] beaconFields := schema.Beacon{}.Fields() _ = beaconFields // beaconDescName is the schema descriptor for name field. diff --git a/tavern/internal/ent/schema/beacon.go b/tavern/internal/ent/schema/beacon.go index 300e681da..2f80f04a4 100644 --- a/tavern/internal/ent/schema/beacon.go +++ b/tavern/internal/ent/schema/beacon.go @@ -1,18 +1,20 @@ package schema import ( + "context" "crypto/rand" "encoding/base64" "fmt" "io" - - "realm.pub/tavern/internal/namegen" + "time" "entgo.io/contrib/entgql" "entgo.io/ent" "entgo.io/ent/schema" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" + "realm.pub/tavern/internal/ent/hook" + "realm.pub/tavern/internal/namegen" ) // Beacon holds the schema definition for the Beacon entity. @@ -58,6 +60,13 @@ func (Beacon) Fields() []ent.Field { entgql.Skip(entgql.SkipMutationUpdateInput), ). Comment("Timestamp of when a task was last claimed or updated for the beacon."), + field.Time("next_seen_at"). + Optional(). + Annotations( + entgql.OrderField("NEXT_SEEN_AT"), + entgql.Skip(entgql.SkipMutationUpdateInput), + ). + Comment("Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead)."), field.Uint64("interval"). Optional(). Annotations( @@ -94,6 +103,44 @@ func (Beacon) Annotations() []schema.Annotation { } } +// Hooks defines middleware for mutations for the ent. +func (Beacon) Hooks() []ent.Hook { + return []ent.Hook{ + hook.On(HookDeriveBeaconInfo(), ent.OpCreate|ent.OpUpdate|ent.OpUpdateOne), + } +} + +// HookDeriveBeaconInfo will update beacon info (e.g. next_seen_at) whenever it is mutated. +func HookDeriveBeaconInfo() ent.Hook { + // Get the relevant methods from the Beacon Mutation + type bMutation interface { + LastSeenAt() (time.Time, bool) + Interval() (uint64, bool) + SetNextSeenAt(time.Time) + } + + return func(next ent.Mutator) ent.Mutator { + return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { + // Get the mutation + b, ok := m.(bMutation) + if !ok { + return nil, fmt.Errorf("expected beacon mutation in schema hook, got: %+v", m) + } + + // Set next_seen_at + lastSeenAt, hasLastSeenAt := b.LastSeenAt() + interval, hasInterval := b.Interval() + if hasLastSeenAt && hasInterval { + b.SetNextSeenAt( + lastSeenAt.Add(time.Duration(interval) * time.Second), + ) + } + + return next.Mutate(ctx, m) + }) + } +} + func newRandomIdentifier() string { buf := make([]byte, 64) _, err := io.ReadFull(rand.Reader, buf) diff --git a/tavern/internal/graphql/generated/ent.generated.go b/tavern/internal/graphql/generated/ent.generated.go index 5e86aa4bf..1c938746a 100644 --- a/tavern/internal/graphql/generated/ent.generated.go +++ b/tavern/internal/graphql/generated/ent.generated.go @@ -513,6 +513,47 @@ func (ec *executionContext) fieldContext_Beacon_lastSeenAt(ctx context.Context, return fc, nil } +func (ec *executionContext) _Beacon_nextSeenAt(ctx context.Context, field graphql.CollectedField, obj *ent.Beacon) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Beacon_nextSeenAt(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.NextSeenAt, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(time.Time) + fc.Result = res + return ec.marshalOTime2timeᚐTime(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Beacon_nextSeenAt(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Beacon", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Time does not have child fields") + }, + } + return fc, nil +} + func (ec *executionContext) _Beacon_interval(ctx context.Context, field graphql.CollectedField, obj *ent.Beacon) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Beacon_interval(ctx, field) if err != nil { @@ -1299,6 +1340,8 @@ func (ec *executionContext) fieldContext_Host_beacons(ctx context.Context, field return ec.fieldContext_Beacon_agentIdentifier(ctx, field) case "lastSeenAt": return ec.fieldContext_Beacon_lastSeenAt(ctx, field) + case "nextSeenAt": + return ec.fieldContext_Beacon_nextSeenAt(ctx, field) case "interval": return ec.fieldContext_Beacon_interval(ctx, field) case "host": @@ -1943,6 +1986,8 @@ func (ec *executionContext) fieldContext_Query_beacons(ctx context.Context, fiel return ec.fieldContext_Beacon_agentIdentifier(ctx, field) case "lastSeenAt": return ec.fieldContext_Beacon_lastSeenAt(ctx, field) + case "nextSeenAt": + return ec.fieldContext_Beacon_nextSeenAt(ctx, field) case "interval": return ec.fieldContext_Beacon_interval(ctx, field) case "host": @@ -3665,6 +3710,8 @@ func (ec *executionContext) fieldContext_Task_beacon(ctx context.Context, field return ec.fieldContext_Beacon_agentIdentifier(ctx, field) case "lastSeenAt": return ec.fieldContext_Beacon_lastSeenAt(ctx, field) + case "nextSeenAt": + return ec.fieldContext_Beacon_nextSeenAt(ctx, field) case "interval": return ec.fieldContext_Beacon_interval(ctx, field) case "host": @@ -4565,7 +4612,7 @@ func (ec *executionContext) unmarshalInputBeaconWhereInput(ctx context.Context, asMap[k] = v } - fieldsInOrder := [...]string{"not", "and", "or", "id", "idNEQ", "idIn", "idNotIn", "idGT", "idGTE", "idLT", "idLTE", "name", "nameNEQ", "nameIn", "nameNotIn", "nameGT", "nameGTE", "nameLT", "nameLTE", "nameContains", "nameHasPrefix", "nameHasSuffix", "nameEqualFold", "nameContainsFold", "principal", "principalNEQ", "principalIn", "principalNotIn", "principalGT", "principalGTE", "principalLT", "principalLTE", "principalContains", "principalHasPrefix", "principalHasSuffix", "principalIsNil", "principalNotNil", "principalEqualFold", "principalContainsFold", "identifier", "identifierNEQ", "identifierIn", "identifierNotIn", "identifierGT", "identifierGTE", "identifierLT", "identifierLTE", "identifierContains", "identifierHasPrefix", "identifierHasSuffix", "identifierEqualFold", "identifierContainsFold", "agentIdentifier", "agentIdentifierNEQ", "agentIdentifierIn", "agentIdentifierNotIn", "agentIdentifierGT", "agentIdentifierGTE", "agentIdentifierLT", "agentIdentifierLTE", "agentIdentifierContains", "agentIdentifierHasPrefix", "agentIdentifierHasSuffix", "agentIdentifierIsNil", "agentIdentifierNotNil", "agentIdentifierEqualFold", "agentIdentifierContainsFold", "lastSeenAt", "lastSeenAtNEQ", "lastSeenAtIn", "lastSeenAtNotIn", "lastSeenAtGT", "lastSeenAtGTE", "lastSeenAtLT", "lastSeenAtLTE", "lastSeenAtIsNil", "lastSeenAtNotNil", "interval", "intervalNEQ", "intervalIn", "intervalNotIn", "intervalGT", "intervalGTE", "intervalLT", "intervalLTE", "intervalIsNil", "intervalNotNil", "hasHost", "hasHostWith", "hasTasks", "hasTasksWith"} + fieldsInOrder := [...]string{"not", "and", "or", "id", "idNEQ", "idIn", "idNotIn", "idGT", "idGTE", "idLT", "idLTE", "name", "nameNEQ", "nameIn", "nameNotIn", "nameGT", "nameGTE", "nameLT", "nameLTE", "nameContains", "nameHasPrefix", "nameHasSuffix", "nameEqualFold", "nameContainsFold", "principal", "principalNEQ", "principalIn", "principalNotIn", "principalGT", "principalGTE", "principalLT", "principalLTE", "principalContains", "principalHasPrefix", "principalHasSuffix", "principalIsNil", "principalNotNil", "principalEqualFold", "principalContainsFold", "identifier", "identifierNEQ", "identifierIn", "identifierNotIn", "identifierGT", "identifierGTE", "identifierLT", "identifierLTE", "identifierContains", "identifierHasPrefix", "identifierHasSuffix", "identifierEqualFold", "identifierContainsFold", "agentIdentifier", "agentIdentifierNEQ", "agentIdentifierIn", "agentIdentifierNotIn", "agentIdentifierGT", "agentIdentifierGTE", "agentIdentifierLT", "agentIdentifierLTE", "agentIdentifierContains", "agentIdentifierHasPrefix", "agentIdentifierHasSuffix", "agentIdentifierIsNil", "agentIdentifierNotNil", "agentIdentifierEqualFold", "agentIdentifierContainsFold", "lastSeenAt", "lastSeenAtNEQ", "lastSeenAtIn", "lastSeenAtNotIn", "lastSeenAtGT", "lastSeenAtGTE", "lastSeenAtLT", "lastSeenAtLTE", "lastSeenAtIsNil", "lastSeenAtNotNil", "nextSeenAt", "nextSeenAtNEQ", "nextSeenAtIn", "nextSeenAtNotIn", "nextSeenAtGT", "nextSeenAtGTE", "nextSeenAtLT", "nextSeenAtLTE", "nextSeenAtIsNil", "nextSeenAtNotNil", "interval", "intervalNEQ", "intervalIn", "intervalNotIn", "intervalGT", "intervalGTE", "intervalLT", "intervalLTE", "intervalIsNil", "intervalNotNil", "hasHost", "hasHostWith", "hasTasks", "hasTasksWith"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -5265,6 +5312,96 @@ func (ec *executionContext) unmarshalInputBeaconWhereInput(ctx context.Context, return it, err } it.LastSeenAtNotNil = data + case "nextSeenAt": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAt")) + data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAt = data + case "nextSeenAtNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtNEQ")) + data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAtNEQ = data + case "nextSeenAtIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtIn")) + data, err := ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAtIn = data + case "nextSeenAtNotIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtNotIn")) + data, err := ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAtNotIn = data + case "nextSeenAtGT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtGT")) + data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAtGT = data + case "nextSeenAtGTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtGTE")) + data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAtGTE = data + case "nextSeenAtLT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtLT")) + data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAtLT = data + case "nextSeenAtLTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtLTE")) + data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAtLTE = data + case "nextSeenAtIsNil": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtIsNil")) + data, err := ec.unmarshalOBoolean2bool(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAtIsNil = data + case "nextSeenAtNotNil": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtNotNil")) + data, err := ec.unmarshalOBoolean2bool(ctx, v) + if err != nil { + return it, err + } + it.NextSeenAtNotNil = data case "interval": var err error @@ -10320,6 +10457,8 @@ func (ec *executionContext) _Beacon(ctx context.Context, sel ast.SelectionSet, o out.Values[i] = ec._Beacon_agentIdentifier(ctx, field, obj) case "lastSeenAt": out.Values[i] = ec._Beacon_lastSeenAt(ctx, field, obj) + case "nextSeenAt": + out.Values[i] = ec._Beacon_nextSeenAt(ctx, field, obj) case "interval": out.Values[i] = ec._Beacon_interval(ctx, field, obj) case "host": diff --git a/tavern/internal/graphql/generated/mutation.generated.go b/tavern/internal/graphql/generated/mutation.generated.go index ee55d23b0..62f4bbbf2 100644 --- a/tavern/internal/graphql/generated/mutation.generated.go +++ b/tavern/internal/graphql/generated/mutation.generated.go @@ -392,6 +392,8 @@ func (ec *executionContext) fieldContext_Mutation_updateBeacon(ctx context.Conte return ec.fieldContext_Beacon_agentIdentifier(ctx, field) case "lastSeenAt": return ec.fieldContext_Beacon_lastSeenAt(ctx, field) + case "nextSeenAt": + return ec.fieldContext_Beacon_nextSeenAt(ctx, field) case "interval": return ec.fieldContext_Beacon_interval(ctx, field) case "host": diff --git a/tavern/internal/graphql/generated/root_.generated.go b/tavern/internal/graphql/generated/root_.generated.go index 534c2918b..1bf6fef01 100644 --- a/tavern/internal/graphql/generated/root_.generated.go +++ b/tavern/internal/graphql/generated/root_.generated.go @@ -52,6 +52,7 @@ type ComplexityRoot struct { Interval func(childComplexity int) int LastSeenAt func(childComplexity int) int Name func(childComplexity int) int + NextSeenAt func(childComplexity int) int Principal func(childComplexity int) int Tasks func(childComplexity int) int } @@ -241,6 +242,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Beacon.Name(childComplexity), true + case "Beacon.nextSeenAt": + if e.complexity.Beacon.NextSeenAt == nil { + break + } + + return e.complexity.Beacon.NextSeenAt(childComplexity), true + case "Beacon.principal": if e.complexity.Beacon.Principal == nil { break @@ -1059,6 +1067,8 @@ type Beacon implements Node { agentIdentifier: String """Timestamp of when a task was last claimed or updated for the beacon.""" lastSeenAt: Time + """Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead).""" + nextSeenAt: Time """Duration until next callback, in seconds.""" interval: Uint64 """Host this beacon is running on.""" @@ -1076,6 +1086,7 @@ input BeaconOrder { """Properties by which Beacon connections can be ordered.""" enum BeaconOrderField { LAST_SEEN_AT + NEXT_SEEN_AT INTERVAL } """ @@ -1166,6 +1177,17 @@ input BeaconWhereInput { lastSeenAtLTE: Time lastSeenAtIsNil: Boolean lastSeenAtNotNil: Boolean + """next_seen_at field predicates""" + nextSeenAt: Time + nextSeenAtNEQ: Time + nextSeenAtIn: [Time!] + nextSeenAtNotIn: [Time!] + nextSeenAtGT: Time + nextSeenAtGTE: Time + nextSeenAtLT: Time + nextSeenAtLTE: Time + nextSeenAtIsNil: Boolean + nextSeenAtNotNil: Boolean """interval field predicates""" interval: Uint64 intervalNEQ: Uint64 diff --git a/tavern/internal/graphql/schema.graphql b/tavern/internal/graphql/schema.graphql index 654f0b661..1d3406bb7 100644 --- a/tavern/internal/graphql/schema.graphql +++ b/tavern/internal/graphql/schema.graphql @@ -17,6 +17,8 @@ type Beacon implements Node { agentIdentifier: String """Timestamp of when a task was last claimed or updated for the beacon.""" lastSeenAt: Time + """Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead).""" + nextSeenAt: Time """Duration until next callback, in seconds.""" interval: Uint64 """Host this beacon is running on.""" @@ -34,6 +36,7 @@ input BeaconOrder { """Properties by which Beacon connections can be ordered.""" enum BeaconOrderField { LAST_SEEN_AT + NEXT_SEEN_AT INTERVAL } """ @@ -124,6 +127,17 @@ input BeaconWhereInput { lastSeenAtLTE: Time lastSeenAtIsNil: Boolean lastSeenAtNotNil: Boolean + """next_seen_at field predicates""" + nextSeenAt: Time + nextSeenAtNEQ: Time + nextSeenAtIn: [Time!] + nextSeenAtNotIn: [Time!] + nextSeenAtGT: Time + nextSeenAtGTE: Time + nextSeenAtLT: Time + nextSeenAtLTE: Time + nextSeenAtIsNil: Boolean + nextSeenAtNotNil: Boolean """interval field predicates""" interval: Uint64 intervalNEQ: Uint64 diff --git a/tavern/internal/graphql/schema/ent.graphql b/tavern/internal/graphql/schema/ent.graphql index f049210fb..6ce7a8f06 100644 --- a/tavern/internal/graphql/schema/ent.graphql +++ b/tavern/internal/graphql/schema/ent.graphql @@ -12,6 +12,8 @@ type Beacon implements Node { agentIdentifier: String """Timestamp of when a task was last claimed or updated for the beacon.""" lastSeenAt: Time + """Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead).""" + nextSeenAt: Time """Duration until next callback, in seconds.""" interval: Uint64 """Host this beacon is running on.""" @@ -29,6 +31,7 @@ input BeaconOrder { """Properties by which Beacon connections can be ordered.""" enum BeaconOrderField { LAST_SEEN_AT + NEXT_SEEN_AT INTERVAL } """ @@ -119,6 +122,17 @@ input BeaconWhereInput { lastSeenAtLTE: Time lastSeenAtIsNil: Boolean lastSeenAtNotNil: Boolean + """next_seen_at field predicates""" + nextSeenAt: Time + nextSeenAtNEQ: Time + nextSeenAtIn: [Time!] + nextSeenAtNotIn: [Time!] + nextSeenAtGT: Time + nextSeenAtGTE: Time + nextSeenAtLT: Time + nextSeenAtLTE: Time + nextSeenAtIsNil: Boolean + nextSeenAtNotNil: Boolean """interval field predicates""" interval: Uint64 intervalNEQ: Uint64 diff --git a/tavern/internal/www/schema.graphql b/tavern/internal/www/schema.graphql index 654f0b661..1d3406bb7 100644 --- a/tavern/internal/www/schema.graphql +++ b/tavern/internal/www/schema.graphql @@ -17,6 +17,8 @@ type Beacon implements Node { agentIdentifier: String """Timestamp of when a task was last claimed or updated for the beacon.""" lastSeenAt: Time + """Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead).""" + nextSeenAt: Time """Duration until next callback, in seconds.""" interval: Uint64 """Host this beacon is running on.""" @@ -34,6 +36,7 @@ input BeaconOrder { """Properties by which Beacon connections can be ordered.""" enum BeaconOrderField { LAST_SEEN_AT + NEXT_SEEN_AT INTERVAL } """ @@ -124,6 +127,17 @@ input BeaconWhereInput { lastSeenAtLTE: Time lastSeenAtIsNil: Boolean lastSeenAtNotNil: Boolean + """next_seen_at field predicates""" + nextSeenAt: Time + nextSeenAtNEQ: Time + nextSeenAtIn: [Time!] + nextSeenAtNotIn: [Time!] + nextSeenAtGT: Time + nextSeenAtGTE: Time + nextSeenAtLT: Time + nextSeenAtLTE: Time + nextSeenAtIsNil: Boolean + nextSeenAtNotNil: Boolean """interval field predicates""" interval: Uint64 intervalNEQ: Uint64 From 667b141dfc79cd1ec254c610752fbb633d4156cb Mon Sep 17 00:00:00 2001 From: KCarretto Date: Sun, 21 Jan 2024 18:24:49 +0000 Subject: [PATCH 2/2] remove next_seen_at since it's unwanted --- tavern/internal/c2/api_claim_tasks_test.go | 9 -- tavern/internal/ent/beacon.go | 13 +- tavern/internal/ent/beacon/beacon.go | 15 -- tavern/internal/ent/beacon/where.go | 55 ------- tavern/internal/ent/beacon_create.go | 91 +---------- tavern/internal/ent/beacon_update.go | 52 ------- tavern/internal/ent/client.go | 3 +- tavern/internal/ent/gql_collection.go | 5 - tavern/internal/ent/gql_pagination.go | 18 --- tavern/internal/ent/gql_where_input.go | 42 ------ tavern/internal/ent/migrate/schema.go | 3 +- tavern/internal/ent/mutation.go | 75 +--------- tavern/internal/ent/runtime/runtime.go | 2 - tavern/internal/ent/schema/beacon.go | 48 ------ .../graphql/generated/ent.generated.go | 141 +----------------- .../graphql/generated/mutation.generated.go | 2 - .../graphql/generated/root_.generated.go | 22 --- tavern/internal/graphql/schema.graphql | 14 -- tavern/internal/graphql/schema/ent.graphql | 14 -- tavern/internal/www/schema.graphql | 14 -- 20 files changed, 7 insertions(+), 631 deletions(-) diff --git a/tavern/internal/c2/api_claim_tasks_test.go b/tavern/internal/c2/api_claim_tasks_test.go index b535988f1..59adf6c1d 100644 --- a/tavern/internal/c2/api_claim_tasks_test.go +++ b/tavern/internal/c2/api_claim_tasks_test.go @@ -40,8 +40,6 @@ func TestClaimTasks(t *testing.T) { wantBeaconExist bool wantBeaconLastSeenAtBefore time.Time wantBeaconLastSeenAtAfter time.Time - wantBeaconNextSeenAtBefore time.Time - wantBeaconNextSeenAtAfter time.Time }{ { name: "First_Callback", @@ -65,8 +63,6 @@ func TestClaimTasks(t *testing.T) { wantCode: codes.OK, wantBeaconExist: true, - wantBeaconNextSeenAtBefore: time.Now().UTC().Add(120 * time.Second), - wantBeaconNextSeenAtAfter: time.Now().UTC().Add(-120 * time.Second), wantBeaconLastSeenAtBefore: time.Now().UTC().Add(10 * time.Second), wantBeaconLastSeenAtAfter: time.Now().UTC().Add(-10 * time.Second), }, @@ -92,8 +88,6 @@ func TestClaimTasks(t *testing.T) { wantCode: codes.OK, wantBeaconExist: true, - wantBeaconNextSeenAtBefore: time.Now().UTC().Add(200 * time.Second), - wantBeaconNextSeenAtAfter: time.Now().UTC().Add(-200 * time.Second), wantBeaconLastSeenAtBefore: time.Now().UTC().Add(10 * time.Second), wantBeaconLastSeenAtAfter: time.Now().UTC().Add(-10 * time.Second), }, @@ -124,8 +118,6 @@ func TestClaimTasks(t *testing.T) { wantCode: codes.OK, wantBeaconExist: true, - wantBeaconNextSeenAtBefore: time.Now().UTC().Add(200 * time.Second), - wantBeaconNextSeenAtAfter: time.Now().UTC().Add(-200 * time.Second), wantBeaconLastSeenAtBefore: time.Now().UTC().Add(10 * time.Second), wantBeaconLastSeenAtAfter: time.Now().UTC().Add(-10 * time.Second), }, @@ -164,7 +156,6 @@ func TestClaimTasks(t *testing.T) { // Beacon Assertions assert.WithinRange(t, testBeacon.LastSeenAt, tc.wantBeaconLastSeenAtAfter, tc.wantBeaconLastSeenAtBefore) - assert.WithinRange(t, testBeacon.NextSeenAt, tc.wantBeaconNextSeenAtAfter, tc.wantBeaconNextSeenAtBefore) }) } } diff --git a/tavern/internal/ent/beacon.go b/tavern/internal/ent/beacon.go index 727ed7412..7f752517d 100644 --- a/tavern/internal/ent/beacon.go +++ b/tavern/internal/ent/beacon.go @@ -28,8 +28,6 @@ type Beacon struct { AgentIdentifier string `json:"agent_identifier,omitempty"` // Timestamp of when a task was last claimed or updated for the beacon. LastSeenAt time.Time `json:"last_seen_at,omitempty"` - // Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead). - NextSeenAt time.Time `json:"next_seen_at,omitempty"` // Duration until next callback, in seconds. Interval uint64 `json:"interval,omitempty"` // Edges holds the relations/edges for other nodes in the graph. @@ -85,7 +83,7 @@ func (*Beacon) scanValues(columns []string) ([]any, error) { values[i] = new(sql.NullInt64) case beacon.FieldName, beacon.FieldPrincipal, beacon.FieldIdentifier, beacon.FieldAgentIdentifier: values[i] = new(sql.NullString) - case beacon.FieldLastSeenAt, beacon.FieldNextSeenAt: + case beacon.FieldLastSeenAt: values[i] = new(sql.NullTime) case beacon.ForeignKeys[0]: // beacon_host values[i] = new(sql.NullInt64) @@ -140,12 +138,6 @@ func (b *Beacon) assignValues(columns []string, values []any) error { } else if value.Valid { b.LastSeenAt = value.Time } - case beacon.FieldNextSeenAt: - if value, ok := values[i].(*sql.NullTime); !ok { - return fmt.Errorf("unexpected type %T for field next_seen_at", values[i]) - } else if value.Valid { - b.NextSeenAt = value.Time - } case beacon.FieldInterval: if value, ok := values[i].(*sql.NullInt64); !ok { return fmt.Errorf("unexpected type %T for field interval", values[i]) @@ -220,9 +212,6 @@ func (b *Beacon) String() string { builder.WriteString("last_seen_at=") builder.WriteString(b.LastSeenAt.Format(time.ANSIC)) builder.WriteString(", ") - builder.WriteString("next_seen_at=") - builder.WriteString(b.NextSeenAt.Format(time.ANSIC)) - builder.WriteString(", ") builder.WriteString("interval=") builder.WriteString(fmt.Sprintf("%v", b.Interval)) builder.WriteByte(')') diff --git a/tavern/internal/ent/beacon/beacon.go b/tavern/internal/ent/beacon/beacon.go index 7fae1d93d..7b7e8550b 100644 --- a/tavern/internal/ent/beacon/beacon.go +++ b/tavern/internal/ent/beacon/beacon.go @@ -3,7 +3,6 @@ package beacon import ( - "entgo.io/ent" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" ) @@ -23,8 +22,6 @@ const ( FieldAgentIdentifier = "agent_identifier" // FieldLastSeenAt holds the string denoting the last_seen_at field in the database. FieldLastSeenAt = "last_seen_at" - // FieldNextSeenAt holds the string denoting the next_seen_at field in the database. - FieldNextSeenAt = "next_seen_at" // FieldInterval holds the string denoting the interval field in the database. FieldInterval = "interval" // EdgeHost holds the string denoting the host edge name in mutations. @@ -57,7 +54,6 @@ var Columns = []string{ FieldIdentifier, FieldAgentIdentifier, FieldLastSeenAt, - FieldNextSeenAt, FieldInterval, } @@ -82,13 +78,7 @@ func ValidColumn(column string) bool { return false } -// Note that the variables below are initialized by the runtime -// package on the initialization of the application. Therefore, -// it should be imported in the main as follows: -// -// import _ "realm.pub/tavern/internal/ent/runtime" var ( - Hooks [1]ent.Hook // DefaultName holds the default value on creation for the "name" field. DefaultName func() string // NameValidator is a validator for the "name" field. It is called by the builders before save. @@ -136,11 +126,6 @@ func ByLastSeenAt(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldLastSeenAt, opts...).ToFunc() } -// ByNextSeenAt orders the results by the next_seen_at field. -func ByNextSeenAt(opts ...sql.OrderTermOption) OrderOption { - return sql.OrderByField(FieldNextSeenAt, opts...).ToFunc() -} - // ByInterval orders the results by the interval field. func ByInterval(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldInterval, opts...).ToFunc() diff --git a/tavern/internal/ent/beacon/where.go b/tavern/internal/ent/beacon/where.go index 6e0263bf6..0f5c15a00 100644 --- a/tavern/internal/ent/beacon/where.go +++ b/tavern/internal/ent/beacon/where.go @@ -80,11 +80,6 @@ func LastSeenAt(v time.Time) predicate.Beacon { return predicate.Beacon(sql.FieldEQ(FieldLastSeenAt, v)) } -// NextSeenAt applies equality check predicate on the "next_seen_at" field. It's identical to NextSeenAtEQ. -func NextSeenAt(v time.Time) predicate.Beacon { - return predicate.Beacon(sql.FieldEQ(FieldNextSeenAt, v)) -} - // Interval applies equality check predicate on the "interval" field. It's identical to IntervalEQ. func Interval(v uint64) predicate.Beacon { return predicate.Beacon(sql.FieldEQ(FieldInterval, v)) @@ -420,56 +415,6 @@ func LastSeenAtNotNil() predicate.Beacon { return predicate.Beacon(sql.FieldNotNull(FieldLastSeenAt)) } -// NextSeenAtEQ applies the EQ predicate on the "next_seen_at" field. -func NextSeenAtEQ(v time.Time) predicate.Beacon { - return predicate.Beacon(sql.FieldEQ(FieldNextSeenAt, v)) -} - -// NextSeenAtNEQ applies the NEQ predicate on the "next_seen_at" field. -func NextSeenAtNEQ(v time.Time) predicate.Beacon { - return predicate.Beacon(sql.FieldNEQ(FieldNextSeenAt, v)) -} - -// NextSeenAtIn applies the In predicate on the "next_seen_at" field. -func NextSeenAtIn(vs ...time.Time) predicate.Beacon { - return predicate.Beacon(sql.FieldIn(FieldNextSeenAt, vs...)) -} - -// NextSeenAtNotIn applies the NotIn predicate on the "next_seen_at" field. -func NextSeenAtNotIn(vs ...time.Time) predicate.Beacon { - return predicate.Beacon(sql.FieldNotIn(FieldNextSeenAt, vs...)) -} - -// NextSeenAtGT applies the GT predicate on the "next_seen_at" field. -func NextSeenAtGT(v time.Time) predicate.Beacon { - return predicate.Beacon(sql.FieldGT(FieldNextSeenAt, v)) -} - -// NextSeenAtGTE applies the GTE predicate on the "next_seen_at" field. -func NextSeenAtGTE(v time.Time) predicate.Beacon { - return predicate.Beacon(sql.FieldGTE(FieldNextSeenAt, v)) -} - -// NextSeenAtLT applies the LT predicate on the "next_seen_at" field. -func NextSeenAtLT(v time.Time) predicate.Beacon { - return predicate.Beacon(sql.FieldLT(FieldNextSeenAt, v)) -} - -// NextSeenAtLTE applies the LTE predicate on the "next_seen_at" field. -func NextSeenAtLTE(v time.Time) predicate.Beacon { - return predicate.Beacon(sql.FieldLTE(FieldNextSeenAt, v)) -} - -// NextSeenAtIsNil applies the IsNil predicate on the "next_seen_at" field. -func NextSeenAtIsNil() predicate.Beacon { - return predicate.Beacon(sql.FieldIsNull(FieldNextSeenAt)) -} - -// NextSeenAtNotNil applies the NotNil predicate on the "next_seen_at" field. -func NextSeenAtNotNil() predicate.Beacon { - return predicate.Beacon(sql.FieldNotNull(FieldNextSeenAt)) -} - // IntervalEQ applies the EQ predicate on the "interval" field. func IntervalEQ(v uint64) predicate.Beacon { return predicate.Beacon(sql.FieldEQ(FieldInterval, v)) diff --git a/tavern/internal/ent/beacon_create.go b/tavern/internal/ent/beacon_create.go index bb03ddac5..84f7c64d8 100644 --- a/tavern/internal/ent/beacon_create.go +++ b/tavern/internal/ent/beacon_create.go @@ -94,20 +94,6 @@ func (bc *BeaconCreate) SetNillableLastSeenAt(t *time.Time) *BeaconCreate { return bc } -// SetNextSeenAt sets the "next_seen_at" field. -func (bc *BeaconCreate) SetNextSeenAt(t time.Time) *BeaconCreate { - bc.mutation.SetNextSeenAt(t) - return bc -} - -// SetNillableNextSeenAt sets the "next_seen_at" field if the given value is not nil. -func (bc *BeaconCreate) SetNillableNextSeenAt(t *time.Time) *BeaconCreate { - if t != nil { - bc.SetNextSeenAt(*t) - } - return bc -} - // SetInterval sets the "interval" field. func (bc *BeaconCreate) SetInterval(u uint64) *BeaconCreate { bc.mutation.SetInterval(u) @@ -155,9 +141,7 @@ func (bc *BeaconCreate) Mutation() *BeaconMutation { // Save creates the Beacon in the database. func (bc *BeaconCreate) Save(ctx context.Context) (*Beacon, error) { - if err := bc.defaults(); err != nil { - return nil, err - } + bc.defaults() return withHooks(ctx, bc.sqlSave, bc.mutation, bc.hooks) } @@ -184,22 +168,15 @@ func (bc *BeaconCreate) ExecX(ctx context.Context) { } // defaults sets the default values of the builder before save. -func (bc *BeaconCreate) defaults() error { +func (bc *BeaconCreate) defaults() { if _, ok := bc.mutation.Name(); !ok { - if beacon.DefaultName == nil { - return fmt.Errorf("ent: uninitialized beacon.DefaultName (forgotten import ent/runtime?)") - } v := beacon.DefaultName() bc.mutation.SetName(v) } if _, ok := bc.mutation.Identifier(); !ok { - if beacon.DefaultIdentifier == nil { - return fmt.Errorf("ent: uninitialized beacon.DefaultIdentifier (forgotten import ent/runtime?)") - } v := beacon.DefaultIdentifier() bc.mutation.SetIdentifier(v) } - return nil } // check runs all checks and user-defined validators on the builder. @@ -280,10 +257,6 @@ func (bc *BeaconCreate) createSpec() (*Beacon, *sqlgraph.CreateSpec) { _spec.SetField(beacon.FieldLastSeenAt, field.TypeTime, value) _node.LastSeenAt = value } - if value, ok := bc.mutation.NextSeenAt(); ok { - _spec.SetField(beacon.FieldNextSeenAt, field.TypeTime, value) - _node.NextSeenAt = value - } if value, ok := bc.mutation.Interval(); ok { _spec.SetField(beacon.FieldInterval, field.TypeUint64, value) _node.Interval = value @@ -439,24 +412,6 @@ func (u *BeaconUpsert) ClearLastSeenAt() *BeaconUpsert { return u } -// SetNextSeenAt sets the "next_seen_at" field. -func (u *BeaconUpsert) SetNextSeenAt(v time.Time) *BeaconUpsert { - u.Set(beacon.FieldNextSeenAt, v) - return u -} - -// UpdateNextSeenAt sets the "next_seen_at" field to the value that was provided on create. -func (u *BeaconUpsert) UpdateNextSeenAt() *BeaconUpsert { - u.SetExcluded(beacon.FieldNextSeenAt) - return u -} - -// ClearNextSeenAt clears the value of the "next_seen_at" field. -func (u *BeaconUpsert) ClearNextSeenAt() *BeaconUpsert { - u.SetNull(beacon.FieldNextSeenAt) - return u -} - // SetInterval sets the "interval" field. func (u *BeaconUpsert) SetInterval(v uint64) *BeaconUpsert { u.Set(beacon.FieldInterval, v) @@ -603,27 +558,6 @@ func (u *BeaconUpsertOne) ClearLastSeenAt() *BeaconUpsertOne { }) } -// SetNextSeenAt sets the "next_seen_at" field. -func (u *BeaconUpsertOne) SetNextSeenAt(v time.Time) *BeaconUpsertOne { - return u.Update(func(s *BeaconUpsert) { - s.SetNextSeenAt(v) - }) -} - -// UpdateNextSeenAt sets the "next_seen_at" field to the value that was provided on create. -func (u *BeaconUpsertOne) UpdateNextSeenAt() *BeaconUpsertOne { - return u.Update(func(s *BeaconUpsert) { - s.UpdateNextSeenAt() - }) -} - -// ClearNextSeenAt clears the value of the "next_seen_at" field. -func (u *BeaconUpsertOne) ClearNextSeenAt() *BeaconUpsertOne { - return u.Update(func(s *BeaconUpsert) { - s.ClearNextSeenAt() - }) -} - // SetInterval sets the "interval" field. func (u *BeaconUpsertOne) SetInterval(v uint64) *BeaconUpsertOne { return u.Update(func(s *BeaconUpsert) { @@ -940,27 +874,6 @@ func (u *BeaconUpsertBulk) ClearLastSeenAt() *BeaconUpsertBulk { }) } -// SetNextSeenAt sets the "next_seen_at" field. -func (u *BeaconUpsertBulk) SetNextSeenAt(v time.Time) *BeaconUpsertBulk { - return u.Update(func(s *BeaconUpsert) { - s.SetNextSeenAt(v) - }) -} - -// UpdateNextSeenAt sets the "next_seen_at" field to the value that was provided on create. -func (u *BeaconUpsertBulk) UpdateNextSeenAt() *BeaconUpsertBulk { - return u.Update(func(s *BeaconUpsert) { - s.UpdateNextSeenAt() - }) -} - -// ClearNextSeenAt clears the value of the "next_seen_at" field. -func (u *BeaconUpsertBulk) ClearNextSeenAt() *BeaconUpsertBulk { - return u.Update(func(s *BeaconUpsert) { - s.ClearNextSeenAt() - }) -} - // SetInterval sets the "interval" field. func (u *BeaconUpsertBulk) SetInterval(v uint64) *BeaconUpsertBulk { return u.Update(func(s *BeaconUpsert) { diff --git a/tavern/internal/ent/beacon_update.go b/tavern/internal/ent/beacon_update.go index acd7b06fe..dbc1c31b7 100644 --- a/tavern/internal/ent/beacon_update.go +++ b/tavern/internal/ent/beacon_update.go @@ -104,26 +104,6 @@ func (bu *BeaconUpdate) ClearLastSeenAt() *BeaconUpdate { return bu } -// SetNextSeenAt sets the "next_seen_at" field. -func (bu *BeaconUpdate) SetNextSeenAt(t time.Time) *BeaconUpdate { - bu.mutation.SetNextSeenAt(t) - return bu -} - -// SetNillableNextSeenAt sets the "next_seen_at" field if the given value is not nil. -func (bu *BeaconUpdate) SetNillableNextSeenAt(t *time.Time) *BeaconUpdate { - if t != nil { - bu.SetNextSeenAt(*t) - } - return bu -} - -// ClearNextSeenAt clears the value of the "next_seen_at" field. -func (bu *BeaconUpdate) ClearNextSeenAt() *BeaconUpdate { - bu.mutation.ClearNextSeenAt() - return bu -} - // SetInterval sets the "interval" field. func (bu *BeaconUpdate) SetInterval(u uint64) *BeaconUpdate { bu.mutation.ResetInterval() @@ -292,12 +272,6 @@ func (bu *BeaconUpdate) sqlSave(ctx context.Context) (n int, err error) { if bu.mutation.LastSeenAtCleared() { _spec.ClearField(beacon.FieldLastSeenAt, field.TypeTime) } - if value, ok := bu.mutation.NextSeenAt(); ok { - _spec.SetField(beacon.FieldNextSeenAt, field.TypeTime, value) - } - if bu.mutation.NextSeenAtCleared() { - _spec.ClearField(beacon.FieldNextSeenAt, field.TypeTime) - } if value, ok := bu.mutation.Interval(); ok { _spec.SetField(beacon.FieldInterval, field.TypeUint64, value) } @@ -475,26 +449,6 @@ func (buo *BeaconUpdateOne) ClearLastSeenAt() *BeaconUpdateOne { return buo } -// SetNextSeenAt sets the "next_seen_at" field. -func (buo *BeaconUpdateOne) SetNextSeenAt(t time.Time) *BeaconUpdateOne { - buo.mutation.SetNextSeenAt(t) - return buo -} - -// SetNillableNextSeenAt sets the "next_seen_at" field if the given value is not nil. -func (buo *BeaconUpdateOne) SetNillableNextSeenAt(t *time.Time) *BeaconUpdateOne { - if t != nil { - buo.SetNextSeenAt(*t) - } - return buo -} - -// ClearNextSeenAt clears the value of the "next_seen_at" field. -func (buo *BeaconUpdateOne) ClearNextSeenAt() *BeaconUpdateOne { - buo.mutation.ClearNextSeenAt() - return buo -} - // SetInterval sets the "interval" field. func (buo *BeaconUpdateOne) SetInterval(u uint64) *BeaconUpdateOne { buo.mutation.ResetInterval() @@ -693,12 +647,6 @@ func (buo *BeaconUpdateOne) sqlSave(ctx context.Context) (_node *Beacon, err err if buo.mutation.LastSeenAtCleared() { _spec.ClearField(beacon.FieldLastSeenAt, field.TypeTime) } - if value, ok := buo.mutation.NextSeenAt(); ok { - _spec.SetField(beacon.FieldNextSeenAt, field.TypeTime, value) - } - if buo.mutation.NextSeenAtCleared() { - _spec.ClearField(beacon.FieldNextSeenAt, field.TypeTime) - } if value, ok := buo.mutation.Interval(); ok { _spec.SetField(beacon.FieldInterval, field.TypeUint64, value) } diff --git a/tavern/internal/ent/client.go b/tavern/internal/ent/client.go index d97a77577..88ef3dc98 100644 --- a/tavern/internal/ent/client.go +++ b/tavern/internal/ent/client.go @@ -400,8 +400,7 @@ func (c *BeaconClient) QueryTasks(b *Beacon) *TaskQuery { // Hooks returns the client hooks. func (c *BeaconClient) Hooks() []Hook { - hooks := c.hooks.Beacon - return append(hooks[:len(hooks):len(hooks)], beacon.Hooks[:]...) + return c.hooks.Beacon } // Interceptors returns the client interceptors. diff --git a/tavern/internal/ent/gql_collection.go b/tavern/internal/ent/gql_collection.go index 7a1cc4b39..6edfff931 100644 --- a/tavern/internal/ent/gql_collection.go +++ b/tavern/internal/ent/gql_collection.go @@ -86,11 +86,6 @@ func (b *BeaconQuery) collectField(ctx context.Context, opCtx *graphql.Operation selectedFields = append(selectedFields, beacon.FieldLastSeenAt) fieldSeen[beacon.FieldLastSeenAt] = struct{}{} } - case "nextSeenAt": - if _, ok := fieldSeen[beacon.FieldNextSeenAt]; !ok { - selectedFields = append(selectedFields, beacon.FieldNextSeenAt) - fieldSeen[beacon.FieldNextSeenAt] = struct{}{} - } case "interval": if _, ok := fieldSeen[beacon.FieldInterval]; !ok { selectedFields = append(selectedFields, beacon.FieldInterval) diff --git a/tavern/internal/ent/gql_pagination.go b/tavern/internal/ent/gql_pagination.go index e896c12c4..50196d99b 100644 --- a/tavern/internal/ent/gql_pagination.go +++ b/tavern/internal/ent/gql_pagination.go @@ -325,20 +325,6 @@ var ( } }, } - // BeaconOrderFieldNextSeenAt orders Beacon by next_seen_at. - BeaconOrderFieldNextSeenAt = &BeaconOrderField{ - Value: func(b *Beacon) (ent.Value, error) { - return b.NextSeenAt, nil - }, - column: beacon.FieldNextSeenAt, - toTerm: beacon.ByNextSeenAt, - toCursor: func(b *Beacon) Cursor { - return Cursor{ - ID: b.ID, - Value: b.NextSeenAt, - } - }, - } // BeaconOrderFieldInterval orders Beacon by interval. BeaconOrderFieldInterval = &BeaconOrderField{ Value: func(b *Beacon) (ent.Value, error) { @@ -361,8 +347,6 @@ func (f BeaconOrderField) String() string { switch f.column { case BeaconOrderFieldLastSeenAt.column: str = "LAST_SEEN_AT" - case BeaconOrderFieldNextSeenAt.column: - str = "NEXT_SEEN_AT" case BeaconOrderFieldInterval.column: str = "INTERVAL" } @@ -383,8 +367,6 @@ func (f *BeaconOrderField) UnmarshalGQL(v interface{}) error { switch str { case "LAST_SEEN_AT": *f = *BeaconOrderFieldLastSeenAt - case "NEXT_SEEN_AT": - *f = *BeaconOrderFieldNextSeenAt case "INTERVAL": *f = *BeaconOrderFieldInterval default: diff --git a/tavern/internal/ent/gql_where_input.go b/tavern/internal/ent/gql_where_input.go index 0675b4500..35f291807 100644 --- a/tavern/internal/ent/gql_where_input.go +++ b/tavern/internal/ent/gql_where_input.go @@ -111,18 +111,6 @@ type BeaconWhereInput struct { LastSeenAtIsNil bool `json:"lastSeenAtIsNil,omitempty"` LastSeenAtNotNil bool `json:"lastSeenAtNotNil,omitempty"` - // "next_seen_at" field predicates. - NextSeenAt *time.Time `json:"nextSeenAt,omitempty"` - NextSeenAtNEQ *time.Time `json:"nextSeenAtNEQ,omitempty"` - NextSeenAtIn []time.Time `json:"nextSeenAtIn,omitempty"` - NextSeenAtNotIn []time.Time `json:"nextSeenAtNotIn,omitempty"` - NextSeenAtGT *time.Time `json:"nextSeenAtGT,omitempty"` - NextSeenAtGTE *time.Time `json:"nextSeenAtGTE,omitempty"` - NextSeenAtLT *time.Time `json:"nextSeenAtLT,omitempty"` - NextSeenAtLTE *time.Time `json:"nextSeenAtLTE,omitempty"` - NextSeenAtIsNil bool `json:"nextSeenAtIsNil,omitempty"` - NextSeenAtNotNil bool `json:"nextSeenAtNotNil,omitempty"` - // "interval" field predicates. Interval *uint64 `json:"interval,omitempty"` IntervalNEQ *uint64 `json:"intervalNEQ,omitempty"` @@ -437,36 +425,6 @@ func (i *BeaconWhereInput) P() (predicate.Beacon, error) { if i.LastSeenAtNotNil { predicates = append(predicates, beacon.LastSeenAtNotNil()) } - if i.NextSeenAt != nil { - predicates = append(predicates, beacon.NextSeenAtEQ(*i.NextSeenAt)) - } - if i.NextSeenAtNEQ != nil { - predicates = append(predicates, beacon.NextSeenAtNEQ(*i.NextSeenAtNEQ)) - } - if len(i.NextSeenAtIn) > 0 { - predicates = append(predicates, beacon.NextSeenAtIn(i.NextSeenAtIn...)) - } - if len(i.NextSeenAtNotIn) > 0 { - predicates = append(predicates, beacon.NextSeenAtNotIn(i.NextSeenAtNotIn...)) - } - if i.NextSeenAtGT != nil { - predicates = append(predicates, beacon.NextSeenAtGT(*i.NextSeenAtGT)) - } - if i.NextSeenAtGTE != nil { - predicates = append(predicates, beacon.NextSeenAtGTE(*i.NextSeenAtGTE)) - } - if i.NextSeenAtLT != nil { - predicates = append(predicates, beacon.NextSeenAtLT(*i.NextSeenAtLT)) - } - if i.NextSeenAtLTE != nil { - predicates = append(predicates, beacon.NextSeenAtLTE(*i.NextSeenAtLTE)) - } - if i.NextSeenAtIsNil { - predicates = append(predicates, beacon.NextSeenAtIsNil()) - } - if i.NextSeenAtNotNil { - predicates = append(predicates, beacon.NextSeenAtNotNil()) - } if i.Interval != nil { predicates = append(predicates, beacon.IntervalEQ(*i.Interval)) } diff --git a/tavern/internal/ent/migrate/schema.go b/tavern/internal/ent/migrate/schema.go index 3cbed7e03..dbe7d4e38 100644 --- a/tavern/internal/ent/migrate/schema.go +++ b/tavern/internal/ent/migrate/schema.go @@ -16,7 +16,6 @@ var ( {Name: "identifier", Type: field.TypeString, Unique: true}, {Name: "agent_identifier", Type: field.TypeString, Nullable: true}, {Name: "last_seen_at", Type: field.TypeTime, Nullable: true}, - {Name: "next_seen_at", Type: field.TypeTime, Nullable: true}, {Name: "interval", Type: field.TypeUint64, Nullable: true}, {Name: "beacon_host", Type: field.TypeInt}, } @@ -28,7 +27,7 @@ var ( ForeignKeys: []*schema.ForeignKey{ { Symbol: "beacons_hosts_host", - Columns: []*schema.Column{BeaconsColumns[8]}, + Columns: []*schema.Column{BeaconsColumns[7]}, RefColumns: []*schema.Column{HostsColumns[0]}, OnDelete: schema.NoAction, }, diff --git a/tavern/internal/ent/mutation.go b/tavern/internal/ent/mutation.go index c5ca1e765..6ddbfa74e 100644 --- a/tavern/internal/ent/mutation.go +++ b/tavern/internal/ent/mutation.go @@ -52,7 +52,6 @@ type BeaconMutation struct { identifier *string agent_identifier *string last_seen_at *time.Time - next_seen_at *time.Time interval *uint64 addinterval *int64 clearedFields map[string]struct{} @@ -383,55 +382,6 @@ func (m *BeaconMutation) ResetLastSeenAt() { delete(m.clearedFields, beacon.FieldLastSeenAt) } -// SetNextSeenAt sets the "next_seen_at" field. -func (m *BeaconMutation) SetNextSeenAt(t time.Time) { - m.next_seen_at = &t -} - -// NextSeenAt returns the value of the "next_seen_at" field in the mutation. -func (m *BeaconMutation) NextSeenAt() (r time.Time, exists bool) { - v := m.next_seen_at - if v == nil { - return - } - return *v, true -} - -// OldNextSeenAt returns the old "next_seen_at" field's value of the Beacon entity. -// If the Beacon object wasn't provided to the builder, the object is fetched from the database. -// An error is returned if the mutation operation is not UpdateOne, or the database query fails. -func (m *BeaconMutation) OldNextSeenAt(ctx context.Context) (v time.Time, err error) { - if !m.op.Is(OpUpdateOne) { - return v, errors.New("OldNextSeenAt is only allowed on UpdateOne operations") - } - if m.id == nil || m.oldValue == nil { - return v, errors.New("OldNextSeenAt requires an ID field in the mutation") - } - oldValue, err := m.oldValue(ctx) - if err != nil { - return v, fmt.Errorf("querying old value for OldNextSeenAt: %w", err) - } - return oldValue.NextSeenAt, nil -} - -// ClearNextSeenAt clears the value of the "next_seen_at" field. -func (m *BeaconMutation) ClearNextSeenAt() { - m.next_seen_at = nil - m.clearedFields[beacon.FieldNextSeenAt] = struct{}{} -} - -// NextSeenAtCleared returns if the "next_seen_at" field was cleared in this mutation. -func (m *BeaconMutation) NextSeenAtCleared() bool { - _, ok := m.clearedFields[beacon.FieldNextSeenAt] - return ok -} - -// ResetNextSeenAt resets all changes to the "next_seen_at" field. -func (m *BeaconMutation) ResetNextSeenAt() { - m.next_seen_at = nil - delete(m.clearedFields, beacon.FieldNextSeenAt) -} - // SetInterval sets the "interval" field. func (m *BeaconMutation) SetInterval(u uint64) { m.interval = &u @@ -629,7 +579,7 @@ func (m *BeaconMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *BeaconMutation) Fields() []string { - fields := make([]string, 0, 7) + fields := make([]string, 0, 6) if m.name != nil { fields = append(fields, beacon.FieldName) } @@ -645,9 +595,6 @@ func (m *BeaconMutation) Fields() []string { if m.last_seen_at != nil { fields = append(fields, beacon.FieldLastSeenAt) } - if m.next_seen_at != nil { - fields = append(fields, beacon.FieldNextSeenAt) - } if m.interval != nil { fields = append(fields, beacon.FieldInterval) } @@ -669,8 +616,6 @@ func (m *BeaconMutation) Field(name string) (ent.Value, bool) { return m.AgentIdentifier() case beacon.FieldLastSeenAt: return m.LastSeenAt() - case beacon.FieldNextSeenAt: - return m.NextSeenAt() case beacon.FieldInterval: return m.Interval() } @@ -692,8 +637,6 @@ func (m *BeaconMutation) OldField(ctx context.Context, name string) (ent.Value, return m.OldAgentIdentifier(ctx) case beacon.FieldLastSeenAt: return m.OldLastSeenAt(ctx) - case beacon.FieldNextSeenAt: - return m.OldNextSeenAt(ctx) case beacon.FieldInterval: return m.OldInterval(ctx) } @@ -740,13 +683,6 @@ func (m *BeaconMutation) SetField(name string, value ent.Value) error { } m.SetLastSeenAt(v) return nil - case beacon.FieldNextSeenAt: - v, ok := value.(time.Time) - if !ok { - return fmt.Errorf("unexpected type %T for field %s", value, name) - } - m.SetNextSeenAt(v) - return nil case beacon.FieldInterval: v, ok := value.(uint64) if !ok { @@ -808,9 +744,6 @@ func (m *BeaconMutation) ClearedFields() []string { if m.FieldCleared(beacon.FieldLastSeenAt) { fields = append(fields, beacon.FieldLastSeenAt) } - if m.FieldCleared(beacon.FieldNextSeenAt) { - fields = append(fields, beacon.FieldNextSeenAt) - } if m.FieldCleared(beacon.FieldInterval) { fields = append(fields, beacon.FieldInterval) } @@ -837,9 +770,6 @@ func (m *BeaconMutation) ClearField(name string) error { case beacon.FieldLastSeenAt: m.ClearLastSeenAt() return nil - case beacon.FieldNextSeenAt: - m.ClearNextSeenAt() - return nil case beacon.FieldInterval: m.ClearInterval() return nil @@ -866,9 +796,6 @@ func (m *BeaconMutation) ResetField(name string) error { case beacon.FieldLastSeenAt: m.ResetLastSeenAt() return nil - case beacon.FieldNextSeenAt: - m.ResetNextSeenAt() - return nil case beacon.FieldInterval: m.ResetInterval() return nil diff --git a/tavern/internal/ent/runtime/runtime.go b/tavern/internal/ent/runtime/runtime.go index a518c0f54..57804764e 100644 --- a/tavern/internal/ent/runtime/runtime.go +++ b/tavern/internal/ent/runtime/runtime.go @@ -20,8 +20,6 @@ import ( // (default values, validators, hooks and policies) and stitches it // to their package variables. func init() { - beaconHooks := schema.Beacon{}.Hooks() - beacon.Hooks[0] = beaconHooks[0] beaconFields := schema.Beacon{}.Fields() _ = beaconFields // beaconDescName is the schema descriptor for name field. diff --git a/tavern/internal/ent/schema/beacon.go b/tavern/internal/ent/schema/beacon.go index 2f80f04a4..6e1ca6af8 100644 --- a/tavern/internal/ent/schema/beacon.go +++ b/tavern/internal/ent/schema/beacon.go @@ -1,19 +1,16 @@ package schema import ( - "context" "crypto/rand" "encoding/base64" "fmt" "io" - "time" "entgo.io/contrib/entgql" "entgo.io/ent" "entgo.io/ent/schema" "entgo.io/ent/schema/edge" "entgo.io/ent/schema/field" - "realm.pub/tavern/internal/ent/hook" "realm.pub/tavern/internal/namegen" ) @@ -60,13 +57,6 @@ func (Beacon) Fields() []ent.Field { entgql.Skip(entgql.SkipMutationUpdateInput), ). Comment("Timestamp of when a task was last claimed or updated for the beacon."), - field.Time("next_seen_at"). - Optional(). - Annotations( - entgql.OrderField("NEXT_SEEN_AT"), - entgql.Skip(entgql.SkipMutationUpdateInput), - ). - Comment("Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead)."), field.Uint64("interval"). Optional(). Annotations( @@ -103,44 +93,6 @@ func (Beacon) Annotations() []schema.Annotation { } } -// Hooks defines middleware for mutations for the ent. -func (Beacon) Hooks() []ent.Hook { - return []ent.Hook{ - hook.On(HookDeriveBeaconInfo(), ent.OpCreate|ent.OpUpdate|ent.OpUpdateOne), - } -} - -// HookDeriveBeaconInfo will update beacon info (e.g. next_seen_at) whenever it is mutated. -func HookDeriveBeaconInfo() ent.Hook { - // Get the relevant methods from the Beacon Mutation - type bMutation interface { - LastSeenAt() (time.Time, bool) - Interval() (uint64, bool) - SetNextSeenAt(time.Time) - } - - return func(next ent.Mutator) ent.Mutator { - return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) { - // Get the mutation - b, ok := m.(bMutation) - if !ok { - return nil, fmt.Errorf("expected beacon mutation in schema hook, got: %+v", m) - } - - // Set next_seen_at - lastSeenAt, hasLastSeenAt := b.LastSeenAt() - interval, hasInterval := b.Interval() - if hasLastSeenAt && hasInterval { - b.SetNextSeenAt( - lastSeenAt.Add(time.Duration(interval) * time.Second), - ) - } - - return next.Mutate(ctx, m) - }) - } -} - func newRandomIdentifier() string { buf := make([]byte, 64) _, err := io.ReadFull(rand.Reader, buf) diff --git a/tavern/internal/graphql/generated/ent.generated.go b/tavern/internal/graphql/generated/ent.generated.go index 1c938746a..5e86aa4bf 100644 --- a/tavern/internal/graphql/generated/ent.generated.go +++ b/tavern/internal/graphql/generated/ent.generated.go @@ -513,47 +513,6 @@ func (ec *executionContext) fieldContext_Beacon_lastSeenAt(ctx context.Context, return fc, nil } -func (ec *executionContext) _Beacon_nextSeenAt(ctx context.Context, field graphql.CollectedField, obj *ent.Beacon) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_Beacon_nextSeenAt(ctx, field) - if err != nil { - return graphql.Null - } - ctx = graphql.WithFieldContext(ctx, fc) - defer func() { - if r := recover(); r != nil { - ec.Error(ctx, ec.Recover(ctx, r)) - ret = graphql.Null - } - }() - resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { - ctx = rctx // use context from middleware stack in children - return obj.NextSeenAt, nil - }) - if err != nil { - ec.Error(ctx, err) - return graphql.Null - } - if resTmp == nil { - return graphql.Null - } - res := resTmp.(time.Time) - fc.Result = res - return ec.marshalOTime2timeᚐTime(ctx, field.Selections, res) -} - -func (ec *executionContext) fieldContext_Beacon_nextSeenAt(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { - fc = &graphql.FieldContext{ - Object: "Beacon", - Field: field, - IsMethod: false, - IsResolver: false, - Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type Time does not have child fields") - }, - } - return fc, nil -} - func (ec *executionContext) _Beacon_interval(ctx context.Context, field graphql.CollectedField, obj *ent.Beacon) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Beacon_interval(ctx, field) if err != nil { @@ -1340,8 +1299,6 @@ func (ec *executionContext) fieldContext_Host_beacons(ctx context.Context, field return ec.fieldContext_Beacon_agentIdentifier(ctx, field) case "lastSeenAt": return ec.fieldContext_Beacon_lastSeenAt(ctx, field) - case "nextSeenAt": - return ec.fieldContext_Beacon_nextSeenAt(ctx, field) case "interval": return ec.fieldContext_Beacon_interval(ctx, field) case "host": @@ -1986,8 +1943,6 @@ func (ec *executionContext) fieldContext_Query_beacons(ctx context.Context, fiel return ec.fieldContext_Beacon_agentIdentifier(ctx, field) case "lastSeenAt": return ec.fieldContext_Beacon_lastSeenAt(ctx, field) - case "nextSeenAt": - return ec.fieldContext_Beacon_nextSeenAt(ctx, field) case "interval": return ec.fieldContext_Beacon_interval(ctx, field) case "host": @@ -3710,8 +3665,6 @@ func (ec *executionContext) fieldContext_Task_beacon(ctx context.Context, field return ec.fieldContext_Beacon_agentIdentifier(ctx, field) case "lastSeenAt": return ec.fieldContext_Beacon_lastSeenAt(ctx, field) - case "nextSeenAt": - return ec.fieldContext_Beacon_nextSeenAt(ctx, field) case "interval": return ec.fieldContext_Beacon_interval(ctx, field) case "host": @@ -4612,7 +4565,7 @@ func (ec *executionContext) unmarshalInputBeaconWhereInput(ctx context.Context, asMap[k] = v } - fieldsInOrder := [...]string{"not", "and", "or", "id", "idNEQ", "idIn", "idNotIn", "idGT", "idGTE", "idLT", "idLTE", "name", "nameNEQ", "nameIn", "nameNotIn", "nameGT", "nameGTE", "nameLT", "nameLTE", "nameContains", "nameHasPrefix", "nameHasSuffix", "nameEqualFold", "nameContainsFold", "principal", "principalNEQ", "principalIn", "principalNotIn", "principalGT", "principalGTE", "principalLT", "principalLTE", "principalContains", "principalHasPrefix", "principalHasSuffix", "principalIsNil", "principalNotNil", "principalEqualFold", "principalContainsFold", "identifier", "identifierNEQ", "identifierIn", "identifierNotIn", "identifierGT", "identifierGTE", "identifierLT", "identifierLTE", "identifierContains", "identifierHasPrefix", "identifierHasSuffix", "identifierEqualFold", "identifierContainsFold", "agentIdentifier", "agentIdentifierNEQ", "agentIdentifierIn", "agentIdentifierNotIn", "agentIdentifierGT", "agentIdentifierGTE", "agentIdentifierLT", "agentIdentifierLTE", "agentIdentifierContains", "agentIdentifierHasPrefix", "agentIdentifierHasSuffix", "agentIdentifierIsNil", "agentIdentifierNotNil", "agentIdentifierEqualFold", "agentIdentifierContainsFold", "lastSeenAt", "lastSeenAtNEQ", "lastSeenAtIn", "lastSeenAtNotIn", "lastSeenAtGT", "lastSeenAtGTE", "lastSeenAtLT", "lastSeenAtLTE", "lastSeenAtIsNil", "lastSeenAtNotNil", "nextSeenAt", "nextSeenAtNEQ", "nextSeenAtIn", "nextSeenAtNotIn", "nextSeenAtGT", "nextSeenAtGTE", "nextSeenAtLT", "nextSeenAtLTE", "nextSeenAtIsNil", "nextSeenAtNotNil", "interval", "intervalNEQ", "intervalIn", "intervalNotIn", "intervalGT", "intervalGTE", "intervalLT", "intervalLTE", "intervalIsNil", "intervalNotNil", "hasHost", "hasHostWith", "hasTasks", "hasTasksWith"} + fieldsInOrder := [...]string{"not", "and", "or", "id", "idNEQ", "idIn", "idNotIn", "idGT", "idGTE", "idLT", "idLTE", "name", "nameNEQ", "nameIn", "nameNotIn", "nameGT", "nameGTE", "nameLT", "nameLTE", "nameContains", "nameHasPrefix", "nameHasSuffix", "nameEqualFold", "nameContainsFold", "principal", "principalNEQ", "principalIn", "principalNotIn", "principalGT", "principalGTE", "principalLT", "principalLTE", "principalContains", "principalHasPrefix", "principalHasSuffix", "principalIsNil", "principalNotNil", "principalEqualFold", "principalContainsFold", "identifier", "identifierNEQ", "identifierIn", "identifierNotIn", "identifierGT", "identifierGTE", "identifierLT", "identifierLTE", "identifierContains", "identifierHasPrefix", "identifierHasSuffix", "identifierEqualFold", "identifierContainsFold", "agentIdentifier", "agentIdentifierNEQ", "agentIdentifierIn", "agentIdentifierNotIn", "agentIdentifierGT", "agentIdentifierGTE", "agentIdentifierLT", "agentIdentifierLTE", "agentIdentifierContains", "agentIdentifierHasPrefix", "agentIdentifierHasSuffix", "agentIdentifierIsNil", "agentIdentifierNotNil", "agentIdentifierEqualFold", "agentIdentifierContainsFold", "lastSeenAt", "lastSeenAtNEQ", "lastSeenAtIn", "lastSeenAtNotIn", "lastSeenAtGT", "lastSeenAtGTE", "lastSeenAtLT", "lastSeenAtLTE", "lastSeenAtIsNil", "lastSeenAtNotNil", "interval", "intervalNEQ", "intervalIn", "intervalNotIn", "intervalGT", "intervalGTE", "intervalLT", "intervalLTE", "intervalIsNil", "intervalNotNil", "hasHost", "hasHostWith", "hasTasks", "hasTasksWith"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -5312,96 +5265,6 @@ func (ec *executionContext) unmarshalInputBeaconWhereInput(ctx context.Context, return it, err } it.LastSeenAtNotNil = data - case "nextSeenAt": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAt")) - data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAt = data - case "nextSeenAtNEQ": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtNEQ")) - data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAtNEQ = data - case "nextSeenAtIn": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtIn")) - data, err := ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAtIn = data - case "nextSeenAtNotIn": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtNotIn")) - data, err := ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAtNotIn = data - case "nextSeenAtGT": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtGT")) - data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAtGT = data - case "nextSeenAtGTE": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtGTE")) - data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAtGTE = data - case "nextSeenAtLT": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtLT")) - data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAtLT = data - case "nextSeenAtLTE": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtLTE")) - data, err := ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAtLTE = data - case "nextSeenAtIsNil": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtIsNil")) - data, err := ec.unmarshalOBoolean2bool(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAtIsNil = data - case "nextSeenAtNotNil": - var err error - - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("nextSeenAtNotNil")) - data, err := ec.unmarshalOBoolean2bool(ctx, v) - if err != nil { - return it, err - } - it.NextSeenAtNotNil = data case "interval": var err error @@ -10457,8 +10320,6 @@ func (ec *executionContext) _Beacon(ctx context.Context, sel ast.SelectionSet, o out.Values[i] = ec._Beacon_agentIdentifier(ctx, field, obj) case "lastSeenAt": out.Values[i] = ec._Beacon_lastSeenAt(ctx, field, obj) - case "nextSeenAt": - out.Values[i] = ec._Beacon_nextSeenAt(ctx, field, obj) case "interval": out.Values[i] = ec._Beacon_interval(ctx, field, obj) case "host": diff --git a/tavern/internal/graphql/generated/mutation.generated.go b/tavern/internal/graphql/generated/mutation.generated.go index 62f4bbbf2..ee55d23b0 100644 --- a/tavern/internal/graphql/generated/mutation.generated.go +++ b/tavern/internal/graphql/generated/mutation.generated.go @@ -392,8 +392,6 @@ func (ec *executionContext) fieldContext_Mutation_updateBeacon(ctx context.Conte return ec.fieldContext_Beacon_agentIdentifier(ctx, field) case "lastSeenAt": return ec.fieldContext_Beacon_lastSeenAt(ctx, field) - case "nextSeenAt": - return ec.fieldContext_Beacon_nextSeenAt(ctx, field) case "interval": return ec.fieldContext_Beacon_interval(ctx, field) case "host": diff --git a/tavern/internal/graphql/generated/root_.generated.go b/tavern/internal/graphql/generated/root_.generated.go index 1bf6fef01..534c2918b 100644 --- a/tavern/internal/graphql/generated/root_.generated.go +++ b/tavern/internal/graphql/generated/root_.generated.go @@ -52,7 +52,6 @@ type ComplexityRoot struct { Interval func(childComplexity int) int LastSeenAt func(childComplexity int) int Name func(childComplexity int) int - NextSeenAt func(childComplexity int) int Principal func(childComplexity int) int Tasks func(childComplexity int) int } @@ -242,13 +241,6 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Beacon.Name(childComplexity), true - case "Beacon.nextSeenAt": - if e.complexity.Beacon.NextSeenAt == nil { - break - } - - return e.complexity.Beacon.NextSeenAt(childComplexity), true - case "Beacon.principal": if e.complexity.Beacon.Principal == nil { break @@ -1067,8 +1059,6 @@ type Beacon implements Node { agentIdentifier: String """Timestamp of when a task was last claimed or updated for the beacon.""" lastSeenAt: Time - """Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead).""" - nextSeenAt: Time """Duration until next callback, in seconds.""" interval: Uint64 """Host this beacon is running on.""" @@ -1086,7 +1076,6 @@ input BeaconOrder { """Properties by which Beacon connections can be ordered.""" enum BeaconOrderField { LAST_SEEN_AT - NEXT_SEEN_AT INTERVAL } """ @@ -1177,17 +1166,6 @@ input BeaconWhereInput { lastSeenAtLTE: Time lastSeenAtIsNil: Boolean lastSeenAtNotNil: Boolean - """next_seen_at field predicates""" - nextSeenAt: Time - nextSeenAtNEQ: Time - nextSeenAtIn: [Time!] - nextSeenAtNotIn: [Time!] - nextSeenAtGT: Time - nextSeenAtGTE: Time - nextSeenAtLT: Time - nextSeenAtLTE: Time - nextSeenAtIsNil: Boolean - nextSeenAtNotNil: Boolean """interval field predicates""" interval: Uint64 intervalNEQ: Uint64 diff --git a/tavern/internal/graphql/schema.graphql b/tavern/internal/graphql/schema.graphql index 1d3406bb7..654f0b661 100644 --- a/tavern/internal/graphql/schema.graphql +++ b/tavern/internal/graphql/schema.graphql @@ -17,8 +17,6 @@ type Beacon implements Node { agentIdentifier: String """Timestamp of when a task was last claimed or updated for the beacon.""" lastSeenAt: Time - """Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead).""" - nextSeenAt: Time """Duration until next callback, in seconds.""" interval: Uint64 """Host this beacon is running on.""" @@ -36,7 +34,6 @@ input BeaconOrder { """Properties by which Beacon connections can be ordered.""" enum BeaconOrderField { LAST_SEEN_AT - NEXT_SEEN_AT INTERVAL } """ @@ -127,17 +124,6 @@ input BeaconWhereInput { lastSeenAtLTE: Time lastSeenAtIsNil: Boolean lastSeenAtNotNil: Boolean - """next_seen_at field predicates""" - nextSeenAt: Time - nextSeenAtNEQ: Time - nextSeenAtIn: [Time!] - nextSeenAtNotIn: [Time!] - nextSeenAtGT: Time - nextSeenAtGTE: Time - nextSeenAtLT: Time - nextSeenAtLTE: Time - nextSeenAtIsNil: Boolean - nextSeenAtNotNil: Boolean """interval field predicates""" interval: Uint64 intervalNEQ: Uint64 diff --git a/tavern/internal/graphql/schema/ent.graphql b/tavern/internal/graphql/schema/ent.graphql index 6ce7a8f06..f049210fb 100644 --- a/tavern/internal/graphql/schema/ent.graphql +++ b/tavern/internal/graphql/schema/ent.graphql @@ -12,8 +12,6 @@ type Beacon implements Node { agentIdentifier: String """Timestamp of when a task was last claimed or updated for the beacon.""" lastSeenAt: Time - """Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead).""" - nextSeenAt: Time """Duration until next callback, in seconds.""" interval: Uint64 """Host this beacon is running on.""" @@ -31,7 +29,6 @@ input BeaconOrder { """Properties by which Beacon connections can be ordered.""" enum BeaconOrderField { LAST_SEEN_AT - NEXT_SEEN_AT INTERVAL } """ @@ -122,17 +119,6 @@ input BeaconWhereInput { lastSeenAtLTE: Time lastSeenAtIsNil: Boolean lastSeenAtNotNil: Boolean - """next_seen_at field predicates""" - nextSeenAt: Time - nextSeenAtNEQ: Time - nextSeenAtIn: [Time!] - nextSeenAtNotIn: [Time!] - nextSeenAtGT: Time - nextSeenAtGTE: Time - nextSeenAtLT: Time - nextSeenAtLTE: Time - nextSeenAtIsNil: Boolean - nextSeenAtNotNil: Boolean """interval field predicates""" interval: Uint64 intervalNEQ: Uint64 diff --git a/tavern/internal/www/schema.graphql b/tavern/internal/www/schema.graphql index 1d3406bb7..654f0b661 100644 --- a/tavern/internal/www/schema.graphql +++ b/tavern/internal/www/schema.graphql @@ -17,8 +17,6 @@ type Beacon implements Node { agentIdentifier: String """Timestamp of when a task was last claimed or updated for the beacon.""" lastSeenAt: Time - """Timestamp of when the beacon will next callback (if it's in the past, the beacon may be dead).""" - nextSeenAt: Time """Duration until next callback, in seconds.""" interval: Uint64 """Host this beacon is running on.""" @@ -36,7 +34,6 @@ input BeaconOrder { """Properties by which Beacon connections can be ordered.""" enum BeaconOrderField { LAST_SEEN_AT - NEXT_SEEN_AT INTERVAL } """ @@ -127,17 +124,6 @@ input BeaconWhereInput { lastSeenAtLTE: Time lastSeenAtIsNil: Boolean lastSeenAtNotNil: Boolean - """next_seen_at field predicates""" - nextSeenAt: Time - nextSeenAtNEQ: Time - nextSeenAtIn: [Time!] - nextSeenAtNotIn: [Time!] - nextSeenAtGT: Time - nextSeenAtGTE: Time - nextSeenAtLT: Time - nextSeenAtLTE: Time - nextSeenAtIsNil: Boolean - nextSeenAtNotNil: Boolean """interval field predicates""" interval: Uint64 intervalNEQ: Uint64