Ejemplo n.º 1
0
func (self *ShardConfiguration) ParseAndValidate(defaultShardDuration time.Duration) error {
	var err error
	if self.Split == 0 {
		self.Split = 1
	}
	if self.SplitRandom == "" {
		self.hasRandomSplit = false
	} else {
		self.hasRandomSplit = true
		self.splitRandomRegex, err = regexp.Compile(self.SplitRandom)
		if err != nil {
			return err
		}
	}
	if self.Duration == "" {
		self.parsedDuration = defaultShardDuration
		return nil
	}
	val, err := common.ParseTimeDuration(self.Duration)
	if err != nil {
		return err
	}
	self.parsedDuration = time.Duration(val)
	return nil
}
Ejemplo n.º 2
0
// parse time expressions, e.g. now() - 1d
func parseTime(value *Value) (int64, error) {
	if value.Type != ValueExpression {
		if value.IsFunctionCall() && value.Name == "now" {
			return time.Now().UnixNano(), nil
		}

		if value.IsFunctionCall() {
			return 0, fmt.Errorf("Invalid use of function %s", value.Name)
		}

		if value.Type == ValueString {
			t, err := parseTimeString(value.Name)
			return t.UnixNano(), err
		}

		return common.ParseTimeDuration(value.Name)
	}

	leftValue, err := parseTime(value.Elems[0])
	if err != nil {
		return 0, err
	}
	rightValue, err := parseTime(value.Elems[1])
	if err != nil {
		return 0, err
	}
	switch value.Name {
	case "+":
		return leftValue + rightValue, nil
	case "-":
		return leftValue - rightValue, nil
	default:
		return 0, fmt.Errorf("Cannot use '%s' in a time expression", value.Name)
	}
}
Ejemplo n.º 3
0
func (self GroupByClause) GetGroupByTime() (*time.Duration, error) {
	for _, groupBy := range self.Elems {
		if groupBy.IsFunctionCall() {
			// TODO: check the number of arguments and return an error
			if len(groupBy.Elems) != 1 {
				return nil, common.NewQueryError(common.WrongNumberOfArguments, "time function only accepts one argument")
			}
			// TODO: check the function name
			// TODO: error checking
			arg := groupBy.Elems[0].Name
			durationInt, err := common.ParseTimeDuration(arg)
			if err != nil {
				return nil, common.NewQueryError(common.InvalidArgument, fmt.Sprintf("invalid argument %s to the time function", arg))
			}
			duration := time.Duration(durationInt)
			return &duration, nil
		}
	}
	return nil, nil
}
Ejemplo n.º 4
0
func (self GroupByClause) GetGroupByTime() (*time.Duration, error) {
	for _, groupBy := range self.Elems {
		if groupBy.IsFunctionCall() && strings.ToLower(groupBy.Name) == "time" {
			// TODO: check the number of arguments and return an error
			if len(groupBy.Elems) != 1 {
				return nil, common.NewQueryError(common.WrongNumberOfArguments, "time function only accepts one argument")
			}

			if groupBy.Elems[0].Type != ValueDuration {
				log.Debug("Get a time function without a duration argument %v", groupBy.Elems[0].Type)
			}
			arg := groupBy.Elems[0].Name
			durationInt, err := common.ParseTimeDuration(arg)
			if err != nil {
				return nil, common.NewQueryError(common.InvalidArgument, fmt.Sprintf("invalid argument %s to the time function", arg))
			}
			duration := time.Duration(durationInt)
			return &duration, nil
		}
	}
	return nil, nil
}