|
| 1 | +package data |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/deis/workflow-manager-api/rest" |
| 9 | +) |
| 10 | + |
| 11 | +type keyAndTime struct { |
| 12 | + key string |
| 13 | + time time.Time |
| 14 | +} |
| 15 | + |
| 16 | +func (k keyAndTime) String() string { |
| 17 | + return fmt.Sprintf("%s (%s)", k.key, k.time) |
| 18 | +} |
| 19 | + |
| 20 | +// ErrImpossibleFilter is the error returned when a caller tries to create a new ClusterAgeFilter |
| 21 | +// with parameters that would create a filter that is guaranteed to produce no results. One such |
| 22 | +// "impossible" query is a "created before" time is after a "created " time that is after (i.e. |
| 23 | +// an impossible filter). See the documentation on ClusterAgeFilter for examples of impossible |
| 24 | +// filters |
| 25 | +type ErrImpossibleFilter struct { |
| 26 | + vals []keyAndTime |
| 27 | + reason string |
| 28 | +} |
| 29 | + |
| 30 | +// Error is the error interface implementation |
| 31 | +func (e ErrImpossibleFilter) Error() string { |
| 32 | + strs := make([]string, len(e.vals)) |
| 33 | + for i := 0; i < len(e.vals); i++ { |
| 34 | + strs[i] = e.vals[i].String() |
| 35 | + } |
| 36 | + return fmt.Sprintf("impossible filter for keys/times (%s): %s", strings.Join(strs, ", "), e.reason) |
| 37 | +} |
| 38 | + |
| 39 | +// ClusterAgeFilter is the struct used to filter on cluster ages. It represents the conjunction |
| 40 | +// of all of its fields. For example: |
| 41 | +// |
| 42 | +// created_time<=CreatedBefore |
| 43 | +// AND |
| 44 | +// created_time>=CreatedAfter |
| 45 | +// AND |
| 46 | +// checked_in_time<=CheckedInBefore |
| 47 | +// AND |
| 48 | +// checked_in_time>=CheckedInAfter |
| 49 | +type ClusterAgeFilter struct { |
| 50 | + CheckedInBefore time.Time |
| 51 | + CheckedInAfter time.Time |
| 52 | + CreatedBefore time.Time |
| 53 | + CreatedAfter time.Time |
| 54 | +} |
| 55 | + |
| 56 | +// NewClusterAgeFilter returns a new ClusterAgeFilter if the given times can result in a valid |
| 57 | +// query that would return clusters. If not, returns nil and an ErrImpossibleFilter error |
| 58 | +func NewClusterAgeFilter( |
| 59 | + checkedInBefore, |
| 60 | + checkedInAfter, |
| 61 | + createdBefore, |
| 62 | + createdAfter time.Time, |
| 63 | +) (*ClusterAgeFilter, error) { |
| 64 | + candidate := ClusterAgeFilter{ |
| 65 | + CheckedInBefore: checkedInBefore, |
| 66 | + CheckedInAfter: checkedInAfter, |
| 67 | + CreatedBefore: createdBefore, |
| 68 | + CreatedAfter: createdAfter, |
| 69 | + } |
| 70 | + if err := candidate.checkValid(); err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + return &candidate, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (c ClusterAgeFilter) checkValid() error { |
| 77 | + if c.CreatedBefore.After(c.CheckedInBefore) { |
| 78 | + // you can't have clusters that were checked in before they were created |
| 79 | + return ErrImpossibleFilter{ |
| 80 | + vals: []keyAndTime{ |
| 81 | + keyAndTime{key: rest.CreatedBeforeQueryStringKey, time: c.CreatedBefore}, |
| 82 | + keyAndTime{key: rest.CheckedInBeforeQueryStringKey, time: c.CheckedInBefore}, |
| 83 | + }, |
| 84 | + reason: fmt.Sprintf( |
| 85 | + "%s needs to be greater than or equal to %s", |
| 86 | + rest.CheckedInBeforeQueryStringKey, |
| 87 | + rest.CreatedBeforeQueryStringKey, |
| 88 | + ), |
| 89 | + } |
| 90 | + } else if c.CheckedInAfter.After(c.CheckedInBefore) || c.CheckedInAfter.Equal(c.CheckedInBefore) { |
| 91 | + // you can't have clusters that were checked in before time T-1 |
| 92 | + // and at the same time checked in after time T+1 |
| 93 | + return ErrImpossibleFilter{ |
| 94 | + vals: []keyAndTime{ |
| 95 | + keyAndTime{key: rest.CheckedInBeforeQueryStringKey, time: c.CheckedInBefore}, |
| 96 | + keyAndTime{key: rest.CheckedInAfterQueryStringKey, time: c.CheckedInAfter}, |
| 97 | + }, |
| 98 | + reason: fmt.Sprintf( |
| 99 | + "%s needs to be greater than %s", |
| 100 | + rest.CheckedInBeforeQueryStringKey, |
| 101 | + rest.CheckedInAfterQueryStringKey, |
| 102 | + ), |
| 103 | + } |
| 104 | + } else if c.CreatedAfter.After(c.CreatedBefore) || c.CreatedAfter.Equal(c.CreatedBefore) { |
| 105 | + // you can't have clusters that were created after time T+1 |
| 106 | + // and at the same time created before time T-1 |
| 107 | + return ErrImpossibleFilter{ |
| 108 | + vals: []keyAndTime{ |
| 109 | + keyAndTime{key: rest.CreatedAfterQueryStringKey, time: c.CreatedAfter}, |
| 110 | + keyAndTime{key: rest.CreatedBeforeQueryStringKey, time: c.CreatedBefore}, |
| 111 | + }, |
| 112 | + reason: fmt.Sprintf( |
| 113 | + "%s needs to be greater than %s", |
| 114 | + rest.CreatedBeforeQueryStringKey, |
| 115 | + rest.CreatedAfterQueryStringKey, |
| 116 | + ), |
| 117 | + } |
| 118 | + } else if c.CheckedInBefore.Before(c.CreatedAfter) || c.CheckedInBefore.Equal(c.CreatedAfter) { |
| 119 | + // you can't have clusters that were checked in before time T-1 |
| 120 | + // and at the same time created after time T+1 |
| 121 | + return ErrImpossibleFilter{ |
| 122 | + vals: []keyAndTime{ |
| 123 | + keyAndTime{key: rest.CheckedInBeforeQueryStringKey, time: c.CheckedInBefore}, |
| 124 | + keyAndTime{key: rest.CreatedAfterQueryStringKey, time: c.CreatedAfter}, |
| 125 | + }, |
| 126 | + reason: fmt.Sprintf( |
| 127 | + "%s needs to be after %s", |
| 128 | + rest.CheckedInBeforeQueryStringKey, |
| 129 | + rest.CreatedAfterQueryStringKey, |
| 130 | + ), |
| 131 | + } |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func (c ClusterAgeFilter) checkedInBeforeTimestamp() string { |
| 137 | + return c.CheckedInBefore.Format(StdTimestampFmt) |
| 138 | +} |
| 139 | + |
| 140 | +func (c ClusterAgeFilter) checkedInAfterTimestamp() string { |
| 141 | + return c.CheckedInAfter.Format(StdTimestampFmt) |
| 142 | +} |
| 143 | + |
| 144 | +func (c ClusterAgeFilter) createdBeforeTimestamp() string { |
| 145 | + return c.CreatedBefore.Format(StdTimestampFmt) |
| 146 | +} |
| 147 | + |
| 148 | +func (c ClusterAgeFilter) createdAfterTimestamp() string { |
| 149 | + return c.CreatedAfter.Format(StdTimestampFmt) |
| 150 | +} |
0 commit comments