lapi detailed metrics: db schema (#3099)

This commit is contained in:
mmetc 2024-07-01 12:14:55 +02:00 committed by GitHub
parent b081065c8e
commit 1acc35442c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 4251 additions and 10 deletions

View file

@ -36,7 +36,13 @@ type Bouncer struct {
// LastPull holds the value of the "last_pull" field.
LastPull *time.Time `json:"last_pull"`
// AuthType holds the value of the "auth_type" field.
AuthType string `json:"auth_type"`
AuthType string `json:"auth_type"`
// Osname holds the value of the "osname" field.
Osname string `json:"osname,omitempty"`
// Osversion holds the value of the "osversion" field.
Osversion string `json:"osversion,omitempty"`
// Featureflags holds the value of the "featureflags" field.
Featureflags string `json:"featureflags,omitempty"`
selectValues sql.SelectValues
}
@ -49,7 +55,7 @@ func (*Bouncer) scanValues(columns []string) ([]any, error) {
values[i] = new(sql.NullBool)
case bouncer.FieldID:
values[i] = new(sql.NullInt64)
case bouncer.FieldName, bouncer.FieldAPIKey, bouncer.FieldIPAddress, bouncer.FieldType, bouncer.FieldVersion, bouncer.FieldAuthType:
case bouncer.FieldName, bouncer.FieldAPIKey, bouncer.FieldIPAddress, bouncer.FieldType, bouncer.FieldVersion, bouncer.FieldAuthType, bouncer.FieldOsname, bouncer.FieldOsversion, bouncer.FieldFeatureflags:
values[i] = new(sql.NullString)
case bouncer.FieldCreatedAt, bouncer.FieldUpdatedAt, bouncer.FieldLastPull:
values[i] = new(sql.NullTime)
@ -135,6 +141,24 @@ func (b *Bouncer) assignValues(columns []string, values []any) error {
} else if value.Valid {
b.AuthType = value.String
}
case bouncer.FieldOsname:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field osname", values[i])
} else if value.Valid {
b.Osname = value.String
}
case bouncer.FieldOsversion:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field osversion", values[i])
} else if value.Valid {
b.Osversion = value.String
}
case bouncer.FieldFeatureflags:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field featureflags", values[i])
} else if value.Valid {
b.Featureflags = value.String
}
default:
b.selectValues.Set(columns[i], values[i])
}
@ -201,6 +225,15 @@ func (b *Bouncer) String() string {
builder.WriteString(", ")
builder.WriteString("auth_type=")
builder.WriteString(b.AuthType)
builder.WriteString(", ")
builder.WriteString("osname=")
builder.WriteString(b.Osname)
builder.WriteString(", ")
builder.WriteString("osversion=")
builder.WriteString(b.Osversion)
builder.WriteString(", ")
builder.WriteString("featureflags=")
builder.WriteString(b.Featureflags)
builder.WriteByte(')')
return builder.String()
}

View file

@ -33,6 +33,12 @@ const (
FieldLastPull = "last_pull"
// FieldAuthType holds the string denoting the auth_type field in the database.
FieldAuthType = "auth_type"
// FieldOsname holds the string denoting the osname field in the database.
FieldOsname = "osname"
// FieldOsversion holds the string denoting the osversion field in the database.
FieldOsversion = "osversion"
// FieldFeatureflags holds the string denoting the featureflags field in the database.
FieldFeatureflags = "featureflags"
// Table holds the table name of the bouncer in the database.
Table = "bouncers"
)
@ -50,6 +56,9 @@ var Columns = []string{
FieldVersion,
FieldLastPull,
FieldAuthType,
FieldOsname,
FieldOsversion,
FieldFeatureflags,
}
// ValidColumn reports if the column name is valid (part of the table columns).
@ -132,3 +141,18 @@ func ByLastPull(opts ...sql.OrderTermOption) OrderOption {
func ByAuthType(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldAuthType, opts...).ToFunc()
}
// ByOsname orders the results by the osname field.
func ByOsname(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldOsname, opts...).ToFunc()
}
// ByOsversion orders the results by the osversion field.
func ByOsversion(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldOsversion, opts...).ToFunc()
}
// ByFeatureflags orders the results by the featureflags field.
func ByFeatureflags(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldFeatureflags, opts...).ToFunc()
}

View file

