// 2020 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. // Package tools serves as helper or utility functions for other applications within the Migrations App. package tools import ( "strings" "time" ) const ( // OneMonth is the second representation of a OneMonth time-frame OneMonth = 2592000 // TwoMonths is the second representation of a TwoMonths time-frame TwoMonths = 5184000 // FourWeeks is the second representation of a FourWeeks time-frame FourWeeks = 2419200 // ThreeWeeks is the second representation of a ThreeWeeks time-frame ThreeWeeks = 1814400 // TwoWeeks is the second representation of a TwoWeeks time-frame TwoWeeks = 1209600 // OneWeek is the second representation of a OneWeek time-frame OneWeek = 604800 // OneDay is the second representation of a OneDay time-frame OneDay = 86400 ) // InTimeRange is a function that will take an epoch timestamp and a variance interface. // If the timestamp is within the variance interface, it will return true func InTimeRange(epochTime int64, timeRange interface{}) bool { var offset int switch value := timeRange.(type) { case int64: offset = int(value) case int32: offset = int(value) case int: offset = value case string: offset = setTimeFromString(value) } delta := int(time.Now().Unix() - epochTime) return -offset < delta && delta < offset } // UntilTimeRange is a function that will take an epoch timestamp and a variance interface. // If the timestamp is up until the current time, it will return true func UntilTimeRange(epochTime int64, timeRange interface{}, includeToday bool) bool { var offset int switch value := timeRange.(type) { case int64: offset = int(value) case int32: offset = int(value) case int: offset = value case string: offset = setTimeFromString(value) } delta := int(epochTime - time.Now().Unix()) if includeToday { delta += 86400 } return delta < offset && delta > 0 } // setTimeFromString is a small helper function for the InTimeRange function that will return a constant // variance based off of the string received. func setTimeFromString(s string) int { s = strings.ToLower(s) switch { case strings.Contains(s, "1week"): return OneWeek case strings.Contains(s, "2week"): return TwoWeeks case strings.Contains(s, "3week"): return ThreeWeeks case strings.Contains(s, "4week"): return FourWeeks case strings.Contains(s, "1month"): return OneMonth case strings.Contains(s, "2month"): return TwoMonths case strings.Contains(s, "1day"): return OneDay default: return OneWeek } } // CollectByVariance will take a variance type string, and // attempt to pull a list of move-groups based on their execution date // (start.timestamp) that are within the time-variance // collectionFilter will limit the list to move-groups that use a specific collection func (c *Client) CollectByVariance(variance string, collectionFilter *string, includeToday bool) ([]string, error) { var idMap = make([]string, 0) if variance == "" { return idMap, nil } mgs, err := c.CollectMetadata(collectionFilter, false) if err != nil { return idMap, err } for _, mg := range mgs { if UntilTimeRange(mg.Start.Timestamp, variance, includeToday) { // Remove any NOT MOVING groups if IsNotMovingGroup(mg.ID) { continue } idMap = append(idMap, mg.ID) } } return idMap, nil }