-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsplitdecisions.go
More file actions
132 lines (117 loc) · 3.79 KB
/
splitdecisions.go
File metadata and controls
132 lines (117 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package splitdecisions
import (
"fmt"
"github.com/Betterment/testtrack-cli/migrations"
"github.com/Betterment/testtrack-cli/serializers"
"github.com/Betterment/testtrack-cli/splits"
"github.com/Betterment/testtrack-cli/validations"
)
// SplitDecision represents a feature we're marking (un)completed
type SplitDecision struct {
migrationVersion *string
split *string
variant *string
}
// New returns a migration object
func New(split, variant *string) (migrations.IMigration, error) {
migrationVersion, err := migrations.GenerateMigrationVersion()
if err != nil {
return nil, err
}
return &SplitDecision{
migrationVersion: migrationVersion,
split: split,
variant: variant,
}, nil
}
// FromFile reifies a migration from the yaml serializable representation
func FromFile(migrationVersion *string, serializable *serializers.SplitDecision) migrations.IMigration {
return &SplitDecision{
migrationVersion: migrationVersion,
split: &serializable.Split,
variant: &serializable.Variant,
}
}
// Validate validates that a migration may be persisted
func (s *SplitDecision) Validate() error {
return validations.Split("split", s.split)
}
// Filename generates a filename for this migration
func (s *SplitDecision) Filename() *string {
filename := fmt.Sprintf("%s_create_split_decision_%s.yml", *s.migrationVersion, *s.split)
return &filename
}
// File returns a serializable MigrationFile for this migration
func (s *SplitDecision) File() *serializers.MigrationFile {
return &serializers.MigrationFile{
SerializerVersion: serializers.SerializerVersion,
SplitDecision: &serializers.SplitDecision{
Split: *s.split,
Variant: *s.variant,
},
}
}
// SyncPath returns the server path to post the migration to
func (s *SplitDecision) SyncPath() string {
return "api/v2/migrations/split_decision"
}
// Serializable returns a JSON-serializable representation
func (s *SplitDecision) Serializable() interface{} {
return &serializers.SplitDecision{
Split: *s.split,
Variant: *s.variant,
}
}
// MigrationVersion returns the migration version
func (s *SplitDecision) MigrationVersion() *string {
return s.migrationVersion
}
// ResourceKey returns the natural key of the resource under migration
func (s *SplitDecision) ResourceKey() splits.SplitKey {
return splits.SplitKey(*s.split)
}
// SameResourceAs returns whether the migrations refer to the same TestTrack resource
func (s *SplitDecision) SameResourceAs(other migrations.IMigration) bool {
if otherS, ok := other.(splits.ISplitMigration); ok {
return otherS.ResourceKey() == s.ResourceKey()
}
return false
}
// ApplyToSchema applies a migrations changes to in-memory schema representation
func (s *SplitDecision) ApplyToSchema(schema *serializers.Schema, migrationRepo migrations.Repository, idempotently bool) error {
for i, candidate := range schema.Splits {
if candidate.Name == *s.split {
schema.Splits[i].Decided = true
weights, err := splits.NewWeights(candidate.Weights)
if err != nil {
return err
}
err = weights.ReweightToDecision(*s.variant)
if err != nil {
return fmt.Errorf("in split %s in schema: %w", *s.split, err)
}
schema.Splits[i].Weights = *weights
return nil
}
}
if s.migrationVersion != nil {
split := splits.MostRecentNamed(*s.split, *s.migrationVersion, migrationRepo)
if split != nil {
weights := split.Weights()
err := weights.ReweightToDecision(*s.variant)
if err != nil {
return fmt.Errorf("in most recent split migration %s: %w", *s.split, err)
}
schema.Splits = append(schema.Splits, serializers.SchemaSplit{
Name: *s.split,
Weights: *weights,
Decided: true,
})
return nil
}
}
if idempotently {
return nil
}
return fmt.Errorf("couldn't locate split %s in schema to decide", *s.split)
}