@ -104,6 +104,21 @@ func AuthType(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEQ(FieldAuthType, v))
}
// Osname applies equality check predicate on the "osname" field. It's identical to OsnameEQ.
func Osname(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEQ(FieldOsname, v))
}
// Osversion applies equality check predicate on the "osversion" field. It's identical to OsversionEQ.
func Osversion(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEQ(FieldOsversion, v))
}
// Featureflags applies equality check predicate on the "featureflags" field. It's identical to FeatureflagsEQ.
func Featureflags(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEQ(FieldFeatureflags, v))
}
// CreatedAtEQ applies the EQ predicate on the "created_at" field.
func CreatedAtEQ(v time.Time) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEQ(FieldCreatedAt, v))
@ -664,6 +679,231 @@ func AuthTypeContainsFold(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldContainsFold(FieldAuthType, v))
}
// OsnameEQ applies the EQ predicate on the "osname" field.
func OsnameEQ(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEQ(FieldOsname, v))
}
// OsnameNEQ applies the NEQ predicate on the "osname" field.
func OsnameNEQ(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldNEQ(FieldOsname, v))
}
// OsnameIn applies the In predicate on the "osname" field.
func OsnameIn(vs ...string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldIn(FieldOsname, vs...))
}
// OsnameNotIn applies the NotIn predicate on the "osname" field.
func OsnameNotIn(vs ...string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldNotIn(FieldOsname, vs...))
}
// OsnameGT applies the GT predicate on the "osname" field.
func OsnameGT(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldGT(FieldOsname, v))
}
// OsnameGTE applies the GTE predicate on the "osname" field.
func OsnameGTE(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldGTE(FieldOsname, v))
}
// OsnameLT applies the LT predicate on the "osname" field.
func OsnameLT(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldLT(FieldOsname, v))
}
// OsnameLTE applies the LTE predicate on the "osname" field.
func OsnameLTE(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldLTE(FieldOsname, v))
}
// OsnameContains applies the Contains predicate on the "osname" field.
func OsnameContains(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldContains(FieldOsname, v))
}
// OsnameHasPrefix applies the HasPrefix predicate on the "osname" field.
func OsnameHasPrefix(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldHasPrefix(FieldOsname, v))
}
// OsnameHasSuffix applies the HasSuffix predicate on the "osname" field.
func OsnameHasSuffix(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldHasSuffix(FieldOsname, v))
}
// OsnameIsNil applies the IsNil predicate on the "osname" field.
func OsnameIsNil() predicate.Bouncer {
return predicate.Bouncer(sql.FieldIsNull(FieldOsname))
}
// OsnameNotNil applies the NotNil predicate on the "osname" field.
func OsnameNotNil() predicate.Bouncer {
return predicate.Bouncer(sql.FieldNotNull(FieldOsname))
}
// OsnameEqualFold applies the EqualFold predicate on the "osname" field.
func OsnameEqualFold(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEqualFold(FieldOsname, v))
}
// OsnameContainsFold applies the ContainsFold predicate on the "osname" field.
func OsnameContainsFold(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldContainsFold(FieldOsname, v))
}
// OsversionEQ applies the EQ predicate on the "osversion" field.
func OsversionEQ(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEQ(FieldOsversion, v))
}
// OsversionNEQ applies the NEQ predicate on the "osversion" field.
func OsversionNEQ(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldNEQ(FieldOsversion, v))
}
// OsversionIn applies the In predicate on the "osversion" field.
func OsversionIn(vs ...string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldIn(FieldOsversion, vs...))
}
// OsversionNotIn applies the NotIn predicate on the "osversion" field.
func OsversionNotIn(vs ...string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldNotIn(FieldOsversion, vs...))
}
// OsversionGT applies the GT predicate on the "osversion" field.
func OsversionGT(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldGT(FieldOsversion, v))
}
// OsversionGTE applies the GTE predicate on the "osversion" field.
func OsversionGTE(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldGTE(FieldOsversion, v))
}
// OsversionLT applies the LT predicate on the "osversion" field.
func OsversionLT(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldLT(FieldOsversion, v))
}
// OsversionLTE applies the LTE predicate on the "osversion" field.
func OsversionLTE(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldLTE(FieldOsversion, v))
}
// OsversionContains applies the Contains predicate on the "osversion" field.
func OsversionContains(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldContains(FieldOsversion, v))
}
// OsversionHasPrefix applies the HasPrefix predicate on the "osversion" field.
func OsversionHasPrefix(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldHasPrefix(FieldOsversion, v))
}
// OsversionHasSuffix applies the HasSuffix predicate on the "osversion" field.
func OsversionHasSuffix(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldHasSuffix(FieldOsversion, v))
}
// OsversionIsNil applies the IsNil predicate on the "osversion" field.
func OsversionIsNil() predicate.Bouncer {
return predicate.Bouncer(sql.FieldIsNull(FieldOsversion))
}
// OsversionNotNil applies the NotNil predicate on the "osversion" field.
func OsversionNotNil() predicate.Bouncer {
return predicate.Bouncer(sql.FieldNotNull(FieldOsversion))
}
// OsversionEqualFold applies the EqualFold predicate on the "osversion" field.
func OsversionEqualFold(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEqualFold(FieldOsversion, v))
}
// OsversionContainsFold applies the ContainsFold predicate on the "osversion" field.
func OsversionContainsFold(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldContainsFold(FieldOsversion, v))
}
// FeatureflagsEQ applies the EQ predicate on the "featureflags" field.
func FeatureflagsEQ(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEQ(FieldFeatureflags, v))
}
// FeatureflagsNEQ applies the NEQ predicate on the "featureflags" field.
func FeatureflagsNEQ(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldNEQ(FieldFeatureflags, v))
}
// FeatureflagsIn applies the In predicate on the "featureflags" field.
func FeatureflagsIn(vs ...string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldIn(FieldFeatureflags, vs...))
}
// FeatureflagsNotIn applies the NotIn predicate on the "featureflags" field.
func FeatureflagsNotIn(vs ...string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldNotIn(FieldFeatureflags, vs...))
}
// FeatureflagsGT applies the GT predicate on the "featureflags" field.
func FeatureflagsGT(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldGT(FieldFeatureflags, v))
}
// FeatureflagsGTE applies the GTE predicate on the "featureflags" field.
func FeatureflagsGTE(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldGTE(FieldFeatureflags, v))
}
// FeatureflagsLT applies the LT predicate on the "featureflags" field.
func FeatureflagsLT(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldLT(FieldFeatureflags, v))
}
// FeatureflagsLTE applies the LTE predicate on the "featureflags" field.
func FeatureflagsLTE(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldLTE(FieldFeatureflags, v))
}
// FeatureflagsContains applies the Contains predicate on the "featureflags" field.
func FeatureflagsContains(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldContains(FieldFeatureflags, v))
}
// FeatureflagsHasPrefix applies the HasPrefix predicate on the "featureflags" field.
func FeatureflagsHasPrefix(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldHasPrefix(FieldFeatureflags, v))
}
// FeatureflagsHasSuffix applies the HasSuffix predicate on the "featureflags" field.
func FeatureflagsHasSuffix(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldHasSuffix(FieldFeatureflags, v))
}
// FeatureflagsIsNil applies the IsNil predicate on the "featureflags" field.
func FeatureflagsIsNil() predicate.Bouncer {
return predicate.Bouncer(sql.FieldIsNull(FieldFeatureflags))
}
// FeatureflagsNotNil applies the NotNil predicate on the "featureflags" field.
func FeatureflagsNotNil() predicate.Bouncer {
return predicate.Bouncer(sql.FieldNotNull(FieldFeatureflags))
}
// FeatureflagsEqualFold applies the EqualFold predicate on the "featureflags" field.
func FeatureflagsEqualFold(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldEqualFold(FieldFeatureflags, v))
}
// FeatureflagsContainsFold applies the ContainsFold predicate on the "featureflags" field.
func FeatureflagsContainsFold(v string) predicate.Bouncer {
return predicate.Bouncer(sql.FieldContainsFold(FieldFeatureflags, v))
}
// And groups predicates with the AND operator between them.
func And(predicates ...predicate.Bouncer) predicate.Bouncer {
return predicate.Bouncer(sql.AndPredicates(predicates...))

View file

@ -136,6 +136,48 @@ func (bc *BouncerCreate) SetNillableAuthType(s *string) *BouncerCreate {
return bc
}
// SetOsname sets the "osname" field.
func (bc *BouncerCreate) SetOsname(s string) *BouncerCreate {
bc.mutation.SetOsname(s)
return bc
}
// SetNillableOsname sets the "osname" field if the given value is not nil.
func (bc *BouncerCreate) SetNillableOsname(s *string) *BouncerCreate {
if s != nil {
bc.SetOsname(*s)
}
return bc
}
// SetOsversion sets the "osversion" field.
func (bc *BouncerCreate) SetOsversion(s string) *BouncerCreate {
bc.mutation.SetOsversion(s)
return bc
}
// SetNillableOsversion sets the "osversion" field if the given value is not nil.
func (bc *BouncerCreate) SetNillableOsversion(s *string) *BouncerCreate {
if s != nil {
bc.SetOsversion(*s)
}
return bc
}
// SetFeatureflags sets the "featureflags" field.
func (bc *BouncerCreate) SetFeatureflags(s string) *BouncerCreate {
bc.mutation.SetFeatureflags(s)
return bc
}
// SetNillableFeatureflags sets the "featureflags" field if the given value is not nil.
func (bc *BouncerCreate) SetNillableFeatureflags(s *string) *BouncerCreate {
if s != nil {
bc.SetFeatureflags(*s)
}
return bc
}
// Mutation returns the BouncerMutation object of the builder.
func (bc *BouncerCreate) Mutation() *BouncerMutation {
return bc.mutation
@ -275,6 +317,18 @@ func (bc *BouncerCreate) createSpec() (*Bouncer, *sqlgraph.CreateSpec) {
_spec.SetField(bouncer.FieldAuthType, field.TypeString, value)
_node.AuthType = value
}
if value, ok := bc.mutation.Osname(); ok {
_spec.SetField(bouncer.FieldOsname, field.TypeString, value)
_node.Osname = value
}
if value, ok := bc.mutation.Osversion(); ok {
_spec.SetField(bouncer.FieldOsversion, field.TypeString, value)
_node.Osversion = value
}
if value, ok := bc.mutation.Featureflags(); ok {
_spec.SetField(bouncer.FieldFeatureflags, field.TypeString, value)
_node.Featureflags = value
}
return _node, _spec
}

View file

@ -156,6 +156,66 @@ func (bu *BouncerUpdate) SetNillableAuthType(s *string) *BouncerUpdate {
return bu
}
// SetOsname sets the "osname" field.
func (bu *BouncerUpdate) SetOsname(s string) *BouncerUpdate {
bu.mutation.SetOsname(s)
return bu
}
// SetNillableOsname sets the "osname" field if the given value is not nil.
func (bu *BouncerUpdate) SetNillableOsname(s *string) *BouncerUpdate {
if s != nil {
bu.SetOsname(*s)
}
return bu
}
// ClearOsname clears the value of the "osname" field.
func (bu *BouncerUpdate) ClearOsname() *BouncerUpdate {
bu.mutation.ClearOsname()
return bu
}
// SetOsversion sets the "osversion" field.
func (bu *BouncerUpdate) SetOsversion(s string) *BouncerUpdate {
bu.mutation.SetOsversion(s)
return bu
}
// SetNillableOsversion sets the "osversion" field if the given value is not nil.
func (bu *BouncerUpdate) SetNillableOsversion(s *string) *BouncerUpdate {
if s != nil {
bu.SetOsversion(*s)
}
return bu
}
// ClearOsversion clears the value of the "osversion" field.
func (bu *BouncerUpdate) ClearOsversion() *BouncerUpdate {
bu.mutation.ClearOsversion()
return bu
}
// SetFeatureflags sets the "featureflags" field.
func (bu *BouncerUpdate) SetFeatureflags(s string) *BouncerUpdate {
bu.mutation.SetFeatureflags(s)
return bu
}
// SetNillableFeatureflags sets the "featureflags" field if the given value is not nil.
func (bu *BouncerUpdate) SetNillableFeatureflags(s *string) *BouncerUpdate {
if s != nil {
bu.SetFeatureflags(*s)
}
return bu
}
// ClearFeatureflags clears the value of the "featureflags" field.
func (bu *BouncerUpdate) ClearFeatureflags() *BouncerUpdate {
bu.mutation.ClearFeatureflags()
return bu
}
// Mutation returns the BouncerMutation object of the builder.
func (bu *BouncerUpdate) Mutation() *BouncerMutation {
return bu.mutation
@ -242,6 +302,24 @@ func (bu *BouncerUpdate) sqlSave(ctx context.Context) (n int, err error) {
if value, ok := bu.mutation.AuthType(); ok {
_spec.SetField(bouncer.FieldAuthType, field.TypeString, value)
}
if value, ok := bu.mutation.Osname(); ok {
_spec.SetField(bouncer.FieldOsname, field.TypeString, value)
}
if bu.mutation.OsnameCleared() {
_spec.ClearField(bouncer.FieldOsname, field.TypeString)
}
if value, ok := bu.mutation.Osversion(); ok {
_spec.SetField(bouncer.FieldOsversion, field.TypeString, value)
}
if bu.mutation.OsversionCleared() {
_spec.ClearField(bouncer.FieldOsversion, field.TypeString)
}
if value, ok := bu.mutation.Featureflags(); ok {
_spec.SetField(bouncer.FieldFeatureflags, field.TypeString, value)
}
if bu.mutation.FeatureflagsCleared() {
_spec.ClearField(bouncer.FieldFeatureflags, field.TypeString)
}
if n, err = sqlgraph.UpdateNodes(ctx, bu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{bouncer.Label}
@ -390,6 +468,66 @@ func (buo *BouncerUpdateOne) SetNillableAuthType(s *string) *BouncerUpdateOne {
return buo
}
// SetOsname sets the "osname" field.
func (buo *BouncerUpdateOne) SetOsname(s string) *BouncerUpdateOne {
buo.mutation.SetOsname(s)
return buo
}
// SetNillableOsname sets the "osname" field if the given value is not nil.
func (buo *BouncerUpdateOne) SetNillableOsname(s *string) *BouncerUpdateOne {
if s != nil {
buo.SetOsname(*s)
}
return buo
}
// ClearOsname clears the value of the "osname" field.
func (buo *BouncerUpdateOne) ClearOsname() *BouncerUpdateOne {
buo.mutation.ClearOsname()
return buo
}
// SetOsversion sets the "osversion" field.
func (buo *BouncerUpdateOne) SetOsversion(s string) *BouncerUpdateOne {
buo.mutation.SetOsversion(s)
return buo
}
// SetNillableOsversion sets the "osversion" field if the given value is not nil.
func (buo *BouncerUpdateOne) SetNillableOsversion(s *string) *BouncerUpdateOne {
if s != nil {
buo.SetOsversion(*s)
}
return buo
}
// ClearOsversion clears the value of the "osversion" field.
func (buo *BouncerUpdateOne) ClearOsversion() *BouncerUpdateOne {
buo.mutation.ClearOsversion()
return buo
}
// SetFeatureflags sets the "featureflags" field.
func (buo *BouncerUpdateOne) SetFeatureflags(s string) *BouncerUpdateOne {
buo.mutation.SetFeatureflags(s)
return buo
}
// SetNillableFeatureflags sets the "featureflags" field if the given value is not nil.
func (buo *BouncerUpdateOne) SetNillableFeatureflags(s *string) *BouncerUpdateOne {
if s != nil {
buo.SetFeatureflags(*s)
}
return buo
}
// ClearFeatureflags clears the value of the "featureflags" field.
func (buo *BouncerUpdateOne) ClearFeatureflags() *BouncerUpdateOne {
buo.mutation.ClearFeatureflags()
return buo
}
// Mutation returns the BouncerMutation object of the builder.
func (buo *BouncerUpdateOne) Mutation() *BouncerMutation {
return buo.mutation
@ -506,6 +644,24 @@ func (buo *BouncerUpdateOne) sqlSave(ctx context.Context) (_node *Bouncer, err e
if value, ok := buo.mutation.AuthType(); ok {
_spec.SetField(bouncer.FieldAuthType, field.TypeString, value)
}
if value, ok := buo.mutation.Osname(); ok {
_spec.SetField(bouncer.FieldOsname, field.TypeString, value)
}
if buo.mutation.OsnameCleared() {
_spec.ClearField(bouncer.FieldOsname, field.TypeString)
}
if value, ok := buo.mutation.Osversion(); ok {
_spec.SetField(bouncer.FieldOsversion, field.TypeString, value)
}
if buo.mutation.OsversionCleared() {
_spec.ClearField(bouncer.FieldOsversion, field.TypeString)
}
if value, ok := buo.mutation.Featureflags(); ok {
_spec.SetField(bouncer.FieldFeatureflags, field.TypeString, value)
}
if buo.mutation.FeatureflagsCleared() {
_spec.ClearField(bouncer.FieldFeatureflags, field.TypeString)
}
_node = &Bouncer{config: buo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues

View file

@ -23,6 +23,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/database/ent/lock"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/meta"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/metric"
)
// Client is the client that holds all ent builders.
@ -46,6 +47,8 @@ type Client struct {
Machine *MachineClient
// Meta is the client for interacting with the Meta builders.
Meta *MetaClient
// Metric is the client for interacting with the Metric builders.
Metric *MetricClient
}
// NewClient creates a new client configured with the given options.
@ -65,6 +68,7 @@ func (c *Client) init() {
c.Lock = NewLockClient(c.config)
c.Machine = NewMachineClient(c.config)
c.Meta = NewMetaClient(c.config)
c.Metric = NewMetricClient(c.config)
}
type (
@ -165,6 +169,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) {
Lock: NewLockClient(cfg),
Machine: NewMachineClient(cfg),
Meta: NewMetaClient(cfg),
Metric: NewMetricClient(cfg),
}, nil
}
@ -192,6 +197,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
Lock: NewLockClient(cfg),
Machine: NewMachineClient(cfg),
Meta: NewMetaClient(cfg),
Metric: NewMetricClient(cfg),
}, nil
}
@ -222,7 +228,7 @@ func (c *Client) Close() error {
func (c *Client) Use(hooks ...Hook) {
for _, n := range []interface{ Use(...Hook) }{
c.Alert, c.Bouncer, c.ConfigItem, c.Decision, c.Event, c.Lock, c.Machine,
c.Meta,
c.Meta, c.Metric,
} {
n.Use(hooks...)
}
@ -233,7 +239,7 @@ func (c *Client) Use(hooks ...Hook) {
func (c *Client) Intercept(interceptors ...Interceptor) {
for _, n := range []interface{ Intercept(...Interceptor) }{
c.Alert, c.Bouncer, c.ConfigItem, c.Decision, c.Event, c.Lock, c.Machine,
c.Meta,
c.Meta, c.Metric,
} {
n.Intercept(interceptors...)
}
@ -258,6 +264,8 @@ func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
return c.Machine.mutate(ctx, m)
case *MetaMutation:
return c.Meta.mutate(ctx, m)
case *MetricMutation:
return c.Metric.mutate(ctx, m)
default:
return nil, fmt.Errorf("ent: unknown mutation type %T", m)
}
@ -1455,13 +1463,147 @@ func (c *MetaClient) mutate(ctx context.Context, m *MetaMutation) (Value, error)
}
}
// MetricClient is a client for the Metric schema.
type MetricClient struct {
config
}
// NewMetricClient returns a client for the Metric from the given config.
func NewMetricClient(c config) *MetricClient {
return &MetricClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `metric.Hooks(f(g(h())))`.
func (c *MetricClient) Use(hooks ...Hook) {
c.hooks.Metric = append(c.hooks.Metric, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `metric.Intercept(f(g(h())))`.
func (c *MetricClient) Intercept(interceptors ...Interceptor) {
c.inters.Metric = append(c.inters.Metric, interceptors...)
}
// Create returns a builder for creating a Metric entity.
func (c *MetricClient) Create() *MetricCreate {
mutation := newMetricMutation(c.config, OpCreate)
return &MetricCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of Metric entities.
func (c *MetricClient) CreateBulk(builders ...*MetricCreate) *MetricCreateBulk {
return &MetricCreateBulk{config: c.config, builders: builders}
}
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
// a builder and applies setFunc on it.
func (c *MetricClient) MapCreateBulk(slice any, setFunc func(*MetricCreate, int)) *MetricCreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &MetricCreateBulk{err: fmt.Errorf("calling to MetricClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*MetricCreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &MetricCreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for Metric.
func (c *MetricClient) Update() *MetricUpdate {
mutation := newMetricMutation(c.config, OpUpdate)
return &MetricUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *MetricClient) UpdateOne(m *Metric) *MetricUpdateOne {
mutation := newMetricMutation(c.config, OpUpdateOne, withMetric(m))
return &MetricUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *MetricClient) UpdateOneID(id int) *MetricUpdateOne {
mutation := newMetricMutation(c.config, OpUpdateOne, withMetricID(id))
return &MetricUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for Metric.
func (c *MetricClient) Delete() *MetricDelete {
mutation := newMetricMutation(c.config, OpDelete)
return &MetricDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *MetricClient) DeleteOne(m *Metric) *MetricDeleteOne {
return c.DeleteOneID(m.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *MetricClient) DeleteOneID(id int) *MetricDeleteOne {
builder := c.Delete().Where(metric.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &MetricDeleteOne{builder}
}
// Query returns a query builder for Metric.
func (c *MetricClient) Query() *MetricQuery {
return &MetricQuery{
config: c.config,
ctx: &QueryContext{Type: TypeMetric},
inters: c.Interceptors(),
}
}
// Get returns a Metric entity by its id.
func (c *MetricClient) Get(ctx context.Context, id int) (*Metric, error) {
return c.Query().Where(metric.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *MetricClient) GetX(ctx context.Context, id int) *Metric {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// Hooks returns the client hooks.
func (c *MetricClient) Hooks() []Hook {
return c.hooks.Metric
}
// Interceptors returns the client interceptors.
func (c *MetricClient) Interceptors() []Interceptor {
return c.inters.Metric
}
func (c *MetricClient) mutate(ctx context.Context, m *MetricMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&MetricCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&MetricUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&MetricUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&MetricDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown Metric mutation op: %q", m.Op())
}
}
// hooks and interceptors per client, for fast access.
type (
hooks struct {
Alert, Bouncer, ConfigItem, Decision, Event, Lock, Machine, Meta []ent.Hook
Alert, Bouncer, ConfigItem, Decision, Event, Lock, Machine, Meta,
Metric []ent.Hook
}
inters struct {
Alert, Bouncer, ConfigItem, Decision, Event, Lock, Machine,
Meta []ent.Interceptor
Alert, Bouncer, ConfigItem, Decision, Event, Lock, Machine, Meta,
Metric []ent.Interceptor
}
)

View file

@ -20,6 +20,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/database/ent/lock"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/meta"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/metric"
)
// ent aliases to avoid import conflicts in user's code.
@ -88,6 +89,7 @@ func checkColumn(table, column string) error {
lock.Table: lock.ValidColumn,
machine.Table: machine.ValidColumn,
meta.Table: meta.ValidColumn,
metric.Table: metric.ValidColumn,
})
})
return columnCheck(table, column)

View file

@ -105,6 +105,18 @@ func (f MetaFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.MetaMutation", m)
}
// The MetricFunc type is an adapter to allow the use of ordinary
// function as Metric mutator.
type MetricFunc func(context.Context, *ent.MetricMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f MetricFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.MetricMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.MetricMutation", m)
}
// Condition is a hook condition function.
type Condition func(context.Context, ent.Mutation) bool

View file

@ -3,6 +3,7 @@
package ent
import (
"encoding/json"
"fmt"
"strings"
"time"
@ -10,6 +11,7 @@ import (
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/schema"
)
// Machine is the model entity for the Machine schema.
@ -41,6 +43,16 @@ type Machine struct {
Status string `json:"status,omitempty"`
// AuthType holds the value of the "auth_type" field.
AuthType string `json:"auth_type"`
// Osname holds the value of the "osname" field.
Osname string `json:"osname,omitempty"`
// Osversion holds the value of the "osversion" field.
Osversion string `json:"osversion,omitempty"`
// Featureflags holds the value of the "featureflags" field.
Featureflags string `json:"featureflags,omitempty"`
// Hubstate holds the value of the "hubstate" field.
Hubstate map[string]schema.ItemState `json:"hubstate,omitempty"`
// Datasources holds the value of the "datasources" field.
Datasources map[string]int64 `json:"datasources,omitempty"`
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the MachineQuery when eager-loading is set.
Edges MachineEdges `json:"edges"`
@ -70,11 +82,13 @@ func (*Machine) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case machine.FieldHubstate, machine.FieldDatasources:
values[i] = new([]byte)
case machine.FieldIsValidated:
values[i] = new(sql.NullBool)
case machine.FieldID:
values[i] = new(sql.NullInt64)
case machine.FieldMachineId, machine.FieldPassword, machine.FieldIpAddress, machine.FieldScenarios, machine.FieldVersion, machine.FieldStatus, machine.FieldAuthType:
case machine.FieldMachineId, machine.FieldPassword, machine.FieldIpAddress, machine.FieldScenarios, machine.FieldVersion, machine.FieldStatus, machine.FieldAuthType, machine.FieldOsname, machine.FieldOsversion, machine.FieldFeatureflags:
values[i] = new(sql.NullString)
case machine.FieldCreatedAt, machine.FieldUpdatedAt, machine.FieldLastPush, machine.FieldLastHeartbeat:
values[i] = new(sql.NullTime)
@ -173,6 +187,40 @@ func (m *Machine) assignValues(columns []string, values []any) error {
} else if value.Valid {
m.AuthType = value.String
}
case machine.FieldOsname:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field osname", values[i])
} else if value.Valid {
m.Osname = value.String
}
case machine.FieldOsversion:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field osversion", values[i])
} else if value.Valid {
m.Osversion = value.String
}
case machine.FieldFeatureflags:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field featureflags", values[i])
} else if value.Valid {
m.Featureflags = value.String
}
case machine.FieldHubstate:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field hubstate", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &m.Hubstate); err != nil {
return fmt.Errorf("unmarshal field hubstate: %w", err)
}
}
case machine.FieldDatasources:
if value, ok := values[i].(*[]byte); !ok {
return fmt.Errorf("unexpected type %T for field datasources", values[i])
} else if value != nil && len(*value) > 0 {
if err := json.Unmarshal(*value, &m.Datasources); err != nil {
return fmt.Errorf("unmarshal field datasources: %w", err)
}
}
default:
m.selectValues.Set(columns[i], values[i])
}
@ -252,6 +300,21 @@ func (m *Machine) String() string {
builder.WriteString(", ")
builder.WriteString("auth_type=")
builder.WriteString(m.AuthType)
builder.WriteString(", ")
builder.WriteString("osname=")
builder.WriteString(m.Osname)
builder.WriteString(", ")
builder.WriteString("osversion=")
builder.WriteString(m.Osversion)
builder.WriteString(", ")
builder.WriteString("featureflags=")
builder.WriteString(m.Featureflags)
builder.WriteString(", ")
builder.WriteString("hubstate=")
builder.WriteString(fmt.Sprintf("%v", m.Hubstate))
builder.WriteString(", ")
builder.WriteString("datasources=")
builder.WriteString(fmt.Sprintf("%v", m.Datasources))
builder.WriteByte(')')
return builder.String()
}

View file

@ -38,6 +38,16 @@ const (
FieldStatus = "status"
// FieldAuthType holds the string denoting the auth_type field in the database.
FieldAuthType = "auth_type"
// FieldOsname holds the string denoting the osname field in the database.
FieldOsname = "osname"
// FieldOsversion holds the string denoting the osversion field in the database.
FieldOsversion = "osversion"
// FieldFeatureflags holds the string denoting the featureflags field in the database.
FieldFeatureflags = "featureflags"
// FieldHubstate holds the string denoting the hubstate field in the database.
FieldHubstate = "hubstate"
// FieldDatasources holds the string denoting the datasources field in the database.
FieldDatasources = "datasources"
// EdgeAlerts holds the string denoting the alerts edge name in mutations.
EdgeAlerts = "alerts"
// Table holds the table name of the machine in the database.
@ -66,6 +76,11 @@ var Columns = []string{
FieldIsValidated,
FieldStatus,
FieldAuthType,
FieldOsname,
FieldOsversion,
FieldFeatureflags,
FieldHubstate,
FieldDatasources,
}
// ValidColumn reports if the column name is valid (part of the table columns).
@ -163,6 +178,21 @@ func ByAuthType(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldAuthType, opts...).ToFunc()
}
// ByOsname orders the results by the osname field.
func ByOsname(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldOsname, opts...).ToFunc()
}
// ByOsversion orders the results by the osversion field.
func ByOsversion(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldOsversion, opts...).ToFunc()
}
// ByFeatureflags orders the results by the featureflags field.
func ByFeatureflags(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldFeatureflags, opts...).ToFunc()
}
// ByAlertsCount orders the results by alerts count.
func ByAlertsCount(opts ...sql.OrderTermOption) OrderOption {
return func(s *sql.Selector) {

View file

@ -115,6 +115,21 @@ func AuthType(v string) predicate.Machine {
return predicate.Machine(sql.FieldEQ(FieldAuthType, v))
}
// Osname applies equality check predicate on the "osname" field. It's identical to OsnameEQ.
func Osname(v string) predicate.Machine {
return predicate.Machine(sql.FieldEQ(FieldOsname, v))
}
// Osversion applies equality check predicate on the "osversion" field. It's identical to OsversionEQ.
func Osversion(v string) predicate.Machine {
return predicate.Machine(sql.FieldEQ(FieldOsversion, v))
}
// Featureflags applies equality check predicate on the "featureflags" field. It's identical to FeatureflagsEQ.
func Featureflags(v string) predicate.Machine {
return predicate.Machine(sql.FieldEQ(FieldFeatureflags, v))
}
// CreatedAtEQ applies the EQ predicate on the "created_at" field.
func CreatedAtEQ(v time.Time) predicate.Machine {
return predicate.Machine(sql.FieldEQ(FieldCreatedAt, v))
@ -790,6 +805,251 @@ func AuthTypeContainsFold(v string) predicate.Machine {
return predicate.Machine(sql.FieldContainsFold(FieldAuthType, v))
}
// OsnameEQ applies the EQ predicate on the "osname" field.
func OsnameEQ(v string) predicate.Machine {
return predicate.Machine(sql.FieldEQ(FieldOsname, v))
}
// OsnameNEQ applies the NEQ predicate on the "osname" field.
func OsnameNEQ(v string) predicate.Machine {
return predicate.Machine(sql.FieldNEQ(FieldOsname, v))
}
// OsnameIn applies the In predicate on the "osname" field.
func OsnameIn(vs ...string) predicate.Machine {
return predicate.Machine(sql.FieldIn(FieldOsname, vs...))
}
// OsnameNotIn applies the NotIn predicate on the "osname" field.
func OsnameNotIn(vs ...string) predicate.Machine {
return predicate.Machine(sql.FieldNotIn(FieldOsname, vs...))
}
// OsnameGT applies the GT predicate on the "osname" field.
func OsnameGT(v string) predicate.Machine {
return predicate.Machine(sql.FieldGT(FieldOsname, v))
}
// OsnameGTE applies the GTE predicate on the "osname" field.
func OsnameGTE(v string) predicate.Machine {
return predicate.Machine(sql.FieldGTE(FieldOsname, v))
}
// OsnameLT applies the LT predicate on the "osname" field.
func OsnameLT(v string) predicate.Machine {
return predicate.Machine(sql.FieldLT(FieldOsname, v))
}
// OsnameLTE applies the LTE predicate on the "osname" field.
func OsnameLTE(v string) predicate.Machine {
return predicate.Machine(sql.FieldLTE(FieldOsname, v))
}
// OsnameContains applies the Contains predicate on the "osname" field.
func OsnameContains(v string) predicate.Machine {
return predicate.Machine(sql.FieldContains(FieldOsname, v))
}
// OsnameHasPrefix applies the HasPrefix predicate on the "osname" field.
func OsnameHasPrefix(v string) predicate.Machine {
return predicate.Machine(sql.FieldHasPrefix(FieldOsname, v))
}
// OsnameHasSuffix applies the HasSuffix predicate on the "osname" field.
func OsnameHasSuffix(v string) predicate.Machine {
return predicate.Machine(sql.FieldHasSuffix(FieldOsname, v))
}
// OsnameIsNil applies the IsNil predicate on the "osname" field.
func OsnameIsNil() predicate.Machine {
return predicate.Machine(sql.FieldIsNull(FieldOsname))
}
// OsnameNotNil applies the NotNil predicate on the "osname" field.
func OsnameNotNil() predicate.Machine {
return predicate.Machine(sql.FieldNotNull(FieldOsname))
}
// OsnameEqualFold applies the EqualFold predicate on the "osname" field.
func OsnameEqualFold(v string) predicate.Machine {
return predicate.Machine(sql.FieldEqualFold(FieldOsname, v))
}
// OsnameContainsFold applies the ContainsFold predicate on the "osname" field.
func OsnameContainsFold(v string) predicate.Machine {
return predicate.Machine(sql.FieldContainsFold(FieldOsname, v))
}
// OsversionEQ applies the EQ predicate on the "osversion" field.
func OsversionEQ(v string) predicate.Machine {
return predicate.Machine(sql.FieldEQ(FieldOsversion, v))
}
// OsversionNEQ applies the NEQ predicate on the "osversion" field.
func OsversionNEQ(v string) predicate.Machine {
return predicate.Machine(sql.FieldNEQ(FieldOsversion, v))
}
// OsversionIn applies the In predicate on the "osversion" field.
func OsversionIn(vs ...string) predicate.Machine {
return predicate.Machine(sql.FieldIn(FieldOsversion, vs...))
}
// OsversionNotIn applies the NotIn predicate on the "osversion" field.
func OsversionNotIn(vs ...string) predicate.Machine {
return predicate.Machine(sql.FieldNotIn(FieldOsversion, vs...))
}
// OsversionGT applies the GT predicate on the "osversion" field.
func OsversionGT(v string) predicate.Machine {
return predicate.Machine(sql.FieldGT(FieldOsversion, v))
}
// OsversionGTE applies the GTE predicate on the "osversion" field.
func OsversionGTE(v string) predicate.Machine {
return predicate.Machine(sql.FieldGTE(FieldOsversion, v))
}
// OsversionLT applies the LT predicate on the "osversion" field.
func OsversionLT(v string) predicate.Machine {
return predicate.Machine(sql.FieldLT(FieldOsversion, v))
}
// OsversionLTE applies the LTE predicate on the "osversion" field.
func OsversionLTE(v string) predicate.Machine {
return predicate.Machine(sql.FieldLTE(FieldOsversion, v))
}
// OsversionContains applies the Contains predicate on the "osversion" field.
func OsversionContains(v string) predicate.Machine {
return predicate.Machine(sql.FieldContains(FieldOsversion, v))
}
// OsversionHasPrefix applies the HasPrefix predicate on the "osversion" field.
func OsversionHasPrefix(v string) predicate.Machine {
return predicate.Machine(sql.FieldHasPrefix(FieldOsversion, v))
}
// OsversionHasSuffix applies the HasSuffix predicate on the "osversion" field.
func OsversionHasSuffix(v string) predicate.Machine {
return predicate.Machine(sql.FieldHasSuffix(FieldOsversion, v))
}
// OsversionIsNil applies the IsNil predicate on the "osversion" field.
func OsversionIsNil() predicate.Machine {
return predicate.Machine(sql.FieldIsNull(FieldOsversion))
}
// OsversionNotNil applies the NotNil predicate on the "osversion" field.
func OsversionNotNil() predicate.Machine {
return predicate.Machine(sql.FieldNotNull(FieldOsversion))
}
// OsversionEqualFold applies the EqualFold predicate on the "osversion" field.
func OsversionEqualFold(v string) predicate.Machine {
return predicate.Machine(sql.FieldEqualFold(FieldOsversion, v))
}
// OsversionContainsFold applies the ContainsFold predicate on the "osversion" field.
func OsversionContainsFold(v string) predicate.Machine {
return predicate.Machine(sql.FieldContainsFold(FieldOsversion, v))
}
// FeatureflagsEQ applies the EQ predicate on the "featureflags" field.
func FeatureflagsEQ(v string) predicate.Machine {
return predicate.Machine(sql.FieldEQ(FieldFeatureflags, v))
}
// FeatureflagsNEQ applies the NEQ predicate on the "featureflags" field.
func FeatureflagsNEQ(v string) predicate.Machine {
return predicate.Machine(sql.FieldNEQ(FieldFeatureflags, v))
}
// FeatureflagsIn applies the In predicate on the "featureflags" field.
func FeatureflagsIn(vs ...string) predicate.Machine {
return predicate.Machine(sql.FieldIn(FieldFeatureflags, vs...))
}
// FeatureflagsNotIn applies the NotIn predicate on the "featureflags" field.
func FeatureflagsNotIn(vs ...string) predicate.Machine {
return predicate.Machine(sql.FieldNotIn(FieldFeatureflags, vs...))
}
// FeatureflagsGT applies the GT predicate on the "featureflags" field.
func FeatureflagsGT(v string) predicate.Machine {
return predicate.Machine(sql.FieldGT(FieldFeatureflags, v))
}
// FeatureflagsGTE applies the GTE predicate on the "featureflags" field.
func FeatureflagsGTE(v string) predicate.Machine {
return predicate.Machine(sql.FieldGTE(FieldFeatureflags, v))
}
// FeatureflagsLT applies the LT predicate on the "featureflags" field.
func FeatureflagsLT(v string) predicate.Machine {
return predicate.Machine(sql.FieldLT(FieldFeatureflags, v))
}
// FeatureflagsLTE applies the LTE predicate on the "featureflags" field.
func FeatureflagsLTE(v string) predicate.Machine {
return predicate.Machine(sql.FieldLTE(FieldFeatureflags, v))
}
// FeatureflagsContains applies the Contains predicate on the "featureflags" field.
func FeatureflagsContains(v string) predicate.Machine {
return predicate.Machine(sql.FieldContains(FieldFeatureflags, v))
}
// FeatureflagsHasPrefix applies the HasPrefix predicate on the "featureflags" field.
func FeatureflagsHasPrefix(v string) predicate.Machine {
return predicate.Machine(sql.FieldHasPrefix(FieldFeatureflags, v))
}
// FeatureflagsHasSuffix applies the HasSuffix predicate on the "featureflags" field.
func FeatureflagsHasSuffix(v string) predicate.Machine {
return predicate.Machine(sql.FieldHasSuffix(FieldFeatureflags, v))
}
// FeatureflagsIsNil applies the IsNil predicate on the "featureflags" field.
func FeatureflagsIsNil() predicate.Machine {
return predicate.Machine(sql.FieldIsNull(FieldFeatureflags))
}
// FeatureflagsNotNil applies the NotNil predicate on the "featureflags" field.
func FeatureflagsNotNil() predicate.Machine {
return predicate.Machine(sql.FieldNotNull(FieldFeatureflags))
}
// FeatureflagsEqualFold applies the EqualFold predicate on the "featureflags" field.
func FeatureflagsEqualFold(v string) predicate.Machine {
return predicate.Machine(sql.FieldEqualFold(FieldFeatureflags, v))
}
// FeatureflagsContainsFold applies the ContainsFold predicate on the "featureflags" field.
func FeatureflagsContainsFold(v string) predicate.Machine {
return predicate.Machine(sql.FieldContainsFold(FieldFeatureflags, v))
}
// HubstateIsNil applies the IsNil predicate on the "hubstate" field.
func HubstateIsNil() predicate.Machine {
return predicate.Machine(sql.FieldIsNull(FieldHubstate))
}
// HubstateNotNil applies the NotNil predicate on the "hubstate" field.
func HubstateNotNil() predicate.Machine {
return predicate.Machine(sql.FieldNotNull(FieldHubstate))
}
// DatasourcesIsNil applies the IsNil predicate on the "datasources" field.
func DatasourcesIsNil() predicate.Machine {
return predicate.Machine(sql.FieldIsNull(FieldDatasources))
}
// DatasourcesNotNil applies the NotNil predicate on the "datasources" field.
func DatasourcesNotNil() predicate.Machine {
return predicate.Machine(sql.FieldNotNull(FieldDatasources))
}
// HasAlerts applies the HasEdge predicate on the "alerts" edge.
func HasAlerts() predicate.Machine {
return predicate.Machine(func(s *sql.Selector) {

View file

@ -12,6 +12,7 @@ import (
"entgo.io/ent/schema/field"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/schema"
)
// MachineCreate is the builder for creating a Machine entity.
@ -165,6 +166,60 @@ func (mc *MachineCreate) SetNillableAuthType(s *string) *MachineCreate {
return mc
}
// SetOsname sets the "osname" field.
func (mc *MachineCreate) SetOsname(s string) *MachineCreate {
mc.mutation.SetOsname(s)
return mc
}
// SetNillableOsname sets the "osname" field if the given value is not nil.
func (mc *MachineCreate) SetNillableOsname(s *string) *MachineCreate {
if s != nil {
mc.SetOsname(*s)
}
return mc
}
// SetOsversion sets the "osversion" field.
func (mc *MachineCreate) SetOsversion(s string) *MachineCreate {
mc.mutation.SetOsversion(s)
return mc
}
// SetNillableOsversion sets the "osversion" field if the given value is not nil.
func (mc *MachineCreate) SetNillableOsversion(s *string) *MachineCreate {
if s != nil {
mc.SetOsversion(*s)
}
return mc
}
// SetFeatureflags sets the "featureflags" field.
func (mc *MachineCreate) SetFeatureflags(s string) *MachineCreate {
mc.mutation.SetFeatureflags(s)
return mc
}
// SetNillableFeatureflags sets the "featureflags" field if the given value is not nil.
func (mc *MachineCreate) SetNillableFeatureflags(s *string) *MachineCreate {
if s != nil {
mc.SetFeatureflags(*s)
}
return mc
}
// SetHubstate sets the "hubstate" field.
func (mc *MachineCreate) SetHubstate(ms map[string]schema.ItemState) *MachineCreate {
mc.mutation.SetHubstate(ms)
return mc
}
// SetDatasources sets the "datasources" field.
func (mc *MachineCreate) SetDatasources(m map[string]int64) *MachineCreate {
mc.mutation.SetDatasources(m)
return mc
}
// AddAlertIDs adds the "alerts" edge to the Alert entity by IDs.
func (mc *MachineCreate) AddAlertIDs(ids ...int) *MachineCreate {
mc.mutation.AddAlertIDs(ids...)
@ -339,6 +394,26 @@ func (mc *MachineCreate) createSpec() (*Machine, *sqlgraph.CreateSpec) {
_spec.SetField(machine.FieldAuthType, field.TypeString, value)
_node.AuthType = value
}
if value, ok := mc.mutation.Osname(); ok {
_spec.SetField(machine.FieldOsname, field.TypeString, value)
_node.Osname = value
}
if value, ok := mc.mutation.Osversion(); ok {
_spec.SetField(machine.FieldOsversion, field.TypeString, value)
_node.Osversion = value
}
if value, ok := mc.mutation.Featureflags(); ok {
_spec.SetField(machine.FieldFeatureflags, field.TypeString, value)
_node.Featureflags = value
}
if value, ok := mc.mutation.Hubstate(); ok {
_spec.SetField(machine.FieldHubstate, field.TypeJSON, value)
_node.Hubstate = value
}
if value, ok := mc.mutation.Datasources(); ok {
_spec.SetField(machine.FieldDatasources, field.TypeJSON, value)
_node.Datasources = value
}
if nodes := mc.mutation.AlertsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,

View file

@ -14,6 +14,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/schema"
)
// MachineUpdate is the builder for updating Machine entities.
@ -191,6 +192,90 @@ func (mu *MachineUpdate) SetNillableAuthType(s *string) *MachineUpdate {
return mu
}
// SetOsname sets the "osname" field.
func (mu *MachineUpdate) SetOsname(s string) *MachineUpdate {
mu.mutation.SetOsname(s)
return mu
}
// SetNillableOsname sets the "osname" field if the given value is not nil.
func (mu *MachineUpdate) SetNillableOsname(s *string) *MachineUpdate {
if s != nil {
mu.SetOsname(*s)
}
return mu
}
// ClearOsname clears the value of the "osname" field.
func (mu *MachineUpdate) ClearOsname() *MachineUpdate {
mu.mutation.ClearOsname()
return mu
}
// SetOsversion sets the "osversion" field.
func (mu *MachineUpdate) SetOsversion(s string) *MachineUpdate {
mu.mutation.SetOsversion(s)
return mu
}
// SetNillableOsversion sets the "osversion" field if the given value is not nil.
func (mu *MachineUpdate) SetNillableOsversion(s *string) *MachineUpdate {
if s != nil {
mu.SetOsversion(*s)
}
return mu
}
// ClearOsversion clears the value of the "osversion" field.
func (mu *MachineUpdate) ClearOsversion() *MachineUpdate {
mu.mutation.ClearOsversion()
return mu
}
// SetFeatureflags sets the "featureflags" field.
func (mu *MachineUpdate) SetFeatureflags(s string) *MachineUpdate {
mu.mutation.SetFeatureflags(s)
return mu
}
// SetNillableFeatureflags sets the "featureflags" field if the given value is not nil.
func (mu *MachineUpdate) SetNillableFeatureflags(s *string) *MachineUpdate {
if s != nil {
mu.SetFeatureflags(*s)
}
return mu
}
// ClearFeatureflags clears the value of the "featureflags" field.
func (mu *MachineUpdate) ClearFeatureflags() *MachineUpdate {
mu.mutation.ClearFeatureflags()
return mu
}
// SetHubstate sets the "hubstate" field.
func (mu *MachineUpdate) SetHubstate(ms map[string]schema.ItemState) *MachineUpdate {
mu.mutation.SetHubstate(ms)
return mu
}
// ClearHubstate clears the value of the "hubstate" field.
func (mu *MachineUpdate) ClearHubstate() *MachineUpdate {
mu.mutation.ClearHubstate()
return mu
}
// SetDatasources sets the "datasources" field.
func (mu *MachineUpdate) SetDatasources(m map[string]int64) *MachineUpdate {
mu.mutation.SetDatasources(m)
return mu
}
// ClearDatasources clears the value of the "datasources" field.
func (mu *MachineUpdate) ClearDatasources() *MachineUpdate {
mu.mutation.ClearDatasources()
return mu
}
// AddAlertIDs adds the "alerts" edge to the Alert entity by IDs.
func (mu *MachineUpdate) AddAlertIDs(ids ...int) *MachineUpdate {
mu.mutation.AddAlertIDs(ids...)
@ -335,6 +420,36 @@ func (mu *MachineUpdate) sqlSave(ctx context.Context) (n int, err error) {
if value, ok := mu.mutation.AuthType(); ok {
_spec.SetField(machine.FieldAuthType, field.TypeString, value)
}
if value, ok := mu.mutation.Osname(); ok {
_spec.SetField(machine.FieldOsname, field.TypeString, value)
}
if mu.mutation.OsnameCleared() {
_spec.ClearField(machine.FieldOsname, field.TypeString)
}
if value, ok := mu.mutation.Osversion(); ok {
_spec.SetField(machine.FieldOsversion, field.TypeString, value)
}
if mu.mutation.OsversionCleared() {
_spec.ClearField(machine.FieldOsversion, field.TypeString)
}
if value, ok := mu.mutation.Featureflags(); ok {
_spec.SetField(machine.FieldFeatureflags, field.TypeString, value)
}
if mu.mutation.FeatureflagsCleared() {
_spec.ClearField(machine.FieldFeatureflags, field.TypeString)
}
if value, ok := mu.mutation.Hubstate(); ok {
_spec.SetField(machine.FieldHubstate, field.TypeJSON, value)
}
if mu.mutation.HubstateCleared() {
_spec.ClearField(machine.FieldHubstate, field.TypeJSON)
}
if value, ok := mu.mutation.Datasources(); ok {
_spec.SetField(machine.FieldDatasources, field.TypeJSON, value)
}
if mu.mutation.DatasourcesCleared() {
_spec.ClearField(machine.FieldDatasources, field.TypeJSON)
}
if mu.mutation.AlertsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
@ -562,6 +677,90 @@ func (muo *MachineUpdateOne) SetNillableAuthType(s *string) *MachineUpdateOne {
return muo
}
// SetOsname sets the "osname" field.
func (muo *MachineUpdateOne) SetOsname(s string) *MachineUpdateOne {
muo.mutation.SetOsname(s)
return muo
}
// SetNillableOsname sets the "osname" field if the given value is not nil.
func (muo *MachineUpdateOne) SetNillableOsname(s *string) *MachineUpdateOne {
if s != nil {
muo.SetOsname(*s)
}
return muo
}
// ClearOsname clears the value of the "osname" field.
func (muo *MachineUpdateOne) ClearOsname() *MachineUpdateOne {
muo.mutation.ClearOsname()
return muo
}
// SetOsversion sets the "osversion" field.
func (muo *MachineUpdateOne) SetOsversion(s string) *MachineUpdateOne {
muo.mutation.SetOsversion(s)
return muo
}
// SetNillableOsversion sets the "osversion" field if the given value is not nil.
func (muo *MachineUpdateOne) SetNillableOsversion(s *string) *MachineUpdateOne {
if s != nil {
muo.SetOsversion(*s)
}
return muo
}
// ClearOsversion clears the value of the "osversion" field.
func (muo *MachineUpdateOne) ClearOsversion() *MachineUpdateOne {
muo.mutation.ClearOsversion()
return muo
}
// SetFeatureflags sets the "featureflags" field.
func (muo *MachineUpdateOne) SetFeatureflags(s string) *MachineUpdateOne {
muo.mutation.SetFeatureflags(s)
return muo
}
// SetNillableFeatureflags sets the "featureflags" field if the given value is not nil.
func (muo *MachineUpdateOne) SetNillableFeatureflags(s *string) *MachineUpdateOne {
if s != nil {
muo.SetFeatureflags(*s)
}
return muo
}
// ClearFeatureflags clears the value of the "featureflags" field.
func (muo *MachineUpdateOne) ClearFeatureflags() *MachineUpdateOne {
muo.mutation.ClearFeatureflags()
return muo
}
// SetHubstate sets the "hubstate" field.
func (muo *MachineUpdateOne) SetHubstate(ms map[string]schema.ItemState) *MachineUpdateOne {
muo.mutation.SetHubstate(ms)
return muo
}
// ClearHubstate clears the value of the "hubstate" field.
func (muo *MachineUpdateOne) ClearHubstate() *MachineUpdateOne {
muo.mutation.ClearHubstate()
return muo
}
// SetDatasources sets the "datasources" field.
func (muo *MachineUpdateOne) SetDatasources(m map[string]int64) *MachineUpdateOne {
muo.mutation.SetDatasources(m)
return muo
}
// ClearDatasources clears the value of the "datasources" field.
func (muo *MachineUpdateOne) ClearDatasources() *MachineUpdateOne {
muo.mutation.ClearDatasources()
return muo
}
// AddAlertIDs adds the "alerts" edge to the Alert entity by IDs.
func (muo *MachineUpdateOne) AddAlertIDs(ids ...int) *MachineUpdateOne {
muo.mutation.AddAlertIDs(ids...)
@ -736,6 +935,36 @@ func (muo *MachineUpdateOne) sqlSave(ctx context.Context) (_node *Machine, err e
if value, ok := muo.mutation.AuthType(); ok {
_spec.SetField(machine.FieldAuthType, field.TypeString, value)
}
if value, ok := muo.mutation.Osname(); ok {
_spec.SetField(machine.FieldOsname, field.TypeString, value)
}
if muo.mutation.OsnameCleared() {
_spec.ClearField(machine.FieldOsname, field.TypeString)
}
if value, ok := muo.mutation.Osversion(); ok {
_spec.SetField(machine.FieldOsversion, field.TypeString, value)
}
if muo.mutation.OsversionCleared() {
_spec.ClearField(machine.FieldOsversion, field.TypeString)
}
if value, ok := muo.mutation.Featureflags(); ok {
_spec.SetField(machine.FieldFeatureflags, field.TypeString, value)
}
if muo.mutation.FeatureflagsCleared() {
_spec.ClearField(machine.FieldFeatureflags, field.TypeString)
}
if value, ok := muo.mutation.Hubstate(); ok {
_spec.SetField(machine.FieldHubstate, field.TypeJSON, value)
}
if muo.mutation.HubstateCleared() {
_spec.ClearField(machine.FieldHubstate, field.TypeJSON)
}
if value, ok := muo.mutation.Datasources(); ok {
_spec.SetField(machine.FieldDatasources, field.TypeJSON, value)
}
if muo.mutation.DatasourcesCleared() {
_spec.ClearField(machine.FieldDatasources, field.TypeJSON)
}
if muo.mutation.AlertsCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,

154
pkg/database/ent/metric.go Normal file
View file

@ -0,0 +1,154 @@
// Code generated by ent, DO NOT EDIT.
package ent
import (
"fmt"
"strings"
"time"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/metric"
)
// Metric is the model entity for the Metric schema.
type Metric struct {
config `json:"-"`
// ID of the ent.
ID int `json:"id,omitempty"`
// Type of the metrics source: LP=logprocessor, RC=remediation
GeneratedType metric.GeneratedType `json:"generated_type,omitempty"`
// Source of the metrics: machine id, bouncer name...
// It must come from the auth middleware.
GeneratedBy string `json:"generated_by,omitempty"`
// When the metrics are collected/calculated at the source
CollectedAt time.Time `json:"collected_at,omitempty"`
// When the metrics are sent to the console
PushedAt *time.Time `json:"pushed_at,omitempty"`
// The actual metrics (item0)
Payload string `json:"payload,omitempty"`
selectValues sql.SelectValues
}
// scanValues returns the types for scanning values from sql.Rows.
func (*Metric) scanValues(columns []string) ([]any, error) {
values := make([]any, len(columns))
for i := range columns {
switch columns[i] {
case metric.FieldID:
values[i] = new(sql.NullInt64)
case metric.FieldGeneratedType, metric.FieldGeneratedBy, metric.FieldPayload:
values[i] = new(sql.NullString)
case metric.FieldCollectedAt, metric.FieldPushedAt:
values[i] = new(sql.NullTime)
default:
values[i] = new(sql.UnknownType)
}
}
return values, nil
}
// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Metric fields.
func (m *Metric) assignValues(columns []string, values []any) error {
if m, n := len(values), len(columns); m < n {
return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
}
for i := range columns {
switch columns[i] {
case metric.FieldID:
value, ok := values[i].(*sql.NullInt64)
if !ok {
return fmt.Errorf("unexpected type %T for field id", value)
}
m.ID = int(value.Int64)
case metric.FieldGeneratedType:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field generated_type", values[i])
} else if value.Valid {
m.GeneratedType = metric.GeneratedType(value.String)
}
case metric.FieldGeneratedBy:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field generated_by", values[i])
} else if value.Valid {
m.GeneratedBy = value.String
}
case metric.FieldCollectedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field collected_at", values[i])
} else if value.Valid {
m.CollectedAt = value.Time
}
case metric.FieldPushedAt:
if value, ok := values[i].(*sql.NullTime); !ok {
return fmt.Errorf("unexpected type %T for field pushed_at", values[i])
} else if value.Valid {
m.PushedAt = new(time.Time)
*m.PushedAt = value.Time
}
case metric.FieldPayload:
if value, ok := values[i].(*sql.NullString); !ok {
return fmt.Errorf("unexpected type %T for field payload", values[i])
} else if value.Valid {
m.Payload = value.String
}
default:
m.selectValues.Set(columns[i], values[i])
}
}
return nil
}
// Value returns the ent.Value that was dynamically selected and assigned to the Metric.
// This includes values selected through modifiers, order, etc.
func (m *Metric) Value(name string) (ent.Value, error) {
return m.selectValues.Get(name)
}
// Update returns a builder for updating this Metric.
// Note that you need to call Metric.Unwrap() before calling this method if this Metric
// was returned from a transaction, and the transaction was committed or rolled back.
func (m *Metric) Update() *MetricUpdateOne {
return NewMetricClient(m.config).UpdateOne(m)
}
// Unwrap unwraps the Metric entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (m *Metric) Unwrap() *Metric {
_tx, ok := m.config.driver.(*txDriver)
if !ok {
panic("ent: Metric is not a transactional entity")
}
m.config.driver = _tx.drv
return m
}
// String implements the fmt.Stringer.
func (m *Metric) String() string {
var builder strings.Builder
builder.WriteString("Metric(")
builder.WriteString(fmt.Sprintf("id=%v, ", m.ID))
builder.WriteString("generated_type=")
builder.WriteString(fmt.Sprintf("%v", m.GeneratedType))
builder.WriteString(", ")
builder.WriteString("generated_by=")
builder.WriteString(m.GeneratedBy)
builder.WriteString(", ")
builder.WriteString("collected_at=")
builder.WriteString(m.CollectedAt.Format(time.ANSIC))
builder.WriteString(", ")
if v := m.PushedAt; v != nil {
builder.WriteString("pushed_at=")
builder.WriteString(v.Format(time.ANSIC))
}
builder.WriteString(", ")
builder.WriteString("payload=")
builder.WriteString(m.Payload)
builder.WriteByte(')')
return builder.String()
}
// Metrics is a parsable slice of Metric.
type Metrics []*Metric

View file

@ -0,0 +1,104 @@
// Code generated by ent, DO NOT EDIT.
package metric
import (
"fmt"
"entgo.io/ent/dialect/sql"
)
const (
// Label holds the string label denoting the metric type in the database.
Label = "metric"
// FieldID holds the string denoting the id field in the database.
FieldID = "id"
// FieldGeneratedType holds the string denoting the generated_type field in the database.
FieldGeneratedType = "generated_type"
// FieldGeneratedBy holds the string denoting the generated_by field in the database.
FieldGeneratedBy = "generated_by"
// FieldCollectedAt holds the string denoting the collected_at field in the database.
FieldCollectedAt = "collected_at"
// FieldPushedAt holds the string denoting the pushed_at field in the database.
FieldPushedAt = "pushed_at"
// FieldPayload holds the string denoting the payload field in the database.
FieldPayload = "payload"
// Table holds the table name of the metric in the database.
Table = "metrics"
)
// Columns holds all SQL columns for metric fields.
var Columns = []string{
FieldID,
FieldGeneratedType,
FieldGeneratedBy,
FieldCollectedAt,
FieldPushedAt,
FieldPayload,
}
// ValidColumn reports if the column name is valid (part of the table columns).
func ValidColumn(column string) bool {
for i := range Columns {
if column == Columns[i] {
return true
}
}
return false
}
// GeneratedType defines the type for the "generated_type" enum field.
type GeneratedType string
// GeneratedType values.
const (
GeneratedTypeLP GeneratedType = "LP"
GeneratedTypeRC GeneratedType = "RC"
)
func (gt GeneratedType) String() string {
return string(gt)
}
// GeneratedTypeValidator is a validator for the "generated_type" field enum values. It is called by the builders before save.
func GeneratedTypeValidator(gt GeneratedType) error {
switch gt {
case GeneratedTypeLP, GeneratedTypeRC:
return nil
default:
return fmt.Errorf("metric: invalid enum value for generated_type field: %q", gt)
}
}
// OrderOption defines the ordering options for the Metric queries.
type OrderOption func(*sql.Selector)
// ByID orders the results by the id field.
func ByID(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldID, opts...).ToFunc()
}
// ByGeneratedType orders the results by the generated_type field.
func ByGeneratedType(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldGeneratedType, opts...).ToFunc()
}
// ByGeneratedBy orders the results by the generated_by field.
func ByGeneratedBy(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldGeneratedBy, opts...).ToFunc()
}
// ByCollectedAt orders the results by the collected_at field.
func ByCollectedAt(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldCollectedAt, opts...).ToFunc()
}
// ByPushedAt orders the results by the pushed_at field.
func ByPushedAt(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldPushedAt, opts...).ToFunc()
}
// ByPayload orders the results by the payload field.
func ByPayload(opts ...sql.OrderTermOption) OrderOption {
return sql.OrderByField(FieldPayload, opts...).ToFunc()
}

View file

@ -0,0 +1,330 @@
// Code generated by ent, DO NOT EDIT.
package metric
import (
"time"
"entgo.io/ent/dialect/sql"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
)
// ID filters vertices based on their ID field.
func ID(id int) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldID, id))
}
// IDEQ applies the EQ predicate on the ID field.
func IDEQ(id int) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldID, id))
}
// IDNEQ applies the NEQ predicate on the ID field.
func IDNEQ(id int) predicate.Metric {
return predicate.Metric(sql.FieldNEQ(FieldID, id))
}
// IDIn applies the In predicate on the ID field.
func IDIn(ids ...int) predicate.Metric {
return predicate.Metric(sql.FieldIn(FieldID, ids...))
}
// IDNotIn applies the NotIn predicate on the ID field.
func IDNotIn(ids ...int) predicate.Metric {
return predicate.Metric(sql.FieldNotIn(FieldID, ids...))
}
// IDGT applies the GT predicate on the ID field.
func IDGT(id int) predicate.Metric {
return predicate.Metric(sql.FieldGT(FieldID, id))
}
// IDGTE applies the GTE predicate on the ID field.
func IDGTE(id int) predicate.Metric {
return predicate.Metric(sql.FieldGTE(FieldID, id))
}
// IDLT applies the LT predicate on the ID field.
func IDLT(id int) predicate.Metric {
return predicate.Metric(sql.FieldLT(FieldID, id))
}
// IDLTE applies the LTE predicate on the ID field.
func IDLTE(id int) predicate.Metric {
return predicate.Metric(sql.FieldLTE(FieldID, id))
}
// GeneratedBy applies equality check predicate on the "generated_by" field. It's identical to GeneratedByEQ.
func GeneratedBy(v string) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldGeneratedBy, v))
}
// CollectedAt applies equality check predicate on the "collected_at" field. It's identical to CollectedAtEQ.
func CollectedAt(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldCollectedAt, v))
}
// PushedAt applies equality check predicate on the "pushed_at" field. It's identical to PushedAtEQ.
func PushedAt(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldPushedAt, v))
}
// Payload applies equality check predicate on the "payload" field. It's identical to PayloadEQ.
func Payload(v string) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldPayload, v))
}
// GeneratedTypeEQ applies the EQ predicate on the "generated_type" field.
func GeneratedTypeEQ(v GeneratedType) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldGeneratedType, v))
}
// GeneratedTypeNEQ applies the NEQ predicate on the "generated_type" field.
func GeneratedTypeNEQ(v GeneratedType) predicate.Metric {
return predicate.Metric(sql.FieldNEQ(FieldGeneratedType, v))
}
// GeneratedTypeIn applies the In predicate on the "generated_type" field.
func GeneratedTypeIn(vs ...GeneratedType) predicate.Metric {
return predicate.Metric(sql.FieldIn(FieldGeneratedType, vs...))
}
// GeneratedTypeNotIn applies the NotIn predicate on the "generated_type" field.
func GeneratedTypeNotIn(vs ...GeneratedType) predicate.Metric {
return predicate.Metric(sql.FieldNotIn(FieldGeneratedType, vs...))
}
// GeneratedByEQ applies the EQ predicate on the "generated_by" field.
func GeneratedByEQ(v string) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldGeneratedBy, v))
}
// GeneratedByNEQ applies the NEQ predicate on the "generated_by" field.
func GeneratedByNEQ(v string) predicate.Metric {
return predicate.Metric(sql.FieldNEQ(FieldGeneratedBy, v))
}
// GeneratedByIn applies the In predicate on the "generated_by" field.
func GeneratedByIn(vs ...string) predicate.Metric {
return predicate.Metric(sql.FieldIn(FieldGeneratedBy, vs...))
}
// GeneratedByNotIn applies the NotIn predicate on the "generated_by" field.
func GeneratedByNotIn(vs ...string) predicate.Metric {
return predicate.Metric(sql.FieldNotIn(FieldGeneratedBy, vs...))
}
// GeneratedByGT applies the GT predicate on the "generated_by" field.
func GeneratedByGT(v string) predicate.Metric {
return predicate.Metric(sql.FieldGT(FieldGeneratedBy, v))
}
// GeneratedByGTE applies the GTE predicate on the "generated_by" field.
func GeneratedByGTE(v string) predicate.Metric {
return predicate.Metric(sql.FieldGTE(FieldGeneratedBy, v))
}
// GeneratedByLT applies the LT predicate on the "generated_by" field.
func GeneratedByLT(v string) predicate.Metric {
return predicate.Metric(sql.FieldLT(FieldGeneratedBy, v))
}
// GeneratedByLTE applies the LTE predicate on the "generated_by" field.
func GeneratedByLTE(v string) predicate.Metric {
return predicate.Metric(sql.FieldLTE(FieldGeneratedBy, v))
}
// GeneratedByContains applies the Contains predicate on the "generated_by" field.
func GeneratedByContains(v string) predicate.Metric {
return predicate.Metric(sql.FieldContains(FieldGeneratedBy, v))
}
// GeneratedByHasPrefix applies the HasPrefix predicate on the "generated_by" field.
func GeneratedByHasPrefix(v string) predicate.Metric {
return predicate.Metric(sql.FieldHasPrefix(FieldGeneratedBy, v))
}
// GeneratedByHasSuffix applies the HasSuffix predicate on the "generated_by" field.
func GeneratedByHasSuffix(v string) predicate.Metric {
return predicate.Metric(sql.FieldHasSuffix(FieldGeneratedBy, v))
}
// GeneratedByEqualFold applies the EqualFold predicate on the "generated_by" field.
func GeneratedByEqualFold(v string) predicate.Metric {
return predicate.Metric(sql.FieldEqualFold(FieldGeneratedBy, v))
}
// GeneratedByContainsFold applies the ContainsFold predicate on the "generated_by" field.
func GeneratedByContainsFold(v string) predicate.Metric {
return predicate.Metric(sql.FieldContainsFold(FieldGeneratedBy, v))
}
// CollectedAtEQ applies the EQ predicate on the "collected_at" field.
func CollectedAtEQ(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldCollectedAt, v))
}
// CollectedAtNEQ applies the NEQ predicate on the "collected_at" field.
func CollectedAtNEQ(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldNEQ(FieldCollectedAt, v))
}
// CollectedAtIn applies the In predicate on the "collected_at" field.
func CollectedAtIn(vs ...time.Time) predicate.Metric {
return predicate.Metric(sql.FieldIn(FieldCollectedAt, vs...))
}
// CollectedAtNotIn applies the NotIn predicate on the "collected_at" field.
func CollectedAtNotIn(vs ...time.Time) predicate.Metric {
return predicate.Metric(sql.FieldNotIn(FieldCollectedAt, vs...))
}
// CollectedAtGT applies the GT predicate on the "collected_at" field.
func CollectedAtGT(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldGT(FieldCollectedAt, v))
}
// CollectedAtGTE applies the GTE predicate on the "collected_at" field.
func CollectedAtGTE(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldGTE(FieldCollectedAt, v))
}
// CollectedAtLT applies the LT predicate on the "collected_at" field.
func CollectedAtLT(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldLT(FieldCollectedAt, v))
}
// CollectedAtLTE applies the LTE predicate on the "collected_at" field.
func CollectedAtLTE(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldLTE(FieldCollectedAt, v))
}
// PushedAtEQ applies the EQ predicate on the "pushed_at" field.
func PushedAtEQ(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldPushedAt, v))
}
// PushedAtNEQ applies the NEQ predicate on the "pushed_at" field.
func PushedAtNEQ(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldNEQ(FieldPushedAt, v))
}
// PushedAtIn applies the In predicate on the "pushed_at" field.
func PushedAtIn(vs ...time.Time) predicate.Metric {
return predicate.Metric(sql.FieldIn(FieldPushedAt, vs...))
}
// PushedAtNotIn applies the NotIn predicate on the "pushed_at" field.
func PushedAtNotIn(vs ...time.Time) predicate.Metric {
return predicate.Metric(sql.FieldNotIn(FieldPushedAt, vs...))
}
// PushedAtGT applies the GT predicate on the "pushed_at" field.
func PushedAtGT(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldGT(FieldPushedAt, v))
}
// PushedAtGTE applies the GTE predicate on the "pushed_at" field.
func PushedAtGTE(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldGTE(FieldPushedAt, v))
}
// PushedAtLT applies the LT predicate on the "pushed_at" field.
func PushedAtLT(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldLT(FieldPushedAt, v))
}
// PushedAtLTE applies the LTE predicate on the "pushed_at" field.
func PushedAtLTE(v time.Time) predicate.Metric {
return predicate.Metric(sql.FieldLTE(FieldPushedAt, v))
}
// PushedAtIsNil applies the IsNil predicate on the "pushed_at" field.
func PushedAtIsNil() predicate.Metric {
return predicate.Metric(sql.FieldIsNull(FieldPushedAt))
}
// PushedAtNotNil applies the NotNil predicate on the "pushed_at" field.
func PushedAtNotNil() predicate.Metric {
return predicate.Metric(sql.FieldNotNull(FieldPushedAt))
}
// PayloadEQ applies the EQ predicate on the "payload" field.
func PayloadEQ(v string) predicate.Metric {
return predicate.Metric(sql.FieldEQ(FieldPayload, v))
}
// PayloadNEQ applies the NEQ predicate on the "payload" field.
func PayloadNEQ(v string) predicate.Metric {
return predicate.Metric(sql.FieldNEQ(FieldPayload, v))
}
// PayloadIn applies the In predicate on the "payload" field.
func PayloadIn(vs ...string) predicate.Metric {
return predicate.Metric(sql.FieldIn(FieldPayload, vs...))
}
// PayloadNotIn applies the NotIn predicate on the "payload" field.
func PayloadNotIn(vs ...string) predicate.Metric {
return predicate.Metric(sql.FieldNotIn(FieldPayload, vs...))
}
// PayloadGT applies the GT predicate on the "payload" field.
func PayloadGT(v string) predicate.Metric {
return predicate.Metric(sql.FieldGT(FieldPayload, v))
}
// PayloadGTE applies the GTE predicate on the "payload" field.
func PayloadGTE(v string) predicate.Metric {
return predicate.Metric(sql.FieldGTE(FieldPayload, v))
}
// PayloadLT applies the LT predicate on the "payload" field.
func PayloadLT(v string) predicate.Metric {
return predicate.Metric(sql.FieldLT(FieldPayload, v))
}
// PayloadLTE applies the LTE predicate on the "payload" field.
func PayloadLTE(v string) predicate.Metric {
return predicate.Metric(sql.FieldLTE(FieldPayload, v))
}
// PayloadContains applies the Contains predicate on the "payload" field.
func PayloadContains(v string) predicate.Metric {
return predicate.Metric(sql.FieldContains(FieldPayload, v))
}
// PayloadHasPrefix applies the HasPrefix predicate on the "payload" field.
func PayloadHasPrefix(v string) predicate.Metric {
return predicate.Metric(sql.FieldHasPrefix(FieldPayload, v))
}
// PayloadHasSuffix applies the HasSuffix predicate on the "payload" field.
func PayloadHasSuffix(v string) predicate.Metric {
return predicate.Metric(sql.FieldHasSuffix(FieldPayload, v))
}
// PayloadEqualFold applies the EqualFold predicate on the "payload" field.
func PayloadEqualFold(v string) predicate.Metric {
return predicate.Metric(sql.FieldEqualFold(FieldPayload, v))
}
// PayloadContainsFold applies the ContainsFold predicate on the "payload" field.
func PayloadContainsFold(v string) predicate.Metric {
return predicate.Metric(sql.FieldContainsFold(FieldPayload, v))
}
// And groups predicates with the AND operator between them.
func And(predicates ...predicate.Metric) predicate.Metric {
return predicate.Metric(sql.AndPredicates(predicates...))
}
// Or groups predicates with the OR operator between them.
func Or(predicates ...predicate.Metric) predicate.Metric {
return predicate.Metric(sql.OrPredicates(predicates...))
}
// Not applies the not operator on the given predicate.
func Not(p predicate.Metric) predicate.Metric {
return predicate.Metric(sql.NotPredicates(p))
}

