Documentation
¶
Index ¶
- Constants
- Variables
- func AppendTagValueToFlag(tx *gorm.DB, flagID uint, value string) error
- func ApplyFlagTemplate(tx *gorm.DB, flagID uint, template Flag) error
- func CreateFlagEntityType(db *gorm.DB, key string) error
- func CreateFlagKey(key string) (string, error)
- func GetDB() *gorm.DB
- func NewSQLiteDB(filePath string) *gorm.DB
- func NewTestDB() *gorm.DB
- func PopulateTestDB(flag Flag) *gorm.DB
- func PreloadConstraintsDistribution(db *gorm.DB) *gorm.DB
- func PreloadFlagTags(db *gorm.DB) *gorm.DB
- func PreloadSegmentsVariantsTags(db *gorm.DB) *gorm.DB
- func SaveFlagSnapshot(db *gorm.DB, flagID uint, updatedBy string, operation notification.Operation, ...)
- func TestApplyFlagTemplate_CopiesDistributionBitmap(t *testing.T)
- type Attachment
- func (a Attachment) Get(key string) any
- func (a Attachment) GetBool(key string) bool
- func (a Attachment) GetFloat64(key string) float64
- func (a Attachment) GetInt(key string) int
- func (a Attachment) GetString(key string) string
- func (a *Attachment) Scan(value any) error
- func (a Attachment) Value() (driver.Value, error)
- type Constraint
- type ConstraintArray
- type Distribution
- type DistributionArray
- type DistributionDebugLog
- type Flag
- type FlagEntityType
- type FlagEvaluation
- type FlagSnapshot
- type HourlyEvent
- type Logger
- func (l *Logger) Error(ctx context.Context, s string, args ...any)
- func (l *Logger) Info(ctx context.Context, s string, args ...any)
- func (l *Logger) LogMode(level gorm_logger.LogLevel) gorm_logger.Interface
- func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
- func (l *Logger) Warn(ctx context.Context, s string, args ...any)
- type Segment
- type SegmentEvaluation
- type SnapshotNotification
- type Tag
- type User
- type Variant
Constants ¶
const ( // TotalBucketNum represents how many buckets we can use to determine the consistent hashing // distribution and rollout TotalBucketNum uint = 1000 // PercentMultiplier implies that the multiplier between percentage (100) and TotalBucketNum PercentMultiplier uint = TotalBucketNum / uint(100) )
const SegmentDefaultRank = uint(999)
SegmentDefaultRank is the default rank when we create the segment
Variables ¶
var AutoMigrateTables = []any{ Flag{}, Constraint{}, Distribution{}, FlagSnapshot{}, Segment{}, User{}, Variant{}, Tag{}, FlagEntityType{}, HourlyEvent{}, }
AutoMigrateTables stores the entity tables that we can auto migrate in gorm
var OperatorToExprMap = map[string]string{ models.ConstraintOperatorEQ: "==", models.ConstraintOperatorNEQ: "!=", models.ConstraintOperatorLT: "<", models.ConstraintOperatorLTE: "<=", models.ConstraintOperatorGT: ">", models.ConstraintOperatorGTE: ">=", models.ConstraintOperatorEREG: "=~", models.ConstraintOperatorNEREG: "!~", models.ConstraintOperatorIN: "IN", models.ConstraintOperatorNOTIN: "NOT IN", models.ConstraintOperatorCONTAINS: "CONTAINS", models.ConstraintOperatorNOTCONTAINS: "NOT CONTAINS", }
OperatorToExprMap maps from the swagger model operator to condition operator
Functions ¶
func AppendTagValueToFlag ¶
AppendTagValueToFlag finds or creates a tag by value and associates it with the flag.
func ApplyFlagTemplate ¶
ApplyFlagTemplate persists a template's variants, segments, constraints, distributions, and tags onto flagID. Used for create templates (e.g. simple_boolean_flag) and for duplicating an existing flag's graph.
func CreateFlagEntityType ¶
CreateFlagEntityType creates the FlagEntityType if not exists
func CreateFlagKey ¶
CreateFlagKey creates the key based on the given key
func NewSQLiteDB ¶
NewSQLiteDB creates a new sqlite db useful for backup exports and unit tests
func PreloadConstraintsDistribution ¶
PreloadConstraintsDistribution preloads constraints and distributions for segment
func PreloadSegmentsVariantsTags ¶
PreloadSegmentsVariantsTags preloads segments, variants and tags for flag
func SaveFlagSnapshot ¶
func SaveFlagSnapshot(db *gorm.DB, flagID uint, updatedBy string, operation notification.Operation, componentType notification.ComponentType, componentID uint, componentKey string)
SaveFlagSnapshot saves the Flag Snapshot and sends a notification in its own transaction.
Types ¶
type Attachment ¶
Attachment supports dynamic configuration in variant
func (Attachment) GetBool ¶
func (a Attachment) GetBool(key string) bool
GetBool returns the bool value of the key
func (Attachment) GetFloat64 ¶
func (a Attachment) GetFloat64(key string) float64
GetFloat64 returns the float64 value of the key
func (Attachment) GetInt ¶
func (a Attachment) GetInt(key string) int
GetInt returns the int value of the key
func (Attachment) GetString ¶
func (a Attachment) GetString(key string) string
GetString returns the string value of the key
func (*Attachment) Scan ¶
func (a *Attachment) Scan(value any) error
Scan implements scanner interface
type Constraint ¶
type Constraint struct {
gorm.Model
SegmentID uint `gorm:"index:idx_constraint_segmentid"`
Property string
Operator string
Value string `gorm:"type:text"`
}
Constraint is the unit of constraints
func (*Constraint) ToExpr ¶
func (c *Constraint) ToExpr() (conditions.Expr, error)
ToExpr transfer the constraint to conditions.Expr for evaluation
type ConstraintArray ¶
type ConstraintArray []Constraint
ConstraintArray is an array of Constraint
func (ConstraintArray) ToExpr ¶
func (cs ConstraintArray) ToExpr() (conditions.Expr, error)
ToExpr maps ConstraintArray to expr by joining 'AND'
type Distribution ¶
type Distribution struct {
gorm.Model
SegmentID uint `gorm:"index:idx_distribution_segmentid"`
VariantID uint `gorm:"index:idx_distribution_variantid"`
VariantKey string
Percent uint // Percent is an uint from 0 to 100, percent is always derived from Bitmap
Bitmap string `gorm:"type:text" json:"-"`
}
Distribution is the struct represents distribution under segment and links to variant
type DistributionArray ¶
type DistributionArray struct {
VariantIDs []uint
PercentsAccumulated []int // useful for binary search to find the rollout variant
}
DistributionArray is useful for faster evaluation
func (DistributionArray) Rollout ¶
func (d DistributionArray) Rollout(entityID string, salt string, rolloutPercent uint, withDebug bool) (variantID *uint, msg string)
Rollout rolls out the entity based on rolloutPercent. When withDebug is false, the returned message is empty and debug formatting is skipped (hot eval path).
type DistributionDebugLog ¶
type DistributionDebugLog struct {
BucketNum uint
DistributionArray DistributionArray
VariantID uint
RolloutPercent uint
}
DistributionDebugLog is useful for making debug logs
type Flag ¶
type Flag struct {
gorm.Model
Key string `gorm:"type:varchar(64);uniqueIndex:idx_flag_key"`
Description string `gorm:"type:text"`
CreatedBy string
UpdatedBy string
Enabled bool
Segments []Segment
Variants []Variant
Tags []Tag `gorm:"many2many:flags_tags;"`
SnapshotID uint
Notes string `gorm:"type:text"`
DataRecordsEnabled bool
EntityType string
FlagEvaluation FlagEvaluation `gorm:"-" json:"-"`
}
Flag is the unit of flags
func SimpleBooleanFlagTemplate ¶
func SimpleBooleanFlagTemplate() Flag
SimpleBooleanFlagTemplate is the built-in starter template (variant "on", 100% rollout). Only nested Variants/Segments are set; ApplyFlagTemplate ignores flag-level scalars and IDs.
func SourceFlagTemplate ¶
SourceFlagTemplate builds a template from an existing flag for cloning onto another flag. Variant keys and distribution variant keys are preserved; entity IDs and flag scalars are omitted.
func (*Flag) PreloadTags ¶
PreloadTags preloads the tags into flags
func (*Flag) PrepareEvaluation ¶
PrepareEvaluation prepares the information for evaluation
type FlagEntityType ¶
type FlagEntityType struct {
gorm.Model
Key string `gorm:"type:varchar(64);uniqueIndex:flag_entity_type_key"`
}
FlagEntityType is the entity_type that will overwrite into evaluation logs.
type FlagEvaluation ¶
type FlagEvaluation struct {
VariantsMap map[uint]*Variant
TagValues []string // denormalized tag values for eval results
}
FlagEvaluation is a struct that holds the necessary info for evaluation
type FlagSnapshot ¶
type FlagSnapshot struct {
gorm.Model
FlagID uint `gorm:"index:idx_flagsnapshot_flagid"`
UpdatedBy string
Flag []byte `gorm:"type:text"`
}
FlagSnapshot is the snapshot of a flag Any change of the flag will create a new snapshot
type HourlyEvent ¶
type HourlyEvent struct {
ID uint `gorm:"primaryKey;autoIncrement"`
FlagID int64 `gorm:"not null;uniqueIndex:idx_datar_hourly,priority:1"`
BucketHour time.Time `gorm:"not null;uniqueIndex:idx_datar_hourly,priority:2"`
VariantID int64 `gorm:"not null;default:0;uniqueIndex:idx_datar_hourly,priority:3"`
SegmentID int64 `gorm:"not null;default:0;uniqueIndex:idx_datar_hourly,priority:4"`
EvalCount int32 `gorm:"not null;default:0"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
HourlyEvent represents one aggregate row of evaluation counts per hour. The natural key is (flag_id, variant_id, segment_id, bucket_hour). A surrogate auto-increment PK allows adding columns later without table rebuild.
func (HourlyEvent) TableName ¶
func (HourlyEvent) TableName() string
TableName specifies the table name for GORM.
type Logger ¶
type Logger struct {
LogLevel gorm_logger.LogLevel
SlowThreshold time.Duration
IgnoreRecordNotFoundError bool
}
func (*Logger) LogMode ¶
func (l *Logger) LogMode(level gorm_logger.LogLevel) gorm_logger.Interface
type Segment ¶
type Segment struct {
gorm.Model
FlagID uint `gorm:"index:idx_segment_flagid"`
Description string `gorm:"type:text"`
Rank uint
RolloutPercent uint
Constraints ConstraintArray
Distributions []Distribution
// Purely for evaluation
SegmentEvaluation SegmentEvaluation `gorm:"-" json:"-"`
}
Segment is the unit of segmentation
func (*Segment) PrepareEvaluation ¶
PrepareEvaluation prepares the segment for evaluation by parsing constraints and denormalize distributions
type SegmentEvaluation ¶
type SegmentEvaluation struct {
ConditionsExpr conditions.Expr
DistributionArray DistributionArray
FlagIDStr string // pre-formatted flagID string used as salt in rollout
}
SegmentEvaluation is a struct that holds the necessary info for evaluation
type SnapshotNotification ¶
type SnapshotNotification struct {
// contains filtered or unexported fields
}
SnapshotNotification is an opaque handle returned from WriteFlagSnapshotTx. Call NotifyAfterCommit after the outer transaction commits; do not inspect or construct it outside entity.
func WriteFlagSnapshotTx ¶
func WriteFlagSnapshotTx( tx *gorm.DB, flagID uint, updatedBy string, ) (SnapshotNotification, error)
WriteFlagSnapshotTx records a flag snapshot using tx. The caller must Commit or Rollback.
func (SnapshotNotification) NotifyAfterCommit ¶
func (n SnapshotNotification) NotifyAfterCommit( flagID uint, updatedBy string, operation notification.Operation, componentType notification.ComponentType, componentID uint, componentKey string, )
NotifyAfterCommit sends webhook/metrics after the outer transaction committed.
type Tag ¶
type Tag struct {
gorm.Model
Value string `gorm:"type:varchar(64);uniqueIndex:idx_tag_value"`
Flags []*Flag `gorm:"many2many:flags_tags;"`
}
Tag is a descriptive identifier given to ease searchability