View file

@ -0,0 +1,246 @@
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/metric"
)
// MetricCreate is the builder for creating a Metric entity.
type MetricCreate struct {
config
mutation *MetricMutation
hooks []Hook
}
// SetGeneratedType sets the "generated_type" field.
func (mc *MetricCreate) SetGeneratedType(mt metric.GeneratedType) *MetricCreate {
mc.mutation.SetGeneratedType(mt)
return mc
}
// SetGeneratedBy sets the "generated_by" field.
func (mc *MetricCreate) SetGeneratedBy(s string) *MetricCreate {
mc.mutation.SetGeneratedBy(s)
return mc
}
// SetCollectedAt sets the "collected_at" field.
func (mc *MetricCreate) SetCollectedAt(t time.Time) *MetricCreate {
mc.mutation.SetCollectedAt(t)
return mc
}
// SetPushedAt sets the "pushed_at" field.
func (mc *MetricCreate) SetPushedAt(t time.Time) *MetricCreate {
mc.mutation.SetPushedAt(t)
return mc
}
// SetNillablePushedAt sets the "pushed_at" field if the given value is not nil.
func (mc *MetricCreate) SetNillablePushedAt(t *time.Time) *MetricCreate {
if t != nil {
mc.SetPushedAt(*t)
}
return mc
}
// SetPayload sets the "payload" field.
func (mc *MetricCreate) SetPayload(s string) *MetricCreate {
mc.mutation.SetPayload(s)
return mc
}
// Mutation returns the MetricMutation object of the builder.
func (mc *MetricCreate) Mutation() *MetricMutation {
return mc.mutation
}
// Save creates the Metric in the database.
func (mc *MetricCreate) Save(ctx context.Context) (*Metric, error) {
return withHooks(ctx, mc.sqlSave, mc.mutation, mc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (mc *MetricCreate) SaveX(ctx context.Context) *Metric {
v, err := mc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (mc *MetricCreate) Exec(ctx context.Context) error {
_, err := mc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (mc *MetricCreate) ExecX(ctx context.Context) {
if err := mc.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (mc *MetricCreate) check() error {
if _, ok := mc.mutation.GeneratedType(); !ok {
return &ValidationError{Name: "generated_type", err: errors.New(`ent: missing required field "Metric.generated_type"`)}
}
if v, ok := mc.mutation.GeneratedType(); ok {
if err := metric.GeneratedTypeValidator(v); err != nil {
return &ValidationError{Name: "generated_type", err: fmt.Errorf(`ent: validator failed for field "Metric.generated_type": %w`, err)}
}
}
if _, ok := mc.mutation.GeneratedBy(); !ok {
return &ValidationError{Name: "generated_by", err: errors.New(`ent: missing required field "Metric.generated_by"`)}
}
if _, ok := mc.mutation.CollectedAt(); !ok {
return &ValidationError{Name: "collected_at", err: errors.New(`ent: missing required field "Metric.collected_at"`)}
}
if _, ok := mc.mutation.Payload(); !ok {
return &ValidationError{Name: "payload", err: errors.New(`ent: missing required field "Metric.payload"`)}
}
return nil
}
func (mc *MetricCreate) sqlSave(ctx context.Context) (*Metric, error) {
if err := mc.check(); err != nil {
return nil, err
}
_node, _spec := mc.createSpec()
if err := sqlgraph.CreateNode(ctx, mc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
mc.mutation.id = &_node.ID
mc.mutation.done = true
return _node, nil
}
func (mc *MetricCreate) createSpec() (*Metric, *sqlgraph.CreateSpec) {
var (
_node = &Metric{config: mc.config}
_spec = sqlgraph.NewCreateSpec(metric.Table, sqlgraph.NewFieldSpec(metric.FieldID, field.TypeInt))
)
if value, ok := mc.mutation.GeneratedType(); ok {
_spec.SetField(metric.FieldGeneratedType, field.TypeEnum, value)
_node.GeneratedType = value
}
if value, ok := mc.mutation.GeneratedBy(); ok {
_spec.SetField(metric.FieldGeneratedBy, field.TypeString, value)
_node.GeneratedBy = value
}
if value, ok := mc.mutation.CollectedAt(); ok {
_spec.SetField(metric.FieldCollectedAt, field.TypeTime, value)
_node.CollectedAt = value
}
if value, ok := mc.mutation.PushedAt(); ok {
_spec.SetField(metric.FieldPushedAt, field.TypeTime, value)
_node.PushedAt = &value
}
if value, ok := mc.mutation.Payload(); ok {
_spec.SetField(metric.FieldPayload, field.TypeString, value)
_node.Payload = value
}
return _node, _spec
}
// MetricCreateBulk is the builder for creating many Metric entities in bulk.
type MetricCreateBulk struct {
config
err error
builders []*MetricCreate
}
// Save creates the Metric entities in the database.
func (mcb *MetricCreateBulk) Save(ctx context.Context) ([]*Metric, error) {
if mcb.err != nil {
return nil, mcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(mcb.builders))
nodes := make([]*Metric, len(mcb.builders))
mutators := make([]Mutator, len(mcb.builders))
for i := range mcb.builders {
func(i int, root context.Context) {
builder := mcb.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*MetricMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
if err := builder.check(); err != nil {
return nil, err
}
builder.mutation = mutation
var err error
nodes[i], specs[i] = builder.createSpec()
if i < len(mutators)-1 {
_, err = mutators[i+1].Mutate(root, mcb.builders[i+1].mutation)
} else {
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
// Invoke the actual operation on the latest mutation in the chain.
if err = sqlgraph.BatchCreate(ctx, mcb.driver, spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
}
}
if err != nil {
return nil, err
}
mutation.id = &nodes[i].ID
if specs[i].ID.Value != nil {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(id)
}
mutation.done = true
return nodes[i], nil
})
for i := len(builder.hooks) - 1; i >= 0; i-- {
mut = builder.hooks[i](mut)
}
mutators[i] = mut
}(i, ctx)
}
if len(mutators) > 0 {
if _, err := mutators[0].Mutate(ctx, mcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (mcb *MetricCreateBulk) SaveX(ctx context.Context) []*Metric {
v, err := mcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (mcb *MetricCreateBulk) Exec(ctx context.Context) error {
_, err := mcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (mcb *MetricCreateBulk) ExecX(ctx context.Context) {
if err := mcb.Exec(ctx); err != nil {
panic(err)
}
}

View file

@ -0,0 +1,88 @@
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/metric"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
)
// MetricDelete is the builder for deleting a Metric entity.
type MetricDelete struct {
config
hooks []Hook
mutation *MetricMutation
}
// Where appends a list predicates to the MetricDelete builder.
func (md *MetricDelete) Where(ps ...predicate.Metric) *MetricDelete {
md.mutation.Where(ps...)
return md
}
// Exec executes the deletion query and returns how many vertices were deleted.
func (md *MetricDelete) Exec(ctx context.Context) (int, error) {
return withHooks(ctx, md.sqlExec, md.mutation, md.hooks)
}
// ExecX is like Exec, but panics if an error occurs.
func (md *MetricDelete) ExecX(ctx context.Context) int {
n, err := md.Exec(ctx)
if err != nil {
panic(err)
}
return n
}
func (md *MetricDelete) sqlExec(ctx context.Context) (int, error) {
_spec := sqlgraph.NewDeleteSpec(metric.Table, sqlgraph.NewFieldSpec(metric.FieldID, field.TypeInt))
if ps := md.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
affected, err := sqlgraph.DeleteNodes(ctx, md.driver, _spec)
if err != nil && sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
md.mutation.done = true
return affected, err
}
// MetricDeleteOne is the builder for deleting a single Metric entity.
type MetricDeleteOne struct {
md *MetricDelete
}
// Where appends a list predicates to the MetricDelete builder.
func (mdo *MetricDeleteOne) Where(ps ...predicate.Metric) *MetricDeleteOne {
mdo.md.mutation.Where(ps...)
return mdo
}
// Exec executes the deletion query.
func (mdo *MetricDeleteOne) Exec(ctx context.Context) error {
n, err := mdo.md.Exec(ctx)
switch {
case err != nil:
return err
case n == 0:
return &NotFoundError{metric.Label}
default:
return nil
}
}
// ExecX is like Exec, but panics if an error occurs.
func (mdo *MetricDeleteOne) ExecX(ctx context.Context) {
if err := mdo.Exec(ctx); err != nil {
panic(err)
}
}

View file

@ -0,0 +1,526 @@
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"math"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/metric"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
)
// MetricQuery is the builder for querying Metric entities.
type MetricQuery struct {
config
ctx *QueryContext
order []metric.OrderOption
inters []Interceptor
predicates []predicate.Metric
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Where adds a new predicate for the MetricQuery builder.
func (mq *MetricQuery) Where(ps ...predicate.Metric) *MetricQuery {
mq.predicates = append(mq.predicates, ps...)
return mq
}
// Limit the number of records to be returned by this query.
func (mq *MetricQuery) Limit(limit int) *MetricQuery {
mq.ctx.Limit = &limit
return mq
}
// Offset to start from.
func (mq *MetricQuery) Offset(offset int) *MetricQuery {
mq.ctx.Offset = &offset
return mq
}
// Unique configures the query builder to filter duplicate records on query.
// By default, unique is set to true, and can be disabled using this method.
func (mq *MetricQuery) Unique(unique bool) *MetricQuery {
mq.ctx.Unique = &unique
return mq
}
// Order specifies how the records should be ordered.
func (mq *MetricQuery) Order(o ...metric.OrderOption) *MetricQuery {
mq.order = append(mq.order, o...)
return mq
}
// First returns the first Metric entity from the query.
// Returns a *NotFoundError when no Metric was found.
func (mq *MetricQuery) First(ctx context.Context) (*Metric, error) {
nodes, err := mq.Limit(1).All(setContextOp(ctx, mq.ctx, "First"))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{metric.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (mq *MetricQuery) FirstX(ctx context.Context) *Metric {
node, err := mq.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first Metric ID from the query.
// Returns a *NotFoundError when no Metric ID was found.
func (mq *MetricQuery) FirstID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = mq.Limit(1).IDs(setContextOp(ctx, mq.ctx, "FirstID")); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{metric.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (mq *MetricQuery) FirstIDX(ctx context.Context) int {
id, err := mq.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single Metric entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one Metric entity is found.
// Returns a *NotFoundError when no Metric entities are found.
func (mq *MetricQuery) Only(ctx context.Context) (*Metric, error) {
nodes, err := mq.Limit(2).All(setContextOp(ctx, mq.ctx, "Only"))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{metric.Label}
default:
return nil, &NotSingularError{metric.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (mq *MetricQuery) OnlyX(ctx context.Context) *Metric {
node, err := mq.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only Metric ID in the query.
// Returns a *NotSingularError when more than one Metric ID is found.
// Returns a *NotFoundError when no entities are found.
func (mq *MetricQuery) OnlyID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = mq.Limit(2).IDs(setContextOp(ctx, mq.ctx, "OnlyID")); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{metric.Label}
default:
err = &NotSingularError{metric.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (mq *MetricQuery) OnlyIDX(ctx context.Context) int {
id, err := mq.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of Metrics.
func (mq *MetricQuery) All(ctx context.Context) ([]*Metric, error) {
ctx = setContextOp(ctx, mq.ctx, "All")
if err := mq.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*Metric, *MetricQuery]()
return withInterceptors[[]*Metric](ctx, mq, qr, mq.inters)
}
// AllX is like All, but panics if an error occurs.
func (mq *MetricQuery) AllX(ctx context.Context) []*Metric {
nodes, err := mq.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of Metric IDs.
func (mq *MetricQuery) IDs(ctx context.Context) (ids []int, err error) {
if mq.ctx.Unique == nil && mq.path != nil {
mq.Unique(true)
}
ctx = setContextOp(ctx, mq.ctx, "IDs")
if err = mq.Select(metric.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (mq *MetricQuery) IDsX(ctx context.Context) []int {
ids, err := mq.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (mq *MetricQuery) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, mq.ctx, "Count")
if err := mq.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, mq, querierCount[*MetricQuery](), mq.inters)
}
// CountX is like Count, but panics if an error occurs.
func (mq *MetricQuery) CountX(ctx context.Context) int {
count, err := mq.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (mq *MetricQuery) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, mq.ctx, "Exist")
switch _, err := mq.FirstID(ctx); {
case IsNotFound(err):
return false, nil
case err != nil:
return false, fmt.Errorf("ent: check existence: %w", err)
default:
return true, nil
}
}
// ExistX is like Exist, but panics if an error occurs.
func (mq *MetricQuery) ExistX(ctx context.Context) bool {
exist, err := mq.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the MetricQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (mq *MetricQuery) Clone() *MetricQuery {
if mq == nil {
return nil
}
return &MetricQuery{
config: mq.config,
ctx: mq.ctx.Clone(),
order: append([]metric.OrderOption{}, mq.order...),
inters: append([]Interceptor{}, mq.inters...),
predicates: append([]predicate.Metric{}, mq.predicates...),
// clone intermediate query.
sql: mq.sql.Clone(),
path: mq.path,
}
}
// GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: count, max, mean, min, sum.
//
// Example:
//
// var v []struct {
// GeneratedType metric.GeneratedType `json:"generated_type,omitempty"`
// Count int `json:"count,omitempty"`
// }
//
// client.Metric.Query().
// GroupBy(metric.FieldGeneratedType).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (mq *MetricQuery) GroupBy(field string, fields ...string) *MetricGroupBy {
mq.ctx.Fields = append([]string{field}, fields...)
grbuild := &MetricGroupBy{build: mq}
grbuild.flds = &mq.ctx.Fields
grbuild.label = metric.Label
grbuild.scan = grbuild.Scan
return grbuild
}
// Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity.
//
// Example:
//
// var v []struct {
// GeneratedType metric.GeneratedType `json:"generated_type,omitempty"`
// }
//
// client.Metric.Query().
// Select(metric.FieldGeneratedType).
// Scan(ctx, &v)
func (mq *MetricQuery) Select(fields ...string) *MetricSelect {
mq.ctx.Fields = append(mq.ctx.Fields, fields...)
sbuild := &MetricSelect{MetricQuery: mq}
sbuild.label = metric.Label
sbuild.flds, sbuild.scan = &mq.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a MetricSelect configured with the given aggregations.
func (mq *MetricQuery) Aggregate(fns ...AggregateFunc) *MetricSelect {
return mq.Select().Aggregate(fns...)
}
func (mq *MetricQuery) prepareQuery(ctx context.Context) error {
for _, inter := range mq.inters {
if inter == nil {
return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
}
if trv, ok := inter.(Traverser); ok {
if err := trv.Traverse(ctx, mq); err != nil {
return err
}
}
}
for _, f := range mq.ctx.Fields {
if !metric.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if mq.path != nil {
prev, err := mq.path(ctx)
if err != nil {
return err
}
mq.sql = prev
}
return nil
}
func (mq *MetricQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Metric, error) {
var (
nodes = []*Metric{}
_spec = mq.querySpec()
)
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*Metric).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &Metric{config: mq.config}
nodes = append(nodes, node)
return node.assignValues(columns, values)
}
for i := range hooks {
hooks[i](ctx, _spec)
}
if err := sqlgraph.QueryNodes(ctx, mq.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
return nodes, nil
}
func (mq *MetricQuery) sqlCount(ctx context.Context) (int, error) {
_spec := mq.querySpec()
_spec.Node.Columns = mq.ctx.Fields
if len(mq.ctx.Fields) > 0 {
_spec.Unique = mq.ctx.Unique != nil && *mq.ctx.Unique
}
return sqlgraph.CountNodes(ctx, mq.driver, _spec)
}
func (mq *MetricQuery) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(metric.Table, metric.Columns, sqlgraph.NewFieldSpec(metric.FieldID, field.TypeInt))
_spec.From = mq.sql
if unique := mq.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if mq.path != nil {
_spec.Unique = true
}
if fields := mq.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, metric.FieldID)
for i := range fields {
if fields[i] != metric.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
}
if ps := mq.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := mq.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := mq.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := mq.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (mq *MetricQuery) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(mq.driver.Dialect())
t1 := builder.Table(metric.Table)
columns := mq.ctx.Fields
if len(columns) == 0 {
columns = metric.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if mq.sql != nil {
selector = mq.sql
selector.Select(selector.Columns(columns...)...)
}
if mq.ctx.Unique != nil && *mq.ctx.Unique {
selector.Distinct()
}
for _, p := range mq.predicates {
p(selector)
}
for _, p := range mq.order {
p(selector)
}
if offset := mq.ctx.Offset; offset != nil {
// limit is mandatory for offset clause. We start
// with default value, and override it below if needed.
selector.Offset(*offset).Limit(math.MaxInt32)
}
if limit := mq.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// MetricGroupBy is the group-by builder for Metric entities.
type MetricGroupBy struct {
selector
build *MetricQuery
}
// Aggregate adds the given aggregation functions to the group-by query.
func (mgb *MetricGroupBy) Aggregate(fns ...AggregateFunc) *MetricGroupBy {
mgb.fns = append(mgb.fns, fns...)
return mgb
}
// Scan applies the selector query and scans the result into the given value.
func (mgb *MetricGroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, mgb.build.ctx, "GroupBy")
if err := mgb.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*MetricQuery, *MetricGroupBy](ctx, mgb.build, mgb, mgb.build.inters, v)
}
func (mgb *MetricGroupBy) sqlScan(ctx context.Context, root *MetricQuery, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(mgb.fns))
for _, fn := range mgb.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*mgb.flds)+len(mgb.fns))
for _, f := range *mgb.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*mgb.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := mgb.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// MetricSelect is the builder for selecting fields of Metric entities.
type MetricSelect struct {
*MetricQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (ms *MetricSelect) Aggregate(fns ...AggregateFunc) *MetricSelect {
ms.fns = append(ms.fns, fns...)
return ms
}
// Scan applies the selector query and scans the result into the given value.
func (ms *MetricSelect) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, ms.ctx, "Select")
if err := ms.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*MetricQuery, *MetricSelect](ctx, ms.MetricQuery, ms, ms.inters, v)
}
func (ms *MetricSelect) sqlScan(ctx context.Context, root *MetricQuery, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(ms.fns))
for _, fn := range ms.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*ms.selector.flds); {
case n == 0 && len(aggregation) > 0:
selector.Select(aggregation...)
case n != 0 && len(aggregation) > 0:
selector.AppendSelect(aggregation...)
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := ms.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}

View file

@ -0,0 +1,228 @@
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/metric"
"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
)
// MetricUpdate is the builder for updating Metric entities.
type MetricUpdate struct {
config
hooks []Hook
mutation *MetricMutation
}
// Where appends a list predicates to the MetricUpdate builder.
func (mu *MetricUpdate) Where(ps ...predicate.Metric) *MetricUpdate {
mu.mutation.Where(ps...)
return mu
}
// SetPushedAt sets the "pushed_at" field.
func (mu *MetricUpdate) SetPushedAt(t time.Time) *MetricUpdate {
mu.mutation.SetPushedAt(t)
return mu
}
// SetNillablePushedAt sets the "pushed_at" field if the given value is not nil.
func (mu *MetricUpdate) SetNillablePushedAt(t *time.Time) *MetricUpdate {
if t != nil {
mu.SetPushedAt(*t)
}
return mu
}
// ClearPushedAt clears the value of the "pushed_at" field.
func (mu *MetricUpdate) ClearPushedAt() *MetricUpdate {
mu.mutation.ClearPushedAt()
return mu
}
// Mutation returns the MetricMutation object of the builder.
func (mu *MetricUpdate) Mutation() *MetricMutation {
return mu.mutation
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (mu *MetricUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, mu.sqlSave, mu.mutation, mu.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (mu *MetricUpdate) SaveX(ctx context.Context) int {
affected, err := mu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (mu *MetricUpdate) Exec(ctx context.Context) error {
_, err := mu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (mu *MetricUpdate) ExecX(ctx context.Context) {
if err := mu.Exec(ctx); err != nil {
panic(err)
}
}
func (mu *MetricUpdate) sqlSave(ctx context.Context) (n int, err error) {
_spec := sqlgraph.NewUpdateSpec(metric.Table, metric.Columns, sqlgraph.NewFieldSpec(metric.FieldID, field.TypeInt))
if ps := mu.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := mu.mutation.PushedAt(); ok {
_spec.SetField(metric.FieldPushedAt, field.TypeTime, value)
}
if mu.mutation.PushedAtCleared() {
_spec.ClearField(metric.FieldPushedAt, field.TypeTime)
}
if n, err = sqlgraph.UpdateNodes(ctx, mu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{metric.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
mu.mutation.done = true
return n, nil
}
// MetricUpdateOne is the builder for updating a single Metric entity.
type MetricUpdateOne struct {
config
fields []string
hooks []Hook
mutation *MetricMutation
}
// SetPushedAt sets the "pushed_at" field.
func (muo *MetricUpdateOne) SetPushedAt(t time.Time) *MetricUpdateOne {
muo.mutation.SetPushedAt(t)
return muo
}
// SetNillablePushedAt sets the "pushed_at" field if the given value is not nil.
func (muo *MetricUpdateOne) SetNillablePushedAt(t *time.Time) *MetricUpdateOne {
if t != nil {
muo.SetPushedAt(*t)
}
return muo
}
// ClearPushedAt clears the value of the "pushed_at" field.
func (muo *MetricUpdateOne) ClearPushedAt() *MetricUpdateOne {
muo.mutation.ClearPushedAt()
return muo
}
// Mutation returns the MetricMutation object of the builder.
func (muo *MetricUpdateOne) Mutation() *MetricMutation {
return muo.mutation
}
// Where appends a list predicates to the MetricUpdate builder.
func (muo *MetricUpdateOne) Where(ps ...predicate.Metric) *MetricUpdateOne {
muo.mutation.Where(ps...)
return muo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (muo *MetricUpdateOne) Select(field string, fields ...string) *MetricUpdateOne {
muo.fields = append([]string{field}, fields...)
return muo
}
// Save executes the query and returns the updated Metric entity.
func (muo *MetricUpdateOne) Save(ctx context.Context) (*Metric, error) {
return withHooks(ctx, muo.sqlSave, muo.mutation, muo.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (muo *MetricUpdateOne) SaveX(ctx context.Context) *Metric {
node, err := muo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (muo *MetricUpdateOne) Exec(ctx context.Context) error {
_, err := muo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (muo *MetricUpdateOne) ExecX(ctx context.Context) {
if err := muo.Exec(ctx); err != nil {
panic(err)
}
}
func (muo *MetricUpdateOne) sqlSave(ctx context.Context) (_node *Metric, err error) {
_spec := sqlgraph.NewUpdateSpec(metric.Table, metric.Columns, sqlgraph.NewFieldSpec(metric.FieldID, field.TypeInt))
id, ok := muo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Metric.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := muo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, metric.FieldID)
for _, f := range fields {
if !metric.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != metric.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := muo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := muo.mutation.PushedAt(); ok {
_spec.SetField(metric.FieldPushedAt, field.TypeTime, value)
}
if muo.mutation.PushedAtCleared() {
_spec.ClearField(metric.FieldPushedAt, field.TypeTime)
}
_node = &Metric{config: muo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, muo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{metric.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
muo.mutation.done = true
return _node, nil
}

View file

@ -70,6 +70,9 @@ var (
{Name: "version", Type: field.TypeString, Nullable: true},
{Name: "last_pull", Type: field.TypeTime, Nullable: true},
{Name: "auth_type", Type: field.TypeString, Default: "api-key"},
{Name: "osname", Type: field.TypeString, Nullable: true},
{Name: "osversion", Type: field.TypeString, Nullable: true},
{Name: "featureflags", Type: field.TypeString, Nullable: true},
}
// BouncersTable holds the schema information for the "bouncers" table.
BouncersTable = &schema.Table{
@ -204,6 +207,11 @@ var (
{Name: "is_validated", Type: field.TypeBool, Default: false},
{Name: "status", Type: field.TypeString, Nullable: true},
{Name: "auth_type", Type: field.TypeString, Default: "password"},
{Name: "osname", Type: field.TypeString, Nullable: true},
{Name: "osversion", Type: field.TypeString, Nullable: true},
{Name: "featureflags", Type: field.TypeString, Nullable: true},
{Name: "hubstate", Type: field.TypeJSON, Nullable: true},
{Name: "datasources", Type: field.TypeJSON, Nullable: true},
}
// MachinesTable holds the schema information for the "machines" table.
MachinesTable = &schema.Table{
@ -241,6 +249,28 @@ var (
},
},
}
// MetricsColumns holds the columns for the "metrics" table.
MetricsColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "generated_type", Type: field.TypeEnum, Enums: []string{"LP", "RC"}},
{Name: "generated_by", Type: field.TypeString},
{Name: "collected_at", Type: field.TypeTime},
{Name: "pushed_at", Type: field.TypeTime, Nullable: true},
{Name: "payload", Type: field.TypeString},
}
// MetricsTable holds the schema information for the "metrics" table.
MetricsTable = &schema.Table{
Name: "metrics",
Columns: MetricsColumns,
PrimaryKey: []*schema.Column{MetricsColumns[0]},
Indexes: []*schema.Index{
{
Name: "metric_generated_type_generated_by_collected_at",
Unique: true,
Columns: []*schema.Column{MetricsColumns[1], MetricsColumns[2], MetricsColumns[3]},
},
},
}
// Tables holds all the tables in the schema.
Tables = []*schema.Table{
AlertsTable,
@ -251,6 +281,7 @@ var (
LocksTable,
MachinesTable,
MetaTable,
MetricsTable,
}
)

File diff suppressed because it is too large Load diff

View file

@ -29,3 +29,6 @@ type Machine func(*sql.Selector)
// Meta is the predicate function for meta builders.
type Meta func(*sql.Selector)
// Metric is the predicate function for metric builders.
type Metric func(*sql.Selector)

View file

@ -30,6 +30,9 @@ func (Bouncer) Fields() []ent.Field {
field.String("version").Optional().StructTag(`json:"version"`),
field.Time("last_pull").Nillable().Optional().StructTag(`json:"last_pull"`),
field.String("auth_type").StructTag(`json:"auth_type"`).Default(types.ApiKeyAuthType),
field.String("osname").Optional(),
field.String("osversion").Optional(),
field.String("featureflags").Optional(),
}
}

View file

@ -8,6 +8,12 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types"
)
// ItemState is defined here instead of using pkg/models/HubItem to avoid introducing a dependency
type ItemState struct {
Status string `json:"status,omitempty"`
Version string `json:"version,omitempty"`
}
// Machine holds the schema definition for the Machine entity.
type Machine struct {
ent.Schema
@ -38,6 +44,11 @@ func (Machine) Fields() []ent.Field {
Default(false),
field.String("status").Optional(),
field.String("auth_type").Default(types.PasswordAuthType).StructTag(`json:"auth_type"`),
field.String("osname").Optional(),
field.String("osversion").Optional(),
field.String("featureflags").Optional(),
field.JSON("hubstate", map[string]ItemState{}).Optional(),
field.JSON("datasources", map[string]int64{}).Optional(),
}
}

View file

@ -0,0 +1,43 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)
// Metric is actually a set of metrics collected by a device
// (logprocessor, bouncer, etc) at a given time.
type Metric struct {
ent.Schema
}
func (Metric) Fields() []ent.Field {
return []ent.Field{
field.Enum("generated_type").
Values("LP", "RC").
Immutable().
Comment("Type of the metrics source: LP=logprocessor, RC=remediation"),
field.String("generated_by").
Immutable().
Comment("Source of the metrics: machine id, bouncer name...\nIt must come from the auth middleware."),
field.Time("collected_at").
Immutable().
Comment("When the metrics are collected/calculated at the source"),
field.Time("pushed_at").
Nillable().
Optional().
Comment("When the metrics are sent to the console"),
field.String("payload").
Immutable().
Comment("The actual metrics (item0)"),
}
}
func (Metric) Indexes() []ent.Index {
return []ent.Index{
// Don't store the same metrics multiple times.
index.Fields("generated_type", "generated_by", "collected_at").
Unique(),
}
}

View file

@ -28,6 +28,8 @@ type Tx struct {
Machine *MachineClient
// Meta is the client for interacting with the Meta builders.
Meta *MetaClient
// Metric is the client for interacting with the Metric builders.
Metric *MetricClient
// lazily loaded.
client *Client
@ -167,6 +169,7 @@ func (tx *Tx) init() {
tx.Lock = NewLockClient(tx.config)
tx.Machine = NewMachineClient(tx.config)
tx.Meta = NewMetaClient(tx.config)
tx.Metric = NewMetricClient(tx.config)
}
// txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation.